• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "common.h"
2 #include <limits.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include "mbedtls/ctr_drbg.h"
7 
dummy_constant_time(mbedtls_time_t * time)8 mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
9 {
10     (void) time;
11     return 0x5af2a056;
12 }
13 
dummy_init()14 void dummy_init()
15 {
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 }
22 
dummy_send(void * ctx,const unsigned char * buf,size_t len)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
30     if( len > INT_MAX ) {
31         return( -1 );
32     }
33     return( (int) len );
34 }
35 
fuzz_recv(void * ctx,unsigned char * buf,size_t len)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 
41     if(biomemfuzz->Offset == biomemfuzz->Size) {
42         //EOF
43         return( 0 );
44     }
45     if( len > INT_MAX ) {
46         return( -1 );
47     }
48     if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
49         //do not overflow
50         len = biomemfuzz->Size - biomemfuzz->Offset;
51     }
52     memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
53     biomemfuzz->Offset += len;
54     return( (int) len );
55 }
56 
dummy_random(void * p_rng,unsigned char * output,size_t output_len)57 int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
58 {
59     int ret;
60     size_t i;
61 
62 #if defined(MBEDTLS_CTR_DRBG_C)
63     //use mbedtls_ctr_drbg_random to find bugs in it
64     ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
65 #else
66     (void) p_rng;
67     ret = 0;
68 #endif
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 
dummy_entropy(void * data,unsigned char * output,size_t len)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 
fuzz_recv_timeout(void * ctx,unsigned char * buf,size_t len,uint32_t timeout)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 }
98