mirror of https://github.com/bitcoin/bitcoin
This adds an implementation of the ChaCha20Poly1305 AEAD exactly matching the version specified in RFC8439 section 2.8, including tests and official test vectors.pull/28008/head
parent
9fd085a1a4
commit
9ff0768bdc
@ -0,0 +1,101 @@
|
||||
// Copyright (c) 2023 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <crypto/chacha20poly1305.h>
|
||||
|
||||
#include <crypto/common.h>
|
||||
#include <crypto/chacha20.h>
|
||||
#include <crypto/poly1305.h>
|
||||
#include <span.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
|
||||
AEADChaCha20Poly1305::AEADChaCha20Poly1305(Span<const std::byte> key) noexcept : m_chacha20(UCharCast(key.data()))
|
||||
{
|
||||
assert(key.size() == KEYLEN);
|
||||
}
|
||||
|
||||
void AEADChaCha20Poly1305::SetKey(Span<const std::byte> key) noexcept
|
||||
{
|
||||
assert(key.size() == KEYLEN);
|
||||
m_chacha20.SetKey32(UCharCast(key.data()));
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
#ifndef HAVE_TIMINGSAFE_BCMP
|
||||
#define HAVE_TIMINGSAFE_BCMP
|
||||
|
||||
int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n) noexcept
|
||||
{
|
||||
const unsigned char *p1 = b1, *p2 = b2;
|
||||
int ret = 0;
|
||||
for (; n > 0; n--)
|
||||
ret |= *p1++ ^ *p2++;
|
||||
return (ret != 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/** Compute poly1305 tag. chacha20 must be set to the right nonce, block 0. Will be at block 1 after. */
|
||||
void ComputeTag(ChaCha20& chacha20, Span<const std::byte> aad, Span<const std::byte> cipher, Span<std::byte> tag) noexcept
|
||||
{
|
||||
static const std::byte PADDING[16] = {{}};
|
||||
|
||||
// Get block of keystream (use a full 64 byte buffer to avoid the need for chacha20's own buffering).
|
||||
std::byte first_block[64];
|
||||
chacha20.Keystream(UCharCast(first_block), sizeof(first_block));
|
||||
|
||||
// Use the first 32 bytes of the first keystream block as poly1305 key.
|
||||
Poly1305 poly1305{Span{first_block}.first(Poly1305::KEYLEN)};
|
||||
|
||||
// Compute tag:
|
||||
// - Process the padded AAD with Poly1305.
|
||||
const unsigned aad_padding_length = (16 - (aad.size() % 16)) % 16;
|
||||
poly1305.Update(aad).Update(Span{PADDING}.first(aad_padding_length));
|
||||
// - Process the padded ciphertext with Poly1305.
|
||||
const unsigned cipher_padding_length = (16 - (cipher.size() % 16)) % 16;
|
||||
poly1305.Update(cipher).Update(Span{PADDING}.first(cipher_padding_length));
|
||||
// - Process the AAD and plaintext length with Poly1305.
|
||||
std::byte length_desc[Poly1305::TAGLEN];
|
||||
WriteLE64(UCharCast(length_desc), aad.size());
|
||||
WriteLE64(UCharCast(length_desc + 8), cipher.size());
|
||||
poly1305.Update(length_desc);
|
||||
|
||||
// Output tag.
|
||||
poly1305.Finalize(tag);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void AEADChaCha20Poly1305::Encrypt(Span<const std::byte> plain, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> cipher) noexcept
|
||||
{
|
||||
assert(cipher.size() == plain.size() + EXPANSION);
|
||||
|
||||
// Encrypt using ChaCha20 (starting at block 1).
|
||||
m_chacha20.Seek64(nonce, 1);
|
||||
m_chacha20.Crypt(UCharCast(plain.data()), UCharCast(cipher.data()), plain.size());
|
||||
|
||||
// Seek to block 0, and compute tag using key drawn from there.
|
||||
m_chacha20.Seek64(nonce, 0);
|
||||
ComputeTag(m_chacha20, aad, cipher.first(plain.size()), cipher.last(EXPANSION));
|
||||
}
|
||||
|
||||
bool AEADChaCha20Poly1305::Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> plain) noexcept
|
||||
{
|
||||
assert(cipher.size() == plain.size() + EXPANSION);
|
||||
|
||||
// Verify tag (using key drawn from block 0).
|
||||
m_chacha20.Seek64(nonce, 0);
|
||||
std::byte expected_tag[EXPANSION];
|
||||
ComputeTag(m_chacha20, aad, cipher.first(plain.size()), expected_tag);
|
||||
if (timingsafe_bcmp(UCharCast(expected_tag), UCharCast(cipher.data() + plain.size()), EXPANSION)) return false;
|
||||
|
||||
// Decrypt (starting at block 1).
|
||||
m_chacha20.Crypt(UCharCast(cipher.data()), UCharCast(plain.data()), plain.size());
|
||||
return true;
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
// Copyright (c) 2023 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_CRYPTO_CHACHA20POLY1305_H
|
||||
#define BITCOIN_CRYPTO_CHACHA20POLY1305_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <crypto/chacha20.h>
|
||||
#include <crypto/poly1305.h>
|
||||
#include <span.h>
|
||||
|
||||
/** The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2.8. */
|
||||
class AEADChaCha20Poly1305
|
||||
{
|
||||
/** Internal stream cipher. */
|
||||
ChaCha20 m_chacha20;
|
||||
|
||||
public:
|
||||
/** Expected size of key argument in constructor. */
|
||||
static constexpr unsigned KEYLEN = 32;
|
||||
|
||||
/** Expansion when encrypting. */
|
||||
static constexpr unsigned EXPANSION = Poly1305::TAGLEN;
|
||||
|
||||
/** Initialize an AEAD instance with a specified 32-byte key. */
|
||||
AEADChaCha20Poly1305(Span<const std::byte> key) noexcept;
|
||||
|
||||
/** Switch to another 32-byte key. */
|
||||
void SetKey(Span<const std::byte> key) noexcept;
|
||||
|
||||
/** 96-bit nonce type. */
|
||||
using Nonce96 = ChaCha20::Nonce96;
|
||||
|
||||
/** Encrypt a message with a specified 96-bit nonce and aad.
|
||||
*
|
||||
* Requires cipher.size() = plain.size() + EXPANSION.
|
||||
*/
|
||||
void Encrypt(Span<const std::byte> plain, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> cipher) noexcept;
|
||||
|
||||
/** Decrypt a message with a specified 96-bit nonce and aad. Returns true if valid.
|
||||
*
|
||||
* Requires cipher.size() = plain.size() + EXPANSION.
|
||||
*/
|
||||
bool Decrypt(Span<const std::byte> cipher, Span<const std::byte> aad, Nonce96 nonce, Span<std::byte> plain) noexcept;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H
|
Loading…
Reference in new issue