Skip to content

Commit e494b35

Browse files
committed
drivers: crypto: stm32: make tag validation optionable
Allows enabling or disabling AES-CCM and AES-GCM tag validation in the driver. This gives users the flexibility to use an external, preferably constant-time tag validation function if desired. Signed-off-by: Georgij Černyšiov <geo.cgv@gmail.com>
1 parent 30495ed commit e494b35

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

‎drivers/crypto/crypto_stm32.c‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,17 @@ static int crypto_stm32_gcm_decrypt(struct cipher_ctx *ctx, struct cipher_aead_p
426426
out:
427427
k_sem_give(&data->device_sem);
428428

429-
if (ret < 0) {
430-
return ret;
431-
}
429+
IF_ENABLED(CRYPTO_STM32_AES_CCM_GCM_VALIDATE_TAG, (
430+
/* memcmp is vulnerable to timing attacks */
431+
if ((ret >= 0) &&
432+
(memcmp(tag, apkt->tag, ctx->mode_params.ccm_info.tag_len) != 0)) {
433+
/* auth/tag verification fails */
434+
ret = -EFAULT;
435+
}
436+
));
432437

433-
/* memcmp is vulnerable to timing attacks */
434-
if (memcmp(tag, apkt->tag, ctx->mode_params.gcm_info.tag_len) != 0) {
435-
/* auth/tag verification fails */
438+
if (ret < 0) {
436439
apkt->pkt->out_len = 0;
437-
ret = -EFAULT;
438440
} else {
439441
apkt->pkt->out_len = apkt->pkt->in_len;
440442
}
@@ -592,11 +594,17 @@ static int crypto_stm32_ccm_decrypt(struct cipher_ctx *ctx, struct cipher_aead_p
592594
out:
593595
k_sem_give(&data->device_sem);
594596

595-
/* memcmp is vulnerable to timing attacks */
596-
if (memcmp(tag, apkt->tag, ctx->mode_params.ccm_info.tag_len) != 0) {
597-
/* auth/tag verification fails */
597+
IF_ENABLED(CRYPTO_STM32_AES_CCM_GCM_VALIDATE_TAG, (
598+
/* memcmp is vulnerable to timing attacks */
599+
if ((ret >= 0) &&
600+
(memcmp(tag, apkt->tag, ctx->mode_params.ccm_info.tag_len) != 0)) {
601+
/* auth/tag verification fails */
602+
ret = -EFAULT;
603+
}
604+
));
605+
606+
if (ret < 0) {
598607
apkt->pkt->out_len = 0;
599-
ret = -EFAULT;
600608
} else {
601609
apkt->pkt->out_len = apkt->pkt->in_len;
602610
}

0 commit comments

Comments
 (0)