Path: blob/master/libs/tomcrypt/src/encauth/eax/eax_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*/89/**10@file eax_done.c11EAX implementation, terminate session, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_EAX_MODE1617/**18Terminate an EAX session and get the tag.19@param eax The EAX state20@param tag [out] The destination of the authentication tag21@param taglen [in/out] The max length and resulting length of the authentication tag22@return CRYPT_OK if successful23*/24int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)25{26int err;27unsigned char *headermac, *ctmac;28unsigned long x, len;2930LTC_ARGCHK(eax != NULL);31LTC_ARGCHK(tag != NULL);32LTC_ARGCHK(taglen != NULL);3334/* allocate ram */35headermac = XMALLOC(MAXBLOCKSIZE);36ctmac = XMALLOC(MAXBLOCKSIZE);3738if (headermac == NULL || ctmac == NULL) {39if (headermac != NULL) {40XFREE(headermac);41}42if (ctmac != NULL) {43XFREE(ctmac);44}45return CRYPT_MEM;46}4748/* finish ctomac */49len = MAXBLOCKSIZE;50if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) {51goto LBL_ERR;52}5354/* finish headeromac */5556/* note we specifically don't reset len so the two lens are minimal */5758if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) {59goto LBL_ERR;60}6162/* terminate the CTR chain */63if ((err = ctr_done(&eax->ctr)) != CRYPT_OK) {64goto LBL_ERR;65}6667/* compute N xor H xor C */68for (x = 0; x < len && x < *taglen; x++) {69tag[x] = eax->N[x] ^ headermac[x] ^ ctmac[x];70}71*taglen = x;7273err = CRYPT_OK;74LBL_ERR:75#ifdef LTC_CLEAN_STACK76zeromem(ctmac, MAXBLOCKSIZE);77zeromem(headermac, MAXBLOCKSIZE);78zeromem(eax, sizeof(*eax));79#endif8081XFREE(ctmac);82XFREE(headermac);8384return err;85}8687#endif888990