1 /*
2  *  VIA PadLock support functions
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 /*
20  *  This implementation is based on the VIA PadLock Programming Guide:
21  *
22  *  http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
23  *  programming_guide.pdf
24  */
25 
26 #include "common.h"
27 
28 #if defined(MBEDTLS_PADLOCK_C)
29 
30 #include "padlock.h"
31 
32 #include <string.h>
33 
34 #ifndef asm
35 #define asm __asm
36 #endif
37 
38 #if defined(MBEDTLS_HAVE_X86)
39 
40 /*
41  * PadLock detection routine
42  */
mbedtls_padlock_has_support(int feature)43 int mbedtls_padlock_has_support( int feature )
44 {
45     static int flags = -1;
46     int ebx = 0, edx = 0;
47 
48     if( flags == -1 )
49     {
50         asm( "movl  %%ebx, %0           \n\t"
51              "movl  $0xC0000000, %%eax  \n\t"
52              "cpuid                     \n\t"
53              "cmpl  $0xC0000001, %%eax  \n\t"
54              "movl  $0, %%edx           \n\t"
55              "jb    1f                  \n\t"
56              "movl  $0xC0000001, %%eax  \n\t"
57              "cpuid                     \n\t"
58              "1:                        \n\t"
59              "movl  %%edx, %1           \n\t"
60              "movl  %2, %%ebx           \n\t"
61              : "=m" (ebx), "=m" (edx)
62              :  "m" (ebx)
63              : "eax", "ecx", "edx" );
64 
65         flags = edx;
66     }
67 
68     return( flags & feature );
69 }
70 
71 /*
72  * PadLock AES-ECB block en(de)cryption
73  */
mbedtls_padlock_xcryptecb(mbedtls_aes_context * ctx,int mode,const unsigned char input[16],unsigned char output[16])74 int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
75                        int mode,
76                        const unsigned char input[16],
77                        unsigned char output[16] )
78 {
79     int ebx = 0;
80     uint32_t *rk;
81     uint32_t *blk;
82     uint32_t *ctrl;
83     unsigned char buf[256];
84 
85     rk = ctx->buf + ctx->rk_offset;
86 
87     if( ( (long) rk & 15 ) != 0 )
88         return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
89 
90     blk = MBEDTLS_PADLOCK_ALIGN16( buf );
91     memcpy( blk, input, 16 );
92 
93      ctrl = blk + 4;
94     *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
95 
96     asm( "pushfl                        \n\t"
97          "popfl                         \n\t"
98          "movl    %%ebx, %0             \n\t"
99          "movl    $1, %%ecx             \n\t"
100          "movl    %2, %%edx             \n\t"
101          "movl    %3, %%ebx             \n\t"
102          "movl    %4, %%esi             \n\t"
103          "movl    %4, %%edi             \n\t"
104          ".byte  0xf3,0x0f,0xa7,0xc8    \n\t"
105          "movl    %1, %%ebx             \n\t"
106          : "=m" (ebx)
107          :  "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
108          : "memory", "ecx", "edx", "esi", "edi" );
109 
110     memcpy( output, blk, 16 );
111 
112     return( 0 );
113 }
114 
115 /*
116  * PadLock AES-CBC buffer en(de)cryption
117  */
mbedtls_padlock_xcryptcbc(mbedtls_aes_context * ctx,int mode,size_t length,unsigned char iv[16],const unsigned char * input,unsigned char * output)118 int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
119                        int mode,
120                        size_t length,
121                        unsigned char iv[16],
122                        const unsigned char *input,
123                        unsigned char *output )
124 {
125     int ebx = 0;
126     size_t count;
127     uint32_t *rk;
128     uint32_t *iw;
129     uint32_t *ctrl;
130     unsigned char buf[256];
131 
132     rk = ctx->buf + ctx->rk_offset;
133 
134     if( ( (long) input  & 15 ) != 0 ||
135         ( (long) output & 15 ) != 0 ||
136         ( (long) rk & 15 ) != 0 )
137         return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
138 
139     iw = MBEDTLS_PADLOCK_ALIGN16( buf );
140     memcpy( iw, iv, 16 );
141 
142      ctrl = iw + 4;
143     *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
144 
145     count = ( length + 15 ) >> 4;
146 
147     asm( "pushfl                        \n\t"
148          "popfl                         \n\t"
149          "movl    %%ebx, %0             \n\t"
150          "movl    %2, %%ecx             \n\t"
151          "movl    %3, %%edx             \n\t"
152          "movl    %4, %%ebx             \n\t"
153          "movl    %5, %%esi             \n\t"
154          "movl    %6, %%edi             \n\t"
155          "movl    %7, %%eax             \n\t"
156          ".byte  0xf3,0x0f,0xa7,0xd0    \n\t"
157          "movl    %1, %%ebx             \n\t"
158          : "=m" (ebx)
159          :  "m" (ebx), "m" (count), "m" (ctrl),
160             "m"  (rk), "m" (input), "m" (output), "m" (iw)
161          : "memory", "eax", "ecx", "edx", "esi", "edi" );
162 
163     memcpy( iv, iw, 16 );
164 
165     return( 0 );
166 }
167 
168 #endif /* MBEDTLS_HAVE_X86 */
169 
170 #endif /* MBEDTLS_PADLOCK_C */
171