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