1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file ofb_encrypt.c
14   OFB implementation, encrypt data, Tom St Denis
15 */
16 
17 #ifdef LTC_OFB_MODE
18 
19 /**
20   OFB encrypt
21   @param pt     Plaintext
22   @param ct     [out] Ciphertext
23   @param len    Length of plaintext (octets)
24   @param ofb    OFB state
25   @return CRYPT_OK if successful
26 */
ofb_encrypt(const unsigned char * pt,unsigned char * ct,unsigned long len,symmetric_OFB * ofb)27 int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
28 {
29    int err;
30    LTC_ARGCHK(pt != NULL);
31    LTC_ARGCHK(ct != NULL);
32    LTC_ARGCHK(ofb != NULL);
33    if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
34        return err;
35    }
36 
37    /* is blocklen/padlen valid? */
38    if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
39        ofb->padlen   < 0 || ofb->padlen   > (int)sizeof(ofb->IV)) {
40       return CRYPT_INVALID_ARG;
41    }
42 
43    while (len-- > 0) {
44        if (ofb->padlen == ofb->blocklen) {
45           if ((err = cipher_descriptor[ofb->cipher]->ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {
46              return err;
47           }
48           ofb->padlen = 0;
49        }
50        *ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++];
51    }
52    return CRYPT_OK;
53 }
54 
55 #endif
56 
57 /* ref:         $Format:%D$ */
58 /* git commit:  $Format:%H$ */
59 /* commit time: $Format:%ai$ */
60