Path: blob/master/libs/tomcrypt/src/modes/ofb/ofb_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 ofb_encrypt.c12OFB implementation, encrypt data, Tom St Denis13*/1415#ifdef LTC_OFB_MODE1617/**18OFB encrypt19@param pt Plaintext20@param ct [out] Ciphertext21@param len Length of plaintext (octets)22@param ofb OFB state23@return CRYPT_OK if successful24*/25int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)26{27int err;28LTC_ARGCHK(pt != NULL);29LTC_ARGCHK(ct != NULL);30LTC_ARGCHK(ofb != NULL);31if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {32return err;33}3435/* is blocklen/padlen valid? */36if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||37ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) {38return CRYPT_INVALID_ARG;39}4041while (len-- > 0) {42if (ofb->padlen == ofb->blocklen) {43if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {44return err;45}46ofb->padlen = 0;47}48*ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++];49}50return CRYPT_OK;51}5253#endif545556