Skip to content

Commit 40e6c5b

Browse files
committed
crypto: add Poly1305 class with std::byte Span interface
1 parent 50269b3 commit 40e6c5b

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/crypto/poly1305.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#ifndef BITCOIN_CRYPTO_POLY1305_H
66
#define BITCOIN_CRYPTO_POLY1305_H
77

8+
#include <span.h>
9+
10+
#include <cassert>
811
#include <cstdlib>
912
#include <stdint.h>
1013

@@ -32,6 +35,40 @@ void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept;
3235

3336
} // namespace poly1305_donna
3437

38+
/** C++ wrapper with std::byte Span interface around poly1305_donna code. */
39+
class Poly1305
40+
{
41+
poly1305_donna::poly1305_context m_ctx;
42+
43+
public:
44+
/** Length of the output produced by Finalize(). */
45+
static constexpr unsigned TAGLEN = POLY1305_TAGLEN;
46+
47+
/** Length of the keys expected by the constructor. */
48+
static constexpr unsigned KEYLEN = POLY1305_KEYLEN;
49+
50+
/** Construct a Poly1305 object with a given 32-byte key. */
51+
Poly1305(Span<const std::byte> key) noexcept
52+
{
53+
assert(key.size() == KEYLEN);
54+
poly1305_donna::poly1305_init(&m_ctx, UCharCast(key.data()));
55+
}
56+
57+
/** Process message bytes. */
58+
Poly1305& Update(Span<const std::byte> msg) noexcept
59+
{
60+
poly1305_donna::poly1305_update(&m_ctx, UCharCast(msg.data()), msg.size());
61+
return *this;
62+
}
63+
64+
/** Write authentication tag to 16-byte out. */
65+
void Finalize(Span<std::byte> out) noexcept
66+
{
67+
assert(out.size() == TAGLEN);
68+
poly1305_donna::poly1305_finish(&m_ctx, UCharCast(out.data()));
69+
}
70+
};
71+
3572
void poly1305_auth(unsigned char out[POLY1305_TAGLEN], const unsigned char *m, size_t inlen,
3673
const unsigned char key[POLY1305_KEYLEN]);
3774

src/test/crypto_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ static void TestPoly1305(const std::string &hexmessage, const std::string &hexke
191191
tagres.resize(POLY1305_TAGLEN);
192192
poly1305_auth(tagres.data(), m.data(), m.size(), key.data());
193193
BOOST_CHECK(tag == tagres);
194+
195+
// Test incremental interface
196+
for (int splits = 0; splits < 10; ++splits) {
197+
for (int iter = 0; iter < 10; ++iter) {
198+
auto data = MakeByteSpan(m);
199+
Poly1305 poly1305{MakeByteSpan(key)};
200+
for (int chunk = 0; chunk < splits; ++chunk) {
201+
size_t now = InsecureRandRange(data.size() + 1);
202+
poly1305.Update(data.first(now));
203+
data = data.subspan(now);
204+
}
205+
tagres.assign(POLY1305_TAGLEN, 0);
206+
poly1305.Update(data).Finalize(MakeWritableByteSpan(tagres));
207+
BOOST_CHECK(tag == tagres);
208+
}
209+
}
194210
}
195211

196212
static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) {

src/test/fuzz/crypto_poly1305.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,34 @@ FUZZ_TARGET(crypto_poly1305)
2020
std::vector<uint8_t> tag_out(POLY1305_TAGLEN);
2121
poly1305_auth(tag_out.data(), in.data(), in.size(), key.data());
2222
}
23+
24+
25+
FUZZ_TARGET(crypto_poly1305_split)
26+
{
27+
FuzzedDataProvider provider{buffer.data(), buffer.size()};
28+
29+
// Read key and instantiate two Poly1305 objects with it.
30+
auto key = provider.ConsumeBytes<std::byte>(Poly1305::KEYLEN);
31+
key.resize(Poly1305::KEYLEN);
32+
Poly1305 poly_full{key}, poly_split{key};
33+
34+
// Vector that holds all bytes processed so far.
35+
std::vector<std::byte> total_input;
36+
37+
// Process input in pieces.
38+
LIMITED_WHILE(provider.remaining_bytes(), 100) {
39+
auto in = provider.ConsumeRandomLengthString();
40+
poly_split.Update(MakeByteSpan(in));
41+
// Update total_input to match what was processed.
42+
total_input.insert(total_input.end(), MakeByteSpan(in).begin(), MakeByteSpan(in).end());
43+
}
44+
45+
// Process entire input at once.
46+
poly_full.Update(total_input);
47+
48+
// Verify both agree.
49+
std::array<std::byte, Poly1305::TAGLEN> tag_split, tag_full;
50+
poly_split.Finalize(tag_split);
51+
poly_full.Finalize(tag_full);
52+
assert(tag_full == tag_split);
53+
}

0 commit comments

Comments
 (0)