blob: a9d3d3b86f51a40545062330e4db4fc723c1ad82 [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
29 return( len );
30}
31
32int 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
50int 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
64int 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
79int 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}