Skip to content

Commit e35974c

Browse files
erwangocarlescufi
authored andcommitted
drivers: crypto: stm32: Replace buffer len assert by error logic
In case asserts are deactivated, no check is done on buffers length. Remove asserts and return an error when lengths are not correct. Check error in case length is set by API user. Signed-off-by: Erwan Gouriou <[email protected]>
1 parent d647a85 commit e35974c

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

drivers/crypto/crypto_stm32.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ LOG_MODULE_REGISTER(crypto_stm32);
5050

5151
struct crypto_stm32_session crypto_stm32_sessions[CRYPTO_MAX_SESSION];
5252

53-
static void copy_reverse_words(uint8_t *dst_buf, int dst_len,
54-
uint8_t *src_buf, int src_len)
53+
static int copy_reverse_words(uint8_t *dst_buf, int dst_len,
54+
uint8_t *src_buf, int src_len)
5555
{
5656
int i;
5757

58-
__ASSERT_NO_MSG(dst_len >= src_len);
59-
__ASSERT_NO_MSG((dst_len % 4) == 0);
58+
if ((dst_len < src_len) || ((dst_len % 4) != 0)) {
59+
LOG_ERR("Buffer length error");
60+
return -EINVAL;
61+
}
6062

6163
memcpy(dst_buf, src_buf, src_len);
6264
for (i = 0; i < dst_len; i += sizeof(uint32_t)) {
6365
sys_mem_swap(&dst_buf[i], sizeof(uint32_t));
6466
}
67+
68+
return 0;
6569
}
6670

6771
static int do_encrypt(struct cipher_ctx *ctx, uint8_t *in_buf, int in_len,
@@ -175,7 +179,8 @@ static int crypto_stm32_cbc_encrypt(struct cipher_ctx *ctx,
175179

176180
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
177181

178-
copy_reverse_words((uint8_t *)vec, sizeof(vec), iv, BLOCK_LEN_BYTES);
182+
(void)copy_reverse_words((uint8_t *)vec, sizeof(vec), iv, BLOCK_LEN_BYTES);
183+
179184
session->config.pInitVect = vec;
180185

181186
if ((ctx->flags & CAP_NO_IV_PREFIX) == 0U) {
@@ -202,7 +207,8 @@ static int crypto_stm32_cbc_decrypt(struct cipher_ctx *ctx,
202207

203208
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
204209

205-
copy_reverse_words((uint8_t *)vec, sizeof(vec), iv, BLOCK_LEN_BYTES);
210+
(void)copy_reverse_words((uint8_t *)vec, sizeof(vec), iv, BLOCK_LEN_BYTES);
211+
206212
session->config.pInitVect = vec;
207213

208214
if ((ctx->flags & CAP_NO_IV_PREFIX) == 0U) {
@@ -227,7 +233,10 @@ static int crypto_stm32_ctr_encrypt(struct cipher_ctx *ctx,
227233

228234
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
229235

230-
copy_reverse_words((uint8_t *)ctr, sizeof(ctr), iv, ivlen);
236+
if (copy_reverse_words((uint8_t *)ctr, sizeof(ctr), iv, ivlen) != 0) {
237+
return -EIO;
238+
}
239+
231240
session->config.pInitVect = ctr;
232241

233242
ret = do_encrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
@@ -247,7 +256,10 @@ static int crypto_stm32_ctr_decrypt(struct cipher_ctx *ctx,
247256

248257
struct crypto_stm32_session *session = CRYPTO_STM32_SESSN(ctx);
249258

250-
copy_reverse_words((uint8_t *)ctr, sizeof(ctr), iv, ivlen);
259+
if (copy_reverse_words((uint8_t *)ctr, sizeof(ctr), iv, ivlen) != 0) {
260+
return -EIO;
261+
}
262+
251263
session->config.pInitVect = ctr;
252264

253265
ret = do_decrypt(ctx, pkt->in_buf, pkt->in_len, pkt->out_buf);
@@ -285,7 +297,7 @@ static int crypto_stm32_session_setup(const struct device *dev,
285297
enum cipher_mode mode,
286298
enum cipher_op op_type)
287299
{
288-
int ctx_idx;
300+
int ctx_idx, ret;
289301
struct crypto_stm32_session *session;
290302

291303
struct crypto_stm32_data *data = CRYPTO_STM32_DATA(dev);
@@ -394,8 +406,11 @@ static int crypto_stm32_session_setup(const struct device *dev,
394406
}
395407
}
396408

397-
copy_reverse_words((uint8_t *)session->key, CRYPTO_STM32_AES_MAX_KEY_LEN,
398-
ctx->key.bit_stream, ctx->keylen);
409+
ret = copy_reverse_words((uint8_t *)session->key, CRYPTO_STM32_AES_MAX_KEY_LEN,
410+
ctx->key.bit_stream, ctx->keylen);
411+
if (ret != 0) {
412+
return -EIO;
413+
}
399414

400415
session->config.pKey = session->key;
401416
session->config.DataType = CRYP_DATATYPE_8B;

0 commit comments

Comments
 (0)