Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / ofb.c
1 #include "mycrypt.h"
2
3 #ifdef OFB
4
5 int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, 
6               int keylen, int num_rounds, symmetric_OFB *ofb)
7 {
8    int x, err;
9
10    _ARGCHK(IV != NULL);
11    _ARGCHK(key != NULL);
12    _ARGCHK(ofb != NULL);
13
14    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
15       return err;
16    }
17
18    /* copy details */
19    ofb->cipher = cipher;
20    ofb->blocklen = cipher_descriptor[cipher].block_length;
21    for (x = 0; x < ofb->blocklen; x++) {
22        ofb->IV[x] = IV[x];
23    }
24
25    /* init the cipher */
26    ofb->padlen = ofb->blocklen;
27    return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ofb->key);
28 }
29
30 int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
31 {
32    int err;
33    _ARGCHK(pt != NULL);
34    _ARGCHK(ct != NULL);
35    _ARGCHK(ofb != NULL);
36    if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
37        return err;
38    }
39    
40    /* is blocklen/padlen valid? */
41    if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
42        ofb->padlen   < 0 || ofb->padlen   > (int)sizeof(ofb->IV)) {
43       return CRYPT_INVALID_ARG;
44    }
45    
46    while (len-- > 0) {
47        if (ofb->padlen == ofb->blocklen) {
48           cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key);
49           ofb->padlen = 0;
50        }
51        *ct++ = *pt++ ^ ofb->IV[ofb->padlen++];
52    }
53    return CRYPT_OK;
54 }
55
56 int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb)
57 {
58    _ARGCHK(pt != NULL);
59    _ARGCHK(ct != NULL);
60    _ARGCHK(ofb != NULL);
61    return ofb_encrypt(ct, pt, len, ofb);
62 }
63
64
65 #endif
66
67