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 f9_init.c
14   F9 Support, start an F9 state
15 */
16 
17 #ifdef LTC_F9_MODE
18 
19 /** Initialize F9-MAC state
20   @param f9    [out] f9 state to initialize
21   @param cipher  Index of cipher to use
22   @param key     [in]  Secret key
23   @param keylen  Length of secret key in octets
24   Return CRYPT_OK on success
25 */
f9_init(f9_state * f9,int cipher,const unsigned char * key,unsigned long keylen)26 int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen)
27 {
28    int            x, err;
29 
30    LTC_ARGCHK(f9   != NULL);
31    LTC_ARGCHK(key  != NULL);
32 
33    /* schedule the key */
34    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
35       return err;
36    }
37 
38 #ifdef LTC_FAST
39    if (cipher_descriptor[cipher]->block_length % sizeof(LTC_FAST_TYPE)) {
40        return CRYPT_INVALID_ARG;
41    }
42 #endif
43 
44    if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, &f9->key)) != CRYPT_OK) {
45       goto done;
46    }
47 
48    /* make the second key */
49    for (x = 0; (unsigned)x < keylen; x++) {
50       f9->akey[x] = key[x] ^ 0xAA;
51    }
52 
53    /* setup struct */
54    zeromem(f9->IV,  cipher_descriptor[cipher]->block_length);
55    zeromem(f9->ACC, cipher_descriptor[cipher]->block_length);
56    f9->blocksize = cipher_descriptor[cipher]->block_length;
57    f9->cipher    = cipher;
58    f9->buflen    = 0;
59    f9->keylen    = keylen;
60 done:
61    return err;
62 }
63 
64 #endif
65 
66 /* ref:         $Format:%D$ */
67 /* git commit:  $Format:%H$ */
68 /* commit time: $Format:%ai$ */
69 
70