Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 1 | #include "common.h" |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdlib.h> |
| 5 | #include "mbedtls/ctr_drbg.h" |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 6 | |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 7 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) |
| 8 | { |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 9 | (void) time; |
| 10 | return 0x5af2a056; |
| 11 | } |
| 12 | |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 13 | void dummy_init() |
| 14 | { |
Philippe Antoine | 0863382 | 2019-06-04 14:03:06 +0200 | [diff] [blame] | 15 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 16 | mbedtls_platform_set_time( dummy_constant_time ); |
| 17 | #else |
| 18 | fprintf(stderr, "Warning: fuzzing without constant time\n"); |
| 19 | #endif |
| 20 | } |
Philippe Antoine | 499c735 | 2019-06-04 14:14:33 +0200 | [diff] [blame] | 21 | |
| 22 | int dummy_send( void *ctx, const unsigned char *buf, size_t len ) |
| 23 | { |
| 24 | //silence warning about unused parameter |
| 25 | (void) ctx; |
| 26 | (void) buf; |
| 27 | |
| 28 | //pretends we wrote everything ok |
| 29 | return( len ); |
| 30 | } |
| 31 | |
| 32 | int fuzz_recv( void *ctx, unsigned char *buf, size_t len ) |
| 33 | { |
| 34 | //reads from the buffer from fuzzer |
| 35 | fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx; |
| 36 | |
| 37 | if (biomemfuzz->Offset == biomemfuzz->Size) { |
| 38 | //EOF |
| 39 | return (0); |
| 40 | } |
| 41 | if (len + biomemfuzz->Offset > biomemfuzz->Size) { |
| 42 | //do not overflow |
| 43 | len = biomemfuzz->Size - biomemfuzz->Offset; |
| 44 | } |
| 45 | memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len); |
| 46 | biomemfuzz->Offset += len; |
| 47 | return( len ); |
| 48 | } |
| 49 | |
| 50 | int dummy_random( void *p_rng, unsigned char *output, size_t output_len ) |
| 51 | { |
| 52 | int ret; |
| 53 | size_t i; |
| 54 | |
| 55 | //use mbedtls_ctr_drbg_random to find bugs in it |
| 56 | ret = mbedtls_ctr_drbg_random(p_rng, output, output_len); |
| 57 | for (i=0; i<output_len; i++) { |
| 58 | //replace result with pseudo random |
| 59 | output[i] = (unsigned char) rand(); |
| 60 | } |
| 61 | return( ret ); |
| 62 | } |
| 63 | |
| 64 | int dummy_entropy( void *data, unsigned char *output, size_t len ) |
| 65 | { |
| 66 | size_t i; |
| 67 | (void) data; |
| 68 | |
| 69 | //use mbedtls_entropy_func to find bugs in it |
| 70 | //test performance impact of entropy |
| 71 | //ret = mbedtls_entropy_func(data, output, len); |
| 72 | for (i=0; i<len; i++) { |
| 73 | //replace result with pseudo random |
| 74 | output[i] = (unsigned char) rand(); |
| 75 | } |
| 76 | return( 0 ); |
| 77 | } |
| 78 | |
| 79 | int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len, |
| 80 | uint32_t timeout ) |
| 81 | { |
| 82 | (void) timeout; |
| 83 | |
| 84 | return fuzz_recv(ctx, buf, len); |
| 85 | } |