forked from bitcoin/bitcoin
-
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.
Merge bitcoin#19296: tests: Add fuzzing harness for AES{CBC,}256{Encr…
…ypt,Decrypt}, poly1305_auth, CHKDF_HMAC_SHA256_L32, ChaCha20 and ChaCha20Poly1305AEAD cca7c57 tests: Add fuzzing harness for ChaCha20Poly1305AEAD (practicalswift) 2fc4e59 tests: Add fuzzing harness for ChaCha20 (practicalswift) e9e8aac tests: Add fuzzing harness for CHKDF_HMAC_SHA256_L32 (practicalswift) ec86ca1 tests: Add fuzzing harness for poly1305_auth(...) (practicalswift) 4cee53b tests: Add fuzzing harness for AES256CBCEncrypt/AES256CBCDecrypt (practicalswift) 9352c32 tests: Add fuzzing harness for AES256Encrypt/AES256Decrypt (practicalswift) Pull request description: Add fuzzing harness for `AES{CBC,}256{Encrypt,Decrypt}`, `poly1305_auth`, `CHKDF_HMAC_SHA256_L32`, `ChaCha20` and `ChaCha20Poly1305AEAD`. See [`doc/fuzzing.md`](https://github.com/bitcoin/bitcoin/blob/master/doc/fuzzing.md) for information on how to fuzz Bitcoin Core. Don't forget to contribute any coverage increasing inputs you find to the [Bitcoin Core fuzzing corpus repo](https://github.com/bitcoin-core/qa-assets). Happy fuzzing :) ACKs for top commit: laanwj: ACK cca7c57 Tree-SHA512: cff9acefe370c12a3663aa55145371df835479c6ab8f6d81bbf84e0f81a9d6b0d94e45ec545f9dd5e1702744eaa7947a1f4ffed0171f446fc080369161afd740
- Loading branch information
Showing
7 changed files
with
275 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,30 @@ | ||
// Copyright (c) 2020 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/aes.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES256_KEYSIZE); | ||
|
||
AES256Encrypt encrypt{key.data()}; | ||
AES256Decrypt decrypt{key.data()}; | ||
|
||
while (fuzzed_data_provider.ConsumeBool()) { | ||
const std::vector<uint8_t> plaintext = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES_BLOCKSIZE); | ||
std::vector<uint8_t> ciphertext(AES_BLOCKSIZE); | ||
encrypt.Encrypt(ciphertext.data(), plaintext.data()); | ||
std::vector<uint8_t> decrypted_plaintext(AES_BLOCKSIZE); | ||
decrypt.Decrypt(decrypted_plaintext.data(), ciphertext.data()); | ||
assert(decrypted_plaintext == plaintext); | ||
} | ||
} |
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,34 @@ | ||
// Copyright (c) 2020 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/aes.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES256_KEYSIZE); | ||
const std::vector<uint8_t> iv = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES_BLOCKSIZE); | ||
const bool pad = fuzzed_data_provider.ConsumeBool(); | ||
|
||
AES256CBCEncrypt encrypt{key.data(), iv.data(), pad}; | ||
AES256CBCDecrypt decrypt{key.data(), iv.data(), pad}; | ||
|
||
while (fuzzed_data_provider.ConsumeBool()) { | ||
const std::vector<uint8_t> plaintext = ConsumeRandomLengthByteVector(fuzzed_data_provider); | ||
std::vector<uint8_t> ciphertext(plaintext.size() + AES_BLOCKSIZE); | ||
const int encrypt_ret = encrypt.Encrypt(plaintext.data(), plaintext.size(), ciphertext.data()); | ||
ciphertext.resize(encrypt_ret); | ||
std::vector<uint8_t> decrypted_plaintext(ciphertext.size()); | ||
const int decrypt_ret = decrypt.Decrypt(ciphertext.data(), ciphertext.size(), decrypted_plaintext.data()); | ||
decrypted_plaintext.resize(decrypt_ret); | ||
assert(decrypted_plaintext == plaintext || (!pad && plaintext.size() % AES_BLOCKSIZE != 0 && encrypt_ret == 0 && decrypt_ret == 0)); | ||
} | ||
} |
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,50 @@ | ||
// Copyright (c) 2020 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/chacha20.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
|
||
ChaCha20 chacha20; | ||
if (fuzzed_data_provider.ConsumeBool()) { | ||
const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(16, 32)); | ||
chacha20 = ChaCha20{key.data(), key.size()}; | ||
} | ||
while (fuzzed_data_provider.ConsumeBool()) { | ||
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 4)) { | ||
case 0: { | ||
const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(16, 32)); | ||
chacha20.SetKey(key.data(), key.size()); | ||
break; | ||
} | ||
case 1: { | ||
chacha20.SetIV(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); | ||
break; | ||
} | ||
case 2: { | ||
chacha20.Seek(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); | ||
break; | ||
} | ||
case 3: { | ||
std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); | ||
chacha20.Keystream(output.data(), output.size()); | ||
break; | ||
} | ||
case 4: { | ||
std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); | ||
const std::vector<uint8_t> input = ConsumeFixedLengthByteVector(fuzzed_data_provider, output.size()); | ||
chacha20.Crypt(input.data(), output.data(), input.size()); | ||
break; | ||
} | ||
} | ||
} | ||
} |
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,72 @@ | ||
// Copyright (c) 2020 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/chacha_poly_aead.h> | ||
#include <crypto/poly1305.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <limits> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
|
||
const std::vector<uint8_t> k1 = ConsumeFixedLengthByteVector(fuzzed_data_provider, CHACHA20_POLY1305_AEAD_KEY_LEN); | ||
const std::vector<uint8_t> k2 = ConsumeFixedLengthByteVector(fuzzed_data_provider, CHACHA20_POLY1305_AEAD_KEY_LEN); | ||
|
||
ChaCha20Poly1305AEAD aead(k1.data(), k1.size(), k2.data(), k2.size()); | ||
uint64_t seqnr_payload = 0; | ||
uint64_t seqnr_aad = 0; | ||
int aad_pos = 0; | ||
size_t buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096); | ||
std::vector<uint8_t> in(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0); | ||
std::vector<uint8_t> out(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0); | ||
bool is_encrypt = fuzzed_data_provider.ConsumeBool(); | ||
while (fuzzed_data_provider.ConsumeBool()) { | ||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 6)) { | ||
case 0: { | ||
buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(64, 4096); | ||
in = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0); | ||
out = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0); | ||
break; | ||
} | ||
case 1: { | ||
(void)aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(), out.size(), in.data(), buffer_size, is_encrypt); | ||
break; | ||
} | ||
case 2: { | ||
uint32_t len = 0; | ||
const bool ok = aead.GetLength(&len, seqnr_aad, aad_pos, in.data()); | ||
assert(ok); | ||
break; | ||
} | ||
case 3: { | ||
seqnr_payload += 1; | ||
aad_pos += CHACHA20_POLY1305_AEAD_AAD_LEN; | ||
if (aad_pos + CHACHA20_POLY1305_AEAD_AAD_LEN > CHACHA20_ROUND_OUTPUT) { | ||
aad_pos = 0; | ||
seqnr_aad += 1; | ||
} | ||
break; | ||
} | ||
case 4: { | ||
seqnr_payload = fuzzed_data_provider.ConsumeIntegral<int>(); | ||
break; | ||
} | ||
case 5: { | ||
seqnr_aad = fuzzed_data_provider.ConsumeIntegral<int>(); | ||
break; | ||
} | ||
case 6: { | ||
is_encrypt = fuzzed_data_provider.ConsumeBool(); | ||
break; | ||
} | ||
} | ||
} | ||
} |
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,25 @@ | ||
// Copyright (c) 2020 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/hkdf_sha256_32.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
|
||
const std::vector<uint8_t> initial_key_material = ConsumeRandomLengthByteVector(fuzzed_data_provider); | ||
|
||
CHKDF_HMAC_SHA256_L32 hkdf_hmac_sha256_l32(initial_key_material.data(), initial_key_material.size(), fuzzed_data_provider.ConsumeRandomLengthString(1024)); | ||
while (fuzzed_data_provider.ConsumeBool()) { | ||
std::vector<uint8_t> out(32); | ||
hkdf_hmac_sha256_l32.Expand32(fuzzed_data_provider.ConsumeRandomLengthString(128), out.data()); | ||
} | ||
} |
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,22 @@ | ||
// Copyright (c) 2020 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/poly1305.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
|
||
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, POLY1305_KEYLEN); | ||
const std::vector<uint8_t> in = ConsumeRandomLengthByteVector(fuzzed_data_provider); | ||
|
||
std::vector<uint8_t> tag_out(POLY1305_TAGLEN); | ||
poly1305_auth(tag_out.data(), in.data(), in.size(), key.data()); | ||
} |