Path: blob/master/libs/tomcrypt/src/modes/cfb/cfb_encrypt.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_encrypt.c12CFB implementation, encrypt data, Tom St Denis13*/1415#ifdef LTC_CFB_MODE1617/**18CFB encrypt19@param pt Plaintext20@param ct [out] Ciphertext21@param len Length of plaintext (octets)22@param cfb CFB state23@return CRYPT_OK if successful24*/25int cfb_encrypt(const unsigned char *pt, unsigned char *ct, 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 = *pt ^ cfb->IV[cfb->padlen]);51++pt;52++ct;53++(cfb->padlen);54}55return CRYPT_OK;56}5758#endif596061