tomcrypt/src/modes/ofb/ofb_encrypt.c

61 lines
1.5 KiB
C
Raw Normal View History

2004-01-25 17:40:34 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
2004-05-12 20:42:16 +00:00
* guarantee it works.
2004-01-25 17:40:34 +00:00
*
2007-07-20 17:48:02 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
2004-01-25 17:40:34 +00:00
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
/**
@file ofb_encrypt.c
OFB implementation, encrypt data, Tom St Denis
*/
2003-03-03 00:59:24 +00:00
2006-08-30 23:30:00 +00:00
#ifdef LTC_OFB_MODE
2003-03-03 00:59:24 +00:00
2004-12-30 23:55:53 +00:00
/**
OFB encrypt
@param pt Plaintext
@param ct [out] Ciphertext
@param len Length of plaintext (octets)
@param ofb OFB state
@return CRYPT_OK if successful
*/
2003-03-03 00:59:24 +00:00
int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
{
2003-03-03 01:02:42 +00:00
int err;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(ofb != NULL);
2003-03-03 01:02:42 +00:00
if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
return err;
2003-03-03 00:59:24 +00:00
}
2015-12-20 17:05:58 +01:00
2003-03-13 02:12:16 +00:00
/* is blocklen/padlen valid? */
if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) {
return CRYPT_INVALID_ARG;
}
2015-12-20 17:05:58 +01:00
2003-03-03 01:02:42 +00:00
while (len-- > 0) {
2003-03-03 00:59:24 +00:00
if (ofb->padlen == ofb->blocklen) {
2005-11-18 05:15:37 +00:00
if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {
return err;
}
2003-03-03 00:59:24 +00:00
ofb->padlen = 0;
}
2006-12-16 18:10:04 +00:00
*ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++];
2003-03-03 00:59:24 +00:00
}
return CRYPT_OK;
}
#endif
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */