| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
// Adapted from the public domain, estream code by D. Bernstein. |
| 5 |
|
| 6 |
#include <openssl/chacha.h> |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "../internal.h" |
| 12 |
#include "internal.h" |
| 13 |
|
| 14 |
|
| 15 |
// sigma contains the ChaCha constants, which happen to be an ASCII string. |
| 16 |
// "expand 32-byte k" |
| 17 |
static const uint32_t sigma_words[4] = { |
| 18 |
0x61707865, // 'e' 0x65, 'x' 0x78, 'p' 0x70, 'a' 0x61, |
| 19 |
0x3320646e, // 'n' 0x6E, 'd' 0x64, ' ' 0x20, '3' 0x33, |
| 20 |
0x79622d32, // '2' 0x32, '-' 0x2D, 'b' 0x62, 'y' 0x79, |
| 21 |
0x6b206574 // 't' 0x74, 'e' 0x65, ' ' 0x20, 'k' 0x6B |
| 22 |
}; |
| 23 |
|
| 24 |
// QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. |
| 25 |
#define QUARTERROUND(a, b, c, d) \ |
| 26 |
x[a] += x[b]; \ |
| 27 |
x[d] = CRYPTO_rotl_u32(x[d] ^ x[a], 16); \ |
| 28 |
x[c] += x[d]; \ |
| 29 |
x[b] = CRYPTO_rotl_u32(x[b] ^ x[c], 12); \ |
| 30 |
x[a] += x[b]; \ |
| 31 |
x[d] = CRYPTO_rotl_u32(x[d] ^ x[a], 8); \ |
| 32 |
x[c] += x[d]; \ |
| 33 |
x[b] = CRYPTO_rotl_u32(x[b] ^ x[c], 7); |
| 34 |
|
| 35 |
void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32], |
| 36 |
const uint8_t nonce[16]) { |
| 37 |
uint32_t x[16]; |
| 38 |
OPENSSL_memcpy(x, sigma_words, sizeof(sigma_words)); |
| 39 |
#ifdef OPENSSL_BIG_ENDIAN |
| 40 |
for(size_t i = 4; i < 12; i++) { |
| 41 |
x[i] = CRYPTO_load_u32_le(key + (i-4) * sizeof(uint32_t)); |
| 42 |
} |
| 43 |
for(size_t i = 12; i < 16; i++) { |
| 44 |
x[i] = CRYPTO_load_u32_le(nonce + (i-12) * sizeof(uint32_t)); |
| 45 |
} |
| 46 |
#else |
| 47 |
OPENSSL_memcpy(&x[4], key, 32); |
| 48 |
OPENSSL_memcpy(&x[12], nonce, 16); |
| 49 |
#endif |
| 50 |
|
| 51 |
for (size_t i = 0; i < 20; i += 2) { |
| 52 |
QUARTERROUND(0, 4, 8, 12) |
| 53 |
QUARTERROUND(1, 5, 9, 13) |
| 54 |
QUARTERROUND(2, 6, 10, 14) |
| 55 |
QUARTERROUND(3, 7, 11, 15) |
| 56 |
QUARTERROUND(0, 5, 10, 15) |
| 57 |
QUARTERROUND(1, 6, 11, 12) |
| 58 |
QUARTERROUND(2, 7, 8, 13) |
| 59 |
QUARTERROUND(3, 4, 9, 14) |
| 60 |
} |
| 61 |
|
| 62 |
#ifdef OPENSSL_BIG_ENDIAN |
| 63 |
for(size_t i = 0; i < 4; i++) { |
| 64 |
CRYPTO_store_u32_le(out + i * sizeof(uint32_t), x[i]); |
| 65 |
} |
| 66 |
for(size_t i = 12; i < 16; i++) { |
| 67 |
CRYPTO_store_u32_le(out + (i-8) * sizeof(uint32_t), x[i]); |
| 68 |
} |
| 69 |
#else |
| 70 |
OPENSSL_memcpy(out, &x[0], sizeof(uint32_t) * 4); |
| 71 |
OPENSSL_memcpy(&out[16], &x[12], sizeof(uint32_t) * 4); |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
#if defined(CHACHA20_ASM_NOHW) |
| 76 |
static void ChaCha20_ctr32(uint8_t *out, const uint8_t *in, size_t in_len, |
| 77 |
const uint32_t key[8], const uint32_t counter[4]) { |
| 78 |
#if defined(CHACHA20_ASM_NEON) |
| 79 |
if (ChaCha20_ctr32_neon_capable(in_len)) { |
| 80 |
ChaCha20_ctr32_neon(out, in, in_len, key, counter); |
| 81 |
return; |
| 82 |
} |
| 83 |
#endif |
| 84 |
#if defined(CHACHA20_ASM_AVX2) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 85 |
if (ChaCha20_ctr32_avx2_capable(in_len)) { |
| 86 |
ChaCha20_ctr32_avx2(out, in, in_len, key, counter); |
| 87 |
return; |
| 88 |
} |
| 89 |
#endif |
| 90 |
#if defined(CHACHA20_ASM_SSSE3_4X) |
| 91 |
if (ChaCha20_ctr32_ssse3_4x_capable(in_len)) { |
| 92 |
ChaCha20_ctr32_ssse3_4x(out, in, in_len, key, counter); |
| 93 |
return; |
| 94 |
} |
| 95 |
#endif |
| 96 |
#if defined(CHACHA20_ASM_SSSE3) |
| 97 |
if (ChaCha20_ctr32_ssse3_capable(in_len)) { |
| 98 |
ChaCha20_ctr32_ssse3(out, in, in_len, key, counter); |
| 99 |
return; |
| 100 |
} |
| 101 |
#endif |
| 102 |
if (in_len > 0) { |
| 103 |
ChaCha20_ctr32_nohw(out, in, in_len, key, counter); |
| 104 |
} |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
#if defined(CHACHA20_ASM_NOHW) |
| 109 |
|
| 110 |
void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len, |
| 111 |
const uint8_t key[32], const uint8_t nonce[12], |
| 112 |
uint32_t counter) { |
| 113 |
assert(!buffers_alias(out, in_len, in, in_len) || in == out); |
| 114 |
|
| 115 |
uint32_t counter_nonce[4]; |
| 116 |
counter_nonce[0] = counter; |
| 117 |
counter_nonce[1] = CRYPTO_load_u32_le(nonce + 0); |
| 118 |
counter_nonce[2] = CRYPTO_load_u32_le(nonce + 4); |
| 119 |
counter_nonce[3] = CRYPTO_load_u32_le(nonce + 8); |
| 120 |
|
| 121 |
const uint32_t *key_ptr = (const uint32_t *)key; |
| 122 |
#if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64) |
| 123 |
// The assembly expects the key to be four-byte aligned. |
| 124 |
uint32_t key_u32[8]; |
| 125 |
if ((((uintptr_t)key) & 3) != 0) { |
| 126 |
key_u32[0] = CRYPTO_load_u32_le(key + 0); |
| 127 |
key_u32[1] = CRYPTO_load_u32_le(key + 4); |
| 128 |
key_u32[2] = CRYPTO_load_u32_le(key + 8); |
| 129 |
key_u32[3] = CRYPTO_load_u32_le(key + 12); |
| 130 |
key_u32[4] = CRYPTO_load_u32_le(key + 16); |
| 131 |
key_u32[5] = CRYPTO_load_u32_le(key + 20); |
| 132 |
key_u32[6] = CRYPTO_load_u32_le(key + 24); |
| 133 |
key_u32[7] = CRYPTO_load_u32_le(key + 28); |
| 134 |
|
| 135 |
key_ptr = key_u32; |
| 136 |
} |
| 137 |
#endif |
| 138 |
|
| 139 |
while (in_len > 0) { |
| 140 |
// The assembly functions do not have defined overflow behavior. While |
| 141 |
// overflow is almost always a bug in the caller, we prefer our functions to |
| 142 |
// behave the same across platforms, so divide into multiple calls to avoid |
| 143 |
// this case. |
| 144 |
uint64_t todo = 64 * ((UINT64_C(1) << 32) - counter_nonce[0]); |
| 145 |
if (todo > in_len) { |
| 146 |
todo = in_len; |
| 147 |
} |
| 148 |
|
| 149 |
ChaCha20_ctr32(out, in, (size_t)todo, key_ptr, counter_nonce); |
| 150 |
in += todo; |
| 151 |
out += todo; |
| 152 |
in_len -= todo; |
| 153 |
|
| 154 |
// We're either done and will next break out of the loop, or we stopped at |
| 155 |
// the wraparound point and the counter should continue at zero. |
| 156 |
counter_nonce[0] = 0; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#else |
| 161 |
|
| 162 |
// chacha_core performs 20 rounds of ChaCha on the input words in |
| 163 |
// |input| and writes the 64 output bytes to |output|. |
| 164 |
static void chacha_core(uint8_t output[64], const uint32_t input[16]) { |
| 165 |
uint32_t x[16]; |
| 166 |
int i; |
| 167 |
|
| 168 |
OPENSSL_memcpy(x, input, sizeof(uint32_t) * 16); |
| 169 |
for (i = 20; i > 0; i -= 2) { |
| 170 |
QUARTERROUND(0, 4, 8, 12) |
| 171 |
QUARTERROUND(1, 5, 9, 13) |
| 172 |
QUARTERROUND(2, 6, 10, 14) |
| 173 |
QUARTERROUND(3, 7, 11, 15) |
| 174 |
QUARTERROUND(0, 5, 10, 15) |
| 175 |
QUARTERROUND(1, 6, 11, 12) |
| 176 |
QUARTERROUND(2, 7, 8, 13) |
| 177 |
QUARTERROUND(3, 4, 9, 14) |
| 178 |
} |
| 179 |
|
| 180 |
for (i = 0; i < 16; ++i) { |
| 181 |
x[i] += input[i]; |
| 182 |
} |
| 183 |
for (i = 0; i < 16; ++i) { |
| 184 |
CRYPTO_store_u32_le(output + 4 * i, x[i]); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len, |
| 189 |
const uint8_t key[32], const uint8_t nonce[12], |
| 190 |
uint32_t counter) { |
| 191 |
assert(!buffers_alias(out, in_len, in, in_len) || in == out); |
| 192 |
|
| 193 |
uint32_t input[16]; |
| 194 |
uint8_t buf[64]; |
| 195 |
size_t todo, i; |
| 196 |
|
| 197 |
input[0] = sigma_words[0]; |
| 198 |
input[1] = sigma_words[1]; |
| 199 |
input[2] = sigma_words[2]; |
| 200 |
input[3] = sigma_words[3]; |
| 201 |
|
| 202 |
input[4] = CRYPTO_load_u32_le(key + 0); |
| 203 |
input[5] = CRYPTO_load_u32_le(key + 4); |
| 204 |
input[6] = CRYPTO_load_u32_le(key + 8); |
| 205 |
input[7] = CRYPTO_load_u32_le(key + 12); |
| 206 |
|
| 207 |
input[8] = CRYPTO_load_u32_le(key + 16); |
| 208 |
input[9] = CRYPTO_load_u32_le(key + 20); |
| 209 |
input[10] = CRYPTO_load_u32_le(key + 24); |
| 210 |
input[11] = CRYPTO_load_u32_le(key + 28); |
| 211 |
|
| 212 |
input[12] = counter; |
| 213 |
input[13] = CRYPTO_load_u32_le(nonce + 0); |
| 214 |
input[14] = CRYPTO_load_u32_le(nonce + 4); |
| 215 |
input[15] = CRYPTO_load_u32_le(nonce + 8); |
| 216 |
|
| 217 |
while (in_len > 0) { |
| 218 |
todo = sizeof(buf); |
| 219 |
if (in_len < todo) { |
| 220 |
todo = in_len; |
| 221 |
} |
| 222 |
|
| 223 |
chacha_core(buf, input); |
| 224 |
for (i = 0; i < todo; i++) { |
| 225 |
out[i] = in[i] ^ buf[i]; |
| 226 |
} |
| 227 |
|
| 228 |
out += todo; |
| 229 |
in += todo; |
| 230 |
in_len -= todo; |
| 231 |
|
| 232 |
input[12]++; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#endif |