@@ -28,17 +28,22 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
28
28
switch ( keybits )
29
29
{
30
30
case 128 :
31
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
32
- memcpy (ctx -> aes_key , key , 16 );
33
- break ;
31
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_128B ;
32
+ memcpy (ctx -> aes_key , key , 16 );
33
+ break ;
34
34
case 192 :
35
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
36
- memcpy (ctx -> aes_key , key , 24 );
37
- break ;
35
+ #if defined (TARGET_STM32L486xG )
36
+ return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
37
+ #else
38
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_192B ;
39
+ memcpy (ctx -> aes_key , key , 24 );
40
+ break ;
41
+ #endif
42
+
38
43
case 256 :
39
- ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
40
- memcpy (ctx -> aes_key , key , 32 );
41
- break ;
44
+ ctx -> hcryp_aes .Init .KeySize = CRYP_KEYSIZE_256B ;
45
+ memcpy (ctx -> aes_key , key , 32 );
46
+ break ;
42
47
default : return ( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
43
48
}
44
49
@@ -52,6 +57,9 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
52
57
__HAL_RCC_CRYP_CLK_ENABLE ();
53
58
54
59
ctx -> hcryp_aes .Init .pKey = ctx -> aes_key ;
60
+ #if defined (TARGET_STM32L486xG )
61
+ ctx -> hcryp_aes .Init .KeyWriteFlag = CRYP_KEY_WRITE_ENABLE ;
62
+ #endif
55
63
if (HAL_CRYP_Init (& ctx -> hcryp_aes ) == HAL_ERROR )
56
64
return (HAL_ERROR );
57
65
@@ -148,14 +156,46 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
148
156
if ( mode == MBEDTLS_AES_DECRYPT )
149
157
{
150
158
ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
151
-
159
+ #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
152
175
status = HAL_CRYP_AESCBC_Decrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
176
+ #endif
153
177
}
154
178
else
155
179
{
156
180
ctx -> hcryp_aes .Init .pInitVect = & iv [0 ]; // used in process, not in the init
157
-
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
+ #else
158
197
status = HAL_CRYP_AESCBC_Encrypt (& ctx -> hcryp_aes , (uint8_t * )input , length , (uint8_t * )output , 10 );
198
+ #endif
159
199
}
160
200
return ( status );
161
201
}
0 commit comments