Bitcoin ABC 0.32.4
P2P Digital Currency
chacha20poly1305.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 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
6
7#include <crypto/chacha20.h>
8#include <crypto/common.h>
9#include <crypto/poly1305.h>
10#include <span.h>
11#include <support/cleanse.h>
12
13#include <cassert>
14#include <cstddef>
15#include <cstdint>
16#include <iterator>
17
19 : m_chacha20(key) {
20 assert(key.size() == KEYLEN);
21}
22
24 assert(key.size() == KEYLEN);
25 m_chacha20.SetKey(key);
26}
27
28namespace {
29
30int timingsafe_bcmp_internal(const uint8_t *b1, const uint8_t *b2,
31 size_t n) noexcept {
32 const uint8_t *p1 = b1, *p2 = b2;
33 int ret = 0;
34 for (; n > 0; n--) {
35 ret |= *p1++ ^ *p2++;
36 }
37 return (ret != 0);
38}
39
44void ComputeTag(ChaCha20 &chacha20, Span<const std::byte> aad,
45 Span<const std::byte> cipher, Span<std::byte> tag) noexcept {
46 static const std::byte PADDING[16] = {{}};
47
48 // Get block of keystream (use a full 64 byte buffer to avoid the need for
49 // chacha20's own buffering).
50 std::byte first_block[ChaCha20Aligned::BLOCKLEN];
51 chacha20.Keystream(first_block);
52
53 // Use the first 32 bytes of the first keystream block as poly1305 key.
54 Poly1305 poly1305{Span{first_block}.first(Poly1305::KEYLEN)};
55
56 // Compute tag:
57 // - Process the padded AAD with Poly1305.
58 const unsigned aad_padding_length = (16 - (aad.size() % 16)) % 16;
59 poly1305.Update(aad).Update(Span{PADDING}.first(aad_padding_length));
60 // - Process the padded ciphertext with Poly1305.
61 const unsigned cipher_padding_length = (16 - (cipher.size() % 16)) % 16;
62 poly1305.Update(cipher).Update(Span{PADDING}.first(cipher_padding_length));
63 // - Process the AAD and plaintext length with Poly1305.
64 std::byte length_desc[Poly1305::TAGLEN];
65 WriteLE64(UCharCast(length_desc), aad.size());
66 WriteLE64(UCharCast(length_desc + 8), cipher.size());
67 poly1305.Update(length_desc);
68
69 // Output tag.
70 poly1305.Finalize(tag);
71}
72
73} // namespace
74
78 Span<std::byte> cipher) noexcept {
79 assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION);
80
81 // Encrypt using ChaCha20 (starting at block 1).
82 m_chacha20.Seek(nonce, 1);
83 m_chacha20.Crypt(plain1, cipher.first(plain1.size()));
84 m_chacha20.Crypt(plain2,
85 cipher.subspan(plain1.size()).first(plain2.size()));
86
87 // Seek to block 0, and compute tag using key drawn from there.
88 m_chacha20.Seek(nonce, 0);
89 ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION),
90 cipher.last(EXPANSION));
91}
92
95 Span<std::byte> plain1,
96 Span<std::byte> plain2) noexcept {
97 assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION);
98
99 // Verify tag (using key drawn from block 0).
100 m_chacha20.Seek(nonce, 0);
101 std::byte expected_tag[EXPANSION];
102 ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION),
103 expected_tag);
104 if (timingsafe_bcmp_internal(
105 UCharCast(expected_tag),
106 UCharCast(cipher.data() + cipher.size() - EXPANSION), EXPANSION)) {
107 return false;
108 }
109
110 // Decrypt (starting at block 1).
111 m_chacha20.Crypt(cipher.first(plain1.size()), plain1);
112 m_chacha20.Crypt(cipher.subspan(plain1.size()).first(plain2.size()),
113 plain2);
114 return true;
115}
116
118 Span<std::byte> keystream) noexcept {
119 // Skip the first output block, as it's used for generating the poly1305
120 // key.
121 m_chacha20.Seek(nonce, 1);
122 m_chacha20.Keystream(keystream);
123}
124
127 // Generate a full block of keystream, to avoid needing the ChaCha20
128 // buffer, even though we only need KEYLEN (32) bytes.
129 std::byte one_block[ChaCha20Aligned::BLOCKLEN];
130 m_aead.Keystream({0xFFFFFFFF, m_rekey_counter}, one_block);
131 // Switch keys.
132 m_aead.SetKey(Span{one_block}.first(KEYLEN));
133 // Wipe the generated keystream (a copy remains inside m_aead, which
134 // will be cleaned up once it cycles again, or is destroyed).
135 memory_cleanse(one_block, sizeof(one_block));
136 // Update counters.
139 }
140}
141
145 Span<std::byte> cipher) noexcept {
146 m_aead.Encrypt(plain1, plain2, aad, {m_packet_counter, m_rekey_counter},
147 cipher);
148 NextPacket();
149}
150
153 Span<std::byte> plain1,
154 Span<std::byte> plain2) noexcept {
155 bool ret = m_aead.Decrypt(cipher, aad, {m_packet_counter, m_rekey_counter},
156 plain1, plain2);
157 NextPacket();
158 return ret;
159}
AEADChaCha20Poly1305(Span< const std::byte > key) noexcept
Initialize an AEAD instance with a specified 32-byte key.
ChaCha20::Nonce96 Nonce96
96-bit nonce type.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Nonce96 nonce, Span< std::byte > cipher) noexcept
Encrypt a message with a specified 96-bit nonce and aad.
void SetKey(Span< const std::byte > key) noexcept
Switch to another 32-byte key.
bool Decrypt(Span< const std::byte > cipher, Span< const std::byte > aad, Nonce96 nonce, Span< std::byte > plain) noexcept
Decrypt a message with a specified 96-bit nonce and aad.
void Keystream(Nonce96 nonce, Span< std::byte > keystream) noexcept
Get a number of keystream bytes from the underlying stream cipher.
static constexpr unsigned BLOCKLEN
Block size (inputs/outputs to Keystream / Crypt should be multiples of this).
Definition: chacha20.h:37
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:89
void NextPacket() noexcept
Update counters (and if necessary, key) to transition to the next message.
const uint32_t m_rekey_interval
Every how many iterations this cipher rekeys.
bool Decrypt(Span< const std::byte > cipher, Span< const std::byte > aad, Span< std::byte > plain) noexcept
Decrypt a message with a specified aad.
uint32_t m_packet_counter
The number of encryptions/decryptions since the last rekey.
AEADChaCha20Poly1305 m_aead
Internal AEAD.
static constexpr auto KEYLEN
Length of keys expected by the constructor.
uint64_t m_rekey_counter
The number of rekeys performed so far.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
C++ wrapper with std::byte Span interface around poly1305_donna code.
Definition: poly1305.h:38
static constexpr unsigned KEYLEN
Length of the keys expected by the constructor.
Definition: poly1305.h:46
static constexpr unsigned TAGLEN
Length of the output produced by Finalize().
Definition: poly1305.h:43
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:228
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition: common.h:45
uint8_t * UCharCast(char *c)
Definition: span.h:310
assert(!tx.IsCoinBase())