Repository navigation
Expand file tree
/
Copy pathcipher.cpp
More file actions
166 lines (145 loc) · 5.38 KB
/
Copy pathcipher.cpp
File metadata and controls
166 lines (145 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: Apache-2.0
#include "cipher.hpp"
#include <sodium.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
namespace gn::noise {
namespace {
/// Build the 12-byte ChaCha20-Poly1305 IETF nonce per Noise §5.1:
/// four zero bytes followed by @p counter in little-endian order.
void encode_nonce(std::uint64_t counter,
std::uint8_t out[AEAD_NONCE_BYTES]) noexcept {
out[0] = out[1] = out[2] = out[3] = 0;
for (int i = 0; i < 8; ++i) {
out[4 + i] = static_cast<std::uint8_t>((counter >> (i * 8)) & 0xFFu);
}
}
} // namespace
CipherState::CipherState(CipherState&& other) noexcept
: k_(other.k_), n_(other.n_), has_key_(other.has_key_) {
other.zeroize();
}
CipherState& CipherState::operator=(CipherState&& other) noexcept {
if (this != &other) {
zeroize();
k_ = other.k_;
n_ = other.n_;
has_key_ = other.has_key_;
other.zeroize();
}
return *this;
}
CipherState::~CipherState() {
zeroize();
}
void CipherState::initialize_key(const CipherKey& k) noexcept {
k_ = k;
n_ = 0;
has_key_ = true;
}
std::vector<std::uint8_t>
CipherState::encrypt_with_ad(std::span<const std::uint8_t> ad,
std::span<const std::uint8_t> plaintext) {
if (!has_key_) {
return std::vector<std::uint8_t>(plaintext.begin(), plaintext.end());
}
/// Per Noise §5.1: signal an error if incrementing `n` would
/// wrap. ChaCha20-Poly1305-IETF is a deterministic AEAD —
/// reusing a nonce under the same key reveals plaintext XOR
/// and forges authenticator tags. There is no recovery path
/// in the wire protocol once a nonce wraps, so abort rather
/// than continue with broken crypto. The bound is
/// 2^64-1 messages per direction per key — at any realistic
/// rate the bound is unreachable, but the check costs nothing.
if (n_ == std::numeric_limits<std::uint64_t>::max()) {
/// Pre-abort breadcrumb on stderr — CipherState is a pure
/// crypto primitive with no host_api handle, so we cannot
/// reach the kernel log vtable. Without this print line the
/// process dies with bare SIGABRT and operators have no clue
/// why; a single fprintf is justified for an event that
/// terminates the process.
std::fprintf(stderr,
"[noise] cipher.cpp:%d FATAL: encrypt nonce wrap "
"(2^64-1 messages on one direction); aborting to prevent "
"(key, nonce) reuse. Caller should have triggered REKEY "
"well before this point — see handshake.md §4.\n",
__LINE__);
std::abort();
}
std::uint8_t nonce[AEAD_NONCE_BYTES];
encode_nonce(n_, nonce);
std::vector<std::uint8_t> out(plaintext.size() + AEAD_TAG_BYTES);
unsigned long long out_len = 0;
crypto_aead_chacha20poly1305_ietf_encrypt(
out.data(), &out_len,
plaintext.data(), plaintext.size(),
ad.data(), ad.size(),
nullptr,
nonce, k_.data());
out.resize(static_cast<std::size_t>(out_len));
++n_;
return out;
}
std::optional<std::vector<std::uint8_t>>
CipherState::decrypt_with_ad(std::span<const std::uint8_t> ad,
std::span<const std::uint8_t> ciphertext) {
if (!has_key_) {
return std::vector<std::uint8_t>(ciphertext.begin(), ciphertext.end());
}
if (ciphertext.size() < AEAD_TAG_BYTES) {
return std::nullopt;
}
/// Same wrap guard as `encrypt_with_ad`. Decrypt and encrypt
/// share one counter on each end of the bidirectional pair
/// (each direction has its own `CipherState`), so abusing a
/// nonce here is just as catastrophic as on the encrypt side.
if (n_ == std::numeric_limits<std::uint64_t>::max()) {
std::fprintf(stderr,
"[noise] cipher.cpp:%d FATAL: decrypt nonce wrap "
"(2^64-1 messages on one direction); aborting to prevent "
"(key, nonce) reuse. Indicates either no REKEY happened "
"or an active attack stuffing the channel — see "
"handshake.md §4.\n",
__LINE__);
std::abort();
}
std::uint8_t nonce[AEAD_NONCE_BYTES];
encode_nonce(n_, nonce);
std::vector<std::uint8_t> out(ciphertext.size() - AEAD_TAG_BYTES);
unsigned long long out_len = 0;
if (crypto_aead_chacha20poly1305_ietf_decrypt(
out.data(), &out_len,
nullptr,
ciphertext.data(), ciphertext.size(),
ad.data(), ad.size(),
nonce, k_.data()) != 0) {
return std::nullopt;
}
out.resize(static_cast<std::size_t>(out_len));
++n_;
return out;
}
void CipherState::rekey() noexcept {
if (!has_key_) return;
std::uint8_t nonce[AEAD_NONCE_BYTES];
encode_nonce(0xFFFFFFFFFFFFFFFFULL, nonce);
std::uint8_t zeros[CIPHER_KEY_BYTES] = {};
std::uint8_t out[CIPHER_KEY_BYTES + AEAD_TAG_BYTES];
unsigned long long out_len = 0;
crypto_aead_chacha20poly1305_ietf_encrypt(
out, &out_len,
zeros, CIPHER_KEY_BYTES,
nullptr, 0,
nullptr,
nonce, k_.data());
std::memcpy(k_.data(), out, CIPHER_KEY_BYTES);
sodium_memzero(out, sizeof(out));
}
void CipherState::zeroize() noexcept {
sodium_memzero(k_.data(), CIPHER_KEY_BYTES);
n_ = 0;
has_key_ = false;
}
} // namespace gn::noise