|
| 1 | +// Copyright (c) 2021 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 | +#include <crypto/chacha20.h> |
| 6 | +#include <crypto/common.h> |
| 7 | +#include <crypto/rfc8439.h> |
| 8 | + |
| 9 | +#include <cstring> |
| 10 | + |
| 11 | +#ifndef RFC8439_TIMINGSAFE_BCMP |
| 12 | +#define RFC8439_TIMINGSAFE_BCMP |
| 13 | + |
| 14 | +int rfc8439_timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n) |
| 15 | +{ |
| 16 | + const unsigned char *p1 = b1, *p2 = b2; |
| 17 | + int ret = 0; |
| 18 | + |
| 19 | + for (; n > 0; n--) |
| 20 | + ret |= *p1++ ^ *p2++; |
| 21 | + return (ret != 0); |
| 22 | +} |
| 23 | + |
| 24 | +#endif // RFC8439_TIMINGSAFE_BCMP |
| 25 | + |
| 26 | +inline size_t padded16_size(size_t len) |
| 27 | +{ |
| 28 | + return (len % 16 == 0) ? len : (len / 16 + 1) * 16; |
| 29 | +} |
| 30 | + |
| 31 | +std::array<std::byte, POLY1305_TAGLEN> ComputeRFC8439Tag(const std::array<std::byte, POLY1305_KEYLEN>& polykey, |
| 32 | + Span<const std::byte> aad, Span<const std::byte> ciphertext) |
| 33 | +{ |
| 34 | + std::vector<std::byte> bytes_to_authenticate; |
| 35 | + auto padded_aad_size = padded16_size(aad.size()); |
| 36 | + auto padded_ciphertext_size = padded16_size(ciphertext.size()); |
| 37 | + bytes_to_authenticate.resize(padded_aad_size + padded_ciphertext_size + 16, std::byte{0x00}); |
| 38 | + std::copy(aad.begin(), aad.end(), bytes_to_authenticate.begin()); |
| 39 | + std::copy(ciphertext.begin(), ciphertext.end(), bytes_to_authenticate.begin() + padded_aad_size); |
| 40 | + WriteLE64(reinterpret_cast<unsigned char*>(bytes_to_authenticate.data()) + padded_aad_size + padded_ciphertext_size, aad.size()); |
| 41 | + WriteLE64(reinterpret_cast<unsigned char*>(bytes_to_authenticate.data()) + padded_aad_size + padded_ciphertext_size + 8, ciphertext.size()); |
| 42 | + |
| 43 | + std::array<std::byte, POLY1305_TAGLEN> ret_tag; |
| 44 | + poly1305_auth(reinterpret_cast<unsigned char*>(ret_tag.data()), |
| 45 | + reinterpret_cast<const unsigned char*>(bytes_to_authenticate.data()), |
| 46 | + bytes_to_authenticate.size(), |
| 47 | + reinterpret_cast<const unsigned char*>(polykey.data())); |
| 48 | + |
| 49 | + return ret_tag; |
| 50 | +} |
| 51 | + |
| 52 | +std::array<std::byte, POLY1305_KEYLEN> GetPoly1305Key(ChaCha20& c20) |
| 53 | +{ |
| 54 | + c20.SeekRFC8439(0); |
| 55 | + std::array<std::byte, POLY1305_KEYLEN> polykey; |
| 56 | + c20.Keystream(reinterpret_cast<unsigned char*>(polykey.data()), POLY1305_KEYLEN); |
| 57 | + return polykey; |
| 58 | +} |
| 59 | + |
| 60 | +void RFC8439Crypt(ChaCha20& c20, Span<const std::byte> in_bytes, Span<std::byte> out_bytes) |
| 61 | +{ |
| 62 | + assert(in_bytes.size() == out_bytes.size()); |
| 63 | + c20.SeekRFC8439(1); |
| 64 | + c20.Crypt(reinterpret_cast<const unsigned char*>(in_bytes.data()), reinterpret_cast<unsigned char*>(out_bytes.data()), in_bytes.size()); |
| 65 | +} |
| 66 | + |
| 67 | +RFC8439Encrypted RFC8439Encrypt(Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12>& nonce, Span<const std::byte> plaintext) |
| 68 | +{ |
| 69 | + assert(key.size() == RFC8439_KEYLEN); |
| 70 | + RFC8439Encrypted ret; |
| 71 | + |
| 72 | + ChaCha20 c20{reinterpret_cast<const unsigned char*>(key.data()), key.size()}; |
| 73 | + c20.SetRFC8439Nonce(nonce); |
| 74 | + |
| 75 | + std::array<std::byte, POLY1305_KEYLEN> polykey{GetPoly1305Key(c20)}; |
| 76 | + |
| 77 | + ret.ciphertext.resize(plaintext.size()); |
| 78 | + RFC8439Crypt(c20, plaintext, ret.ciphertext); |
| 79 | + ret.tag = ComputeRFC8439Tag(polykey, aad, ret.ciphertext); |
| 80 | + return ret; |
| 81 | +} |
| 82 | + |
| 83 | +RFC8439Decrypted RFC8439Decrypt(Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12>& nonce, const RFC8439Encrypted& encrypted) |
| 84 | +{ |
| 85 | + assert(key.size() == RFC8439_KEYLEN); |
| 86 | + assert(encrypted.tag.size() == POLY1305_TAGLEN); |
| 87 | + |
| 88 | + RFC8439Decrypted ret; |
| 89 | + |
| 90 | + ChaCha20 c20{reinterpret_cast<const unsigned char*>(key.data()), key.size()}; |
| 91 | + c20.SetRFC8439Nonce(nonce); |
| 92 | + |
| 93 | + std::array<std::byte, POLY1305_KEYLEN> polykey{GetPoly1305Key(c20)}; |
| 94 | + auto tag = ComputeRFC8439Tag(polykey, aad, encrypted.ciphertext); |
| 95 | + |
| 96 | + if (rfc8439_timingsafe_bcmp(reinterpret_cast<const unsigned char*>(encrypted.tag.data()), |
| 97 | + reinterpret_cast<const unsigned char*>(tag.data()), encrypted.tag.size()) != 0) { |
| 98 | + ret.success = false; |
| 99 | + return ret; |
| 100 | + } |
| 101 | + |
| 102 | + ret.success = true; |
| 103 | + ret.plaintext.resize(encrypted.ciphertext.size()); |
| 104 | + RFC8439Crypt(c20, encrypted.ciphertext, ret.plaintext); |
| 105 | + return ret; |
| 106 | +} |
0 commit comments