Path: blob/master/libs/tomcrypt/src/mac/omac/omac_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/**11@file omac_done.c12OMAC1 support, terminate a stream, Tom St Denis13*/1415#ifdef LTC_OMAC1617/**18Terminate an OMAC stream19@param omac The OMAC state20@param out [out] Destination for the authentication tag21@param outlen [in/out] The max size and resulting size of the authentication tag22@return CRYPT_OK if successful23*/24int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen)25{26int err, mode;27unsigned x;2829LTC_ARGCHK(omac != NULL);30LTC_ARGCHK(out != NULL);31LTC_ARGCHK(outlen != NULL);32if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) {33return err;34}3536if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) ||37(omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) {38return CRYPT_INVALID_ARG;39}4041/* figure out mode */42if (omac->buflen != omac->blklen) {43/* add the 0x80 byte */44omac->block[omac->buflen++] = 0x80;4546/* pad with 0x00 */47while (omac->buflen < omac->blklen) {48omac->block[omac->buflen++] = 0x00;49}50mode = 1;51} else {52mode = 0;53}5455/* now xor prev + Lu[mode] */56for (x = 0; x < (unsigned)omac->blklen; x++) {57omac->block[x] ^= omac->prev[x] ^ omac->Lu[mode][x];58}5960/* encrypt it */61if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) {62return err;63}64cipher_descriptor[omac->cipher_idx].done(&omac->key);6566/* output it */67for (x = 0; x < (unsigned)omac->blklen && x < *outlen; x++) {68out[x] = omac->block[x];69}70*outlen = x;7172#ifdef LTC_CLEAN_STACK73zeromem(omac, sizeof(*omac));74#endif75return CRYPT_OK;76}7778#endif798081