Path: blob/master/libs/tomcrypt/src/mac/xcbc/xcbc_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 xcbc_done.c12XCBC Support, terminate the state13*/1415#ifdef LTC_XCBC1617/** Terminate the XCBC-MAC state18@param xcbc XCBC state to terminate19@param out [out] Destination for the MAC tag20@param outlen [in/out] Destination size and final tag size21Return CRYPT_OK on success22*/23int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen)24{25int err, x;26LTC_ARGCHK(xcbc != NULL);27LTC_ARGCHK(out != NULL);2829/* check structure */30if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {31return err;32}3334if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) ||35(xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {36return CRYPT_INVALID_ARG;37}3839/* which key do we use? */40if (xcbc->buflen == xcbc->blocksize) {41/* k2 */42for (x = 0; x < xcbc->blocksize; x++) {43xcbc->IV[x] ^= xcbc->K[1][x];44}45} else {46xcbc->IV[xcbc->buflen] ^= 0x80;47/* k3 */48for (x = 0; x < xcbc->blocksize; x++) {49xcbc->IV[x] ^= xcbc->K[2][x];50}51}5253/* encrypt */54cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);55cipher_descriptor[xcbc->cipher].done(&xcbc->key);5657/* extract tag */58for (x = 0; x < xcbc->blocksize && (unsigned long)x < *outlen; x++) {59out[x] = xcbc->IV[x];60}61*outlen = x;6263#ifdef LTC_CLEAN_STACK64zeromem(xcbc, sizeof(*xcbc));65#endif66return CRYPT_OK;67}6869#endif707172