Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / chacha / chacha_generic.c
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 <openssl/cpu.h>
20
21 #if defined(OPENSSL_WINDOWS) || (!defined(OPENSSL_X86_64) && !defined(OPENSSL_X86)) || !defined(__SSE2__)
22
23 /* sigma contains the ChaCha constants, which happen to be an ASCII string. */
24 static const char sigma[16] = "expand 32-byte k";
25
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))
30
31 #define U32TO8_LITTLE(p, v)    \
32   {                            \
33     (p)[0] = (v >> 0) & 0xff;  \
34     (p)[1] = (v >> 8) & 0xff;  \
35     (p)[2] = (v >> 16) & 0xff; \
36     (p)[3] = (v >> 24) & 0xff; \
37   }
38
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))
42
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);
49
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],
54                            size_t counter);
55 #endif
56
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]) {
60   uint32_t x[16];
61   int i;
62
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)
73   }
74
75   for (i = 0; i < 16; ++i) {
76     x[i] = PLUS(x[i], input[i]);
77   }
78   for (i = 0; i < 16; ++i) {
79     U32TO8_LITTLE(output + 4 * i, x[i]);
80   }
81 }
82
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],
85                       size_t counter) {
86   uint32_t input[16];
87   uint8_t buf[64];
88   size_t todo, i;
89
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);
94     return;
95   }
96 #endif
97
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);
102
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);
107
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);
112
113   input[12] = counter;
114   input[13] = ((uint64_t)counter) >> 32;
115   input[14] = U8TO32_LITTLE(nonce + 0);
116   input[15] = U8TO32_LITTLE(nonce + 4);
117
118   while (in_len > 0) {
119     todo = sizeof(buf);
120     if (in_len < todo) {
121       todo = in_len;
122     }
123
124     chacha_core(buf, input);
125     for (i = 0; i < todo; i++) {
126       out[i] = in[i] ^ buf[i];
127     }
128
129     out += todo;
130     in += todo;
131     in_len -= todo;
132
133     input[12]++;
134     if (input[12] == 0) {
135       input[13]++;
136     }
137   }
138 }
139
140 #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 && !OPENSSL_X86 || !__SSE2__ */