1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file f9_init.c
7   F9 Support, start an F9 state
8 */
9 
10 #ifdef LTC_F9_MODE
11 
12 /** Initialize F9-MAC state
13   @param f9    [out] f9 state to initialize
14   @param cipher  Index of cipher to use
15   @param key     [in]  Secret key
16   @param keylen  Length of secret key in octets
17   Return CRYPT_OK on success
18 */
f9_init(f9_state * f9,int cipher,const unsigned char * key,unsigned long keylen)19 int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen)
20 {
21    int            x, err;
22 
23    LTC_ARGCHK(f9   != NULL);
24    LTC_ARGCHK(key  != NULL);
25 
26    /* schedule the key */
27    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
28       return err;
29    }
30 
31 #ifdef LTC_FAST
32    if (cipher_descriptor[cipher]->block_length % sizeof(LTC_FAST_TYPE)) {
33        return CRYPT_INVALID_ARG;
34    }
35 #endif
36 
37    if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, &f9->key)) != CRYPT_OK) {
38       goto done;
39    }
40 
41    /* make the second key */
42    for (x = 0; (unsigned)x < keylen; x++) {
43       f9->akey[x] = key[x] ^ 0xAA;
44    }
45 
46    /* setup struct */
47    zeromem(f9->IV,  cipher_descriptor[cipher]->block_length);
48    zeromem(f9->ACC, cipher_descriptor[cipher]->block_length);
49    f9->blocksize = cipher_descriptor[cipher]->block_length;
50    f9->cipher    = cipher;
51    f9->buflen    = 0;
52    f9->keylen    = keylen;
53 done:
54    return err;
55 }
56 
57 #endif
58 
59