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 cfb_start.c
14 CFB implementation, start chain, Tom St Denis
15 */
16
17
18 #ifdef LTC_CFB_MODE
19
20 /**
21 Initialize a CFB context
22 @param cipher The index of the cipher desired
23 @param IV The initialization vector
24 @param key The secret key
25 @param keylen The length of the secret key (octets)
26 @param num_rounds Number of rounds in the cipher desired (0 for default)
27 @param cfb The CFB state to initialize
28 @return CRYPT_OK if successful
29 */
cfb_start(int cipher,const unsigned char * IV,const unsigned char * key,int keylen,int num_rounds,symmetric_CFB * cfb)30 int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
31 int keylen, int num_rounds, symmetric_CFB *cfb)
32 {
33 int x, err;
34
35 LTC_ARGCHK(IV != NULL);
36 LTC_ARGCHK(key != NULL);
37 LTC_ARGCHK(cfb != NULL);
38
39 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
40 return err;
41 }
42
43
44 /* copy data */
45 cfb->cipher = cipher;
46 cfb->blocklen = cipher_descriptor[cipher]->block_length;
47 for (x = 0; x < cfb->blocklen; x++) {
48 cfb->IV[x] = IV[x];
49 }
50
51 /* init the cipher */
52 if ((err = cipher_descriptor[cipher]->setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) {
53 return err;
54 }
55
56 /* encrypt the IV */
57 cfb->padlen = 0;
58 return cipher_descriptor[cfb->cipher]->ecb_encrypt(cfb->IV, cfb->IV, &cfb->key);
59 }
60
61 #endif
62
63 /* ref: $Format:%D$ */
64 /* git commit: $Format:%H$ */
65 /* commit time: $Format:%ai$ */
66