Skip to content

Commit 8f0de84

Browse files
lucasdietrichcarlescufi
authored andcommitted
drivers: crypto: Refactor encryption and decryption functions for STM32 AES
This patch introduces a unified function pointer approach to handle encryption and decryption operations for the STM32 AES accelerator. - Replace separate `do_encrypt` and `do_decrypt` functions with a generic `do_aes` function, using function pointers to AES HAL functions. Signed-off-by: Lucas Dietrich <[email protected]>
1 parent fbeda59 commit 8f0de84

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

drivers/crypto/crypto_stm32.c

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ LOG_MODULE_REGISTER(crypto_stm32);
4747

4848
struct crypto_stm32_session crypto_stm32_sessions[CRYPTO_MAX_SESSION];
4949

50+
typedef HAL_StatusTypeDef status_t;
51+
52+
/**
53+
* @brief Function pointer type for AES encryption/decryption operations.
54+
*
55+
* This type defines a function pointer for generic AES operations.
56+
*
57+
* @param hcryp Pointer to a CRYP_HandleTypeDef structure that contains
58+
* the configuration information for the CRYP module.
59+
* @param in_data Pointer to input data (plaintext for encryption or ciphertext for decryption).
60+
* @param size Length of the input data in bytes.
61+
* @param out_data Pointer to output data (ciphertext for encryption or plaintext for
62+
* decryption).
63+
* @param timeout Timeout duration in milliseconds.
64+
*
65+
* @retval status_t HAL status of the operation.
66+
*/
67+
typedef status_t (*hal_cryp_aes_op_func_t)(CRYP_HandleTypeDef *hcryp, uint8_t *in_data,
68+
uint16_t size, uint8_t *out_data, uint32_t timeout);
69+
70+
#define hal_ecb_encrypt_op HAL_CRYP_AESECB_Encrypt
71+
#define hal_ecb_decrypt_op HAL_CRYP_AESECB_Decrypt
72+
#define hal_cbc_encrypt_op HAL_CRYP_AESCBC_Encrypt
73+
#define hal_cbc_decrypt_op HAL_CRYP_AESCBC_Decrypt
74+
#define hal_ctr_encrypt_op HAL_CRYP_AESCTR_Encrypt
75+
#define hal_ctr_decrypt_op HAL_CRYP_AESCTR_Decrypt
76+
5077
static int copy_reverse_words(uint8_t *dst_buf, int dst_len,
5178
const uint8_t *src_buf, int src_len)
5279
{
@@ -65,10 +92,10 @@ static int copy_reverse_words(uint8_t *dst_buf, int dst_len,
6592
return 0;
6693
}
6794

68-
static int do_encrypt(struct cipher_ctx *ctx, uint8_t *in_buf, int in_len,
95+
static int do_aes(struct cipher_ctx *ctx, hal_cryp_aes_op_func_t fn, uint8_t *in_buf, int in_len,
6996
uint8_t *out_buf)
7097
{
71-
HAL_StatusTypeDef status;
98+
status_t status;
7299

73100
struct crypto_stm32_data *data = CRYPTO_STM32_DATA(ctx->device);
74101
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
@@ -82,10 +109,9 @@ static int do_encrypt(struct cipher_ctx *ctx, uint8_t *in_buf, int in_len,
82109
return -EIO;
83110
}
84111

85-
status = HAL_CRYP_Encrypt(&data->hcryp, (uint32_t *)in_buf, in_len,
86-
(uint32_t *)out_buf, HAL_MAX_DELAY);
112+
status = fn(&data->hcryp, in_buf, in_len, out_buf, HAL_MAX_DELAY);
87113
if (status != HAL_OK) {
88-
LOG_ERR("Encryption error");
114+
LOG_ERR("Encryption/decryption error");
89115
k_sem_give(&data->device_sem);
90116
return -EIO;
91117
}
@@ -95,34 +121,18 @@ static int do_encrypt(struct cipher_ctx *ctx, uint8_t *in_buf, int in_len,
95121
return 0;
96122
}
97123

98-
static int do_decrypt(struct cipher_ctx *ctx, uint8_t *in_buf, int in_len,
99-
uint8_t *out_buf)
124+
static status_t hal_encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size,
125+
uint8_t *pCypherData, uint32_t Timeout)
100126
{
101-
HAL_StatusTypeDef status;
102-
103-
struct crypto_stm32_data *data = CRYPTO_STM32_DATA(ctx->device);
104-
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
105-
106-
k_sem_take(&data->device_sem, K_FOREVER);
107-
108-
status = HAL_CRYP_SetConfig(&data->hcryp, &session->config);
109-
if (status != HAL_OK) {
110-
LOG_ERR("Configuration error");
111-
k_sem_give(&data->device_sem);
112-
return -EIO;
113-
}
114-
115-
status = HAL_CRYP_Decrypt(&data->hcryp, (uint32_t *)in_buf, in_len,
116-
(uint32_t *)out_buf, HAL_MAX_DELAY);
117-
if (status != HAL_OK) {
118-
LOG_ERR("Decryption error");
119-
k_sem_give(&data->device_sem);
120-
return -EIO;
121-
}
122-
123-
k_sem_give(&data->device_sem);
127+
return HAL_CRYP_Encrypt(hcryp, (uint32_t *)pPlainData, Size, (uint32_t *)pCypherData,
128+
Timeout);
129+
}
124130

125-
return 0;
131+
static status_t hal_decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size,
132+
uint8_t *pPlainData, uint32_t Timeout)
133+
{
134+
return HAL_CRYP_Decrypt(hcryp, (uint32_t *)pCypherData, Size, (uint32_t *)pPlainData,
135+
Timeout);
126136
}
127137

128138
static int crypto_stm32_ecb_encrypt(struct cipher_ctx *ctx,
@@ -138,7 +148,7 @@ static int crypto_stm32_ecb_encrypt(struct cipher_ctx *ctx,
138148
return -EINVAL;
139149
}
140150

141-
ret = do_encrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
151+
ret = do_aes(ctx, hal_ecb_encrypt_op, pkt->in_buf, pkt->in_len, pkt->out_buf);
142152
if (ret == 0) {
143153
pkt->out_len = 16;
144154
}
@@ -159,7 +169,7 @@ static int crypto_stm32_ecb_decrypt(struct cipher_ctx *ctx,
159169
return -EINVAL;
160170
}
161171

162-
ret = do_decrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
172+
ret = do_aes(ctx, hal_ecb_decrypt_op, pkt->in_buf, pkt->in_len, pkt->out_buf);
163173
if (ret == 0) {
164174
pkt->out_len = 16;
165175
}
@@ -186,8 +196,7 @@ static int crypto_stm32_cbc_encrypt(struct cipher_ctx *ctx,
186196
out_offset = 16;
187197
}
188198

189-
ret = do_encrypt(ctx, pkt->in_buf, pkt->in_len,
190-
pkt->out_buf + out_offset);
199+
ret = do_aes(ctx, hal_cbc_encrypt_op, pkt->in_buf, pkt->in_len, pkt->out_buf + out_offset);
191200
if (ret == 0) {
192201
pkt->out_len = pkt->in_len + out_offset;
193202
}
@@ -212,8 +221,7 @@ static int crypto_stm32_cbc_decrypt(struct cipher_ctx *ctx,
212221
in_offset = 16;
213222
}
214223

215-
ret = do_decrypt(ctx, pkt->in_buf + in_offset, pkt->in_len,
216-
pkt->out_buf);
224+
ret = do_aes(ctx, hal_cbc_decrypt_op, pkt->in_buf + in_offset, pkt->in_len, pkt->out_buf);
217225
if (ret == 0) {
218226
pkt->out_len = pkt->in_len - in_offset;
219227
}
@@ -236,7 +244,7 @@ static int crypto_stm32_ctr_encrypt(struct cipher_ctx *ctx,
236244

237245
session->config.pInitVect = ctr;
238246

239-
ret = do_encrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
247+
ret = do_aes(ctx, hal_ctr_encrypt_op, pkt->in_buf, pkt->in_len, pkt->out_buf);
240248
if (ret == 0) {
241249
pkt->out_len = pkt->in_len;
242250
}
@@ -259,7 +267,7 @@ static int crypto_stm32_ctr_decrypt(struct cipher_ctx *ctx,
259267

260268
session->config.pInitVect = ctr;
261269

262-
ret = do_decrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
270+
ret = do_aes(ctx, hal_ctr_decrypt_op, pkt->in_buf, pkt->in_len, pkt->out_buf);
263271
if (ret == 0) {
264272
pkt->out_len = pkt->in_len;
265273
}

0 commit comments

Comments
 (0)