23
23
24
24
#if defined(MBEDTLS_AES_ALT )
25
25
26
+ #if defined(TARGET_STM32L486xG )
27
+ //the following defines are provided to maintain compatibility between STM32 families
28
+ #define __HAL_RCC_CRYP_CLK_ENABLE __HAL_RCC_AES_CLK_ENABLE
29
+ #define __HAL_RCC_CRYP_FORCE_RESET __HAL_RCC_AES_FORCE_RESET
30
+ #define __HAL_RCC_CRYP_RELEASE_RESET __HAL_RCC_AES_RELEASE_RESET
31
+ #define CRYP AES
32
+ #endif
33
+
26
34
static int aes_set_key ( mbedtls_aes_context * ctx , const unsigned char * key , unsigned int keybits )
27
35
{
28
- switch ( keybits )
29
- {
36
+ switch ( keybits ) {
30
37
case 128 :
31
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
32
- memcpy (ctx -> aes_key , key , 16 );
33
- break ;
38
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
39
+ memcpy (ctx -> aes_key , key , 16 );
40
+ break ;
34
41
case 192 :
35
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
36
- memcpy (ctx -> aes_key , key , 24 );
37
- break ;
42
+ #if defined (TARGET_STM32L486xG )
43
+ return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
44
+ #else
45
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
46
+ memcpy (ctx -> aes_key , key , 24 );
47
+ break ;
48
+ #endif
49
+
38
50
case 256 :
39
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
40
- memcpy (ctx -> aes_key , key , 32 );
41
- break ;
51
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
52
+ memcpy (ctx -> aes_key , key , 32 );
53
+ break ;
42
54
default : return ( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
43
55
}
44
56
@@ -52,6 +64,9 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
52
64
__HAL_RCC_CRYP_CLK_ENABLE ();
53
65
54
66
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
67
+ #if defined (TARGET_STM32L486xG )
68
+ ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
69
+ #endif
55
70
if (HAL_CRYP_Init (& ctx -> hcryp_aes ) == HAL_ERROR )
56
71
return (HAL_ERROR );
57
72
@@ -62,7 +77,8 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
62
77
}
63
78
64
79
/* Implementation that should never be optimized out by the compiler */
65
- static void mbedtls_zeroize ( void * v , size_t n ) {
80
+ static void mbedtls_zeroize ( void * v , size_t n )
81
+ {
66
82
volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
67
83
}
68
84
@@ -114,14 +130,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
114
130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
115
131
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
116
132
117
- if (mode == MBEDTLS_AES_DECRYPT ) /* AES decryption */
118
- {
133
+ if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
119
134
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
120
135
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
121
136
mbedtls_aes_decrypt ( ctx , input , output );
122
- }
123
- else /* AES encryption */
124
- {
137
+ } else { /* AES encryption */
125
138
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
126
139
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
127
140
mbedtls_aes_encrypt ( ctx , input , output );
@@ -133,6 +146,31 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
133
146
}
134
147
135
148
#if defined(MBEDTLS_CIPHER_MODE_CBC )
149
+ #if defined (TARGET_STM32L486xG )
150
+ static int st_hal_cryp_cbc ( mbedtls_aes_context * ctx , uint32_t opmode , size_t length ,
151
+ unsigned char iv [16 ], uint8_t * input , uint8_t * output )
152
+ {
153
+ int status = 0 ;
154
+ ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
155
+ if ((ctx -> hcryp_aes .Init .OperatingMode != opmode ) || \
156
+ (ctx -> hcryp_aes .Init .ChainingMode != CRYP_CHAINMODE_AES_CBC ) || \
157
+ (ctx -> hcryp_aes .Init .KeyWriteFlag != CRYP_KEY_WRITE_ENABLE )) {
158
+
159
+ /* Re-initialize AES IP with proper parameters */
160
+ if (HAL_CRYP_DeInit (& ctx -> hcryp_aes ) != HAL_OK )
161
+ return HAL_ERROR ;
162
+ ctx -> hcryp_aes .Init .OperatingMode = opmode ;
163
+ ctx -> hcryp_aes .Init .ChainingMode = CRYP_CHAINMODE_AES_CBC ;
164
+ ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
165
+ if (HAL_CRYP_Init (& ctx -> hcryp_aes ) != HAL_OK )
166
+ return HAL_ERROR ;
167
+ }
168
+
169
+ status = HAL_CRYPEx_AES (& ctx -> hcryp_aes , input , length , output , 10 );
170
+
171
+ return status ;
172
+ }
173
+ #endif /* TARGET_STM32L486xG */
136
174
137
175
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
138
176
int mode ,
@@ -141,22 +179,24 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
141
179
const unsigned char * input ,
142
180
unsigned char * output )
143
181
{
144
- int status = 0 ;
182
+ int status = 0 ;
145
183
if ( length % 16 )
146
184
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
147
-
148
- if ( mode == MBEDTLS_AES_DECRYPT )
149
- {
150
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
151
-
152
- status = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
185
+ #if defined (TARGET_STM32L486xG )
186
+ if ( mode == MBEDTLS_AES_DECRYPT ) {
187
+ status = st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_KEYDERIVATION_DECRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
188
+ } else {
189
+ status = st_hal_cryp_cbc (ctx , CRYP_ALGOMODE_ENCRYPT , length , iv , (uint8_t * )input , (uint8_t * )output );
153
190
}
154
- else
155
- {
156
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
157
-
191
+ #else
192
+ ctx -> hcryp_aes .Init .pInitVect = & iv [0 ];
193
+
194
+ if ( mode == MBEDTLS_AES_DECRYPT ) {
195
+ status = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
196
+ } else {
158
197
status = HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
159
198
}
199
+ #endif
160
200
return ( status );
161
201
}
162
202
#endif /* MBEDTLS_CIPHER_MODE_CBC */
@@ -173,10 +213,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
173
213
int c ;
174
214
size_t n = * iv_off ;
175
215
176
- if ( mode == MBEDTLS_AES_DECRYPT )
177
- {
178
- while ( length -- )
179
- {
216
+ if ( mode == MBEDTLS_AES_DECRYPT ) {
217
+ while ( length -- ) {
180
218
if ( n == 0 )
181
219
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
182
220
@@ -186,11 +224,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
186
224
187
225
n = ( n + 1 ) & 0x0F ;
188
226
}
189
- }
190
- else
191
- {
192
- while ( length -- )
193
- {
227
+ } else {
228
+ while ( length -- ) {
194
229
if ( n == 0 )
195
230
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
196
231
@@ -216,8 +251,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
216
251
unsigned char c ;
217
252
unsigned char ov [17 ];
218
253
219
- while ( length -- )
220
- {
254
+ while ( length -- ) {
221
255
memcpy ( ov , iv , 16 );
222
256
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
223
257
0 commit comments