Skip to content

Commit f1fe49f

Browse files
amjoul01adeaarm
authored andcommitted
cc3xx: support single-shot AES CCM when tunneling is off
Perform AES CTR encryption prior to computing CBC-MAC in CCM mode. Change-Id: I66cf4fcdd5509e3bab2a4772c79526e973b2a137 Signed-off-by: Amjad Ouled-Ameur <amjad.ouled-ameur@arm.com>
1 parent ff267c5 commit f1fe49f

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

platform/ext/target/arm/drivers/cc3xx/psa_driver_api/src/cc3xx_psa_aead.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "cc3xx_psa_aead.h"
17+
#include "cc3xx_psa_cipher.h"
1718
#include "cc3xx_crypto_primitives_private.h"
1819
#include "cc3xx_misc.h"
1920
#include "cc3xx_stdlib.h"
@@ -167,6 +168,12 @@ static psa_status_t aead_crypt(
167168
{
168169
cc3xx_aes_keysize_t key_size;
169170
cc3xx_aes_mode_t mode;
171+
#if !defined(CC3XX_CONFIG_AES_TUNNELLING_ENABLE) && defined(PSA_WANT_ALG_CCM)
172+
uint8_t ctr[AES_IV_LEN] = {0};
173+
bool ctr_required = ((default_alg == PSA_ALG_CCM) && (data_minus_tag > 0) &&
174+
(output_size > 0)) ? true : false;
175+
#endif /* !CC3XX_CONFIG_AES_TUNNELLING_ENABLE && PSA_WANT_ALG_CCM */
176+
170177

171178
switch (key_buffer_size) {
172179
case 16:
@@ -197,6 +204,31 @@ static psa_status_t aead_crypt(
197204
return PSA_ERROR_INVALID_ARGUMENT;
198205
}
199206

207+
#if !defined(CC3XX_CONFIG_AES_TUNNELLING_ENABLE) && defined(PSA_WANT_ALG_CCM)
208+
if (ctr_required) {
209+
c3xx_lowlevel_aes_ccm_init_ctr(ctr, nonce, nonce_length);
210+
211+
/* As AES CBC-MAC computes the tag on plaintext data,
212+
* AES CTR decryption should come beforehand
213+
*/
214+
if (dir == PSA_CRYPTO_DRIVER_DECRYPT) {
215+
status = cc3xx_cipher_encrypt(attributes,
216+
key_buffer, key_buffer_size,
217+
PSA_ALG_CTR,
218+
ctr, sizeof(ctr),
219+
input, data_minus_tag,
220+
output, output_size,
221+
output_length);
222+
if (status != PSA_SUCCESS) {
223+
return status;
224+
}
225+
226+
/* CBC-MAC computes the tag on the decrypted data */
227+
input = output;
228+
}
229+
}
230+
#endif /* !CC3XX_CONFIG_AES_TUNNELLING_ENABLE && PSA_WANT_ALG_CCM */
231+
200232
err = cc3xx_lowlevel_aes_init((dir == PSA_CRYPTO_DRIVER_ENCRYPT) ?
201233
CC3XX_AES_DIRECTION_ENCRYPT : CC3XX_AES_DIRECTION_DECRYPT,
202234
mode, CC3XX_AES_KEY_ID_USER_KEY,
@@ -234,6 +266,21 @@ static psa_status_t aead_crypt(
234266
status = cc3xx_to_psa_err(err);
235267
goto out;
236268
}
269+
270+
#if !defined(CC3XX_CONFIG_AES_TUNNELLING_ENABLE) && defined(PSA_WANT_ALG_CCM)
271+
if (ctr_required && (dir == PSA_CRYPTO_DRIVER_ENCRYPT)) {
272+
status = cc3xx_cipher_encrypt(attributes,
273+
key_buffer, key_buffer_size,
274+
PSA_ALG_CTR,
275+
ctr, sizeof(ctr),
276+
input, data_minus_tag,
277+
output, output_size,
278+
output_length);
279+
if (status != PSA_SUCCESS) {
280+
return status;
281+
}
282+
}
283+
#endif /* !CC3XX_CONFIG_AES_TUNNELLING_ENABLE && PSA_WANT_ALG_CCM */
237284
}
238285
break;
239286
#endif /* PSA_WANT_KEY_TYPE_AES */
@@ -246,7 +293,7 @@ static psa_status_t aead_crypt(
246293
}
247294

248295
/* Bytes produced on finish will take into account all bytes up to finish, minus the tag */
249-
*output_length = bytes_produced_on_finish;
296+
*output_length += bytes_produced_on_finish;
250297

251298
if (dir == PSA_CRYPTO_DRIVER_ENCRYPT) {
252299
/* Put the tag in the correct place in output */

0 commit comments

Comments
 (0)