-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working RFC8439 implementation (saving)
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2021 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/rfc8439.h> | ||
#include <crypto/chacha20.h> | ||
#include <crypto/common.h> | ||
|
||
#include <cstring> | ||
|
||
inline size_t padded16_size(size_t len) { | ||
return (len % 16 == 0) ? len : (len / 16 + 1) * 16; | ||
} | ||
|
||
RFC8439Encrypted RFC8439Encrypt(Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12>&iv, Span<const std::byte> plaintext) { | ||
RFC8439Encrypted ret; | ||
|
||
assert(key.size() == RFC8439_KEYLEN); | ||
ChaCha20 c20{reinterpret_cast<const unsigned char*>(key.data()), key.size()}; | ||
|
||
c20.SetRFC8439IV(iv); | ||
c20.SeekRFC8439(0); | ||
|
||
std::array<std::byte, POLY1305_KEYLEN> polykey; | ||
c20.Keystream(reinterpret_cast<unsigned char*>(polykey.data()), POLY1305_KEYLEN); | ||
|
||
ret.ciphertext.resize(plaintext.size()); | ||
c20.SeekRFC8439(1); | ||
c20.Crypt(reinterpret_cast<const unsigned char*>(plaintext.data()), reinterpret_cast<unsigned char*>(ret.ciphertext.data()), plaintext.size()); | ||
|
||
std::vector<std::byte> bytes_to_authenticate; | ||
auto padded_aad_size = padded16_size(aad.size()); | ||
auto padded_ciphertext_size = padded16_size(ret.ciphertext.size()); | ||
bytes_to_authenticate.resize(padded_aad_size + padded_ciphertext_size + 8 + 8, std::byte{0x00}); | ||
std::copy(aad.begin(), aad.end(), bytes_to_authenticate.begin()); | ||
std::copy(ret.ciphertext.begin(), ret.ciphertext.end(), bytes_to_authenticate.begin() + padded_aad_size); | ||
WriteLE64(reinterpret_cast<unsigned char*>(bytes_to_authenticate.data()) + padded_aad_size + padded_ciphertext_size, aad.size()); | ||
WriteLE64(reinterpret_cast<unsigned char*>(bytes_to_authenticate.data()) + padded_aad_size + padded_ciphertext_size + 8, ret.ciphertext.size()); | ||
|
||
poly1305_auth(reinterpret_cast<unsigned char*>(ret.tag.data()), | ||
reinterpret_cast<const unsigned char*>(bytes_to_authenticate.data()), | ||
bytes_to_authenticate.size(), | ||
reinterpret_cast<const unsigned char*>(polykey.data())); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2021 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_RFC8439_H | ||
#define BITCOIN_CRYPTO_RFC8439_H | ||
|
||
#include <crypto/poly1305.h> | ||
#include <span.h> | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
constexpr static size_t RFC8439_KEYLEN = 32; | ||
|
||
struct RFC8439Encrypted { | ||
std::vector<std::byte> ciphertext; | ||
std::array<std::byte, POLY1305_TAGLEN> tag; | ||
}; | ||
|
||
RFC8439Encrypted RFC8439Encrypt(Span<const std::byte> aad, Span<const std::byte> key, const std::array<std::byte, 12>& iv, Span<const std::byte> plaintext); | ||
|
||
#endif // BITCOIN_CRYPTO_RFC8439_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters