Skip to content

Commit f62e3d8

Browse files
committed
BIP324 Cipher Suite
1 parent ad904f3 commit f62e3d8

14 files changed

+461
-596
lines changed

src/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ crypto_libbitcoin_crypto_base_la_LDFLAGS = $(AM_LDFLAGS) -static
494494
crypto_libbitcoin_crypto_base_la_SOURCES = \
495495
crypto/aes.cpp \
496496
crypto/aes.h \
497-
crypto/chacha_poly_aead.h \
498-
crypto/chacha_poly_aead.cpp \
497+
crypto/bip324_suite.h \
498+
crypto/bip324_suite.cpp \
499499
crypto/chacha20.h \
500500
crypto/chacha20.cpp \
501501
crypto/common.h \

src/Makefile.bench.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ bench_bench_bitcoin_SOURCES = \
1818
bench/bench.cpp \
1919
bench/bench.h \
2020
bench/bench_bitcoin.cpp \
21+
bench/bip324_suite.cpp \
2122
bench/block_assemble.cpp \
2223
bench/ccoins_caching.cpp \
2324
bench/chacha20.cpp \
24-
bench/chacha_poly_aead.cpp \
2525
bench/checkblock.cpp \
2626
bench/checkqueue.cpp \
2727
bench/crypto_hash.cpp \

src/Makefile.test.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ test_fuzz_fuzz_SOURCES = \
243243
test/fuzz/crypto.cpp \
244244
test/fuzz/crypto_aes256.cpp \
245245
test/fuzz/crypto_aes256cbc.cpp \
246+
test/fuzz/crypto_bip324_suite.cpp \
246247
test/fuzz/crypto_chacha20.cpp \
247-
test/fuzz/crypto_chacha20_poly1305_aead.cpp \
248248
test/fuzz/crypto_common.cpp \
249249
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \
250250
test/fuzz/crypto_poly1305.cpp \

src/bench/bip324_suite.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) 2019-2020 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+
6+
#include <assert.h>
7+
#include <bench/bench.h>
8+
#include <crypto/bip324_suite.h>
9+
#include <crypto/rfc8439.h> // for the RFC8439_TAGLEN constant
10+
#include <hash.h>
11+
12+
#include <array>
13+
#include <cstddef>
14+
#include <vector>
15+
16+
/* Number of bytes to process per iteration */
17+
static constexpr uint64_t BUFFER_SIZE_TINY = 64;
18+
static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
19+
static constexpr uint64_t BUFFER_SIZE_LARGE = 1024 * 1024;
20+
21+
static const std::vector<std::byte> zero_vec(BIP324_KEY_LEN, std::byte{0x00});
22+
23+
static void BIP324_CIPHER_SUITE(benchmark::Bench& bench, size_t plaintext_len, bool include_decryption)
24+
{
25+
BIP324Key zero_arr;
26+
memcpy(zero_arr.data(), zero_vec.data(), BIP324_KEY_LEN);
27+
BIP324CipherSuite enc{zero_arr, zero_arr, zero_arr};
28+
BIP324CipherSuite dec{zero_arr, zero_arr, zero_arr};
29+
30+
auto ciphertext_len = BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + plaintext_len + RFC8439_TAGLEN;
31+
32+
std::vector<std::byte> in(plaintext_len, std::byte{0x00});
33+
std::vector<std::byte> out(ciphertext_len, std::byte{0x00});
34+
35+
BIP324HeaderFlags flags{0};
36+
37+
bench.batch(plaintext_len).unit("byte").run([&] {
38+
// encrypt or decrypt the buffer with a static key
39+
const bool crypt_ok_1 = enc.Crypt(in, out, flags, true);
40+
assert(crypt_ok_1);
41+
42+
if (include_decryption) {
43+
// if we decrypt, we need to decrypt the length first
44+
std::array<std::byte, BIP324_LENGTH_FIELD_LEN> len_ciphertext;
45+
memcpy(len_ciphertext.data(), out.data(), BIP324_LENGTH_FIELD_LEN);
46+
(void)dec.DecryptLength(len_ciphertext);
47+
const bool crypt_ok_2 = dec.Crypt({out.data() + BIP324_LENGTH_FIELD_LEN, out.size() - BIP324_LENGTH_FIELD_LEN}, in, flags, false);
48+
assert(crypt_ok_2);
49+
}
50+
});
51+
}
52+
53+
static void BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
54+
{
55+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, false);
56+
}
57+
58+
static void BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
59+
{
60+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, false);
61+
}
62+
63+
static void BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT(benchmark::Bench& bench)
64+
{
65+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, false);
66+
}
67+
68+
static void BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
69+
{
70+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, true);
71+
}
72+
73+
static void BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
74+
{
75+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, true);
76+
}
77+
78+
static void BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT(benchmark::Bench& bench)
79+
{
80+
BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, true);
81+
}
82+
83+
// Add Hash() (dbl-sha256) bench for comparison
84+
85+
static void HASH(benchmark::Bench& bench, size_t buffersize)
86+
{
87+
uint8_t hash[CHash256::OUTPUT_SIZE];
88+
std::vector<uint8_t> in(buffersize, 0);
89+
bench.batch(in.size()).unit("byte").run([&] {
90+
CHash256().Write(in).Finalize(hash);
91+
});
92+
}
93+
94+
static void HASH_64BYTES(benchmark::Bench& bench)
95+
{
96+
HASH(bench, BUFFER_SIZE_TINY);
97+
}
98+
99+
static void HASH_256BYTES(benchmark::Bench& bench)
100+
{
101+
HASH(bench, BUFFER_SIZE_SMALL);
102+
}
103+
104+
static void HASH_1MB(benchmark::Bench& bench)
105+
{
106+
HASH(bench, BUFFER_SIZE_LARGE);
107+
}
108+
109+
BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT);
110+
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT);
111+
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT);
112+
BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT);
113+
BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT);
114+
BENCHMARK(BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT);
115+
BENCHMARK(HASH_64BYTES);
116+
BENCHMARK(HASH_256BYTES);
117+
BENCHMARK(HASH_1MB);

src/bench/chacha_poly_aead.cpp

-126
This file was deleted.

src/crypto/bip324_suite.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2019-2021 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+
#include <crypto/bip324_suite.h>
6+
7+
#include <crypto/common.h>
8+
#include <crypto/poly1305.h>
9+
#include <crypto/sha256.h>
10+
#include <span.h>
11+
#include <support/cleanse.h>
12+
13+
#include <assert.h>
14+
#include <cstring>
15+
#include <string.h>
16+
17+
#ifndef HAVE_TIMINGSAFE_BCMP
18+
19+
int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n)
20+
{
21+
const unsigned char *p1 = b1, *p2 = b2;
22+
int ret = 0;
23+
24+
for (; n > 0; n--)
25+
ret |= *p1++ ^ *p2++;
26+
return (ret != 0);
27+
}
28+
29+
#endif // TIMINGSAFE_BCMP
30+
31+
BIP324CipherSuite::~BIP324CipherSuite()
32+
{
33+
memory_cleanse(payload_key.data(), payload_key.size());
34+
memory_cleanse(rekey_salt.data(), rekey_salt.size());
35+
}
36+
37+
bool BIP324CipherSuite::Crypt(Span<const std::byte> input, Span<std::byte> output,
38+
BIP324HeaderFlags& flags, bool encrypt)
39+
{
40+
// check buffer boundaries
41+
if (
42+
// if we encrypt, make sure the destination has the space for the length field, header, ciphertext and MAC
43+
(encrypt && (output.size() < BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + input.size() + RFC8439_TAGLEN)) ||
44+
// if we decrypt, make sure the source contains at least the header + mac and the destination has the space for the source - MAC - header
45+
(!encrypt && (input.size() < BIP324_HEADER_LEN + RFC8439_TAGLEN || output.size() < input.size() - BIP324_HEADER_LEN - RFC8439_TAGLEN))) {
46+
return false;
47+
}
48+
49+
if (encrypt) {
50+
// input is just the payload
51+
// output will be encrypted length + encrypted (header and payload) + mac tag
52+
uint32_t ciphertext_len = BIP324_HEADER_LEN + input.size();
53+
WriteLE32(reinterpret_cast<unsigned char*>(&ciphertext_len), ciphertext_len);
54+
55+
std::vector<std::byte> input_vec;
56+
input_vec.resize(BIP324_HEADER_LEN + input.size());
57+
58+
// TODO: this can be optimized by changing the RFC8439Encrypt interface to accept a list of inputs.
59+
// But, at the moment, there's a potential bug in out ChaCha20 implementation for plaintexts that
60+
// are not a multiple of 64 bytes -- the rest of the "block" is discarded. An update is in progress
61+
// which will help here.
62+
memcpy(input_vec.data(), &flags, BIP324_HEADER_LEN);
63+
if (!input.empty()) {
64+
memcpy(input_vec.data() + BIP324_HEADER_LEN, input.data(), input.size());
65+
}
66+
67+
auto encrypted = RFC8439Encrypt({}, payload_key, nonce, input_vec);
68+
69+
auto write_pos = output.data();
70+
fsc20.Crypt({reinterpret_cast<std::byte*>(&ciphertext_len), BIP324_LENGTH_FIELD_LEN},
71+
{write_pos, BIP324_LENGTH_FIELD_LEN});
72+
write_pos += BIP324_LENGTH_FIELD_LEN;
73+
memcpy(write_pos, encrypted.ciphertext.data(), encrypted.ciphertext.size());
74+
write_pos += encrypted.ciphertext.size();
75+
memcpy(write_pos, encrypted.tag.data(), encrypted.tag.size());
76+
write_pos += encrypted.tag.size();
77+
} else {
78+
// we must use BIP324CipherSuite::DecryptLength before calling BIP324CipherSuite::Crypt
79+
// input is encrypted (header + payload) and the mac tag
80+
// decrypted header will be put in flags and output will be payload.
81+
auto ciphertext_size = input.size() - RFC8439_TAGLEN;
82+
RFC8439Encrypted encrypted;
83+
encrypted.ciphertext.resize(ciphertext_size);
84+
memcpy(encrypted.ciphertext.data(), input.data(), ciphertext_size);
85+
memcpy(encrypted.tag.data(), input.data() + ciphertext_size, RFC8439_TAGLEN);
86+
auto decrypted = RFC8439Decrypt({}, payload_key, nonce, encrypted);
87+
if (!decrypted.success) {
88+
return false;
89+
}
90+
91+
memcpy(&flags, decrypted.plaintext.data(), BIP324_HEADER_LEN);
92+
if (!output.empty()) {
93+
memcpy(output.data(), decrypted.plaintext.data() + BIP324_HEADER_LEN, ciphertext_size - BIP324_HEADER_LEN);
94+
}
95+
}
96+
97+
msg_ctr++;
98+
if (msg_ctr == REKEY_INTERVAL) {
99+
unsigned char new_key[CSHA256::OUTPUT_SIZE];
100+
assert(CSHA256::OUTPUT_SIZE == BIP324_KEY_LEN);
101+
auto hasher = CSHA256().Write(UCharCast(rekey_salt.data()), rekey_salt.size());
102+
hasher.Write(UCharCast(payload_key.data()), payload_key.size()).Finalize(new_key);
103+
memcpy(payload_key.data(), new_key, BIP324_KEY_LEN);
104+
rekey_ctr++;
105+
msg_ctr = 0;
106+
}
107+
set_nonce();
108+
return true;
109+
}
110+
111+
uint32_t BIP324CipherSuite::DecryptLength(const std::array<std::byte, BIP324_LENGTH_FIELD_LEN>& ciphertext)
112+
{
113+
std::array<uint8_t, BIP324_LENGTH_FIELD_LEN> length_buffer;
114+
fsc20.Crypt(ciphertext, MakeWritableByteSpan(length_buffer));
115+
116+
return (uint32_t{length_buffer[0]}) |
117+
(uint32_t{length_buffer[1]} << 8) |
118+
(uint32_t{length_buffer[2]} << 16);
119+
}

0 commit comments

Comments
 (0)