Path: blob/master/libs/tomcrypt/src/encauth/ocb/ocb_done_decrypt.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 ocb_done_decrypt.c11OCB implementation, terminate decryption, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_OCB_MODE1617/**18Terminate a decrypting OCB state19@param ocb The OCB state20@param ct The ciphertext (if any)21@param ctlen The length of the ciphertext (octets)22@param pt [out] The plaintext23@param tag The authentication tag (to compare against)24@param taglen The length of the authentication tag provided25@param stat [out] The result of the tag comparison26@return CRYPT_OK if the process was successful regardless if the tag is valid27*/28int ocb_done_decrypt(ocb_state *ocb,29const unsigned char *ct, unsigned long ctlen,30unsigned char *pt,31const unsigned char *tag, unsigned long taglen, int *stat)32{33int err;34unsigned char *tagbuf;35unsigned long tagbuflen;3637LTC_ARGCHK(ocb != NULL);38LTC_ARGCHK(pt != NULL);39LTC_ARGCHK(ct != NULL);40LTC_ARGCHK(tag != NULL);41LTC_ARGCHK(stat != NULL);4243/* default to failed */44*stat = 0;4546/* allocate memory */47tagbuf = XMALLOC(MAXBLOCKSIZE);48if (tagbuf == NULL) {49return CRYPT_MEM;50}5152tagbuflen = MAXBLOCKSIZE;53if ((err = s_ocb_done(ocb, ct, ctlen, pt, tagbuf, &tagbuflen, 1)) != CRYPT_OK) {54goto LBL_ERR;55}5657if (taglen <= tagbuflen && XMEM_NEQ(tagbuf, tag, taglen) == 0) {58*stat = 1;59}6061err = CRYPT_OK;62LBL_ERR:63#ifdef LTC_CLEAN_STACK64zeromem(tagbuf, MAXBLOCKSIZE);65#endif6667XFREE(tagbuf);6869return err;70}7172#endif737475