-
Notifications
You must be signed in to change notification settings - Fork 1.6k
nrf_security: Add CCM* support to CRACEN #30789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a040113
515536a
57159ab
28ff387
c18cebb
69947b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -392,6 +392,10 @@ psa_status_t cracen_sw_aes_ccm_update(cracen_aead_operation_t *operation, const | |||
| size_t processed = 0; | ||||
| size_t counter_size = CCM_Q_LEN_FROM_NONCE(operation->nonce_length); | ||||
|
|
||||
| if (output_size < input_length) { | ||||
| return PSA_ERROR_BUFFER_TOO_SMALL; | ||||
| } | ||||
|
|
||||
| operation->ad_finished = true; | ||||
| status = initialize_cbc_mac(operation, &cipher); | ||||
| if (status != PSA_SUCCESS) { | ||||
|
|
@@ -459,6 +463,12 @@ psa_status_t cracen_sw_aes_ccm_finish(cracen_aead_operation_t *operation, uint8_ | |||
| struct sxblkcipher cipher; | ||||
| psa_status_t status; | ||||
|
|
||||
| *ciphertext_length = 0; | ||||
|
|
||||
| if (tag_size < operation->tag_size) { | ||||
| return PSA_ERROR_BUFFER_TOO_SMALL; | ||||
| } | ||||
|
Comment on lines
+468
to
+470
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, it looks like a duplicate to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true |
||||
|
|
||||
| status = initialize_cbc_mac(operation, &cipher); | ||||
| if (status != PSA_SUCCESS) { | ||||
| return status; | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,16 @@ | |
| #include <cracen_sw_aes_ctr.h> | ||
| #endif | ||
|
|
||
| /* CCM* is only defined for L = 2, so its nonce is 15 - L octets and the counter | ||
| * field it leaves inside the 16-octet block is exactly 16 bits wide | ||
| * (IEEE P802.15-4/0537r2 clause 2.2.2 and clause 2.3.1). | ||
| * | ||
| * PSA_NEED_CRACEN_CTR_SIZE_WORKAROUNDS not needed because CCM* wants precisely the hardware CTR | ||
| * width. Adding CCM* to those blocks would replace working hardware with software. | ||
| */ | ||
| #define CCM_STAR_L 2 | ||
| #define CCM_STAR_NONCE_LENGTH (SX_BLKCIPHER_IV_SZ - 1 - CCM_STAR_L) | ||
|
Comment on lines
+42
to
+43
|
||
|
|
||
| static bool is_alg_supported(psa_algorithm_t alg, const psa_key_attributes_t *attributes) | ||
| { | ||
| bool is_supported = false; | ||
|
|
@@ -60,6 +70,10 @@ | |
| IF_ENABLED(PSA_NEED_CRACEN_CTR_AES, | ||
| (is_supported = psa_get_key_type(attributes) == PSA_KEY_TYPE_AES)); | ||
| break; | ||
| case PSA_ALG_CCM_STAR_NO_TAG: | ||
| IF_ENABLED(PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES, | ||
| (is_supported = psa_get_key_type(attributes) == PSA_KEY_TYPE_AES)); | ||
| break; | ||
| case PSA_ALG_ECB_NO_PADDING: | ||
| IF_ENABLED(PSA_NEED_CRACEN_ECB_NO_PADDING_AES, | ||
| (is_supported = psa_get_key_type(attributes) == PSA_KEY_TYPE_AES)); | ||
|
|
@@ -240,6 +254,22 @@ | |
| output_length); | ||
| } | ||
|
|
||
| static size_t single_part_iv_size(psa_algorithm_t alg) | ||
| { | ||
| if (alg == PSA_ALG_STREAM_CIPHER) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly return a removed comment stating the following?
Or how do you feel about moving this value to some macro? There are also other places in this file where it is used (e.g. in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. |
||
| return 12; | ||
| } | ||
|
|
||
| /* CCM* has a 13-octet nonce because L is fixed at 2; everything else prepends a full cipher | ||
| * block. | ||
|
Comment on lines
+263
to
+264
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make more sense to have this comment where |
||
| */ | ||
| if (IS_ENABLED(PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES) && alg == PSA_ALG_CCM_STAR_NO_TAG) { | ||
| return CCM_STAR_NONCE_LENGTH; | ||
| } | ||
|
|
||
| return SX_BLKCIPHER_IV_SZ; | ||
| } | ||
|
|
||
| psa_status_t cracen_cipher_decrypt(const psa_key_attributes_t *attributes, | ||
| const uint8_t *key_buffer, size_t key_buffer_size, | ||
| psa_algorithm_t alg, const uint8_t *input, size_t input_length, | ||
|
|
@@ -251,8 +281,7 @@ | |
|
|
||
| cracen_cipher_operation_t operation = {0}; | ||
| psa_status_t status; | ||
| /* ChaCha20 only supports 12 bytes IV in the single part decryption function */ | ||
| const size_t iv_size = (alg == PSA_ALG_STREAM_CIPHER) ? 12 : SX_BLKCIPHER_IV_SZ; | ||
| const size_t iv_size = single_part_iv_size(alg); | ||
| *output_length = 0; | ||
|
|
||
| #if defined(PSA_NEED_CRACEN_CTR_SIZE_WORKAROUNDS) && defined(PSA_NEED_CRACEN_CTR_AES) | ||
|
|
@@ -309,7 +338,7 @@ | |
| output, output_size, output_length); | ||
| } | ||
|
|
||
| static psa_status_t initialize_cipher(cracen_cipher_operation_t *operation) | ||
|
Check failure on line 341 in subsys/nrf_security/src/drivers/cracen/cracenpsa/src/cracen_psa_cipher.c
|
||
| { | ||
| int sx_status = SX_ERR_UNINITIALIZED_OBJ; | ||
|
|
||
|
|
@@ -353,6 +382,17 @@ | |
| operation->iv); | ||
| } | ||
| break; | ||
| case PSA_ALG_CCM_STAR_NO_TAG: | ||
| if (IS_ENABLED(PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES)) { | ||
| sx_status = operation->dir == CRACEN_DECRYPT | ||
| ? sx_blkcipher_create_aesctr_dec(&operation->cipher, | ||
| &operation->keyref, | ||
| operation->iv) | ||
|
Comment on lines
+387
to
+390
|
||
| : sx_blkcipher_create_aesctr_enc(&operation->cipher, | ||
| &operation->keyref, | ||
| operation->iv); | ||
| } | ||
| break; | ||
|
Comment on lines
+385
to
+395
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could just reuse the |
||
| case PSA_ALG_STREAM_CIPHER: | ||
| if (IS_ENABLED(PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20)) { | ||
| sx_status = operation->dir == CRACEN_DECRYPT | ||
|
|
@@ -440,6 +480,26 @@ | |
| } | ||
| } | ||
|
|
||
| /* CCM* with a zero-length authentication field is AES-CTR over the | ||
| * counter blocks A_i = Flags || Nonce || i, starting at i = 1, where | ||
| * Flags holds L-1 in its low three bits and zero elsewhere (IEEE | ||
| * P802.15-4/0537r2 clause 2.3.1.3). CCM* fixes L = 2, hence the | ||
| * 13-octet nonce that PSA_CIPHER_IV_LENGTH reports for this algorithm. | ||
| */ | ||
| if (IS_ENABLED(PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES) && | ||
| operation->alg == PSA_ALG_CCM_STAR_NO_TAG) { | ||
| if (iv_length != CCM_STAR_NONCE_LENGTH) { | ||
| return PSA_ERROR_INVALID_ARGUMENT; | ||
| } | ||
|
|
||
| operation->iv[0] = CCM_STAR_L - 1; | ||
| memcpy(&operation->iv[1], iv, CCM_STAR_NONCE_LENGTH); | ||
| operation->iv[14] = 0; | ||
| operation->iv[15] = 1; | ||
|
Comment on lines
+495
to
+498
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a mix of define ( |
||
|
|
||
| return PSA_SUCCESS; | ||
| } | ||
|
|
||
| if (iv_length != SX_BLKCIPHER_IV_SZ) { | ||
| return PSA_ERROR_INVALID_ARGUMENT; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,19 @@ config PSA_NEED_CRACEN_CTR_AES | |||||||||||||||||||||||||||
| depends on PSA_WANT_KEY_TYPE_AES | ||||||||||||||||||||||||||||
| depends on PSA_USE_CRACEN_CIPHER_DRIVER | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| config PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES | ||||||||||||||||||||||||||||
| bool | ||||||||||||||||||||||||||||
| default y | ||||||||||||||||||||||||||||
| select PSA_ACCEL_CCM_STAR_NO_TAG_AES_128 | ||||||||||||||||||||||||||||
| select PSA_ACCEL_CCM_STAR_NO_TAG_AES_192 if !CRACEN_HW_VERSION_LITE | ||||||||||||||||||||||||||||
| select PSA_ACCEL_CCM_STAR_NO_TAG_AES_256 | ||||||||||||||||||||||||||||
| depends on PSA_WANT_AES_KEY_SIZE_128 || \ | ||||||||||||||||||||||||||||
| PSA_WANT_AES_KEY_SIZE_192 || \ | ||||||||||||||||||||||||||||
| PSA_WANT_AES_KEY_SIZE_256 | ||||||||||||||||||||||||||||
|
Comment on lines
+65
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
|
||||||||||||||||||||||||||||
| depends on PSA_WANT_ALG_CCM_STAR_NO_TAG | ||||||||||||||||||||||||||||
| depends on PSA_WANT_KEY_TYPE_AES | ||||||||||||||||||||||||||||
| depends on PSA_USE_CRACEN_CIPHER_DRIVER | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| config PSA_NEED_CRACEN_CBC_PKCS7_AES | ||||||||||||||||||||||||||||
| bool | ||||||||||||||||||||||||||||
| default y | ||||||||||||||||||||||||||||
|
|
@@ -101,11 +114,12 @@ config PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20 | |||||||||||||||||||||||||||
| config PSA_NEED_CRACEN_CIPHER_DRIVER | ||||||||||||||||||||||||||||
| bool | ||||||||||||||||||||||||||||
| default y | ||||||||||||||||||||||||||||
| depends on PSA_NEED_CRACEN_CTR_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_CBC_PKCS7_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_CBC_NO_PADDING_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_ECB_NO_PADDING_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20 | ||||||||||||||||||||||||||||
| depends on PSA_NEED_CRACEN_CTR_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_CCM_STAR_NO_TAG_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_CBC_PKCS7_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_CBC_NO_PADDING_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_ECB_NO_PADDING_AES || \ | ||||||||||||||||||||||||||||
| PSA_NEED_CRACEN_STREAM_CIPHER_CHACHA20 | ||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and bring back alignment of |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # CRACEN Key Agreement Driver | ||||||||||||||||||||||||||||
| config PSA_NEED_CRACEN_ECDH_BRAINPOOL_P_R1_256 | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commit title: would make sense to have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider moving this check to
cracen_aead_update()instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oki