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