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_decrypt.c
14   CFB implementation, decrypt data, Tom St Denis
15 */
16 
17 #ifdef LTC_CFB_MODE
18 
19 /**
20    CFB decrypt
21    @param ct      Ciphertext
22    @param pt      [out] Plaintext
23    @param len     Length of ciphertext (octets)
24    @param cfb     CFB state
25    @return CRYPT_OK if successful
26 */
cfb_decrypt(const unsigned char * ct,unsigned char * pt,unsigned long len,symmetric_CFB * cfb)27 int cfb_decrypt(const unsigned char *ct, unsigned char *pt, 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;
53        *pt = *ct ^ cfb->IV[cfb->padlen];
54        ++pt;
55        ++ct;
56        ++(cfb->padlen);
57    }
58    return CRYPT_OK;
59 }
60 
61 #endif
62 
63 
64 /* ref:         $Format:%D$ */
65 /* git commit:  $Format:%H$ */
66 /* commit time: $Format:%ai$ */
67