Bitcoin Core 30.99.0
P2P Digital Currency
chacha20.h
Go to the documentation of this file.
1// Copyright (c) 2017-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_CRYPTO_CHACHA20_H
6#define BITCOIN_CRYPTO_CHACHA20_H
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <iterator>
12#include <span>
13#include <utility>
14
15// classes for ChaCha20 256-bit stream cipher developed by Daniel J. Bernstein
16// https://cr.yp.to/chacha/chacha-20080128.pdf.
17//
18// The 128-bit input is here implemented as a 96-bit nonce and a 32-bit block
19// counter, as in RFC8439 Section 2.3. When the 32-bit block counter overflows
20// the first 32-bit part of the nonce is automatically incremented, making it
21// conceptually compatible with variants that use a 64/64 split instead.
22
25{
26private:
27 uint32_t input[12];
28
29public:
31 static constexpr unsigned KEYLEN{32};
32
34 static constexpr unsigned BLOCKLEN{64};
35
37 ChaCha20Aligned() noexcept = delete;
38
40 ChaCha20Aligned(std::span<const std::byte> key) noexcept;
41
44
46 void SetKey(std::span<const std::byte> key) noexcept;
47
56 using Nonce96 = std::pair<uint32_t, uint64_t>;
57
63 void Seek(Nonce96 nonce, uint32_t block_counter) noexcept;
64
66 void Keystream(std::span<std::byte> out) noexcept;
67
72 void Crypt(std::span<const std::byte> input, std::span<std::byte> output) noexcept;
73};
74
77{
78private:
80 std::array<std::byte, ChaCha20Aligned::BLOCKLEN> m_buffer;
81 unsigned m_bufleft{0};
82
83public:
85 static constexpr unsigned KEYLEN = ChaCha20Aligned::KEYLEN;
86
88 ChaCha20() noexcept = delete;
89
91 ChaCha20(std::span<const std::byte> key) noexcept : m_aligned(key) {}
92
94 ~ChaCha20();
95
97 void SetKey(std::span<const std::byte> key) noexcept;
98
101
103 void Seek(Nonce96 nonce, uint32_t block_counter) noexcept
104 {
105 m_aligned.Seek(nonce, block_counter);
106 m_bufleft = 0;
107 }
108
113 void Crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes) noexcept;
114
116 void Keystream(std::span<std::byte> out) noexcept;
117};
118
127{
128private:
131
133 const uint32_t m_rekey_interval;
134
136 uint32_t m_chunk_counter{0};
137
139 uint64_t m_rekey_counter{0};
140
141public:
143 static constexpr unsigned KEYLEN = 32;
144
145 // No copy or move to protect the secret.
146 FSChaCha20(const FSChaCha20&) = delete;
148 FSChaCha20& operator=(const FSChaCha20&) = delete;
150
152 FSChaCha20(std::span<const std::byte> key, uint32_t rekey_interval) noexcept;
153
155 void Crypt(std::span<const std::byte> input, std::span<std::byte> output) noexcept;
156};
157
158#endif // BITCOIN_CRYPTO_CHACHA20_H
ChaCha20 cipher that only operates on multiples of 64 bytes.
Definition: chacha20.h:25
void Keystream(std::span< std::byte > out) noexcept
outputs the keystream into out, whose length must be a multiple of BLOCKLEN.
Definition: chacha20.cpp:59
void Crypt(std::span< const std::byte > input, std::span< std::byte > output) noexcept
en/deciphers the message <input> and write the result into <output>
Definition: chacha20.cpp:160
ChaCha20Aligned() noexcept=delete
For safety, disallow initialization without key.
std::pair< uint32_t, uint64_t > Nonce96
Type for 96-bit nonces used by the Set function below.
Definition: chacha20.h:56
uint32_t input[12]
Definition: chacha20.h:27
void Seek(Nonce96 nonce, uint32_t block_counter) noexcept
Set the 96-bit nonce and 32-bit block counter.
Definition: chacha20.cpp:51
static constexpr unsigned BLOCKLEN
Block size (inputs/outputs to Keystream / Crypt should be multiples of this).
Definition: chacha20.h:34
static constexpr unsigned KEYLEN
Expected key length in constructor and SetKey.
Definition: chacha20.h:31
void SetKey(std::span< const std::byte > key) noexcept
Set 32-byte key, and seek to nonce 0 and block position 0.
Definition: chacha20.cpp:24
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:77
ChaCha20() noexcept=delete
For safety, disallow initialization without key.
std::array< std::byte, ChaCha20Aligned::BLOCKLEN > m_buffer
Definition: chacha20.h:80
void Seek(Nonce96 nonce, uint32_t block_counter) noexcept
Set the 96-bit nonce and 32-bit block counter.
Definition: chacha20.h:103
ChaCha20Aligned m_aligned
Definition: chacha20.h:79
ChaCha20Aligned::Nonce96 Nonce96
96-bit nonce type.
Definition: chacha20.h:100
Forward-secure ChaCha20.
Definition: chacha20.h:127
ChaCha20 m_chacha20
Internal stream cipher.
Definition: chacha20.h:130
FSChaCha20 & operator=(const FSChaCha20 &)=delete
FSChaCha20(FSChaCha20 &&)=delete
const uint32_t m_rekey_interval
The number of encryptions/decryptions before a rekey happens.
Definition: chacha20.h:133
FSChaCha20 & operator=(FSChaCha20 &&)=delete
FSChaCha20(const FSChaCha20 &)=delete
unsigned int nonce
Definition: miner_tests.cpp:76