Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
crypto_aes256cbc.cpp
Go to the documentation of this file.
1
// Copyright (c) 2020-present 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/aes.h
>
6
#include <
test/fuzz/FuzzedDataProvider.h
>
7
#include <
test/fuzz/fuzz.h
>
8
#include <
test/fuzz/util.h
>
9
10
#include <cassert>
11
#include <cstdint>
12
#include <vector>
13
14
FUZZ_TARGET
(crypto_aes256cbc)
15
{
16
FuzzedDataProvider
fuzzed_data_provider{buffer.data(), buffer.size()};
17
const
std::vector<uint8_t> key =
ConsumeFixedLengthByteVector
(fuzzed_data_provider,
AES256_KEYSIZE
);
18
const
std::vector<uint8_t> iv =
ConsumeFixedLengthByteVector
(fuzzed_data_provider,
AES_BLOCKSIZE
);
19
const
bool
pad = fuzzed_data_provider.
ConsumeBool
();
20
21
AES256CBCEncrypt
encrypt{key.data(), iv.data(), pad};
22
AES256CBCDecrypt
decrypt{key.data(), iv.data(), pad};
23
24
LIMITED_WHILE
(fuzzed_data_provider.
ConsumeBool
(), 10000) {
25
const
std::vector<uint8_t> plaintext =
ConsumeRandomLengthByteVector
(fuzzed_data_provider);
26
std::vector<uint8_t> ciphertext(plaintext.size() +
AES_BLOCKSIZE
);
27
const
int
encrypt_ret = encrypt.
Encrypt
(plaintext.data(), plaintext.size(), ciphertext.data());
28
ciphertext.resize(encrypt_ret);
29
std::vector<uint8_t> decrypted_plaintext(ciphertext.size());
30
const
int
decrypt_ret = decrypt.
Decrypt
(ciphertext.data(), ciphertext.size(), decrypted_plaintext.data());
31
decrypted_plaintext.resize(decrypt_ret);
32
assert
(decrypted_plaintext == plaintext || (!pad && plaintext.size() %
AES_BLOCKSIZE
!= 0 && encrypt_ret == 0 && decrypt_ret == 0));
33
}
34
}
FuzzedDataProvider.h
aes.h
AES256_KEYSIZE
static const int AES256_KEYSIZE
Definition
aes.h:15
AES_BLOCKSIZE
static const int AES_BLOCKSIZE
Definition
aes.h:14
AES256CBCDecrypt
Definition
aes.h:55
AES256CBCDecrypt::Decrypt
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
Definition
aes.cpp:144
AES256CBCEncrypt
Definition
aes.h:42
AES256CBCEncrypt::Encrypt
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
Definition
aes.cpp:127
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition
fuzz.h:22
util.h
ConsumeFixedLengthByteVector
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition
util.h:252
ConsumeRandomLengthByteVector
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition
util.h:57
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0