8#include <botan/internal/chacha.h>
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/rotate.h>
15#if defined(BOTAN_HAS_CPUID)
16 #include <botan/internal/cpuid.h>
28constexpr uint64_t chacha_96bit_nonce_cap = uint64_t{1} << 38;
30inline void chacha_quarter_round(uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) {
48void hchacha(uint32_t output[8],
const uint32_t input[16],
size_t rounds) {
51 uint32_t x00 = input[0];
52 uint32_t x01 = input[1];
53 uint32_t x02 = input[2];
54 uint32_t x03 = input[3];
55 uint32_t x04 = input[4];
56 uint32_t x05 = input[5];
57 uint32_t x06 = input[6];
58 uint32_t x07 = input[7];
59 uint32_t x08 = input[8];
60 uint32_t x09 = input[9];
61 uint32_t x10 = input[10];
62 uint32_t x11 = input[11];
63 uint32_t x12 = input[12];
64 uint32_t x13 = input[13];
65 uint32_t x14 = input[14];
66 uint32_t x15 = input[15];
68 for(
size_t i = 0; i != rounds / 2; ++i) {
69 chacha_quarter_round(x00, x04, x08, x12);
70 chacha_quarter_round(x01, x05, x09, x13);
71 chacha_quarter_round(x02, x06, x10, x14);
72 chacha_quarter_round(x03, x07, x11, x15);
74 chacha_quarter_round(x00, x05, x10, x15);
75 chacha_quarter_round(x01, x06, x11, x12);
76 chacha_quarter_round(x02, x07, x08, x13);
77 chacha_quarter_round(x03, x04, x09, x14);
93 BOTAN_ARG_CHECK(m_rounds == 8 || m_rounds == 12 || m_rounds == 20,
"ChaCha only supports 8, 12 or 20 rounds");
96size_t ChaCha::parallelism() {
97#if defined(BOTAN_HAS_CHACHA_AVX512)
103#if defined(BOTAN_HAS_CHACHA_AVX2)
113#if defined(BOTAN_HAS_CHACHA_AVX512)
119#if defined(BOTAN_HAS_CHACHA_AVX2)
125#if defined(BOTAN_HAS_CHACHA_SIMD32)
134void ChaCha::chacha(uint8_t output[],
size_t output_blocks, uint32_t state[16],
size_t rounds) {
137#if defined(BOTAN_HAS_CHACHA_AVX512)
139 while(output_blocks >= 16) {
140 ChaCha::chacha_avx512_x16(output, state, rounds);
147#if defined(BOTAN_HAS_CHACHA_AVX2)
149 while(output_blocks >= 8) {
150 ChaCha::chacha_avx2_x8(output, state, rounds);
157#if defined(BOTAN_HAS_CHACHA_SIMD32)
159 while(output_blocks >= 4) {
160 ChaCha::chacha_simd32_x4(output, state, rounds);
168 for(
size_t i = 0; i != output_blocks; ++i) {
169 uint32_t x00 = state[0];
170 uint32_t x01 = state[1];
171 uint32_t x02 = state[2];
172 uint32_t x03 = state[3];
173 uint32_t x04 = state[4];
174 uint32_t x05 = state[5];
175 uint32_t x06 = state[6];
176 uint32_t x07 = state[7];
177 uint32_t x08 = state[8];
178 uint32_t x09 = state[9];
179 uint32_t x10 = state[10];
180 uint32_t x11 = state[11];
181 uint32_t x12 = state[12];
182 uint32_t x13 = state[13];
183 uint32_t x14 = state[14];
184 uint32_t x15 = state[15];
186 for(
size_t r = 0; r != rounds / 2; ++r) {
187 chacha_quarter_round(x00, x04, x08, x12);
188 chacha_quarter_round(x01, x05, x09, x13);
189 chacha_quarter_round(x02, x06, x10, x14);
190 chacha_quarter_round(x03, x07, x11, x15);
192 chacha_quarter_round(x00, x05, x10, x15);
193 chacha_quarter_round(x01, x06, x11, x12);
194 chacha_quarter_round(x02, x07, x08, x13);
195 chacha_quarter_round(x03, x04, x09, x14);
215 store_le(x00, output + 64 * i + 4 * 0);
216 store_le(x01, output + 64 * i + 4 * 1);
217 store_le(x02, output + 64 * i + 4 * 2);
218 store_le(x03, output + 64 * i + 4 * 3);
219 store_le(x04, output + 64 * i + 4 * 4);
220 store_le(x05, output + 64 * i + 4 * 5);
221 store_le(x06, output + 64 * i + 4 * 6);
222 store_le(x07, output + 64 * i + 4 * 7);
223 store_le(x08, output + 64 * i + 4 * 8);
224 store_le(x09, output + 64 * i + 4 * 9);
225 store_le(x10, output + 64 * i + 4 * 10);
226 store_le(x11, output + 64 * i + 4 * 11);
227 store_le(x12, output + 64 * i + 4 * 12);
228 store_le(x13, output + 64 * i + 4 * 13);
229 store_le(x14, output + 64 * i + 4 * 14);
230 store_le(x15, output + 64 * i + 4 * 15);
242void ChaCha::cipher_bytes(
const uint8_t in[], uint8_t out[],
size_t length) {
245 if(m_iv_length == 12) {
246 if(length > m_bytes_remaining) {
247 throw Invalid_State(
"ChaCha 96-bit nonce keystream exhausted");
249 m_bytes_remaining -= length;
252 while(length >= m_buffer.size() - m_position) {
253 const size_t available = m_buffer.size() - m_position;
255 xor_buf(out, in, &m_buffer[m_position], available);
256 chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
264 xor_buf(out, in, &m_buffer[m_position], length);
266 m_position += length;
269void ChaCha::generate_keystream(uint8_t out[],
size_t length) {
272 if(m_iv_length == 12) {
273 if(length > m_bytes_remaining) {
274 throw Invalid_State(
"ChaCha 96-bit nonce keystream exhausted");
276 m_bytes_remaining -= length;
279 while(length >= m_buffer.size() - m_position) {
280 const size_t available = m_buffer.size() - m_position;
284 copy_mem(out, &m_buffer[m_position], available);
285 chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
292 copy_mem(out, &m_buffer[m_position], length);
294 m_position += length;
297void ChaCha::initialize_state() {
298 static const uint32_t TAU[] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
300 static const uint32_t SIGMA[] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
302 m_state[4] = m_key[0];
303 m_state[5] = m_key[1];
304 m_state[6] = m_key[2];
305 m_state[7] = m_key[3];
307 if(m_key.size() == 4) {
313 m_state[8] = m_key[0];
314 m_state[9] = m_key[1];
315 m_state[10] = m_key[2];
316 m_state[11] = m_key[3];
318 m_state[0] = SIGMA[0];
319 m_state[1] = SIGMA[1];
320 m_state[2] = SIGMA[2];
321 m_state[3] = SIGMA[3];
323 m_state[8] = m_key[4];
324 m_state[9] = m_key[5];
325 m_state[10] = m_key[6];
326 m_state[11] = m_key[7];
338 return !m_state.empty();
348void ChaCha::key_schedule(std::span<const uint8_t> key) {
349 m_key.resize(key.size() / 4);
354 const size_t chacha_block = 64;
355 m_buffer.resize(parallelism() * chacha_block);
369 return std::make_unique<ChaCha>(m_rounds);
373 return (iv_len == 0 || iv_len == 8 || iv_len == 12 || iv_len == 24);
376void ChaCha::set_iv_bytes(
const uint8_t iv[],
size_t length) {
389 }
else if(length == 8) {
392 }
else if(length == 12) {
396 }
else if(length == 24) {
403 hchacha(hc.data(), m_state.data(), m_rounds);
419 m_iv_length = length;
420 m_state13_post_iv = m_state[13];
422 m_bytes_remaining = chacha_96bit_nonce_cap;
425 chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
435 m_state13_post_iv = 0;
436 m_bytes_remaining = 0;
443 return m_bytes_remaining;
447 return fmt(
"ChaCha({})", m_rounds);
453 const uint64_t block = offset / 64;
455 if(m_iv_length == 12) {
457 if((block >> 32) != 0) {
458 throw Invalid_Argument(
"ChaCha::seek with 96-bit nonce limited to 2^32 blocks (256 GiB)");
460 m_state[12] =
static_cast<uint32_t
>(block);
461 m_state[13] = m_state13_post_iv;
462 m_bytes_remaining = chacha_96bit_nonce_cap - offset;
465 m_state[12] =
static_cast<uint32_t
>(block);
466 m_state[13] = m_state13_post_iv +
static_cast<uint32_t
>(block >> 32);
469 chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
470 m_position = offset % 64;
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
static std::optional< std::string > check(CPUID::Feature feat)
static bool has(CPUID::Feature feat)
std::string name() const override
size_t buffer_size() const override
std::optional< uint64_t > remaining_keystream_bytes() const override
std::unique_ptr< StreamCipher > new_object() const override
Key_Length_Specification key_spec() const override
bool valid_iv_length(size_t iv_len) const override
size_t default_iv_length() const override
std::string provider() const override
bool has_keying_material() const override
void seek(uint64_t offset) override
void set_iv(const uint8_t iv[], size_t iv_len)
void assert_key_material_set() const
void zap(std::vector< T, Alloc > &vec)
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_le(ParamTs &&... params)
BOTAN_FORCE_INLINE constexpr T rotl(T input)
constexpr auto load_le(ParamTs &&... params)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector