]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/cfb/cfb_encrypt.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / modes / cfb / cfb_encrypt.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_encrypt.c
13   CFB implementation, encrypt data, Tom St Denis
14 */
15
16 #ifdef LTC_CFB_MODE
17
18 /**
19   CFB encrypt
20   @param pt     Plaintext
21   @param ct     [out] Ciphertext
22   @param len    Length of plaintext (octets)
23   @param cfb    CFB state
24   @return CRYPT_OK if successful
25 */
26 int cfb_encrypt(const unsigned char *pt, unsigned char *ct, 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 = *pt ^ cfb->IV[cfb->padlen]);
52        ++pt;
53        ++ct;
54        ++(cfb->padlen);
55    }
56    return CRYPT_OK;
57 }
58
59 #endif
60
61 /* ref:         $Format:%D$ */
62 /* git commit:  $Format:%H$ */
63 /* commit time: $Format:%ai$ */