blob: 5e6c84c268a0525b81c259e022563b14f2174d6b [file] [log] [blame]
Philippe Antoine08633822019-06-04 14:03:06 +02001#include "common.h"
Philippe Antoine499c7352019-06-04 14:14:33 +02002#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include "mbedtls/ctr_drbg.h"
Philippe Antoine08633822019-06-04 14:03:06 +02006
Philippe Antoine499c7352019-06-04 14:14:33 +02007mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
8{
Philippe Antoine08633822019-06-04 14:03:06 +02009 (void) time;
10 return 0x5af2a056;
11}
12
Philippe Antoine499c7352019-06-04 14:14:33 +020013void dummy_init()
14{
Philippe Antoine08633822019-06-04 14:03:06 +020015#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 Antoine499c7352019-06-04 14:14:33 +020021
22int 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
Philippe Antoinea82fdd42019-07-10 13:53:40 +020029 if( len > INT_MAX ) {
30 return( -1 );
Philippe Antoine3e408d52019-07-10 01:09:50 +020031 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020032 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020033}
34
35int 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
Philippe Antoinea82fdd42019-07-10 13:53:40 +020040 if(biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020041 //EOF
Philippe Antoinea82fdd42019-07-10 13:53:40 +020042 return( 0 );
Philippe Antoine499c7352019-06-04 14:14:33 +020043 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020044 if( len > INT_MAX ) {
45 return( -1 );
Philippe Antoine23219452019-07-10 08:26:04 +020046 }
Philippe Antoinea82fdd42019-07-10 13:53:40 +020047 if( len + biomemfuzz->Offset > biomemfuzz->Size ) {
Philippe Antoine499c7352019-06-04 14:14:33 +020048 //do not overflow
49 len = biomemfuzz->Size - biomemfuzz->Offset;
50 }
51 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
52 biomemfuzz->Offset += len;
Philippe Antoinea82fdd42019-07-10 13:53:40 +020053 return( (int) len );
Philippe Antoine499c7352019-06-04 14:14:33 +020054}
55
56int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
57{
58 int ret;
59 size_t i;
60
61 //use mbedtls_ctr_drbg_random to find bugs in it
62 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
63 for (i=0; i<output_len; i++) {
64 //replace result with pseudo random
65 output[i] = (unsigned char) rand();
66 }
67 return( ret );
68}
69
70int dummy_entropy( void *data, unsigned char *output, size_t len )
71{
72 size_t i;
73 (void) data;
74
75 //use mbedtls_entropy_func to find bugs in it
76 //test performance impact of entropy
77 //ret = mbedtls_entropy_func(data, output, len);
78 for (i=0; i<len; i++) {
79 //replace result with pseudo random
80 output[i] = (unsigned char) rand();
81 }
82 return( 0 );
83}
84
85int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
86 uint32_t timeout )
87{
88 (void) timeout;
89
90 return fuzz_recv(ctx, buf, len);
91}