1 // SPDX-License-Identifier: BSD-2-Clause 2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 3 * 4 * LibTomCrypt is a library that provides various cryptographic 5 * algorithms in a highly modular and flexible manner. 6 * 7 * The library is free for all purposes without any express 8 * guarantee it works. 9 */ 10 #include "tomcrypt_private.h" 11 12 #ifdef LTC_CCM_MODE 13 14 /** 15 Add AAD to the CCM state 16 @param ccm The CCM state 17 @param adata The additional authentication data to add to the CCM state 18 @param adatalen The length of the AAD data. 19 @return CRYPT_OK on success 20 */ ccm_add_aad(ccm_state * ccm,const unsigned char * adata,unsigned long adatalen)21int ccm_add_aad(ccm_state *ccm, 22 const unsigned char *adata, unsigned long adatalen) 23 { 24 unsigned long y; 25 int err; 26 27 LTC_ARGCHK(ccm != NULL); 28 LTC_ARGCHK(adata != NULL); 29 30 if (ccm->aadlen < ccm->current_aadlen + adatalen) { 31 return CRYPT_INVALID_ARG; 32 } 33 ccm->current_aadlen += adatalen; 34 35 /* now add the data */ 36 for (y = 0; y < adatalen; y++) { 37 if (ccm->x == 16) { 38 /* full block so let's encrypt it */ 39 if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { 40 return err; 41 } 42 ccm->x = 0; 43 } 44 ccm->PAD[ccm->x++] ^= adata[y]; 45 } 46 47 /* remainder? */ 48 if (ccm->aadlen == ccm->current_aadlen) { 49 if (ccm->x != 0) { 50 if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) { 51 return err; 52 } 53 } 54 ccm->x = 0; 55 } 56 57 return CRYPT_OK; 58 } 59 60 #endif 61 62 /* ref: $Format:%D$ */ 63 /* git commit: $Format:%H$ */ 64 /* commit time: $Format:%ai$ */ 65