]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/ofb/ofb_encrypt.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / modes / ofb / ofb_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 ofb_encrypt.c
13   OFB implementation, encrypt data, Tom St Denis
14 */
15
16 #ifdef LTC_OFB_MODE
17
18 /**
19   OFB encrypt
20   @param pt     Plaintext
21   @param ct     [out] Ciphertext
22   @param len    Length of plaintext (octets)
23   @param ofb    OFB state
24   @return CRYPT_OK if successful
25 */
26 int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
27 {
28    int err;
29    LTC_ARGCHK(pt != NULL);
30    LTC_ARGCHK(ct != NULL);
31    LTC_ARGCHK(ofb != NULL);
32    if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
33        return err;
34    }
35
36    /* is blocklen/padlen valid? */
37    if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
38        ofb->padlen   < 0 || ofb->padlen   > (int)sizeof(ofb->IV)) {
39       return CRYPT_INVALID_ARG;
40    }
41
42    while (len-- > 0) {
43        if (ofb->padlen == ofb->blocklen) {
44           if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {
45              return err;
46           }
47           ofb->padlen = 0;
48        }
49        *ct++ = *pt++ ^ ofb->IV[(ofb->padlen)++];
50    }
51    return CRYPT_OK;
52 }
53
54 #endif
55
56 /* ref:         $Format:%D$ */
57 /* git commit:  $Format:%H$ */
58 /* commit time: $Format:%ai$ */