2323
2424#if defined(MBEDTLS_AES_ALT )
2525
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+
2643static int aes_set_key (mbedtls_aes_context * ctx , const unsigned char * key , unsigned int keybits )
2744{
45+ #if TLSPRINT
46+ mbedtls_printf (" ****** aes_set_key *******\n" );
47+ mbedtls_printf ("keybits = %d\n" , keybits );
48+ #endif
49+
2850 switch (keybits ) {
2951 case 128 :
3052 ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
3153 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+
3260 break ;
3361 case 192 :
3462 ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
3563 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+
3672 break ;
3773 case 256 :
3874 ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
3975 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+
4086 break ;
4187 default :
4288 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
67113/* Implementation that should never be optimized out by the compiler */
68114static void mbedtls_zeroize (void * v , size_t n )
69115{
116+ #if TLSPRINT
117+ mbedtls_printf (" ****** mbedtls_zeroize *******\n" );
118+ #endif
119+
70120 volatile unsigned char * p = (unsigned char * )v ;
71121 while (n -- ) {
72122 * p ++ = 0 ;
@@ -76,13 +126,20 @@ static void mbedtls_zeroize(void *v, size_t n)
76126
77127void mbedtls_aes_init (mbedtls_aes_context * ctx )
78128{
79- memset (ctx , 0 , sizeof (mbedtls_aes_context ));
129+ #if TLSPRINT
130+ mbedtls_printf (" ****** mbedtls_aes_init *******\n" );
131+ #endif
80132
133+ memset (ctx , 0 , sizeof (mbedtls_aes_context ));
81134}
82135
83136
84137void mbedtls_aes_free (mbedtls_aes_context * ctx )
85138{
139+ #if TLSPRINT
140+ mbedtls_printf (" ****** mbedtls_aes_free *******\n" );
141+ #endif
142+
86143 if (ctx == NULL ) {
87144 return ;
88145 }
@@ -108,6 +165,19 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
108165 unsigned int keybits )
109166{
110167 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+
111181 ret_val = aes_set_key (ctx , key , keybits );
112182 return (ret_val );
113183}
@@ -116,6 +186,19 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
116186 unsigned int keybits )
117187{
118188 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+
119202 ret_val = aes_set_key (ctx , key , keybits );
120203 return (ret_val );
121204}
@@ -126,20 +209,65 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
126209 const unsigned char input [16 ],
127210 unsigned char output [16 ])
128211{
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
129224
130225 /* allow multi-instance of CRYP use: restore context for CRYP hw module */
131226 ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
132- ctx -> hcryp_aes .Phase = HAL_CRYP_PHASE_READY ;
133227 ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
134228 ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
135229
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+
136242 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 ) {
138245 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
139256 }
140257 } else { /* AES encryption */
141- if (mbedtls_internal_aes_encrypt (ctx , input , output )) {
258+ ret = mbedtls_internal_aes_encrypt (ctx , input , output );
259+ if (ret ) {
142260 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
143271 }
144272 }
145273 /* 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,
151279#if defined(MBEDTLS_CIPHER_MODE_CBC )
152280static int st_cbc_restore_context (mbedtls_aes_context * ctx )
153281{
282+ #if TLSPRINT
283+ mbedtls_printf (" ****** st_cbc_restore_context *******\n" );
284+ #endif
154285 /* allow multi-instance of CRYP use: restore context for CRYP hw module */
155286 ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
156287 /* Re-initialize AES processor with proper parameters
@@ -173,16 +304,29 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
173304{
174305 uint32_t tickstart ;
175306 uint32_t * iv_ptr = (uint32_t * )& iv [0 ];
307+
308+ #if TLSPRINT
309+ mbedtls_printf (" ****** mbedtls_aes_crypt_cbc *******\n" );
310+ #endif
311+
176312 if (length % 16 ) {
177313 return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
178314 }
179- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
315+ ctx -> hcryp_aes .Init .pInitVect = ( uint32_t * ) & iv [0 ];
180316 if (st_cbc_restore_context (ctx ) != 0 ) {
181317 return (ST_ERR_AES_BUSY );
182318 }
183319
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+
184328 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 ) {
186330 return ST_ERR_AES_BUSY ;
187331 }
188332 /* Save the internal IV vector for multi context purpose */
@@ -199,7 +343,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
199343 * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1LR ;
200344 * iv_ptr ++ = ctx -> hcryp_aes .Instance -> IV1RR ;
201345 } 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 ) {
203347 return ST_ERR_AES_BUSY ;
204348 }
205349 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,
222366 int c ;
223367 size_t n = * iv_off ;
224368
369+ #if TLSPRINT
370+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb128 *******\n" );
371+ #endif
372+
225373 if (mode == MBEDTLS_AES_DECRYPT ) {
226374 while (length -- ) {
227375 if (n == 0 )
@@ -264,6 +412,10 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
264412 unsigned char c ;
265413 unsigned char ov [17 ];
266414
415+ #if TLSPRINT
416+ mbedtls_printf (" ****** mbedtls_aes_crypt_cfb8 *******\n" );
417+ #endif
418+
267419 while (length -- ) {
268420 memcpy (ov , iv , 16 );
269421 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,
327479 const unsigned char input [16 ],
328480 unsigned char output [16 ])
329481{
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 ) {
331487 // error found
332488 return ST_ERR_AES_BUSY ;
333489 }
@@ -339,7 +495,11 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
339495 const unsigned char input [16 ],
340496 unsigned char output [16 ])
341497{
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 ) {
343503 // error found
344504 return ST_ERR_AES_BUSY ;
345505 }
@@ -351,13 +511,19 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
351511 const unsigned char input [16 ],
352512 unsigned char output [16 ])
353513{
514+ #if TLSPRINT
515+ mbedtls_printf (" ****** mbedtls_aes_encrypt *******\n" );
516+ #endif
354517 mbedtls_internal_aes_encrypt (ctx , input , output );
355518}
356519
357520void mbedtls_aes_decrypt (mbedtls_aes_context * ctx ,
358521 const unsigned char input [16 ],
359522 unsigned char output [16 ])
360523{
524+ #if TLSPRINT
525+ mbedtls_printf (" ****** mbedtls_aes_decrypt *******\n" );
526+ #endif
361527 mbedtls_internal_aes_decrypt (ctx , input , output );
362528}
363529#endif /* MBEDTLS_DEPRECATED_REMOVED */
0 commit comments