/src/ndpi/fuzz/fuzz_gcrypt_aes.cpp
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <stdint.h> |
3 | | #include "fuzzer/FuzzedDataProvider.h" |
4 | | |
5 | | #define MBEDTLS_CHECK_RETURN_TYPICAL |
6 | | #include "gcrypt/aes.h" |
7 | | |
8 | | extern int force_no_aesni; |
9 | | |
10 | 145 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
11 | 145 | FuzzedDataProvider fuzzed_data(data, size); |
12 | 145 | mbedtls_aes_context *ctx; |
13 | 145 | int key_lens[] = { 128, 192, 256, 512 /* invalid */ }; |
14 | 145 | unsigned char *input, *output, *key; |
15 | 145 | int i, key_len, mode, rc; |
16 | | |
17 | | /* No real memory allocations involved */ |
18 | | |
19 | 145 | if(fuzzed_data.remaining_bytes() < 1 + 1 + 4 + 512 / 8 + 16) |
20 | 13 | return -1; |
21 | | |
22 | 132 | posix_memalign((void **)&input, 8, 16); |
23 | 132 | posix_memalign((void **)&output, 8, 16); |
24 | 132 | posix_memalign((void **)&key, 8, 512 / 8); |
25 | 132 | ctx = (mbedtls_aes_context *)malloc(sizeof(mbedtls_aes_context)); |
26 | | |
27 | 132 | force_no_aesni = 0; |
28 | 132 | if(fuzzed_data.ConsumeBool()) |
29 | 73 | force_no_aesni = 1; |
30 | | |
31 | 132 | mode = MBEDTLS_AES_ENCRYPT; |
32 | 132 | if(fuzzed_data.ConsumeBool()) |
33 | 58 | mode = MBEDTLS_AES_DECRYPT; |
34 | | |
35 | 132 | mbedtls_aes_init(ctx); |
36 | | |
37 | 132 | key_len = fuzzed_data.PickValueInArray(key_lens); |
38 | 132 | std::vector<unsigned char>k = fuzzed_data.ConsumeBytes<u_int8_t>(key_len / 8); |
39 | 132 | std::vector<u_int8_t>in = fuzzed_data.ConsumeBytes<uint8_t>(16); |
40 | | |
41 | 2.24k | for(i = 0; i < 16; i++) |
42 | 2.11k | input[i] = in[i]; |
43 | 4.49k | for(i = 0; i < key_len / 8; i++) |
44 | 4.36k | key[i] = k[i]; |
45 | | |
46 | 132 | if(mode == MBEDTLS_AES_ENCRYPT) |
47 | 74 | rc = mbedtls_aes_setkey_enc(ctx, key, key_len); |
48 | 58 | else |
49 | 58 | rc = mbedtls_aes_setkey_dec(ctx, key, key_len); |
50 | | |
51 | 132 | if(rc == 0) |
52 | 92 | mbedtls_aes_crypt_ecb(ctx, mode, input, output); |
53 | | |
54 | 132 | mbedtls_aes_free(ctx); |
55 | | |
56 | 132 | free(ctx); |
57 | 132 | free(key); |
58 | 132 | free(input); |
59 | 132 | free(output); |
60 | | |
61 | 132 | return 0; |
62 | 145 | } |