14
14
*/
15
15
16
16
#include "cc3xx_psa_aead.h"
17
+ #include "cc3xx_psa_cipher.h"
17
18
#include "cc3xx_crypto_primitives_private.h"
18
19
#include "cc3xx_misc.h"
19
20
#include "cc3xx_stdlib.h"
@@ -167,6 +168,12 @@ static psa_status_t aead_crypt(
167
168
{
168
169
cc3xx_aes_keysize_t key_size ;
169
170
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
+
170
177
171
178
switch (key_buffer_size ) {
172
179
case 16 :
@@ -197,6 +204,31 @@ static psa_status_t aead_crypt(
197
204
return PSA_ERROR_INVALID_ARGUMENT ;
198
205
}
199
206
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
+
200
232
err = cc3xx_lowlevel_aes_init ((dir == PSA_CRYPTO_DRIVER_ENCRYPT ) ?
201
233
CC3XX_AES_DIRECTION_ENCRYPT : CC3XX_AES_DIRECTION_DECRYPT ,
202
234
mode , CC3XX_AES_KEY_ID_USER_KEY ,
@@ -234,6 +266,21 @@ static psa_status_t aead_crypt(
234
266
status = cc3xx_to_psa_err (err );
235
267
goto out ;
236
268
}
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 */
237
284
}
238
285
break ;
239
286
#endif /* PSA_WANT_KEY_TYPE_AES */
@@ -246,7 +293,7 @@ static psa_status_t aead_crypt(
246
293
}
247
294
248
295
/* 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 ;
250
297
251
298
if (dir == PSA_CRYPTO_DRIVER_ENCRYPT ) {
252
299
/* Put the tag in the correct place in output */
0 commit comments