/src/libtomcrypt/src/encauth/ccm/ccm_init.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
2 | | /* SPDX-License-Identifier: Unlicense */ |
3 | | #include "tomcrypt_private.h" |
4 | | |
5 | | #ifdef LTC_CCM_MODE |
6 | | |
7 | | /** |
8 | | Initialize a CCM state |
9 | | @param ccm The CCM state to initialize |
10 | | @param cipher The index of the cipher to use |
11 | | @param key The secret key |
12 | | @param keylen The length of the secret key |
13 | | @param ptlen The length of the plain/cipher text that will be processed |
14 | | @param taglen The max length of the MAC tag |
15 | | @param aadlen The length of the AAD |
16 | | |
17 | | @return CRYPT_OK on success |
18 | | */ |
19 | | int ccm_init(ccm_state *ccm, int cipher, |
20 | | const unsigned char *key, int keylen, int ptlen, int taglen, int aadlen) |
21 | 3 | { |
22 | 3 | int err; |
23 | | |
24 | 3 | LTC_ARGCHK(ccm != NULL); |
25 | 3 | LTC_ARGCHK(key != NULL); |
26 | | |
27 | 3 | XMEMSET(ccm, 0, sizeof(ccm_state)); |
28 | | |
29 | | /* check cipher input */ |
30 | 3 | if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { |
31 | 0 | return err; |
32 | 0 | } |
33 | 3 | if (cipher_descriptor[cipher].block_length != 16) { |
34 | 0 | return CRYPT_INVALID_CIPHER; |
35 | 0 | } |
36 | | |
37 | | /* make sure the taglen is valid */ |
38 | 3 | if (taglen < 4 || taglen > 16 || (taglen % 2) == 1 || aadlen < 0 || ptlen < 0) { |
39 | 0 | return CRYPT_INVALID_ARG; |
40 | 0 | } |
41 | 3 | ccm->taglen = taglen; |
42 | | |
43 | | /* schedule key */ |
44 | 3 | if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) { |
45 | 0 | return err; |
46 | 0 | } |
47 | 3 | ccm->cipher = cipher; |
48 | | |
49 | | /* let's get the L value */ |
50 | 3 | ccm->ptlen = ptlen; |
51 | 3 | ccm->L = 0; |
52 | 6 | while (ptlen) { |
53 | 3 | ++ccm->L; |
54 | 3 | ptlen >>= 8; |
55 | 3 | } |
56 | 3 | if (ccm->L <= 1) { |
57 | 3 | ccm->L = 2; |
58 | 3 | } |
59 | | |
60 | 3 | ccm->aadlen = aadlen; |
61 | 3 | return CRYPT_OK; |
62 | 3 | } |
63 | | |
64 | | #endif |