comparison libtomcrypt/src/stream/chacha/chacha_setup.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
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 {
32 const char *constants;
33
34 LTC_ARGCHK(st != NULL);
35 LTC_ARGCHK(key != NULL);
36 LTC_ARGCHK(keylen == 32 || keylen == 16);
37
38 if (rounds == 0) rounds = 20;
39
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 */
45 key += 16;
46 constants = sigma;
47 } else { /* 128bit */
48 constants = tau;
49 }
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) */
60 return CRYPT_OK;
61 }
62
63 #endif
64
65 /* ref: $Format:%D$ */
66 /* git commit: $Format:%H$ */
67 /* commit time: $Format:%ai$ */