1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file ofb_encrypt.c
7 OFB implementation, encrypt data, Tom St Denis
8 */
9
10 #ifdef LTC_OFB_MODE
11
12 /**
13 OFB encrypt
14 @param pt Plaintext
15 @param ct [out] Ciphertext
16 @param len Length of plaintext (octets)
17 @param ofb OFB state
18 @return CRYPT_OK if successful
19 */
ofb_encrypt(const unsigned char * pt,unsigned char * ct,unsigned long len,symmetric_OFB * ofb)20 int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
21 {
22 int err;
23 LTC_ARGCHK(pt != NULL);
24 LTC_ARGCHK(ct != NULL);
25 LTC_ARGCHK(ofb != NULL);
26 if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
27 return err;
28 }
29
30 /* is blocklen/padlen valid? */
31 if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
32 ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) {
33 return CRYPT_INVALID_ARG;
34 }
35
36 while (len-- > 0) {
37 if (ofb->padlen == ofb->blocklen) {
38 if ((err = cipher_descriptor[ofb->cipher]->ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {
39 return err;
40 }
41 ofb->padlen = 0;
42 }
43 *ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++];
44 }
45 return CRYPT_OK;
46 }
47
48 #endif
49