1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 /* Adapted from the public domain, estream code by D. Bernstein. */
16 
17 #include <openssl/chacha.h>
18 
19 #include <string.h>
20 
21 #include <openssl/cpu.h>
22 
23 
24 #if defined(OPENSSL_WINDOWS) || (!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)) || !defined(__SSE2__)
25 
26 /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
27 static const uint8_t sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3',
28                                    '2', '-', 'b', 'y', 't', 'e', ' ', 'k' };
29 
30 #define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
31 #define XOR(v, w) ((v) ^ (w))
32 #define PLUS(x, y) ((x) + (y))
33 #define PLUSONE(v) (PLUS((v), 1))
34 
35 #define U32TO8_LITTLE(p, v)    \
36   {                            \
37     (p)[0] = (v >> 0) & 0xff;  \
38     (p)[1] = (v >> 8) & 0xff;  \
39     (p)[2] = (v >> 16) & 0xff; \
40     (p)[3] = (v >> 24) & 0xff; \
41   }
42 
43 #define U8TO32_LITTLE(p)                              \
44   (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
45    ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
46 
47 /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
48 #define QUARTERROUND(a,b,c,d) \
49   x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
50   x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
51   x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
52   x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
53 
54 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
55 /* Defined in chacha_vec.c */
56 void CRYPTO_chacha_20_neon(uint8_t *out, const uint8_t *in, size_t in_len,
57                            const uint8_t key[32], const uint8_t nonce[12],
58                            uint32_t counter);
59 #endif
60 
61 /* chacha_core performs 20 rounds of ChaCha on the input words in
62  * |input| and writes the 64 output bytes to |output|. */
chacha_core(uint8_t output[64],const uint32_t input[16])63 static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
64   uint32_t x[16];
65   int i;
66 
67   memcpy(x, input, sizeof(uint32_t) * 16);
68   for (i = 20; i > 0; i -= 2) {
69     QUARTERROUND(0, 4, 8, 12)
70     QUARTERROUND(1, 5, 9, 13)
71     QUARTERROUND(2, 6, 10, 14)
72     QUARTERROUND(3, 7, 11, 15)
73     QUARTERROUND(0, 5, 10, 15)
74     QUARTERROUND(1, 6, 11, 12)
75     QUARTERROUND(2, 7, 8, 13)
76     QUARTERROUND(3, 4, 9, 14)
77   }
78 
79   for (i = 0; i < 16; ++i) {
80     x[i] = PLUS(x[i], input[i]);
81   }
82   for (i = 0; i < 16; ++i) {
83     U32TO8_LITTLE(output + 4 * i, x[i]);
84   }
85 }
86 
CRYPTO_chacha_20(uint8_t * out,const uint8_t * in,size_t in_len,const uint8_t key[32],const uint8_t nonce[12],uint32_t counter)87 void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
88                       const uint8_t key[32], const uint8_t nonce[12],
89                       uint32_t counter) {
90   uint32_t input[16];
91   uint8_t buf[64];
92   size_t todo, i;
93 
94 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
95   if (CRYPTO_is_NEON_capable()) {
96     CRYPTO_chacha_20_neon(out, in, in_len, key, nonce, counter);
97     return;
98   }
99 #endif
100 
101   input[0] = U8TO32_LITTLE(sigma + 0);
102   input[1] = U8TO32_LITTLE(sigma + 4);
103   input[2] = U8TO32_LITTLE(sigma + 8);
104   input[3] = U8TO32_LITTLE(sigma + 12);
105 
106   input[4] = U8TO32_LITTLE(key + 0);
107   input[5] = U8TO32_LITTLE(key + 4);
108   input[6] = U8TO32_LITTLE(key + 8);
109   input[7] = U8TO32_LITTLE(key + 12);
110 
111   input[8] = U8TO32_LITTLE(key + 16);
112   input[9] = U8TO32_LITTLE(key + 20);
113   input[10] = U8TO32_LITTLE(key + 24);
114   input[11] = U8TO32_LITTLE(key + 28);
115 
116   input[12] = counter;
117   input[13] = U8TO32_LITTLE(nonce + 0);
118   input[14] = U8TO32_LITTLE(nonce + 4);
119   input[15] = U8TO32_LITTLE(nonce + 8);
120 
121   while (in_len > 0) {
122     todo = sizeof(buf);
123     if (in_len < todo) {
124       todo = in_len;
125     }
126 
127     chacha_core(buf, input);
128     for (i = 0; i < todo; i++) {
129       out[i] = in[i] ^ buf[i];
130     }
131 
132     out += todo;
133     in += todo;
134     in_len -= todo;
135 
136     input[12]++;
137   }
138 }
139 
140 #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 || !__SSE2__ */
141