1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* The implementation is based on:
11 * chacha-ref.c version 20080118
12 * Public domain from D. J. Bernstein
19 static const char * const sigma = "expand 32-byte k";
20 static const char * const tau = "expand 16-byte k";
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
30 int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds)
32 const char *constants;
34 LTC_ARGCHK(st != NULL);
35 LTC_ARGCHK(key != NULL);
36 LTC_ARGCHK(keylen == 32 || keylen == 16);
38 if (rounds == 0) rounds = 20;
40 LOAD32L(st->input[4], key + 0);
41 LOAD32L(st->input[5], key + 4);
42 LOAD32L(st->input[6], key + 8);
43 LOAD32L(st->input[7], key + 12);
44 if (keylen == 32) { /* 256bit */
50 LOAD32L(st->input[8], key + 0);
51 LOAD32L(st->input[9], key + 4);
52 LOAD32L(st->input[10], key + 8);
53 LOAD32L(st->input[11], key + 12);
54 LOAD32L(st->input[0], constants + 0);
55 LOAD32L(st->input[1], constants + 4);
56 LOAD32L(st->input[2], constants + 8);
57 LOAD32L(st->input[3], constants + 12);
58 st->rounds = rounds; /* e.g. 20 for chacha20 */
59 st->ivlen = 0; /* will be set later by chacha_ivctr(32|64) */
65 /* ref: $Format:%D$ */
66 /* git commit: $Format:%H$ */
67 /* commit time: $Format:%ai$ */