Skip to content

Commit 4e5c933

Browse files
committed
Switch all callers from poly1305_auth to Poly1305 class
This also removes the old poly1305_auth interface, as it no longer serves any function. The new Poly1305 class based interface is more modern and safe.
1 parent 8871f7d commit 4e5c933

File tree

8 files changed

+39
-47
lines changed

8 files changed

+39
-47
lines changed

src/bench/chacha_poly_aead.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ static ChaCha20Poly1305AEAD aead(k1, 32, k2, 32);
2323

2424
static void CHACHA20_POLY1305_AEAD(benchmark::Bench& bench, size_t buffersize, bool include_decryption)
2525
{
26-
std::vector<unsigned char> in(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
27-
std::vector<unsigned char> out(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
26+
std::vector<unsigned char> in(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
27+
std::vector<unsigned char> out(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
2828
uint64_t seqnr_payload = 0;
2929
uint64_t seqnr_aad = 0;
3030
int aad_pos = 0;

src/bench/poly1305.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
#include <bench/bench.h>
77
#include <crypto/poly1305.h>
88

9+
#include <span.h>
10+
911
/* Number of bytes to process per iteration */
1012
static constexpr uint64_t BUFFER_SIZE_TINY = 64;
1113
static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
1214
static constexpr uint64_t BUFFER_SIZE_LARGE = 1024*1024;
1315

1416
static void POLY1305(benchmark::Bench& bench, size_t buffersize)
1517
{
16-
std::vector<unsigned char> tag(POLY1305_TAGLEN, 0);
17-
std::vector<unsigned char> key(POLY1305_KEYLEN, 0);
18-
std::vector<unsigned char> in(buffersize, 0);
18+
std::vector<std::byte> tag(Poly1305::TAGLEN, {});
19+
std::vector<std::byte> key(Poly1305::KEYLEN, {});
20+
std::vector<std::byte> in(buffersize, {});
1921
bench.batch(in.size()).unit("byte").run([&] {
20-
poly1305_auth(tag.data(), in.data(), in.size(), key.data());
22+
Poly1305{key}.Update(in).Finalize(tag);
2123
});
2224
}
2325

src/crypto/chacha_poly_aead.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int
5050
// check buffer boundaries
5151
if (
5252
// if we encrypt, make sure the source contains at least the expected AAD and the destination has at least space for the source + MAC
53-
(is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN || dest_len < src_len + POLY1305_TAGLEN)) ||
53+
(is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN || dest_len < src_len + Poly1305::TAGLEN)) ||
5454
// if we decrypt, make sure the source contains at least the expected AAD+MAC and the destination has at least space for the source - MAC
55-
(!is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN || dest_len < src_len - POLY1305_TAGLEN))) {
55+
(!is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN || dest_len < src_len - Poly1305::TAGLEN))) {
5656
return false;
5757
}
5858

59-
unsigned char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
59+
unsigned char expected_tag[Poly1305::TAGLEN], poly_key[Poly1305::KEYLEN];
6060
memset(poly_key, 0, sizeof(poly_key));
6161

6262
// block counter 0 for the poly1305 key
@@ -67,18 +67,20 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int
6767

6868
// if decrypting, verify the tag prior to decryption
6969
if (!is_encrypt) {
70-
const unsigned char* tag = src + src_len - POLY1305_TAGLEN;
71-
poly1305_auth(expected_tag, src, src_len - POLY1305_TAGLEN, poly_key);
70+
const unsigned char* tag = src + src_len - Poly1305::TAGLEN;
71+
Poly1305{MakeByteSpan(poly_key)}
72+
.Update(AsBytes(Span{src, src_len - Poly1305::TAGLEN}))
73+
.Finalize(MakeWritableByteSpan(expected_tag));
7274

7375
// constant time compare the calculated MAC with the provided MAC
74-
if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
76+
if (timingsafe_bcmp(expected_tag, tag, Poly1305::TAGLEN) != 0) {
7577
memory_cleanse(expected_tag, sizeof(expected_tag));
7678
memory_cleanse(poly_key, sizeof(poly_key));
7779
return false;
7880
}
7981
memory_cleanse(expected_tag, sizeof(expected_tag));
8082
// MAC has been successfully verified, make sure we don't convert it in decryption
81-
src_len -= POLY1305_TAGLEN;
83+
src_len -= Poly1305::TAGLEN;
8284
}
8385

8486
// calculate and cache the next 64byte keystream block if requested sequence number is not yet the cache
@@ -99,7 +101,9 @@ bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int
99101
// If encrypting, calculate and append tag
100102
if (is_encrypt) {
101103
// the poly1305 tag expands over the AAD (3 bytes length) & encrypted payload
102-
poly1305_auth(dest + src_len, dest, src_len, poly_key);
104+
Poly1305{MakeByteSpan(poly_key)}
105+
.Update(AsBytes(Span{dest, src_len}))
106+
.Finalize(AsWritableBytes(Span{dest + src_len, Poly1305::TAGLEN}));
103107
}
104108

105109
// cleanse no longer required MAC and polykey

src/crypto/poly1305.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,3 @@ void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes)
221221
}
222222

223223
} // namespace poly1305_donna
224-
225-
void poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, const unsigned char key[32]) {
226-
using namespace poly1305_donna;
227-
poly1305_context ctx;
228-
poly1305_init(&ctx, key);
229-
poly1305_update(&ctx, m, bytes);
230-
poly1305_finish(&ctx, mac);
231-
}

src/crypto/poly1305.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <cstdlib>
1212
#include <stdint.h>
1313

14-
#define POLY1305_KEYLEN 32
15-
#define POLY1305_TAGLEN 16
1614
#define POLY1305_BLOCK_SIZE 16
1715

1816
namespace poly1305_donna {
@@ -42,10 +40,10 @@ class Poly1305
4240

4341
public:
4442
/** Length of the output produced by Finalize(). */
45-
static constexpr unsigned TAGLEN = POLY1305_TAGLEN;
43+
static constexpr unsigned TAGLEN{16};
4644

4745
/** Length of the keys expected by the constructor. */
48-
static constexpr unsigned KEYLEN = POLY1305_KEYLEN;
46+
static constexpr unsigned KEYLEN{32};
4947

5048
/** Construct a Poly1305 object with a given 32-byte key. */
5149
Poly1305(Span<const std::byte> key) noexcept
@@ -69,7 +67,4 @@ class Poly1305
6967
}
7068
};
7169

72-
void poly1305_auth(unsigned char out[POLY1305_TAGLEN], const unsigned char *m, size_t inlen,
73-
const unsigned char key[POLY1305_KEYLEN]);
74-
7570
#endif // BITCOIN_CRYPTO_POLY1305_H

src/test/crypto_tests.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,25 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk
184184

185185
static void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag)
186186
{
187-
std::vector<unsigned char> key = ParseHex(hexkey);
188-
std::vector<unsigned char> m = ParseHex(hexmessage);
189-
std::vector<unsigned char> tag = ParseHex(hextag);
190-
std::vector<unsigned char> tagres;
191-
tagres.resize(POLY1305_TAGLEN);
192-
poly1305_auth(tagres.data(), m.data(), m.size(), key.data());
187+
auto key = ParseHex<std::byte>(hexkey);
188+
auto m = ParseHex<std::byte>(hexmessage);
189+
auto tag = ParseHex<std::byte>(hextag);
190+
std::vector<std::byte> tagres(Poly1305::TAGLEN);
191+
Poly1305{key}.Update(m).Finalize(tagres);
193192
BOOST_CHECK(tag == tagres);
194193

195194
// Test incremental interface
196195
for (int splits = 0; splits < 10; ++splits) {
197196
for (int iter = 0; iter < 10; ++iter) {
198-
auto data = MakeByteSpan(m);
199-
Poly1305 poly1305{MakeByteSpan(key)};
197+
auto data = Span{m};
198+
Poly1305 poly1305{key};
200199
for (int chunk = 0; chunk < splits; ++chunk) {
201200
size_t now = InsecureRandRange(data.size() + 1);
202201
poly1305.Update(data.first(now));
203202
data = data.subspan(now);
204203
}
205-
tagres.assign(POLY1305_TAGLEN, 0);
206-
poly1305.Update(data).Finalize(MakeWritableByteSpan(tagres));
204+
tagres.assign(Poly1305::TAGLEN, std::byte{});
205+
poly1305.Update(data).Finalize(tagres);
207206
BOOST_CHECK(tag == tagres);
208207
}
209208
}
@@ -858,7 +857,7 @@ static void TestChaCha20Poly1305AEAD(bool must_succeed, unsigned int expected_aa
858857
std::vector<unsigned char> expected_ciphertext_and_mac = ParseHex(hex_encrypted_message);
859858
std::vector<unsigned char> expected_ciphertext_and_mac_sequence999 = ParseHex(hex_encrypted_message_seq_999);
860859

861-
std::vector<unsigned char> ciphertext_buf(plaintext_buf.size() + POLY1305_TAGLEN, 0);
860+
std::vector<unsigned char> ciphertext_buf(plaintext_buf.size() + Poly1305::TAGLEN, 0);
862861
std::vector<unsigned char> plaintext_buf_new(plaintext_buf.size(), 0);
863862
std::vector<unsigned char> cmp_ctx_buffer(64);
864863
uint32_t out_len = 0;

src/test/fuzz/crypto_chacha20_poly1305_aead.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ FUZZ_TARGET(crypto_chacha20_poly1305_aead)
2626
uint64_t seqnr_aad = 0;
2727
int aad_pos = 0;
2828
size_t buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
29-
std::vector<uint8_t> in(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
30-
std::vector<uint8_t> out(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
29+
std::vector<uint8_t> in(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
30+
std::vector<uint8_t> out(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
3131
bool is_encrypt = fuzzed_data_provider.ConsumeBool();
3232
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
3333
CallOneOf(
3434
fuzzed_data_provider,
3535
[&] {
3636
buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(64, 4096);
37-
in = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
38-
out = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
37+
in = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
38+
out = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + Poly1305::TAGLEN, 0);
3939
},
4040
[&] {
4141
(void)aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(), out.size(), in.data(), buffer_size, is_encrypt);

src/test/fuzz/crypto_poly1305.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ FUZZ_TARGET(crypto_poly1305)
1414
{
1515
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
1616

17-
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, POLY1305_KEYLEN);
17+
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, Poly1305::KEYLEN);
1818
const std::vector<uint8_t> in = ConsumeRandomLengthByteVector(fuzzed_data_provider);
1919

20-
std::vector<uint8_t> tag_out(POLY1305_TAGLEN);
21-
poly1305_auth(tag_out.data(), in.data(), in.size(), key.data());
20+
std::vector<std::byte> tag_out(Poly1305::TAGLEN);
21+
Poly1305{MakeByteSpan(key)}.Update(MakeByteSpan(in)).Finalize(tag_out);
2222
}
2323

2424

0 commit comments

Comments
 (0)