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+
4652namespace chip {
4753namespace 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}
0 commit comments