Bitcoin Core 28.99.0
P2P Digital Currency
poly1305.h
Go to the documentation of this file.
1// Copyright (c) 2019-2022 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#ifndef BITCOIN_CRYPTO_POLY1305_H
6#define BITCOIN_CRYPTO_POLY1305_H
7
8#include <span.h>
9
10#include <cassert>
11#include <cstdlib>
12#include <stdint.h>
13
14#define POLY1305_BLOCK_SIZE 16
15
16namespace poly1305_donna {
17
18// Based on the public domain implementation by Andrew Moon
19// poly1305-donna-32.h from https://github.com/floodyberry/poly1305-donna
20
21typedef struct {
22 uint32_t r[5];
23 uint32_t h[5];
24 uint32_t pad[4];
25 size_t leftover;
26 unsigned char buffer[POLY1305_BLOCK_SIZE];
27 unsigned char final;
29
30void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept;
31void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept;
32void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept;
33
34} // namespace poly1305_donna
35
38{
40
41public:
43 static constexpr unsigned TAGLEN{16};
44
46 static constexpr unsigned KEYLEN{32};
47
50 {
51 assert(key.size() == KEYLEN);
53 }
54
57 {
59 return *this;
60 }
61
64 {
65 assert(out.size() == TAGLEN);
67 }
68};
69
70#endif // BITCOIN_CRYPTO_POLY1305_H
C++ wrapper with std::byte Span interface around poly1305_donna code.
Definition: poly1305.h:38
poly1305_donna::poly1305_context m_ctx
Definition: poly1305.h:39
static constexpr unsigned KEYLEN
Length of the keys expected by the constructor.
Definition: poly1305.h:46
Poly1305 & Update(Span< const std::byte > msg) noexcept
Process message bytes.
Definition: poly1305.h:56
void Finalize(Span< std::byte > out) noexcept
Write authentication tag to 16-byte out.
Definition: poly1305.h:63
static constexpr unsigned TAGLEN
Length of the output produced by Finalize().
Definition: poly1305.h:43
Poly1305(Span< const std::byte > key) noexcept
Construct a Poly1305 object with a given 32-byte key.
Definition: poly1305.h:49
void poly1305_update(poly1305_context *st, const unsigned char *m, size_t bytes) noexcept
Definition: poly1305.cpp:186
void poly1305_init(poly1305_context *st, const unsigned char key[32]) noexcept
Definition: poly1305.cpp:15
void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept
Definition: poly1305.cpp:100
#define POLY1305_BLOCK_SIZE
Definition: poly1305.h:14
unsigned char * UCharCast(char *c)
Definition: span.h:281
assert(!tx.IsCoinBase())