]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/cbc/cbc_start.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / modes / cbc / cbc_start.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 cbc_start.c
13    CBC implementation, start chain, Tom St Denis
14 */
15
16 #ifdef LTC_CBC_MODE
17
18 /**
19    Initialize a CBC context
20    @param cipher      The index of the cipher desired
21    @param IV          The initialization vector
22    @param key         The secret key
23    @param keylen      The length of the secret key (octets)
24    @param num_rounds  Number of rounds in the cipher desired (0 for default)
25    @param cbc         The CBC state to initialize
26    @return CRYPT_OK if successful
27 */
28 int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
29               int keylen, int num_rounds, symmetric_CBC *cbc)
30 {
31    int x, err;
32
33    LTC_ARGCHK(IV != NULL);
34    LTC_ARGCHK(key != NULL);
35    LTC_ARGCHK(cbc != NULL);
36
37    /* bad param? */
38    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
39       return err;
40    }
41
42    /* setup cipher */
43    if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) {
44       return err;
45    }
46
47    /* copy IV */
48    cbc->blocklen = cipher_descriptor[cipher].block_length;
49    cbc->cipher   = cipher;
50    for (x = 0; x < cbc->blocklen; x++) {
51        cbc->IV[x] = IV[x];
52    }
53    return CRYPT_OK;
54 }
55
56 #endif
57
58 /* ref:         $Format:%D$ */
59 /* git commit:  $Format:%H$ */
60 /* commit time: $Format:%ai$ */