UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
chacha20_poly1305.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_CHACHA20_POLY1305_HPP
2#define SECP256K1_CHACHA20_POLY1305_HPP
3#pragma once
4
5// ============================================================================
6// ChaCha20-Poly1305 AEAD (RFC 8439)
7// ============================================================================
8// Used by BIP-324 for authenticated encryption of P2P transport packets.
9// Zero external dependencies — built entirely on integer arithmetic.
10//
11// ChaCha20: 256-bit key, 96-bit nonce, 32-bit counter stream cipher
12// Poly1305: One-time authenticator producing 128-bit tags
13// AEAD: Combines both per RFC 8439 Section 2.8
14// ============================================================================
15
16#include <array>
17#include <cstdint>
18#include <cstddef>
19
20namespace secp256k1 {
21
22// -- ChaCha20 -----------------------------------------------------------------
23
24// Encrypt/decrypt in-place (XOR with keystream). counter starts at given value.
25void chacha20_crypt(const std::uint8_t key[32],
26 const std::uint8_t nonce[12],
27 std::uint32_t counter,
28 std::uint8_t* data, std::size_t len) noexcept;
29
30// Generate raw keystream block (64 bytes) for a specific counter value.
31void chacha20_block(const std::uint8_t key[32],
32 const std::uint8_t nonce[12],
33 std::uint32_t counter,
34 std::uint8_t out[64]) noexcept;
35
36// -- Poly1305 -----------------------------------------------------------------
37
38// One-shot Poly1305 MAC: tag = Poly1305(key, data)
39// key must be 32 bytes (r || s) as produced by ChaCha20 block 0.
40std::array<std::uint8_t, 16> poly1305_mac(
41 const std::uint8_t key[32],
42 const std::uint8_t* data, std::size_t len) noexcept;
43
44// -- ChaCha20-Poly1305 AEAD (RFC 8439) ----------------------------------------
45
46// Encrypt plaintext and produce authentication tag.
47// out must have space for plaintext_len bytes.
48// tag receives the 16-byte Poly1305 authentication tag.
50 const std::uint8_t key[32],
51 const std::uint8_t nonce[12],
52 const std::uint8_t* aad, std::size_t aad_len,
53 const std::uint8_t* plaintext, std::size_t plaintext_len,
54 std::uint8_t* out,
55 std::uint8_t tag[16]) noexcept;
56
57// Decrypt ciphertext and verify authentication tag.
58// Returns true on success (tag valid), false on failure (out is zeroed).
60 const std::uint8_t key[32],
61 const std::uint8_t nonce[12],
62 const std::uint8_t* aad, std::size_t aad_len,
63 const std::uint8_t* ciphertext, std::size_t ciphertext_len,
64 const std::uint8_t tag[16],
65 std::uint8_t* out) noexcept;
66
67} // namespace secp256k1
68
69#endif // SECP256K1_CHACHA20_POLY1305_HPP
std::array< std::uint8_t, 16 > poly1305_mac(const std::uint8_t key[32], const std::uint8_t *data, std::size_t len) noexcept
bool aead_chacha20_poly1305_decrypt(const std::uint8_t key[32], const std::uint8_t nonce[12], const std::uint8_t *aad, std::size_t aad_len, const std::uint8_t *ciphertext, std::size_t ciphertext_len, const std::uint8_t tag[16], std::uint8_t *out) noexcept
void aead_chacha20_poly1305_encrypt(const std::uint8_t key[32], const std::uint8_t nonce[12], const std::uint8_t *aad, std::size_t aad_len, const std::uint8_t *plaintext, std::size_t plaintext_len, std::uint8_t *out, std::uint8_t tag[16]) noexcept
void chacha20_block(const std::uint8_t key[32], const std::uint8_t nonce[12], std::uint32_t counter, std::uint8_t out[64]) noexcept
void chacha20_crypt(const std::uint8_t key[32], const std::uint8_t nonce[12], std::uint32_t counter, std::uint8_t *data, std::size_t len) noexcept