From 86115a8a65c98d0665100b5ae85cc661d1404783 Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Tue, 3 Oct 2023 09:13:43 +0200 Subject: fuzz: extend fuzzing coverage --- fuzz/fuzz_gcrypt_aes.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 fuzz/fuzz_gcrypt_aes.cpp (limited to 'fuzz/fuzz_gcrypt_aes.cpp') diff --git a/fuzz/fuzz_gcrypt_aes.cpp b/fuzz/fuzz_gcrypt_aes.cpp new file mode 100644 index 000000000..1469ab0ce --- /dev/null +++ b/fuzz/fuzz_gcrypt_aes.cpp @@ -0,0 +1,62 @@ +#include +#include +#include "fuzzer/FuzzedDataProvider.h" + +#define MBEDTLS_CHECK_RETURN_TYPICAL +#include "../src/lib/third_party/include/gcrypt/aes.h" + +extern int force_no_aesni; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + FuzzedDataProvider fuzzed_data(data, size); + mbedtls_aes_context *ctx; + int key_lens[] = { 128, 192, 256, 512 /* invalid */ }; + unsigned char *input, *output, *key; + int i, key_len, mode, rc; + + /* No real memory allocations involved */ + + if(fuzzed_data.remaining_bytes() < 1 + 1 + 4 + 512 / 8 + 16) + return -1; + + posix_memalign((void **)&input, 8, 16); + posix_memalign((void **)&output, 8, 16); + posix_memalign((void **)&key, 8, 512 / 8); + ctx = (mbedtls_aes_context *)malloc(sizeof(mbedtls_aes_context)); + + force_no_aesni = 0; + if(fuzzed_data.ConsumeBool()) + force_no_aesni = 1; + + mode = MBEDTLS_AES_ENCRYPT; + if(fuzzed_data.ConsumeBool()) + mode = MBEDTLS_AES_DECRYPT; + + mbedtls_aes_init(ctx); + + key_len = fuzzed_data.PickValueInArray(key_lens); + std::vectork = fuzzed_data.ConsumeBytes(key_len / 8); + std::vectorin = fuzzed_data.ConsumeBytes(16); + + for(i = 0; i < 16; i++) + input[i] = in[i]; + for(i = 0; i < key_len / 8; i++) + key[i] = k[i]; + + if(mode == MBEDTLS_AES_ENCRYPT) + rc = mbedtls_aes_setkey_enc(ctx, key, key_len); + else + rc = mbedtls_aes_setkey_dec(ctx, key, key_len); + + if(rc == 0) + mbedtls_aes_crypt_ecb(ctx, mode, input, output); + + mbedtls_aes_free(ctx); + + free(ctx); + free(key); + free(input); + free(output); + + return 0; +} -- cgit v1.2.3