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 cfb_encrypt.c
14   CFB implementation, encrypt data, Tom St Denis
15 */
16 
17 #ifdef LTC_CFB_MODE
18 
19 /**
20   CFB encrypt
21   @param pt     Plaintext
22   @param ct     [out] Ciphertext
23   @param len    Length of plaintext (octets)
24   @param cfb    CFB state
25   @return CRYPT_OK if successful
26 */
cfb_encrypt(const unsigned char * pt,unsigned char * ct,unsigned long len,symmetric_CFB * cfb)27 int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb)
28 {
29    int err;
30 
31    LTC_ARGCHK(pt != NULL);
32    LTC_ARGCHK(ct != NULL);
33    LTC_ARGCHK(cfb != NULL);
34 
35    if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
36        return err;
37    }
38 
39    /* is blocklen/padlen valid? */
40    if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
41        cfb->padlen   < 0 || cfb->padlen   > (int)sizeof(cfb->pad)) {
42       return CRYPT_INVALID_ARG;
43    }
44 
45    while (len-- > 0) {
46        if (cfb->padlen == cfb->blocklen) {
47           if ((err = cipher_descriptor[cfb->cipher]->ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {
48              return err;
49           }
50           cfb->padlen = 0;
51        }
52        cfb->pad[cfb->padlen] = (*ct = *pt ^ cfb->IV[cfb->padlen]);
53        ++pt;
54        ++ct;
55        ++(cfb->padlen);
56    }
57    return CRYPT_OK;
58 }
59 
60 #endif
61 
62 /* ref:         $Format:%D$ */
63 /* git commit:  $Format:%H$ */
64 /* commit time: $Format:%ai$ */
65