blob: 169b4804fe893aa1702df5b3a0d223f2d95f6194 [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 Antoine3e408d52019-07-10 01:09:50 +020029 if (len > INT_MAX) {
30 return -1;
31 }
32 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
40 if (biomemfuzz->Offset == biomemfuzz->Size) {
41 //EOF
42 return (0);
43 }
44 if (len + biomemfuzz->Offset > biomemfuzz->Size) {
45 //do not overflow
46 len = biomemfuzz->Size - biomemfuzz->Offset;
47 }
48 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
49 biomemfuzz->Offset += len;
50 return( len );
51}
52
53int dummy_random( void *p_rng, unsigned char *output, size_t output_len )
54{
55 int ret;
56 size_t i;
57
58 //use mbedtls_ctr_drbg_random to find bugs in it
59 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
60 for (i=0; i<output_len; i++) {
61 //replace result with pseudo random
62 output[i] = (unsigned char) rand();
63 }
64 return( ret );
65}
66
67int dummy_entropy( void *data, unsigned char *output, size_t len )
68{
69 size_t i;
70 (void) data;
71
72 //use mbedtls_entropy_func to find bugs in it
73 //test performance impact of entropy
74 //ret = mbedtls_entropy_func(data, output, len);
75 for (i=0; i<len; i++) {
76 //replace result with pseudo random
77 output[i] = (unsigned char) rand();
78 }
79 return( 0 );
80}
81
82int fuzz_recv_timeout( void *ctx, unsigned char *buf, size_t len,
83 uint32_t timeout )
84{
85 (void) timeout;
86
87 return fuzz_recv(ctx, buf, len);
88}