Path: blob/master/libs/tomcrypt/src/encauth/ocb3/ocb3_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 ocb3_done.c11OCB implementation, INTERNAL ONLY helper, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB3_MODE1617/**18Finish OCB processing and compute the tag19@param ocb The OCB state20@param tag [out] The destination for the authentication tag21@param taglen [in/out] The max size and resulting size of the authentication tag22@return CRYPT_OK if successful23*/24int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen)25{26unsigned char tmp[MAXBLOCKSIZE];27int err, x;2829LTC_ARGCHK(ocb != NULL);30LTC_ARGCHK(tag != NULL);31LTC_ARGCHK(taglen != NULL);32if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {33goto LBL_ERR;34}3536/* check taglen */37if ((int)*taglen < ocb->tag_len) {38*taglen = (unsigned long)ocb->tag_len;39return CRYPT_BUFFER_OVERFLOW;40}4142/* finalize AAD processing */4344if (ocb->adata_buffer_bytes>0) {45/* Offset_* = Offset_m xor L_* */46ocb3_int_xor_blocks(ocb->aOffset_current, ocb->aOffset_current, ocb->L_star, ocb->block_len);4748/* CipherInput = (A_* || 1 || zeros(127-bitlen(A_*))) xor Offset_* */49ocb3_int_xor_blocks(tmp, ocb->adata_buffer, ocb->aOffset_current, ocb->adata_buffer_bytes);50for(x=ocb->adata_buffer_bytes; x<ocb->block_len; x++) {51if (x == ocb->adata_buffer_bytes) {52tmp[x] = 0x80 ^ ocb->aOffset_current[x];53}54else {55tmp[x] = 0x00 ^ ocb->aOffset_current[x];56}57}5859/* Sum = Sum_m xor ENCIPHER(K, CipherInput) */60if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {61goto LBL_ERR;62}63ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len);64}6566/* finalize TAG computing */6768/* at this point ocb->aSum_current = HASH(K, A) */69/* tag = tag ^ HASH(K, A) */70ocb3_int_xor_blocks(tmp, ocb->tag_part, ocb->aSum_current, ocb->block_len);7172/* copy tag bytes */73for(x = 0; x < ocb->tag_len; x++) tag[x] = tmp[x];74*taglen = (unsigned long)ocb->tag_len;7576err = CRYPT_OK;7778LBL_ERR:79#ifdef LTC_CLEAN_STACK80zeromem(tmp, MAXBLOCKSIZE);81zeromem(ocb, sizeof(*ocb));82#endif8384return err;85}8687#endif888990