1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13    @file cbc_start.c
14    CBC implementation, start chain, Tom St Denis
15 */
16 
17 #ifdef LTC_CBC_MODE
18 
19 /**
20    Initialize a CBC context
21    @param cipher      The index of the cipher desired
22    @param IV          The initialization vector
23    @param key         The secret key
24    @param keylen      The length of the secret key (octets)
25    @param num_rounds  Number of rounds in the cipher desired (0 for default)
26    @param cbc         The CBC state to initialize
27    @return CRYPT_OK if successful
28 */
cbc_start(int cipher,const unsigned char * IV,const unsigned char * key,int keylen,int num_rounds,symmetric_CBC * cbc)29 int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
30               int keylen, int num_rounds, symmetric_CBC *cbc)
31 {
32    int x, err;
33 
34    LTC_ARGCHK(IV != NULL);
35    LTC_ARGCHK(key != NULL);
36    LTC_ARGCHK(cbc != NULL);
37 
38    /* bad param? */
39    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
40       return err;
41    }
42 
43    /* setup cipher */
44    if ((err = cipher_descriptor[cipher]->setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) {
45       return err;
46    }
47 
48    /* copy IV */
49    cbc->blocklen = cipher_descriptor[cipher]->block_length;
50    cbc->cipher   = cipher;
51    for (x = 0; x < cbc->blocklen; x++) {
52        cbc->IV[x] = IV[x];
53    }
54    return CRYPT_OK;
55 }
56 
57 #endif
58 
59 /* ref:         $Format:%D$ */
60 /* git commit:  $Format:%H$ */
61 /* commit time: $Format:%ai$ */
62