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
38
ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
32
39
memcpy (ctx -> aes_key , key , 16 );
@@ -70,7 +77,8 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
70
77
}
71
78
72
79
/* Implementation that should never be optimized out by the compiler */
73
- static void mbedtls_zeroize ( void * v , size_t n ) {
80
+ static void mbedtls_zeroize ( void * v , size_t n )
81
+ {
74
82
volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
75
83
}
76
84
@@ -122,14 +130,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
122
130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
123
131
ctx -> hcryp_aes .Instance -> CR = ctx -> ctx_save_cr ;
124
132
125
- if (mode == MBEDTLS_AES_DECRYPT ) /* AES decryption */
126
- {
133
+ if (mode == MBEDTLS_AES_DECRYPT ) { /* AES decryption */
127
134
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
128
135
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
129
136
mbedtls_aes_decrypt ( ctx , input , output );
130
- }
131
- else /* AES encryption */
132
- {
137
+ } else { /* AES encryption */
133
138
ctx -> hcryp_aes .Init .DataType = CRYP_DATATYPE_8B ;
134
139
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
135
140
mbedtls_aes_encrypt ( ctx , input , output );
@@ -141,6 +146,31 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
141
146
}
142
147
143
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 */
144
174
145
175
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
146
176
int mode ,
@@ -149,54 +179,24 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
149
179
const unsigned char * input ,
150
180
unsigned char * output )
151
181
{
152
- int status = 0 ;
182
+ int status = 0 ;
153
183
if ( length % 16 )
154
184
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
155
-
156
- if ( mode == MBEDTLS_AES_DECRYPT )
157
- {
158
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
159
185
#if defined (TARGET_STM32L486xG )
160
- if ((ctx -> hcryp_aes .Init .OperatingMode != CRYP_ALGOMODE_KEYDERIVATION_DECRYPT ) || \
161
- (ctx -> hcryp_aes .Init .ChainingMode != CRYP_CHAINMODE_AES_CBC ) || \
162
- (ctx -> hcryp_aes .Init .KeyWriteFlag != CRYP_KEY_WRITE_ENABLE )) {
163
- /* Re-initialize AES IP with proper parameters */
164
- if (HAL_CRYP_DeInit (& ctx -> hcryp_aes ) != HAL_OK )
165
- return HAL_ERROR ;
166
- ctx -> hcryp_aes .Init .OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT ;
167
- ctx -> hcryp_aes .Init .ChainingMode = CRYP_CHAINMODE_AES_CBC ;
168
- ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
169
- if (HAL_CRYP_Init (& ctx -> hcryp_aes ) != HAL_OK )
170
- return HAL_ERROR ;
171
- }
172
-
173
- status = HAL_CRYPEx_AES (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
174
- #else
175
- status = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
176
- #endif
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 );
177
190
}
178
- else
179
- {
180
- ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
181
- #if defined (TARGET_STM32L486xG )
182
- if ((ctx -> hcryp_aes .Init .OperatingMode != CRYP_ALGOMODE_ENCRYPT ) || \
183
- (ctx -> hcryp_aes .Init .ChainingMode != CRYP_CHAINMODE_AES_CBC ) || \
184
- (ctx -> hcryp_aes .Init .KeyWriteFlag != CRYP_KEY_WRITE_ENABLE )) {
185
- /* Re-initialize AES IP with proper parameters */
186
- if (HAL_CRYP_DeInit (& ctx -> hcryp_aes ) != HAL_OK )
187
- return HAL_ERROR ;
188
- ctx -> hcryp_aes .Init .OperatingMode = CRYP_ALGOMODE_ENCRYPT ;
189
- ctx -> hcryp_aes .Init .ChainingMode = CRYP_CHAINMODE_AES_CBC ;
190
- ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
191
- if (HAL_CRYP_Init (& ctx -> hcryp_aes ) != HAL_OK )
192
- return HAL_ERROR ;
193
- }
194
-
195
- status = HAL_CRYPEx_AES (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
196
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 {
197
197
status = HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
198
- #endif
199
198
}
199
+ #endif
200
200
return ( status );
201
201
}
202
202
#endif /* MBEDTLS_CIPHER_MODE_CBC */
@@ -213,10 +213,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
213
213
int c ;
214
214
size_t n = * iv_off ;
215
215
216
- if ( mode == MBEDTLS_AES_DECRYPT )
217
- {
218
- while ( length -- )
219
- {
216
+ if ( mode == MBEDTLS_AES_DECRYPT ) {
217
+ while ( length -- ) {
220
218
if ( n == 0 )
221
219
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
222
220
@@ -226,11 +224,8 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
226
224
227
225
n = ( n + 1 ) & 0x0F ;
228
226
}
229
- }
230
- else
231
- {
232
- while ( length -- )
233
- {
227
+ } else {
228
+ while ( length -- ) {
234
229
if ( n == 0 )
235
230
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
236
231
@@ -256,8 +251,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
256
251
unsigned char c ;
257
252
unsigned char ov [17 ];
258
253
259
- while ( length -- )
260
- {
254
+ while ( length -- ) {
261
255
memcpy ( ov , iv , 16 );
262
256
mbedtls_aes_crypt_ecb ( ctx , MBEDTLS_AES_ENCRYPT , iv , iv );
263
257
0 commit comments