23
23
24
24
#if defined(MBEDTLS_AES_ALT )
25
25
26
+ #if MBED_CONF_MBED_TRACE_ENABLE
27
+ #define TLSPRINT 1
28
+ #endif
29
+
30
+ static uint32_t swap (uint32_t in )
31
+ {
32
+ uint32_t in1 , in2 , in3 , in4 , out ;
33
+
34
+ in1 = ((in & 0xff000000 ) >> 24 );
35
+ in2 = ((in & 0x00FF0000 ) >> 8 );
36
+ in3 = ((in & 0x0000FF00 ) << 8 );
37
+ in4 = ((in & 0xFF ) << 24 );
38
+ out = in1 | in2 | in3 | in4 ;
39
+
40
+ return out ;
41
+ }
42
+
26
43
static int aes_set_key (mbedtls_aes_context * ctx , const unsigned char * key , unsigned int keybits )
27
44
{
45
+ #if TLSPRINT
46
+ mbedtls_printf (" ****** aes_set_key *******\n" );
47
+ mbedtls_printf ("keybits = %d\n" , keybits );
48
+ #endif
49
+
28
50
switch (keybits ) {
29
51
case 128 :
30
52
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
31
53
memcpy (ctx -> aes_key , key , 16 );
54
+
55
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
56
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
57
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
58
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
59
+
32
60
break ;
33
61
case 192 :
34
62
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
35
63
memcpy (ctx -> aes_key , key , 24 );
64
+
65
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
66
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
67
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
68
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
69
+ ctx -> aes_key [4 ] = swap (ctx -> aes_key [4 ]);
70
+ ctx -> aes_key [5 ] = swap (ctx -> aes_key [5 ]);
71
+
36
72
break ;
37
73
case 256 :
38
74
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
39
75
memcpy (ctx -> aes_key , key , 32 );
76
+
77
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
78
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
79
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
80
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
81
+ ctx -> aes_key [4 ] = swap (ctx -> aes_key [4 ]);
82
+ ctx -> aes_key [5 ] = swap (ctx -> aes_key [5 ]);
83
+ ctx -> aes_key [6 ] = swap (ctx -> aes_key [6 ]);
84
+ ctx -> aes_key [7 ] = swap (ctx -> aes_key [7 ]);
85
+
40
86
break ;
41
87
default :
42
88
return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@@ -67,6 +113,10 @@ static int aes_set_key(mbedtls_aes_context *ctx, const unsigned char *key, unsig
67
113
/* Implementation that should never be optimized out by the compiler */
68
114
static void mbedtls_zeroize (void * v , size_t n )
69
115
{
116
+ #if TLSPRINT
117
+ mbedtls_printf (" ****** mbedtls_zeroize *******\n" );
118
+ #endif
119
+
70
120
volatile unsigned char * p = (unsigned char * )v ;
71
121
while (n -- ) {
72
122
* p ++ = 0 ;
@@ -76,13 +126,20 @@ static void mbedtls_zeroize(void *v, size_t n)
76
126
77
127
void mbedtls_aes_init (mbedtls_aes_context * ctx )
78
128
{
79
- memset (ctx , 0 , sizeof (mbedtls_aes_context ));
129
+ #if TLSPRINT
130
+ mbedtls_printf (" ****** mbedtls_aes_init *******\n" );
131
+ #endif
80
132
133
+ memset (ctx , 0 , sizeof (mbedtls_aes_context ));
81
134
}
82
135
83
136
84
137
void mbedtls_aes_free (mbedtls_aes_context * ctx )
85
138
{
139
+ #if TLSPRINT
140
+ mbedtls_printf (" ****** mbedtls_aes_free *******\n" );
141
+ #endif
142
+
86
143
if (ctx == NULL ) {
87
144
return ;
88
145
}
@@ -108,6 +165,19 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
108
165
unsigned int keybits )
109
166
{
110
167
int ret_val = 0 ;
168
+
169
+ #if TLSPRINT
170
+ mbedtls_printf (" ****** mbedtls_aes_setkey_enc *******\n" );
171
+ mbedtls_printf ("enc keybits : %d\n" , keybits );
172
+ mbedtls_printf ("enc key :\n" );
173
+ for (int i = 1 ; i <= keybits / 8 ; i ++ ) {
174
+ mbedtls_printf ("%x\t" , key [i - 1 ]);
175
+ if ((i % 8 ) == 0 ) {
176
+ mbedtls_printf ("\n" );
177
+ }
178
+ }
179
+ #endif
180
+
111
181
ret_val = aes_set_key (ctx , key , keybits );
112
182
return (ret_val );
113
183
}
@@ -116,6 +186,19 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
116
186
unsigned int keybits )
117
187
{
118
188
int ret_val = 0 ;
189
+
190
+ #if TLSPRINT
191
+ mbedtls_printf (" ****** mbedtls_aes_setkey_dec *******\n" );
192
+ mbedtls_printf ("dec keybits : %d\n" , keybits );
193
+ mbedtls_printf ("enc key:\n" );
194
+ for (int i = 1 ; i <= keybits / 8 ; i ++ ) {
195
+ mbedtls_printf ("%x\t" , key [i - 1 ]);
196
+ if ((i % 8 ) == 0 ) {
197
+ mbedtls_printf ("\n" );
198
+ }
199
+ }
200
+ #endif
201
+
119
202
ret_val = aes_set_key (ctx , key , keybits );
120
203
return (ret_val );
121
204
}
@@ -126,20 +209,65 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
126
209
const unsigned char input [16 ],
127
210
unsigned char output [16 ])
128
211
{
212
+ int ret ;
213
+
214
+ #if TLSPRINT
215
+ mbedtls_printf (" ****** mbedtls_aes_crypt_ecb (%s)*******\n" , mode == MBEDTLS_AES_DECRYPT ? "decrypt" : "encrypt" );
216
+ mbedtls_printf ("input:\n" );
217
+ for (int i = 1 ; i <= 16 ; i ++ ) {
218
+ mbedtls_printf ("%x\t" , input [i - 1 ]);
219
+ if ((i % 8 ) == 0 ) {
220
+ mbedtls_printf ("\n" );
221
+ }
222
+ }
223
+ #endif
129
224
130
225
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131
226
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
132
- ctx -> hcryp_aes .Phase = HAL_CRYP_PHASE_READY ;
133
227
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
134
228
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
135
229
230
+ /* Set the Algo if not configured till now */
231
+ if (CRYP_AES_ECB != (ctx -> hcryp_aes .Instance -> CR & CRYP_AES_ECB )) {
232
+ ctx -> hcryp_aes .Init .Algorithm = CRYP_AES_ECB ;
233
+
234
+ /* Configure the CRYP */
235
+ HAL_CRYP_SetConfig (& ctx -> hcryp_aes , & ctx -> hcryp_aes .Init );
236
+
237
+ #if TLSPRINT
238
+ mbedtls_printf (" ****** AES ECB algo configuration set : %ld *******\n" , CRYP_AES_ECB );
239
+ #endif
240
+ }
241
+
136
242
if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
137
- if (mbedtls_internal_aes_decrypt (ctx , input , output )) {
243
+ ret = mbedtls_internal_aes_decrypt (ctx , input , output );
244
+ if (ret ) {
138
245
return ST_ERR_AES_BUSY ;
246
+ } else {
247
+ #if TLSPRINT
248
+ mbedtls_printf ("dec output :\n" );
249
+ for (int j = 1 ; j <= 16 ; j ++ ) {
250
+ mbedtls_printf ("%x\t" , output [j - 1 ]);
251
+ if ((j % 8 ) == 0 ) {
252
+ mbedtls_printf ("\n" );
253
+ }
254
+ }
255
+ #endif
139
256
}
140
257
} else { /* AES encryption */
141
- if (mbedtls_internal_aes_encrypt (ctx , input , output )) {
258
+ ret = mbedtls_internal_aes_encrypt (ctx , input , output );
259
+ if (ret ) {
142
260
return ST_ERR_AES_BUSY ;
261
+ } else {
262
+ #if TLSPRINT
263
+ mbedtls_printf ("enc output :\n" );
264
+ for (int k = 1 ; k <= 16 ; k ++ ) {
265
+ mbedtls_printf ("%x\t" , output [k - 1 ]);
266
+ if ((k % 8 ) == 0 ) {
267
+ mbedtls_printf ("\n" );
268
+ }
269
+ }
270
+ #endif
143
271
}
144
272
}
145
273
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
@@ -151,6 +279,9 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
151
279
#if defined(MBEDTLS_CIPHER_MODE_CBC )
152
280
static int st_cbc_restore_context (mbedtls_aes_context * ctx )
153
281
{
282
+ #if TLSPRINT
283
+ mbedtls_printf (" ****** st_cbc_restore_context *******\n" );
284
+ #endif
154
285
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
155
286
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
156
287
/* Re-initialize AES processor with proper parameters
@@ -173,16 +304,29 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
173
304
{
174
305
uint32_t tickstart ;
175
306
uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
307
+
308
+ #if TLSPRINT
309
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cbc *******\n" );
310
+ #endif
311
+
176
312
if (length % 16 ) {
177
313
return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
178
314
}
179
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
315
+ ctx -> hcryp_aes .Init .pInitVect = ( uint32_t * ) & iv [0 ];
180
316
if (st_cbc_restore_context (ctx ) != 0 ) {
181
317
return (ST_ERR_AES_BUSY );
182
318
}
183
319
320
+ /* Set the Algo if not configured till now */
321
+ if (CRYP_AES_CBC != (ctx -> hcryp_aes .Instance -> CR & CRYP_AES_CBC )) {
322
+ ctx -> hcryp_aes .Init .Algorithm = CRYP_AES_CBC ;
323
+
324
+ /* Configure the CRYP */
325
+ HAL_CRYP_SetConfig (& ctx -> hcryp_aes , & ctx -> hcryp_aes .Init );
326
+ }
327
+
184
328
if (mode == MBEDTLS_AES_DECRYPT ) {
185
- if (HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 ) != HAL_OK ) {
329
+ if (HAL_CRYP_Decrypt (& ctx -> hcryp_aes , (uint32_t * )input , length / 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
186
330
return ST_ERR_AES_BUSY ;
187
331
}
188
332
/* Save the internal IV vector for multi context purpose */
@@ -199,7 +343,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
199
343
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1LR ;
200
344
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1RR ;
201
345
} else {
202
- if (HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 ) != HAL_OK ) {
346
+ if (HAL_CRYP_Encrypt (& ctx -> hcryp_aes , (uint32_t * )input , length / 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
203
347
return ST_ERR_AES_BUSY ;
204
348
}
205
349
memcpy (iv , output , 16 ); /* current output is the IV vector for the next call */
@@ -222,6 +366,10 @@ int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
222
366
int c ;
223
367
size_t n = * iv_off ;
224
368
369
+ #if TLSPRINT
370
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb128 *******\n" );
371
+ #endif
372
+
225
373
if (mode == MBEDTLS_AES_DECRYPT ) {
226
374
while (length -- ) {
227
375
if (n == 0 )
@@ -264,6 +412,10 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
264
412
unsigned char c ;
265
413
unsigned char ov [17 ];
266
414
415
+ #if TLSPRINT
416
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb8 *******\n" );
417
+ #endif
418
+
267
419
while (length -- ) {
268
420
memcpy (ov , iv , 16 );
269
421
if (mbedtls_aes_crypt_ecb (ctx , MBEDTLS_AES_ENCRYPT , iv , iv ) != 0 ) {
@@ -327,7 +479,11 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
327
479
const unsigned char input [16 ],
328
480
unsigned char output [16 ])
329
481
{
330
- if (HAL_CRYP_AESECB_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 ) != HAL_OK ) {
482
+ #if TLSPRINT
483
+ mbedtls_printf (" ****** mbedtls_internal_aes_encrypt *******\n" );
484
+ #endif
485
+
486
+ if (HAL_CRYP_Encrypt (& ctx -> hcryp_aes , (uint32_t * )input , 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
331
487
// error found
332
488
return ST_ERR_AES_BUSY ;
333
489
}
@@ -339,7 +495,11 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
339
495
const unsigned char input [16 ],
340
496
unsigned char output [16 ])
341
497
{
342
- if (HAL_CRYP_AESECB_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 ) != HAL_OK ) {
498
+ #if TLSPRINT
499
+ mbedtls_printf (" ****** mbedtls_internal_aes_decrypt *******\n" );
500
+ #endif
501
+
502
+ if (HAL_CRYP_Decrypt (& ctx -> hcryp_aes , (uint32_t * )input , 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
343
503
// error found
344
504
return ST_ERR_AES_BUSY ;
345
505
}
@@ -351,13 +511,19 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
351
511
const unsigned char input [16 ],
352
512
unsigned char output [16 ])
353
513
{
514
+ #if TLSPRINT
515
+ mbedtls_printf (" ****** mbedtls_aes_encrypt *******\n" );
516
+ #endif
354
517
mbedtls_internal_aes_encrypt (ctx , input , output );
355
518
}
356
519
357
520
void mbedtls_aes_decrypt (mbedtls_aes_context * ctx ,
358
521
const unsigned char input [16 ],
359
522
unsigned char output [16 ])
360
523
{
524
+ #if TLSPRINT
525
+ mbedtls_printf (" ****** mbedtls_aes_decrypt *******\n" );
526
+ #endif
361
527
mbedtls_internal_aes_decrypt (ctx , input , output );
362
528
}
363
529
#endif /* MBEDTLS_DEPRECATED_REMOVED */
0 commit comments