Path: blob/master/libs/tomcrypt/src/encauth/ccm/ccm_add_aad.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910#ifdef LTC_CCM_MODE1112/**13Add AAD to the CCM state14@param ccm The CCM state15@param adata The additional authentication data to add to the CCM state16@param adatalen The length of the AAD data.17@return CRYPT_OK on success18*/19int ccm_add_aad(ccm_state *ccm,20const unsigned char *adata, unsigned long adatalen)21{22unsigned long y;23int err;2425LTC_ARGCHK(ccm != NULL);26LTC_ARGCHK(adata != NULL);2728if (ccm->aadlen < ccm->current_aadlen + adatalen) {29return CRYPT_INVALID_ARG;30}31ccm->current_aadlen += adatalen;3233/* now add the data */34for (y = 0; y < adatalen; y++) {35if (ccm->x == 16) {36/* full block so let's encrypt it */37if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {38return err;39}40ccm->x = 0;41}42ccm->PAD[ccm->x++] ^= adata[y];43}4445/* remainder? */46if (ccm->aadlen == ccm->current_aadlen) {47if (ccm->x != 0) {48if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {49return err;50}51}52ccm->x = 0;53}5455return CRYPT_OK;56}5758#endif596061