@@ -67,27 +67,37 @@ typedef HAL_StatusTypeDef status_t;
6767typedef status_t (* hal_cryp_aes_op_func_t )(CRYP_HandleTypeDef * hcryp , uint8_t * in_data ,
6868 uint16_t size , uint8_t * out_data , uint32_t timeout );
6969
70+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
7071#define hal_ecb_encrypt_op HAL_CRYP_AESECB_Encrypt
7172#define hal_ecb_decrypt_op HAL_CRYP_AESECB_Decrypt
7273#define hal_cbc_encrypt_op HAL_CRYP_AESCBC_Encrypt
7374#define hal_cbc_decrypt_op HAL_CRYP_AESCBC_Decrypt
7475#define hal_ctr_encrypt_op HAL_CRYP_AESCTR_Encrypt
7576#define hal_ctr_decrypt_op HAL_CRYP_AESCTR_Decrypt
77+ #else
78+ #define hal_ecb_encrypt_op hal_encrypt
79+ #define hal_ecb_decrypt_op hal_decrypt
80+ #define hal_cbc_encrypt_op hal_encrypt
81+ #define hal_cbc_decrypt_op hal_decrypt
82+ #define hal_ctr_encrypt_op hal_encrypt
83+ #define hal_ctr_decrypt_op hal_decrypt
84+ #endif
7685
77- static int copy_reverse_words (uint8_t * dst_buf , int dst_len ,
78- const uint8_t * src_buf , int src_len )
86+ static int copy_words_adjust_endianness (uint8_t * dst_buf , int dst_len , const uint8_t * src_buf ,
87+ int src_len )
7988{
80- int i ;
81-
8289 if ((dst_len < src_len ) || ((dst_len % 4 ) != 0 )) {
8390 LOG_ERR ("Buffer length error" );
8491 return - EINVAL ;
8592 }
8693
8794 memcpy (dst_buf , src_buf , src_len );
88- for (i = 0 ; i < dst_len ; i += sizeof (uint32_t )) {
95+
96+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
97+ for (int i = 0 ; i < dst_len ; i += sizeof (uint32_t )) {
8998 sys_mem_swap (& dst_buf [i ], sizeof (uint32_t ));
9099 }
100+ #endif
91101
92102 return 0 ;
93103}
@@ -102,12 +112,19 @@ static int do_aes(struct cipher_ctx *ctx, hal_cryp_aes_op_func_t fn, uint8_t *in
102112
103113 k_sem_take (& data -> device_sem , K_FOREVER );
104114
115+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
116+ /* Device is initialized from the configuration in the encryption/decryption function
117+ * called bellow.
118+ */
119+ memcpy (& data -> hcryp .Init , & session -> config , sizeof (session -> config ));
120+ #else
105121 status = HAL_CRYP_SetConfig (& data -> hcryp , & session -> config );
106122 if (status != HAL_OK ) {
107123 LOG_ERR ("Configuration error" );
108124 k_sem_give (& data -> device_sem );
109125 return - EIO ;
110126 }
127+ #endif
111128
112129 status = fn (& data -> hcryp , in_buf , in_len , out_buf , HAL_MAX_DELAY );
113130 if (status != HAL_OK ) {
@@ -121,6 +138,7 @@ static int do_aes(struct cipher_ctx *ctx, hal_cryp_aes_op_func_t fn, uint8_t *in
121138 return 0 ;
122139}
123140
141+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
124142static status_t hal_encrypt (CRYP_HandleTypeDef * hcryp , uint8_t * pPlainData , uint16_t Size ,
125143 uint8_t * pCypherData , uint32_t Timeout )
126144{
@@ -134,6 +152,7 @@ static status_t hal_decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uin
134152 return HAL_CRYP_Decrypt (hcryp , (uint32_t * )pCypherData , Size , (uint32_t * )pPlainData ,
135153 Timeout );
136154}
155+ #endif
137156
138157static int crypto_stm32_ecb_encrypt (struct cipher_ctx * ctx ,
139158 struct cipher_pkt * pkt )
@@ -186,7 +205,7 @@ static int crypto_stm32_cbc_encrypt(struct cipher_ctx *ctx,
186205
187206 struct crypto_stm32_session * session = CRYPTO_STM32_SESSN (ctx );
188207
189- (void )copy_reverse_words ((uint8_t * )vec , sizeof (vec ), iv , BLOCK_LEN_BYTES );
208+ (void )copy_words_adjust_endianness ((uint8_t * )vec , sizeof (vec ), iv , BLOCK_LEN_BYTES );
190209
191210 session -> config .pInitVect = vec ;
192211
@@ -213,7 +232,7 @@ static int crypto_stm32_cbc_decrypt(struct cipher_ctx *ctx,
213232
214233 struct crypto_stm32_session * session = CRYPTO_STM32_SESSN (ctx );
215234
216- (void )copy_reverse_words ((uint8_t * )vec , sizeof (vec ), iv , BLOCK_LEN_BYTES );
235+ (void )copy_words_adjust_endianness ((uint8_t * )vec , sizeof (vec ), iv , BLOCK_LEN_BYTES );
217236
218237 session -> config .pInitVect = vec ;
219238
@@ -238,7 +257,7 @@ static int crypto_stm32_ctr_encrypt(struct cipher_ctx *ctx,
238257
239258 struct crypto_stm32_session * session = CRYPTO_STM32_SESSN (ctx );
240259
241- if (copy_reverse_words ((uint8_t * )ctr , sizeof (ctr ), iv , ivlen ) != 0 ) {
260+ if (copy_words_adjust_endianness ((uint8_t * )ctr , sizeof (ctr ), iv , ivlen ) != 0 ) {
242261 return - EIO ;
243262 }
244263
@@ -261,7 +280,7 @@ static int crypto_stm32_ctr_decrypt(struct cipher_ctx *ctx,
261280
262281 struct crypto_stm32_session * session = CRYPTO_STM32_SESSN (ctx );
263282
264- if (copy_reverse_words ((uint8_t * )ctr , sizeof (ctr ), iv , ivlen ) != 0 ) {
283+ if (copy_words_adjust_endianness ((uint8_t * )ctr , sizeof (ctr ), iv , ivlen ) != 0 ) {
265284 return - EIO ;
266285 }
267286
@@ -305,8 +324,6 @@ static int crypto_stm32_session_setup(const struct device *dev,
305324 int ctx_idx , ret ;
306325 struct crypto_stm32_session * session ;
307326
308- struct crypto_stm32_data * data = CRYPTO_STM32_DATA (dev );
309-
310327 if (ctx -> flags & ~(CRYP_SUPPORT )) {
311328 LOG_ERR ("Unsupported flag" );
312329 return - EINVAL ;
@@ -353,13 +370,17 @@ static int crypto_stm32_session_setup(const struct device *dev,
353370 session = & crypto_stm32_sessions [ctx_idx ];
354371 memset (& session -> config , 0 , sizeof (session -> config ));
355372
373+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
374+ struct crypto_stm32_data * data = CRYPTO_STM32_DATA (dev );
375+
356376 if (data -> hcryp .State == HAL_CRYP_STATE_RESET ) {
357377 if (HAL_CRYP_Init (& data -> hcryp ) != HAL_OK ) {
358378 LOG_ERR ("Initialization error" );
359379 session -> in_use = false;
360380 return - EIO ;
361381 }
362382 }
383+ #endif
363384
364385 switch (ctx -> keylen ) {
365386 case 16U :
@@ -378,15 +399,21 @@ static int crypto_stm32_session_setup(const struct device *dev,
378399 if (op_type == CRYPTO_CIPHER_OP_ENCRYPT ) {
379400 switch (mode ) {
380401 case CRYPTO_CIPHER_MODE_ECB :
402+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
381403 session -> config .Algorithm = CRYP_AES_ECB ;
404+ #endif
382405 ctx -> ops .block_crypt_hndlr = crypto_stm32_ecb_encrypt ;
383406 break ;
384407 case CRYPTO_CIPHER_MODE_CBC :
408+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
385409 session -> config .Algorithm = CRYP_AES_CBC ;
410+ #endif
386411 ctx -> ops .cbc_crypt_hndlr = crypto_stm32_cbc_encrypt ;
387412 break ;
388413 case CRYPTO_CIPHER_MODE_CTR :
414+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
389415 session -> config .Algorithm = CRYP_AES_CTR ;
416+ #endif
390417 ctx -> ops .ctr_crypt_hndlr = crypto_stm32_ctr_encrypt ;
391418 break ;
392419 default :
@@ -395,31 +422,40 @@ static int crypto_stm32_session_setup(const struct device *dev,
395422 } else {
396423 switch (mode ) {
397424 case CRYPTO_CIPHER_MODE_ECB :
425+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
398426 session -> config .Algorithm = CRYP_AES_ECB ;
427+ #endif
399428 ctx -> ops .block_crypt_hndlr = crypto_stm32_ecb_decrypt ;
400429 break ;
401430 case CRYPTO_CIPHER_MODE_CBC :
431+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
402432 session -> config .Algorithm = CRYP_AES_CBC ;
433+ #endif
403434 ctx -> ops .cbc_crypt_hndlr = crypto_stm32_cbc_decrypt ;
404435 break ;
405436 case CRYPTO_CIPHER_MODE_CTR :
437+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
406438 session -> config .Algorithm = CRYP_AES_CTR ;
439+ #endif
407440 ctx -> ops .ctr_crypt_hndlr = crypto_stm32_ctr_decrypt ;
408441 break ;
409442 default :
410443 break ;
411444 }
412445 }
413446
414- ret = copy_reverse_words ((uint8_t * )session -> key , CRYPTO_STM32_AES_MAX_KEY_LEN ,
447+ ret = copy_words_adjust_endianness ((uint8_t * )session -> key , CRYPTO_STM32_AES_MAX_KEY_LEN ,
415448 ctx -> key .bit_stream , ctx -> keylen );
416449 if (ret != 0 ) {
417450 return - EIO ;
418451 }
419452
420453 session -> config .pKey = session -> key ;
421454 session -> config .DataType = CRYP_DATATYPE_8B ;
455+
456+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
422457 session -> config .DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE ;
458+ #endif
423459
424460 ctx -> drv_sessn_state = session ;
425461 ctx -> device = dev ;
@@ -448,12 +484,14 @@ static int crypto_stm32_session_free(const struct device *dev,
448484 }
449485 }
450486
487+ #if !DT_HAS_COMPAT_STATUS_OKAY (st_stm32l4_aes )
451488 /* Deinitialize and reset peripheral. */
452489 if (HAL_CRYP_DeInit (& data -> hcryp ) != HAL_OK ) {
453490 LOG_ERR ("Deinitialization error" );
454491 k_sem_give (& data -> session_sem );
455492 return - EIO ;
456493 }
494+ #endif
457495
458496 (void )reset_line_toggle_dt (& cfg -> reset );
459497
0 commit comments