1 #include "common.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "mbedtls/ctr_drbg.h"
6 
dummy_constant_time(mbedtls_time_t * time)7 mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
8 {
9     (void) time;
10     return 0x5af2a056;
11 }
12 
dummy_init()13 void dummy_init()
14 {
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 }
21 
dummy_send(void * ctx,const unsigned char * buf,size_t len)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     if( len > INT_MAX ) {
30         return( -1 );
31     }
32     return( (int) len );
33 }
34 
fuzz_recv(void * ctx,unsigned char * buf,size_t len)35 int fuzz_recv( void *ctx, unsigned char *buf, size_t len )
36 {
37     //reads from the buffer from fuzzer
38     fuzzBufferOffset_t * biomemfuzz = (fuzzBufferOffset_t *) ctx;
39 
40     if(biomemfuzz->Offset == biomemfuzz->Size) {
41         //EOF
42         return( 0 );
43     }
44     if( len > INT_MAX ) {
45         return( -1 );
46     }
47     if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
48         //do not overflow
49         len = biomemfuzz->Size - biomemfuzz->Offset;
50     }
51     memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
52     biomemfuzz->Offset += len;
53     return( (int) len );
54 }
55 
dummy_random(void * p_rng,unsigned char * output,size_t output_len)56 int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
57 {
58     int ret;
59     size_t i;
60 
61 #if defined(MBEDTLS_CTR_DRBG_C)
62     //use mbedtls_ctr_drbg_random to find bugs in it
63     ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
64 #else
65     (void) p_rng;
66     ret = 0;
67 #endif
68     for (i=0; i<output_len; i++) {
69         //replace result with pseudo random
70         output[i] = (unsigned char) rand();
71     }
72     return( ret );
73 }
74 
dummy_entropy(void * data,unsigned char * output,size_t len)75 int dummy_entropy( void *data, unsigned char *output, size_t len )
76 {
77     size_t i;
78     (void) data;
79 
80     //use mbedtls_entropy_func to find bugs in it
81     //test performance impact of entropy
82     //ret = mbedtls_entropy_func(data, output, len);
83     for (i=0; i<len; i++) {
84         //replace result with pseudo random
85         output[i] = (unsigned char) rand();
86     }
87     return( 0 );
88 }
89 
fuzz_recv_timeout(void * ctx,unsigned char * buf,size_t len,uint32_t timeout)90 int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
91                       uint32_t timeout )
92 {
93     (void) timeout;
94 
95     return fuzz_recv(ctx, buf, len);
96 }
97