1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 /* Adapted from the public domain, estream code by D. Bernstein. */
17 #include <openssl/chacha.h>
19 #include <openssl/cpu.h>
21 #if defined(OPENSSL_WINDOWS) || (!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)) || !defined(__SSE2__)
23 /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
24 static const char sigma[16] = "expand 32-byte k";
26 #define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
27 #define XOR(v, w) ((v) ^ (w))
28 #define PLUS(x, y) ((x) + (y))
29 #define PLUSONE(v) (PLUS((v), 1))
31 #define U32TO8_LITTLE(p, v) \
33 (p)[0] = (v >> 0) & 0xff; \
34 (p)[1] = (v >> 8) & 0xff; \
35 (p)[2] = (v >> 16) & 0xff; \
36 (p)[3] = (v >> 24) & 0xff; \
39 #define U8TO32_LITTLE(p) \
40 (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
41 ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
43 /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
44 #define QUARTERROUND(a,b,c,d) \
45 x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
46 x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
47 x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
48 x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
50 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
51 /* Defined in chacha_vec.c */
52 void CRYPTO_chacha_20_neon(uint8_t *out, const uint8_t *in, size_t in_len,
53 const uint8_t key[32], const uint8_t nonce[8],
57 /* chacha_core performs |num_rounds| rounds of ChaCha20 on the input words in
58 * |input| and writes the 64 output bytes to |output|. */
59 static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
63 memcpy(x, input, sizeof(uint32_t) * 16);
64 for (i = 20; i > 0; i -= 2) {
65 QUARTERROUND(0, 4, 8, 12)
66 QUARTERROUND(1, 5, 9, 13)
67 QUARTERROUND(2, 6, 10, 14)
68 QUARTERROUND(3, 7, 11, 15)
69 QUARTERROUND(0, 5, 10, 15)
70 QUARTERROUND(1, 6, 11, 12)
71 QUARTERROUND(2, 7, 8, 13)
72 QUARTERROUND(3, 4, 9, 14)
75 for (i = 0; i < 16; ++i) {
76 x[i] = PLUS(x[i], input[i]);
78 for (i = 0; i < 16; ++i) {
79 U32TO8_LITTLE(output + 4 * i, x[i]);
83 void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
84 const uint8_t key[32], const uint8_t nonce[8],
90 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
91 if (CRYPTO_is_NEON_capable() && ((intptr_t)in & 15) == 0 &&
92 ((intptr_t)out & 15) == 0) {
93 CRYPTO_chacha_20_neon(out, in, in_len, key, nonce, counter);
98 input[0] = U8TO32_LITTLE(sigma + 0);
99 input[1] = U8TO32_LITTLE(sigma + 4);
100 input[2] = U8TO32_LITTLE(sigma + 8);
101 input[3] = U8TO32_LITTLE(sigma + 12);
103 input[4] = U8TO32_LITTLE(key + 0);
104 input[5] = U8TO32_LITTLE(key + 4);
105 input[6] = U8TO32_LITTLE(key + 8);
106 input[7] = U8TO32_LITTLE(key + 12);
108 input[8] = U8TO32_LITTLE(key + 16);
109 input[9] = U8TO32_LITTLE(key + 20);
110 input[10] = U8TO32_LITTLE(key + 24);
111 input[11] = U8TO32_LITTLE(key + 28);
114 input[13] = ((uint64_t)counter) >> 32;
115 input[14] = U8TO32_LITTLE(nonce + 0);
116 input[15] = U8TO32_LITTLE(nonce + 4);
124 chacha_core(buf, input);
125 for (i = 0; i < todo; i++) {
126 out[i] = in[i] ^ buf[i];
134 if (input[12] == 0) {
140 #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 || !__SSE2__ */