Skip to content

Commit 21be437

Browse files
committed
[nrf toup][crypto] Add support for single-part AEAD operation
This commit add support for single-part AEAD operations. Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
1 parent 00413ab commit 21be437

6 files changed

Lines changed: 80 additions & 9 deletions

File tree

config/nrfconnect/chip-module/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ else()
196196
endif()
197197

198198
if (CONFIG_CHIP_CRYPTO_PSA)
199-
matter_add_gn_arg_string("chip_crypto" "psa")
200-
matter_add_gn_arg_bool ("chip_crypto_psa_spake2p" CONFIG_PSA_WANT_ALG_SPAKE2P_MATTER)
201-
matter_add_gn_arg_bool ("chip_use_cracen_kmu" CONFIG_CHIP_STORE_KEYS_IN_KMU)
199+
matter_add_gn_arg_string("chip_crypto" "psa")
200+
matter_add_gn_arg_bool ("chip_crypto_psa_spake2p" CONFIG_PSA_WANT_ALG_SPAKE2P_MATTER)
201+
matter_add_gn_arg_bool ("chip_crypto_psa_aead_single_part" CONFIG_CHIP_CRYPTO_PSA_AEAD_SINGLE_PART)
202+
matter_add_gn_arg_bool ("chip_use_cracen_kmu" CONFIG_CHIP_STORE_KEYS_IN_KMU)
202203
endif()
203204

204205
if (BOARD STREQUAL "native_sim")

config/nrfconnect/chip-module/Kconfig.defaults

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ config CHIP_CRYPTO_PSA
351351

352352
if CHIP_CRYPTO_PSA
353353

354+
config CHIP_CRYPTO_PSA_AEAD_SINGLE_PART
355+
default y if SOC_NRF54LM20A
356+
354357
config PSA_CRYPTO_DRIVER_OBERON
355358
default y if (SOC_SERIES_NRF52X || SOC_SERIES_NRF53X)
356359

config/zephyr/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ config CHIP_CRYPTO_PSA
291291
based on the PSA crypto API (instead of the default implementation, which
292292
is based on the legacy mbedTLS APIs).
293293

294+
config CHIP_CRYPTO_PSA_AEAD_SINGLE_PART
295+
bool "Use PSA AEAD single-part API"
296+
depends on CHIP_CRYPTO_PSA
297+
help
298+
When enabled, the single-part AEAD API is used with a compile-time
299+
static buffer. Otherwise, the multipart API path is used.
300+
294301
config CHIP_PERSISTENT_SUBSCRIPTIONS
295302
bool "Persistent subscriptions"
296303
help

src/crypto/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ buildconfig_header("crypto_buildconfig") {
5252
"CHIP_CRYPTO_MBEDTLS=${chip_crypto_mbedtls}",
5353
"CHIP_CRYPTO_PSA=${chip_crypto_psa}",
5454
"CHIP_CRYPTO_PSA_SPAKE2P=${chip_crypto_psa_spake2p}",
55+
"CHIP_CRYPTO_PSA_AEAD_SINGLE_PART=${chip_crypto_psa_aead_single_part}",
5556
"CHIP_CRYPTO_OPENSSL=${chip_crypto_openssl}",
5657
"CHIP_CRYPTO_BORINGSSL=${chip_crypto_boringssl}",
5758
"CHIP_CRYPTO_PLATFORM=${chip_crypto_platform}",

src/crypto/CHIPCryptoPALPSA.cpp

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
#include <string.h>
4444
#include <type_traits>
4545

46+
#if CHIP_CRYPTO_PSA_AEAD_SINGLE_PART
47+
#define PSA_AEAD_MAX_PLAINTEXT CHIP_CONFIG_DEFAULT_UDP_MTU_SIZE
48+
#define PSA_AEAD_MAX_TAG 16
49+
#define PSA_AEAD_TEMP_BUFFER_SIZE (PSA_AEAD_MAX_PLAINTEXT + PSA_AEAD_MAX_TAG)
50+
#endif
51+
4652
namespace chip {
4753
namespace Crypto {
4854

@@ -71,8 +77,32 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
7177

7278
const psa_algorithm_t algorithm = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_length);
7379
psa_status_t status = PSA_SUCCESS;
80+
size_t out_length = 0;
81+
82+
#ifdef CHIP_CRYPTO_PSA_AEAD_SINGLE_PART
83+
uint8_t temp_buf[PSA_AEAD_TEMP_BUFFER_SIZE];
84+
85+
VerifyOrReturnError(plaintext_length + tag_length <= PSA_AEAD_TEMP_BUFFER_SIZE,
86+
CHIP_ERROR_INVALID_ARGUMENT);
87+
88+
status = psa_aead_encrypt(key.As<psa_key_id_t>(), algorithm,
89+
nonce, nonce_length,
90+
aad, aad_length,
91+
plaintext, plaintext_length,
92+
temp_buf, sizeof(temp_buf),
93+
&out_length);
94+
95+
VerifyOrReturnError(status == PSA_SUCCESS && out_length == plaintext_length + tag_length,
96+
CHIP_ERROR_INTERNAL);
97+
98+
if (plaintext_length)
99+
{
100+
memcpy(ciphertext, temp_buf, plaintext_length);
101+
}
102+
103+
memcpy(tag, temp_buf + plaintext_length, tag_length);
104+
#else
74105
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
75-
size_t out_length;
76106
size_t tag_out_length;
77107

78108
status = psa_aead_encrypt_setup(&operation, key.As<psa_key_id_t>(), algorithm);
@@ -110,6 +140,7 @@ CHIP_ERROR AES_CCM_encrypt(const uint8_t * plaintext, size_t plaintext_length, c
110140
status = psa_aead_finish(&operation, nullptr, 0, &out_length, tag, tag_length, &tag_out_length);
111141
}
112142
VerifyOrReturnError(status == PSA_SUCCESS && tag_length == tag_out_length, CHIP_ERROR_INTERNAL);
143+
#endif
113144

114145
return CHIP_NO_ERROR;
115146
}
@@ -125,8 +156,32 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_length,
125156

126157
const psa_algorithm_t algorithm = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, tag_length);
127158
psa_status_t status = PSA_SUCCESS;
159+
size_t out_length = 0;
160+
161+
#ifdef CHIP_CRYPTO_PSA_AEAD_SINGLE_PART
162+
uint8_t temp_buf[PSA_AEAD_TEMP_BUFFER_SIZE];
163+
164+
VerifyOrReturnError(ciphertext_length + tag_length <= PSA_AEAD_TEMP_BUFFER_SIZE,
165+
CHIP_ERROR_INVALID_ARGUMENT);
166+
167+
if (ciphertext_length)
168+
{
169+
memcpy(temp_buf, ciphertext, ciphertext_length);
170+
}
171+
172+
memcpy(temp_buf + ciphertext_length, tag, tag_length);
173+
174+
status = psa_aead_decrypt(key.As<psa_key_id_t>(), algorithm,
175+
nonce, nonce_length,
176+
aad, aad_length,
177+
temp_buf, ciphertext_length + tag_length,
178+
plaintext, ciphertext_length,
179+
&out_length);
180+
181+
VerifyOrReturnError(status == PSA_SUCCESS && out_length == ciphertext_length,
182+
CHIP_ERROR_INTERNAL);
183+
#else
128184
psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
129-
size_t outLength;
130185

131186
status = psa_aead_decrypt_setup(&operation, key.As<psa_key_id_t>(), algorithm);
132187
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
@@ -150,20 +205,21 @@ CHIP_ERROR AES_CCM_decrypt(const uint8_t * ciphertext, size_t ciphertext_length,
150205
if (ciphertext_length != 0)
151206
{
152207
status = psa_aead_update(&operation, ciphertext, ciphertext_length, plaintext,
153-
PSA_AEAD_UPDATE_OUTPUT_SIZE(PSA_KEY_TYPE_AES, algorithm, ciphertext_length), &outLength);
208+
PSA_AEAD_UPDATE_OUTPUT_SIZE(PSA_KEY_TYPE_AES, algorithm, ciphertext_length), &out_length);
154209
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
155210

156-
plaintext += outLength;
211+
plaintext += out_length;
157212

158-
status = psa_aead_verify(&operation, plaintext, PSA_AEAD_VERIFY_OUTPUT_SIZE(PSA_KEY_TYPE_AES, algorithm), &outLength, tag,
213+
status = psa_aead_verify(&operation, plaintext, PSA_AEAD_VERIFY_OUTPUT_SIZE(PSA_KEY_TYPE_AES, algorithm), &out_length, tag,
159214
tag_length);
160215
}
161216
else
162217
{
163-
status = psa_aead_verify(&operation, nullptr, 0, &outLength, tag, tag_length);
218+
status = psa_aead_verify(&operation, nullptr, 0, &out_length, tag, tag_length);
164219
}
165220

166221
VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL);
222+
#endif
167223

168224
return CHIP_NO_ERROR;
169225
}

src/crypto/crypto.gni

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ declare_args() {
2222

2323
# Use PSA Spake2+ implementation. Only used if chip_crypto == "psa"
2424
chip_crypto_psa_spake2p = false
25+
26+
# Use PSA AEAD single-part implementation.
27+
chip_crypto_psa_aead_single_part = false
2528
}
2629

2730
assert(

0 commit comments

Comments
 (0)