| 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 <assert.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <openssl/cpu.h> |
| 23 |
|
| 24 |
#include "../internal.h" |
| 25 |
|
| 26 |
|
| 27 |
#define U8TO32_LITTLE(p) \ |
| 28 |
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \ |
| 29 |
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24)) |
| 30 |
|
| 31 |
#if !defined(OPENSSL_NO_ASM) && \ |
| 32 |
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ |
| 33 |
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
| 34 |
|
| 35 |
/* ChaCha20_ctr32 is defined in asm/chacha-*.pl. */ |
| 36 |
void ChaCha20_ctr32(uint8_t *out, const uint8_t *in, size_t in_len, |
| 37 |
const uint32_t key[8], const uint32_t counter[4]); |
| 38 |
|
| 39 |
void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len, |
| 40 |
const uint8_t key[32], const uint8_t nonce[12], |
| 41 |
uint32_t counter) { |
| 42 |
assert(!buffers_alias(out, in_len, in, in_len) || in == out); |
| 43 |
|
| 44 |
uint32_t counter_nonce[4]; counter_nonce[0] = counter; |
| 45 |
counter_nonce[1] = U8TO32_LITTLE(nonce + 0); |
| 46 |
counter_nonce[2] = U8TO32_LITTLE(nonce + 4); |
| 47 |
counter_nonce[3] = U8TO32_LITTLE(nonce + 8); |
| 48 |
|
| 49 |
const uint32_t *key_ptr = (const uint32_t *)key; |
| 50 |
#if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64) |
| 51 |
/* The assembly expects the key to be four-byte aligned. */ |
| 52 |
uint32_t key_u32[8]; |
| 53 |
if ((((uintptr_t)key) & 3) != 0) { |
| 54 |
key_u32[0] = U8TO32_LITTLE(key + 0); |
| 55 |
key_u32[1] = U8TO32_LITTLE(key + 4); |
| 56 |
key_u32[2] = U8TO32_LITTLE(key + 8); |
| 57 |
key_u32[3] = U8TO32_LITTLE(key + 12); |
| 58 |
key_u32[4] = U8TO32_LITTLE(key + 16); |
| 59 |
key_u32[5] = U8TO32_LITTLE(key + 20); |
| 60 |
key_u32[6] = U8TO32_LITTLE(key + 24); |
| 61 |
key_u32[7] = U8TO32_LITTLE(key + 28); |
| 62 |
|
| 63 |
key_ptr = key_u32; |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
ChaCha20_ctr32(out, in, in_len, key_ptr, counter_nonce); |
| 68 |
} |
| 69 |
|
| 70 |
#else |
| 71 |
|
| 72 |
/* sigma contains the ChaCha constants, which happen to be an ASCII string. */ |
| 73 |
static const uint8_t sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', |
| 74 |
'2', '-', 'b', 'y', 't', 'e', ' ', 'k' }; |
| 75 |
|
| 76 |
#define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) |
| 77 |
#define XOR(v, w) ((v) ^ (w)) |
| 78 |
#define PLUS(x, y) ((x) + (y)) |
| 79 |
#define PLUSONE(v) (PLUS((v), 1)) |
| 80 |
|
| 81 |
#define U32TO8_LITTLE(p, v) \ |
| 82 |
{ \ |
| 83 |
(p)[0] = (v >> 0) & 0xff; \ |
| 84 |
(p)[1] = (v >> 8) & 0xff; \ |
| 85 |
(p)[2] = (v >> 16) & 0xff; \ |
| 86 |
(p)[3] = (v >> 24) & 0xff; \ |
| 87 |
} |
| 88 |
|
| 89 |
/* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */ |
| 90 |
#define QUARTERROUND(a,b,c,d) \ |
| 91 |
x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ |
| 92 |
x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ |
| 93 |
x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ |
| 94 |
x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); |
| 95 |
|
| 96 |
/* chacha_core performs 20 rounds of ChaCha on the input words in |
| 97 |
* |input| and writes the 64 output bytes to |output|. */ |
| 98 |
static void chacha_core(uint8_t output[64], const uint32_t input[16]) { |
| 99 |
uint32_t x[16]; |
| 100 |
int i; |
| 101 |
|
| 102 |
memcpy(x, input, sizeof(uint32_t) * 16); |
| 103 |
for (i = 20; i > 0; i -= 2) { |
| 104 |
QUARTERROUND(0, 4, 8, 12) |
| 105 |
QUARTERROUND(1, 5, 9, 13) |
| 106 |
QUARTERROUND(2, 6, 10, 14) |
| 107 |
QUARTERROUND(3, 7, 11, 15) |
| 108 |
QUARTERROUND(0, 5, 10, 15) |
| 109 |
QUARTERROUND(1, 6, 11, 12) |
| 110 |
QUARTERROUND(2, 7, 8, 13) |
| 111 |
QUARTERROUND(3, 4, 9, 14) |
| 112 |
} |
| 113 |
|
| 114 |
for (i = 0; i < 16; ++i) { |
| 115 |
x[i] = PLUS(x[i], input[i]); |
| 116 |
} |
| 117 |
for (i = 0; i < 16; ++i) { |
| 118 |
U32TO8_LITTLE(output + 4 * i, x[i]); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len, |
| 123 |
const uint8_t key[32], const uint8_t nonce[12], |
| 124 |
uint32_t counter) { |
| 125 |
assert(!buffers_alias(out, in_len, in, in_len) || in == out); |
| 126 |
|
| 127 |
uint32_t input[16]; |
| 128 |
uint8_t buf[64]; |
| 129 |
size_t todo, i; |
| 130 |
|
| 131 |
input[0] = U8TO32_LITTLE(sigma + 0); |
| 132 |
input[1] = U8TO32_LITTLE(sigma + 4); |
| 133 |
input[2] = U8TO32_LITTLE(sigma + 8); |
| 134 |
input[3] = U8TO32_LITTLE(sigma + 12); |
| 135 |
|
| 136 |
input[4] = U8TO32_LITTLE(key + 0); |
| 137 |
input[5] = U8TO32_LITTLE(key + 4); |
| 138 |
input[6] = U8TO32_LITTLE(key + 8); |
| 139 |
input[7] = U8TO32_LITTLE(key + 12); |
| 140 |
|
| 141 |
input[8] = U8TO32_LITTLE(key + 16); |
| 142 |
input[9] = U8TO32_LITTLE(key + 20); |
| 143 |
input[10] = U8TO32_LITTLE(key + 24); |
| 144 |
input[11] = U8TO32_LITTLE(key + 28); |
| 145 |
|
| 146 |
input[12] = counter; |
| 147 |
input[13] = U8TO32_LITTLE(nonce + 0); |
| 148 |
input[14] = U8TO32_LITTLE(nonce + 4); |
| 149 |
input[15] = U8TO32_LITTLE(nonce + 8); |
| 150 |
|
| 151 |
while (in_len > 0) { |
| 152 |
todo = sizeof(buf); |
| 153 |
if (in_len < todo) { |
| 154 |
todo = in_len; |
| 155 |
} |
| 156 |
|
| 157 |
chacha_core(buf, input); |
| 158 |
for (i = 0; i < todo; i++) { |
| 159 |
out[i] = in[i] ^ buf[i]; |
| 160 |
} |
| 161 |
|
| 162 |
out += todo; |
| 163 |
in += todo; |
| 164 |
in_len -= todo; |
| 165 |
|
| 166 |
input[12]++; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
#endif |