23
23
24
24
#if defined(MBEDTLS_AES_ALT )
25
25
26
+ static uint32_t swap (uint32_t in )
27
+ {
28
+ uint32_t in1 , in2 , in3 , in4 , out ;
29
+
30
+ in1 = ((in & 0xff000000 ) >> 24 );
31
+ in2 = ((in & 0x00FF0000 ) >> 8 );
32
+ in3 = ((in & 0x0000FF00 ) << 8 );
33
+ in4 = ((in & 0xFF ) << 24 );
34
+ out = in1 | in2 | in3 | in4 ;
35
+
36
+ return out ;
37
+ }
38
+
26
39
static int aes_set_key (mbedtls_aes_context * ctx , const unsigned char * key , unsigned int keybits )
27
40
{
41
+ mbedtls_printf (" ****** aes_set_key *******\n" );
42
+ mbedtls_printf ("keybits = %d\n" , keybits );
43
+
28
44
switch (keybits ) {
29
45
case 128 :
30
46
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
31
47
memcpy (ctx -> aes_key , key , 16 );
48
+
49
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
50
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
51
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
52
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
53
+
32
54
break ;
33
55
case 192 :
34
56
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
35
57
memcpy (ctx -> aes_key , key , 24 );
58
+
59
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
60
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
61
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
62
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
63
+ ctx -> aes_key [4 ] = swap (ctx -> aes_key [4 ]);
64
+ ctx -> aes_key [5 ] = swap (ctx -> aes_key [5 ]);
65
+
36
66
break ;
37
67
case 256 :
38
68
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
39
69
memcpy (ctx -> aes_key , key , 32 );
70
+
71
+ ctx -> aes_key [0 ] = swap (ctx -> aes_key [0 ]);
72
+ ctx -> aes_key [1 ] = swap (ctx -> aes_key [1 ]);
73
+ ctx -> aes_key [2 ] = swap (ctx -> aes_key [2 ]);
74
+ ctx -> aes_key [3 ] = swap (ctx -> aes_key [3 ]);
75
+ ctx -> aes_key [4 ] = swap (ctx -> aes_key [4 ]);
76
+ ctx -> aes_key [5 ] = swap (ctx -> aes_key [5 ]);
77
+ ctx -> aes_key [6 ] = swap (ctx -> aes_key [6 ]);
78
+ ctx -> aes_key [7 ] = swap (ctx -> aes_key [7 ]);
79
+
40
80
break ;
41
81
default :
42
82
return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@@ -67,6 +107,8 @@ static int aes_set_key(mbedtls_aes_context *ctx, const unsigned char *key, unsig
67
107
/* Implementation that should never be optimized out by the compiler */
68
108
static void mbedtls_zeroize (void * v , size_t n )
69
109
{
110
+ mbedtls_printf (" ****** mbedtls_zeroize *******\n" );
111
+
70
112
volatile unsigned char * p = (unsigned char * )v ;
71
113
while (n -- ) {
72
114
* p ++ = 0 ;
@@ -76,13 +118,16 @@ static void mbedtls_zeroize(void *v, size_t n)
76
118
77
119
void mbedtls_aes_init (mbedtls_aes_context * ctx )
78
120
{
79
- memset ( ctx , 0 , sizeof ( mbedtls_aes_context ) );
121
+ mbedtls_printf ( " ****** mbedtls_aes_init *******\n" );
80
122
123
+ memset (ctx , 0 , sizeof (mbedtls_aes_context ));
81
124
}
82
125
83
126
84
127
void mbedtls_aes_free (mbedtls_aes_context * ctx )
85
128
{
129
+ mbedtls_printf (" ****** mbedtls_aes_free *******\n" );
130
+
86
131
if (ctx == NULL ) {
87
132
return ;
88
133
}
@@ -108,6 +153,16 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
108
153
unsigned int keybits )
109
154
{
110
155
int ret_val = 0 ;
156
+
157
+ mbedtls_printf (" ****** mbedtls_aes_setkey_enc *******\n" );
158
+ mbedtls_printf ("enc keybits : %d\n" , keybits );
159
+ mbedtls_printf ("enc key :\n" );
160
+ for (int i = 1 ; i <= keybits /8 ; i ++ )
161
+ {
162
+ mbedtls_printf ("%x\t" , key [i - 1 ]);
163
+ if ((i %8 ) == 0 ) mbedtls_printf ("\n" );
164
+ }
165
+
111
166
ret_val = aes_set_key (ctx , key , keybits );
112
167
return (ret_val );
113
168
}
@@ -116,6 +171,16 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
116
171
unsigned int keybits )
117
172
{
118
173
int ret_val = 0 ;
174
+
175
+ mbedtls_printf (" ****** mbedtls_aes_setkey_dec *******\n" );
176
+ mbedtls_printf ("dec keybits : %d\n" , keybits );
177
+ mbedtls_printf ("enc key:\n" );
178
+ for (int i = 1 ; i <= keybits /8 ; i ++ )
179
+ {
180
+ mbedtls_printf ("%x\t" , key [i - 1 ]);
181
+ if ((i %8 ) == 0 ) mbedtls_printf ("\n" );
182
+ }
183
+
119
184
ret_val = aes_set_key (ctx , key , keybits );
120
185
return (ret_val );
121
186
}
@@ -126,21 +191,60 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
126
191
const unsigned char input [16 ],
127
192
unsigned char output [16 ])
128
193
{
194
+ int ret ;
195
+
196
+ mbedtls_printf (" ****** mbedtls_aes_crypt_ecb (%s)*******\n" ,mode == MBEDTLS_AES_DECRYPT ?"decrypt" :"encrypt" );
197
+ mbedtls_printf ("input:\n" );
198
+ for (int i = 1 ; i <= 16 ; i ++ )
199
+ {
200
+ mbedtls_printf ("%x\t" , input [i - 1 ]);
201
+ if ((i %8 ) == 0 ) mbedtls_printf ("\n" );
202
+ }
129
203
130
204
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131
205
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
132
- ctx -> hcryp_aes .Phase = HAL_CRYP_PHASE_READY ;
133
206
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
134
207
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
135
208
209
+ /* Set the Algo if not configured till now */
210
+ if ( CRYP_AES_ECB != (ctx -> hcryp_aes .Instance -> CR & CRYP_AES_ECB ))
211
+ {
212
+ ctx -> hcryp_aes .Init .Algorithm = CRYP_AES_ECB ;
213
+
214
+ /* Configure the CRYP */
215
+ HAL_CRYP_SetConfig (& ctx -> hcryp_aes , & ctx -> hcryp_aes .Init );
216
+
217
+ mbedtls_printf (" ****** AES ECB algo configuration set : %ld *******\n" , CRYP_AES_ECB );
218
+ }
219
+
136
220
if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
137
- if (mbedtls_internal_aes_decrypt (ctx , input , output )) {
221
+ ret = mbedtls_internal_aes_decrypt (ctx , input , output );
222
+ if (ret ) {
138
223
return ST_ERR_AES_BUSY ;
139
224
}
225
+ else
226
+ {
227
+ mbedtls_printf ("dec output :\n" );
228
+ for (int j = 1 ; j <= 16 ; j ++ )
229
+ {
230
+ mbedtls_printf ("%x\t" , output [j - 1 ]);
231
+ if ((j %8 ) == 0 ) mbedtls_printf ("\n" );
232
+ }
233
+ }
140
234
} else { /* AES encryption */
141
- if (mbedtls_internal_aes_encrypt (ctx , input , output )) {
235
+ ret = mbedtls_internal_aes_encrypt (ctx , input , output );
236
+ if (ret ) {
142
237
return ST_ERR_AES_BUSY ;
143
238
}
239
+ else
240
+ {
241
+ mbedtls_printf ("enc output :\n" );
242
+ for (int k = 1 ; k <= 16 ; k ++ )
243
+ {
244
+ mbedtls_printf ("%x\t" , output [k - 1 ]);
245
+ if ((k %8 ) == 0 ) mbedtls_printf ("\n" );
246
+ }
247
+ }
144
248
}
145
249
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
146
250
ctx -> ctx_save_cr = ctx -> hcryp_aes .Instance -> CR ;
@@ -151,6 +255,7 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
151
255
#if defined(MBEDTLS_CIPHER_MODE_CBC )
152
256
static int st_cbc_restore_context (mbedtls_aes_context * ctx )
153
257
{
258
+ mbedtls_printf (" ****** st_cbc_restore_context *******\n" );
154
259
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
155
260
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
156
261
/* Re-initialize AES processor with proper parameters
@@ -173,16 +278,28 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
173
278
{
174
279
uint32_t tickstart ;
175
280
uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
281
+
282
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cbc *******\n" );
283
+
176
284
if (length % 16 ) {
177
285
return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
178
286
}
179
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
287
+ ctx -> hcryp_aes .Init .pInitVect = ( uint32_t * ) & iv [0 ];
180
288
if (st_cbc_restore_context (ctx ) != 0 ) {
181
289
return (ST_ERR_AES_BUSY );
182
290
}
183
291
292
+ /* Set the Algo if not configured till now */
293
+ if ( CRYP_AES_CBC != (ctx -> hcryp_aes .Instance -> CR & CRYP_AES_CBC ))
294
+ {
295
+ ctx -> hcryp_aes .Init .Algorithm = CRYP_AES_CBC ;
296
+
297
+ /* Configure the CRYP */
298
+ HAL_CRYP_SetConfig (& ctx -> hcryp_aes , & ctx -> hcryp_aes .Init );
299
+ }
300
+
184
301
if (mode == MBEDTLS_AES_DECRYPT ) {
185
- if (HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 ) != HAL_OK ) {
302
+ if (HAL_CRYP_Decrypt (& ctx -> hcryp_aes , (uint32_t * )input , length / 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
186
303
return ST_ERR_AES_BUSY ;
187
304
}
188
305
/* Save the internal IV vector for multi context purpose */
@@ -199,7 +316,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
199
316
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1LR ;
200
317
* iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1RR ;
201
318
} else {
202
- if (HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 ) != HAL_OK ) {
319
+ if (HAL_CRYP_Encrypt (& ctx -> hcryp_aes , (uint32_t * )input , length / 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
203
320
return ST_ERR_AES_BUSY ;
204
321
}
205
322
memcpy (iv , output , 16 ); /* current output is the IV vector for the next call */
@@ -222,6 +339,8 @@ int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
222
339
int c ;
223
340
size_t n = * iv_off ;
224
341
342
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb128 *******\n" );
343
+
225
344
if (mode == MBEDTLS_AES_DECRYPT ) {
226
345
while (length -- ) {
227
346
if (n == 0 )
@@ -264,6 +383,8 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
264
383
unsigned char c ;
265
384
unsigned char ov [17 ];
266
385
386
+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb8 *******\n" );
387
+
267
388
while (length -- ) {
268
389
memcpy (ov , iv , 16 );
269
390
if (mbedtls_aes_crypt_ecb (ctx , MBEDTLS_AES_ENCRYPT , iv , iv ) != 0 ) {
@@ -327,7 +448,9 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
327
448
const unsigned char input [16 ],
328
449
unsigned char output [16 ])
329
450
{
330
- if (HAL_CRYP_AESECB_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 ) != HAL_OK ) {
451
+ mbedtls_printf (" ****** mbedtls_internal_aes_encrypt *******\n" );
452
+
453
+ if (HAL_CRYP_Encrypt (& ctx -> hcryp_aes , (uint32_t * )input , 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
331
454
// error found
332
455
return ST_ERR_AES_BUSY ;
333
456
}
@@ -339,7 +462,9 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
339
462
const unsigned char input [16 ],
340
463
unsigned char output [16 ])
341
464
{
342
- if (HAL_CRYP_AESECB_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , 16 , (uint8_t * )output , 10 ) != HAL_OK ) {
465
+ mbedtls_printf (" ****** mbedtls_internal_aes_decrypt *******\n" );
466
+
467
+ if (HAL_CRYP_Decrypt (& ctx -> hcryp_aes , (uint32_t * )input , 4 , (uint32_t * )output , 10 ) != HAL_OK ) {
343
468
// error found
344
469
return ST_ERR_AES_BUSY ;
345
470
}
@@ -351,13 +476,15 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
351
476
const unsigned char input [16 ],
352
477
unsigned char output [16 ])
353
478
{
479
+ mbedtls_printf (" ****** mbedtls_aes_encrypt *******\n" );
354
480
mbedtls_internal_aes_encrypt (ctx , input , output );
355
481
}
356
482
357
483
void mbedtls_aes_decrypt (mbedtls_aes_context * ctx ,
358
484
const unsigned char input [16 ],
359
485
unsigned char output [16 ])
360
486
{
487
+ mbedtls_printf (" ****** mbedtls_aes_decrypt *******\n" );
361
488
mbedtls_internal_aes_decrypt (ctx , input , output );
362
489
}
363
490
#endif /* MBEDTLS_DEPRECATED_REMOVED */
0 commit comments