Skip to content

Commit a657bc0

Browse files
committed
STM32F4 V1.19.0 -> V1.25.0 : MBEDTLS adaptation
1 parent 4d91379 commit a657bc0

File tree

2 files changed

+177
-10
lines changed

2 files changed

+177
-10
lines changed

features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/aes_alt.c

Lines changed: 176 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,66 @@
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+
2643
static 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 */
68114
static 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

77127
void 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

84137
void 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,18 @@ 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+
{
175+
mbedtls_printf("%x\t", key[i-1]);
176+
if ((i%8) == 0) mbedtls_printf("\n");
177+
}
178+
#endif
179+
111180
ret_val = aes_set_key(ctx, key, keybits);
112181
return (ret_val);
113182
}
@@ -116,6 +185,18 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
116185
unsigned int keybits)
117186
{
118187
int ret_val = 0;
188+
189+
#if TLSPRINT
190+
mbedtls_printf(" ****** mbedtls_aes_setkey_dec *******\n");
191+
mbedtls_printf("dec keybits : %d\n", keybits);
192+
mbedtls_printf("enc key:\n");
193+
for (int i=1; i<= keybits/8; i++)
194+
{
195+
mbedtls_printf("%x\t", key[i-1]);
196+
if ((i%8) == 0) mbedtls_printf("\n");
197+
}
198+
#endif
199+
119200
ret_val = aes_set_key(ctx, key, keybits);
120201
return (ret_val);
121202
}
@@ -126,21 +207,68 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
126207
const unsigned char input[16],
127208
unsigned char output[16])
128209
{
210+
int ret;
211+
212+
#if TLSPRINT
213+
mbedtls_printf(" ****** mbedtls_aes_crypt_ecb (%s)*******\n",mode == MBEDTLS_AES_DECRYPT?"decrypt":"encrypt" );
214+
mbedtls_printf("input:\n");
215+
for (int i=1; i<= 16; i++)
216+
{
217+
mbedtls_printf("%x\t", input[i-1]);
218+
if ((i%8) == 0) mbedtls_printf("\n");
219+
}
220+
#endif
129221

130222
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131223
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
132-
ctx->hcryp_aes.Phase = HAL_CRYP_PHASE_READY;
133224
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
134225
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
135226

227+
/* Set the Algo if not configured till now */
228+
if ( CRYP_AES_ECB != (ctx->hcryp_aes.Instance->CR & CRYP_AES_ECB))
229+
{
230+
ctx->hcryp_aes.Init.Algorithm = CRYP_AES_ECB;
231+
232+
/* Configure the CRYP */
233+
HAL_CRYP_SetConfig(&ctx->hcryp_aes, &ctx->hcryp_aes.Init);
234+
235+
#if TLSPRINT
236+
mbedtls_printf(" ****** AES ECB algo configuration set : %ld *******\n", CRYP_AES_ECB);
237+
#endif
238+
}
239+
136240
if (mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */
137-
if (mbedtls_internal_aes_decrypt(ctx, input, output)) {
241+
ret = mbedtls_internal_aes_decrypt(ctx, input, output);
242+
if (ret) {
138243
return ST_ERR_AES_BUSY;
139244
}
245+
else
246+
{
247+
#if TLSPRINT
248+
mbedtls_printf("dec output :\n");
249+
for (int j=1; j<= 16; j++)
250+
{
251+
mbedtls_printf("%x\t", output[j-1]);
252+
if ((j%8) == 0) mbedtls_printf("\n");
253+
}
254+
#endif
255+
}
140256
} else { /* AES encryption */
141-
if (mbedtls_internal_aes_encrypt(ctx, input, output)) {
257+
ret = mbedtls_internal_aes_encrypt(ctx, input, output);
258+
if (ret) {
142259
return ST_ERR_AES_BUSY;
143260
}
261+
else
262+
{
263+
#if TLSPRINT
264+
mbedtls_printf("enc output :\n");
265+
for (int k=1; k<= 16; k++)
266+
{
267+
mbedtls_printf("%x\t", output[k-1]);
268+
if ((k%8) == 0) mbedtls_printf("\n");
269+
}
270+
#endif
271+
}
144272
}
145273
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
146274
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
@@ -151,6 +279,9 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
151279
#if defined(MBEDTLS_CIPHER_MODE_CBC)
152280
static 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,30 @@ 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+
{
323+
ctx->hcryp_aes.Init.Algorithm = CRYP_AES_CBC;
324+
325+
/* Configure the CRYP */
326+
HAL_CRYP_SetConfig(&ctx->hcryp_aes, &ctx->hcryp_aes.Init);
327+
}
328+
184329
if (mode == MBEDTLS_AES_DECRYPT) {
185-
if (HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) {
330+
if (HAL_CRYP_Decrypt(&ctx->hcryp_aes, (uint32_t *)input, length/4, (uint32_t *)output, 10) != HAL_OK) {
186331
return ST_ERR_AES_BUSY;
187332
}
188333
/* Save the internal IV vector for multi context purpose */
@@ -199,7 +344,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
199344
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1LR;
200345
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1RR;
201346
} else {
202-
if (HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) {
347+
if (HAL_CRYP_Encrypt(&ctx->hcryp_aes, (uint32_t *)input, length/4, (uint32_t *)output, 10) != HAL_OK) {
203348
return ST_ERR_AES_BUSY;
204349
}
205350
memcpy(iv, output, 16); /* current output is the IV vector for the next call */
@@ -222,6 +367,10 @@ int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
222367
int c;
223368
size_t n = *iv_off;
224369

370+
#if TLSPRINT
371+
mbedtls_printf(" ****** mbedtls_aes_crypt_cfb128 *******\n");
372+
#endif
373+
225374
if (mode == MBEDTLS_AES_DECRYPT) {
226375
while (length--) {
227376
if (n == 0)
@@ -264,6 +413,10 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
264413
unsigned char c;
265414
unsigned char ov[17];
266415

416+
#if TLSPRINT
417+
mbedtls_printf(" ****** mbedtls_aes_crypt_cfb8 *******\n");
418+
#endif
419+
267420
while (length--) {
268421
memcpy(ov, iv, 16);
269422
if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) {
@@ -327,7 +480,11 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
327480
const unsigned char input[16],
328481
unsigned char output[16])
329482
{
330-
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
483+
#if TLSPRINT
484+
mbedtls_printf(" ****** mbedtls_internal_aes_encrypt *******\n");
485+
#endif
486+
487+
if (HAL_CRYP_Encrypt(&ctx->hcryp_aes, (uint32_t *)input, 4, (uint32_t *)output, 10) != HAL_OK) {
331488
// error found
332489
return ST_ERR_AES_BUSY;
333490
}
@@ -339,7 +496,11 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
339496
const unsigned char input[16],
340497
unsigned char output[16])
341498
{
342-
if (HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) != HAL_OK) {
499+
#if TLSPRINT
500+
mbedtls_printf(" ****** mbedtls_internal_aes_decrypt *******\n");
501+
#endif
502+
503+
if (HAL_CRYP_Decrypt(&ctx->hcryp_aes, (uint32_t *)input, 4, (uint32_t *)output, 10) != HAL_OK) {
343504
// error found
344505
return ST_ERR_AES_BUSY;
345506
}
@@ -351,13 +512,19 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
351512
const unsigned char input[16],
352513
unsigned char output[16])
353514
{
515+
#if TLSPRINT
516+
mbedtls_printf(" ****** mbedtls_aes_encrypt *******\n");
517+
#endif
354518
mbedtls_internal_aes_encrypt(ctx, input, output);
355519
}
356520

357521
void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
358522
const unsigned char input[16],
359523
unsigned char output[16])
360524
{
525+
#if TLSPRINT
526+
mbedtls_printf(" ****** mbedtls_aes_decrypt *******\n");
527+
#endif
361528
mbedtls_internal_aes_decrypt(ctx, input, output);
362529
}
363530
#endif /* MBEDTLS_DEPRECATED_REMOVED */

features/mbedtls/targets/TARGET_STM/TARGET_STM32F4/aes_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern "C" {
4242
* generating an extra round key
4343
*/
4444
typedef struct {
45-
unsigned char aes_key[32]; /* Decryption key */
45+
uint32_t aes_key[8]; /* Decryption key */
4646
CRYP_HandleTypeDef hcryp_aes;
4747
uint32_t ctx_save_cr; /* save context for multi-instance */
4848
}

0 commit comments

Comments
 (0)