/src/dropbear/libtomcrypt/src/stream/chacha/chacha_setup.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | /* The implementation is based on: |
11 | | * chacha-ref.c version 20080118 |
12 | | * Public domain from D. J. Bernstein |
13 | | */ |
14 | | |
15 | | #include "tomcrypt.h" |
16 | | |
17 | | #ifdef LTC_CHACHA |
18 | | |
19 | | static const char * const sigma = "expand 32-byte k"; |
20 | | static const char * const tau = "expand 16-byte k"; |
21 | | |
22 | | /** |
23 | | Initialize an ChaCha context (only the key) |
24 | | @param st [out] The destination of the ChaCha state |
25 | | @param key The secret key |
26 | | @param keylen The length of the secret key (octets) |
27 | | @param rounds Number of rounds (e.g. 20 for ChaCha20) |
28 | | @return CRYPT_OK if successful |
29 | | */ |
30 | | int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds) |
31 | 33.9k | { |
32 | 33.9k | const char *constants; |
33 | | |
34 | 33.9k | LTC_ARGCHK(st != NULL); |
35 | 33.9k | LTC_ARGCHK(key != NULL); |
36 | 33.9k | LTC_ARGCHK(keylen == 32 || keylen == 16); |
37 | | |
38 | 33.9k | if (rounds == 0) rounds = 20; |
39 | | |
40 | 33.9k | LOAD32L(st->input[4], key + 0); |
41 | 33.9k | LOAD32L(st->input[5], key + 4); |
42 | 33.9k | LOAD32L(st->input[6], key + 8); |
43 | 33.9k | LOAD32L(st->input[7], key + 12); |
44 | 33.9k | if (keylen == 32) { /* 256bit */ |
45 | 33.9k | key += 16; |
46 | 33.9k | constants = sigma; |
47 | 33.9k | } else { /* 128bit */ |
48 | 0 | constants = tau; |
49 | 0 | } |
50 | 33.9k | LOAD32L(st->input[8], key + 0); |
51 | 33.9k | LOAD32L(st->input[9], key + 4); |
52 | 33.9k | LOAD32L(st->input[10], key + 8); |
53 | 33.9k | LOAD32L(st->input[11], key + 12); |
54 | 33.9k | LOAD32L(st->input[0], constants + 0); |
55 | 33.9k | LOAD32L(st->input[1], constants + 4); |
56 | 33.9k | LOAD32L(st->input[2], constants + 8); |
57 | 33.9k | LOAD32L(st->input[3], constants + 12); |
58 | 33.9k | st->rounds = rounds; /* e.g. 20 for chacha20 */ |
59 | 33.9k | st->ivlen = 0; /* will be set later by chacha_ivctr(32|64) */ |
60 | 33.9k | return CRYPT_OK; |
61 | 33.9k | } |
62 | | |
63 | | #endif |
64 | | |
65 | | /* ref: $Format:%D$ */ |
66 | | /* git commit: $Format:%H$ */ |
67 | | /* commit time: $Format:%ai$ */ |