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