1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6    @file cfb_start.c
7    CFB implementation, start chain, Tom St Denis
8 */
9 
10 
11 #ifdef LTC_CFB_MODE
12 
13 /**
14    Initialize a CFB context
15    @param cipher      The index of the cipher desired
16    @param IV          The initialization vector
17    @param key         The secret key
18    @param keylen      The length of the secret key (octets)
19    @param num_rounds  Number of rounds in the cipher desired (0 for default)
20    @param cfb         The CFB state to initialize
21    @return CRYPT_OK if successful
22 */
cfb_start(int cipher,const unsigned char * IV,const unsigned char * key,int keylen,int num_rounds,symmetric_CFB * cfb)23 int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
24               int keylen, int num_rounds, symmetric_CFB *cfb)
25 {
26    int x, err;
27 
28    LTC_ARGCHK(IV != NULL);
29    LTC_ARGCHK(key != NULL);
30    LTC_ARGCHK(cfb != NULL);
31 
32    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
33       return err;
34    }
35 
36 
37    /* copy data */
38    cfb->cipher = cipher;
39    cfb->blocklen = cipher_descriptor[cipher]->block_length;
40    for (x = 0; x < cfb->blocklen; x++) {
41        cfb->IV[x] = IV[x];
42    }
43 
44    /* init the cipher */
45    if ((err = cipher_descriptor[cipher]->setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) {
46       return err;
47    }
48 
49    /* encrypt the IV */
50    cfb->padlen = 0;
51    return cipher_descriptor[cfb->cipher]->ecb_encrypt(cfb->IV, cfb->IV, &cfb->key);
52 }
53 
54 #endif
55