blob: 41fa858a41dba64e5e2993e8bb4627db4e5747ec [file] [log] [blame]
Felix Conway858b8292025-03-25 10:06:53 +00001#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
2
Philippe Antoine08633822019-06-04 14:03:06 +02003#include "common.h"
Dave Rodgman351c71b2021-12-06 17:50:53 +00004#include <limits.h>
Philippe Antoine499c7352019-06-04 14:14:33 +02005#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include "mbedtls/ctr_drbg.h"
Philippe Antoine08633822019-06-04 14:03:06 +02009
Andrzej Kurek06a00af2022-03-02 10:56:22 -050010#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010011mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Philippe Antoine499c7352019-06-04 14:14:33 +020012{
Philippe Antoine08633822019-06-04 14:03:06 +020013 (void) time;
14 return 0x5af2a056;
15}
Daniel Axtensf0710242020-05-28 11:43:41 +100016#endif
Philippe Antoine08633822019-06-04 14:03:06 +020017
Gowtham Suresh Kumar186731b2023-07-26 15:47:45 +010018void dummy_init(void)
Philippe Antoine499c7352019-06-04 14:14:33 +020019{
David Horstmannca534592021-11-29 18:57:10 +000020#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_platform_set_time(dummy_constant_time);
Philippe Antoine08633822019-06-04 14:03:06 +020022#else
23 fprintf(stderr, "Warning: fuzzing without constant time\n");
24#endif
25}
Philippe Antoine499c7352019-06-04 14:14:33 +020026
Gilles Peskine449bd832023-01-11 14:50:10 +010027int dummy_send(void *ctx, const unsigned char *buf, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020028{
29 //silence warning about unused parameter
30 (void) ctx;
31 (void) buf;
32
33 //pretends we wrote everything ok
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (len > INT_MAX) {
35 return -1;
Philippe Antoine3e408d52019-07-10 01:09:50 +020036 }
Gilles Peskine449bd832023-01-11 14:50:10 +010037 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020038}
39
Gilles Peskine449bd832023-01-11 14:50:10 +010040int fuzz_recv(void *ctx, unsigned char *buf, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020041{
42 //reads from the buffer from fuzzer
Gilles Peskine449bd832023-01-11 14:50:10 +010043 fuzzBufferOffset_t *biomemfuzz = (fuzzBufferOffset_t *) ctx;
Philippe Antoine499c7352019-06-04 14:14:33 +020044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 if (biomemfuzz->Offset == biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020046 //EOF
Gilles Peskine449bd832023-01-11 14:50:10 +010047 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020048 }
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (len > INT_MAX) {
50 return -1;
Philippe Antoine23219452019-07-10 08:26:04 +020051 }
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (len + biomemfuzz->Offset > biomemfuzz->Size) {
Philippe Antoine499c7352019-06-04 14:14:33 +020053 //do not overflow
54 len = biomemfuzz->Size - biomemfuzz->Offset;
55 }
56 memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len);
57 biomemfuzz->Offset += len;
Gilles Peskine449bd832023-01-11 14:50:10 +010058 return (int) len;
Philippe Antoine499c7352019-06-04 14:14:33 +020059}
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061int dummy_random(void *p_rng, unsigned char *output, size_t output_len)
Philippe Antoine499c7352019-06-04 14:14:33 +020062{
63 int ret;
64 size_t i;
65
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020066#if defined(MBEDTLS_CTR_DRBG_C)
Paul Elliotta1dc3e52022-02-14 18:26:21 +000067 //mbedtls_ctr_drbg_random requires a valid mbedtls_ctr_drbg_context in p_rng
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if (p_rng != NULL) {
Paul Elliotta1dc3e52022-02-14 18:26:21 +000069 //use mbedtls_ctr_drbg_random to find bugs in it
70 ret = mbedtls_ctr_drbg_random(p_rng, output, output_len);
Paul Elliott5d7e61f2022-02-15 16:05:17 +000071 } else {
72 //fall through to pseudo-random
73 ret = 0;
Paul Elliotta1dc3e52022-02-14 18:26:21 +000074 }
Manuel Pégourié-Gonnarda89040c2020-05-20 10:35:01 +020075#else
76 (void) p_rng;
77 ret = 0;
78#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010079 for (i = 0; i < output_len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020080 //replace result with pseudo random
81 output[i] = (unsigned char) rand();
82 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return ret;
Philippe Antoine499c7352019-06-04 14:14:33 +020084}
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086int dummy_entropy(void *data, unsigned char *output, size_t len)
Philippe Antoine499c7352019-06-04 14:14:33 +020087{
88 size_t i;
89 (void) data;
90
91 //use mbedtls_entropy_func to find bugs in it
92 //test performance impact of entropy
93 //ret = mbedtls_entropy_func(data, output, len);
Gilles Peskine449bd832023-01-11 14:50:10 +010094 for (i = 0; i < len; i++) {
Philippe Antoine499c7352019-06-04 14:14:33 +020095 //replace result with pseudo random
96 output[i] = (unsigned char) rand();
97 }
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Philippe Antoine499c7352019-06-04 14:14:33 +020099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int fuzz_recv_timeout(void *ctx, unsigned char *buf, size_t len,
102 uint32_t timeout)
Philippe Antoine499c7352019-06-04 14:14:33 +0200103{
104 (void) timeout;
105
106 return fuzz_recv(ctx, buf, len);
107}