Path: blob/master/libs/tomcrypt/src/encauth/ccm/ccm_done.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/**13Terminate a CCM stream14@param ccm The CCM state15@param tag [out] The destination for the MAC tag16@param taglen [in/out] The length of the MAC tag17@return CRYPT_OK on success18*/19int ccm_done(ccm_state *ccm,20unsigned char *tag, unsigned long *taglen)21{22unsigned long x, y;23int err;2425LTC_ARGCHK(ccm != NULL);2627/* Check all data have been processed */28if (ccm->ptlen != ccm->current_ptlen) {29return CRYPT_ERROR;30}3132LTC_ARGCHK(tag != NULL);33LTC_ARGCHK(taglen != NULL);3435if (ccm->x != 0) {36if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {37return err;38}39}4041/* setup CTR for the TAG (zero the count) */42for (y = 15; y > 15 - ccm->L; y--) {43ccm->ctr[y] = 0x00;44}45if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {46return err;47}4849cipher_descriptor[ccm->cipher].done(&ccm->K);5051/* store the TAG */52for (x = 0; x < 16 && x < *taglen; x++) {53tag[x] = ccm->PAD[x] ^ ccm->CTRPAD[x];54}55*taglen = x;5657return CRYPT_OK;58}5960#endif616263