Path: blob/master/libs/tomcrypt/src/modes/cfb/cfb_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*/8#include "tomcrypt.h"910/**11@file cfb_decrypt.c12CFB implementation, decrypt data, Tom St Denis13*/1415#ifdef LTC_CFB_MODE1617/**18CFB decrypt19@param ct Ciphertext20@param pt [out] Plaintext21@param len Length of ciphertext (octets)22@param cfb CFB state23@return CRYPT_OK if successful24*/25int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb)26{27int err;2829LTC_ARGCHK(pt != NULL);30LTC_ARGCHK(ct != NULL);31LTC_ARGCHK(cfb != NULL);3233if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {34return err;35}3637/* is blocklen/padlen valid? */38if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||39cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {40return CRYPT_INVALID_ARG;41}4243while (len-- > 0) {44if (cfb->padlen == cfb->blocklen) {45if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {46return err;47}48cfb->padlen = 0;49}50cfb->pad[cfb->padlen] = *ct;51*pt = *ct ^ cfb->IV[cfb->padlen];52++pt;53++ct;54++(cfb->padlen);55}56return CRYPT_OK;57}5859#endif606162