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