/src/mbedtls/programs/fuzz/common.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
8 | | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
9 | | mbedtls_time_t dummy_constant_time(mbedtls_time_t *time) |
10 | 13.8k | { |
11 | 13.8k | (void) time; |
12 | 13.8k | return 0x5af2a056; |
13 | 13.8k | } |
14 | | #endif |
15 | | |
16 | | void dummy_init(void) |
17 | 6 | { |
18 | 6 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
19 | 6 | mbedtls_platform_set_time(dummy_constant_time); |
20 | | #else |
21 | | fprintf(stderr, "Warning: fuzzing without constant time\n"); |
22 | | #endif |
23 | 6 | } |
24 | | |
25 | | int dummy_send(void *ctx, const unsigned char *buf, size_t len) |
26 | 25.7k | { |
27 | | //silence warning about unused parameter |
28 | 25.7k | (void) ctx; |
29 | 25.7k | (void) buf; |
30 | | |
31 | | //pretends we wrote everything ok |
32 | 25.7k | if (len > INT_MAX) { |
33 | 0 | return -1; |
34 | 0 | } |
35 | 25.7k | return (int) len; |
36 | 25.7k | } |
37 | | |
38 | | int fuzz_recv(void *ctx, unsigned char *buf, size_t len) |
39 | 33.8k | { |
40 | | //reads from the buffer from fuzzer |
41 | 33.8k | fuzzBufferOffset_t *biomemfuzz = (fuzzBufferOffset_t *) ctx; |
42 | | |
43 | 33.8k | if (biomemfuzz->Offset == biomemfuzz->Size) { |
44 | | //EOF |
45 | 1.66k | return 0; |
46 | 1.66k | } |
47 | 32.1k | if (len > INT_MAX) { |
48 | 0 | return -1; |
49 | 0 | } |
50 | 32.1k | if (len + biomemfuzz->Offset > biomemfuzz->Size) { |
51 | | //do not overflow |
52 | 7.04k | len = biomemfuzz->Size - biomemfuzz->Offset; |
53 | 7.04k | } |
54 | 32.1k | memcpy(buf, biomemfuzz->Data + biomemfuzz->Offset, len); |
55 | 32.1k | biomemfuzz->Offset += len; |
56 | 32.1k | return (int) len; |
57 | 32.1k | } |
58 | | |
59 | | int dummy_random(void *p_rng, unsigned char *output, size_t output_len) |
60 | 71.8k | { |
61 | 71.8k | int ret; |
62 | 71.8k | size_t i; |
63 | | |
64 | 71.8k | #if defined(MBEDTLS_CTR_DRBG_C) |
65 | | //mbedtls_ctr_drbg_random requires a valid mbedtls_ctr_drbg_context in p_rng |
66 | 71.8k | if (p_rng != NULL) { |
67 | | //use mbedtls_ctr_drbg_random to find bugs in it |
68 | 71.8k | ret = mbedtls_ctr_drbg_random(p_rng, output, output_len); |
69 | 71.8k | } else { |
70 | | //fall through to pseudo-random |
71 | 0 | ret = 0; |
72 | 0 | } |
73 | | #else |
74 | | (void) p_rng; |
75 | | ret = 0; |
76 | | #endif |
77 | 741k | for (i = 0; i < output_len; i++) { |
78 | | //replace result with pseudo random |
79 | 669k | output[i] = (unsigned char) rand(); |
80 | 669k | } |
81 | 71.8k | return ret; |
82 | 71.8k | } |
83 | | |
84 | | int dummy_entropy(void *data, unsigned char *output, size_t len) |
85 | 12.7k | { |
86 | 12.7k | size_t i; |
87 | 12.7k | (void) data; |
88 | | |
89 | | //use mbedtls_entropy_func to find bugs in it |
90 | | //test performance impact of entropy |
91 | | //ret = mbedtls_entropy_func(data, output, len); |
92 | 626k | for (i = 0; i < len; i++) { |
93 | | //replace result with pseudo random |
94 | 613k | output[i] = (unsigned char) rand(); |
95 | 613k | } |
96 | 12.7k | return 0; |
97 | 12.7k | } |
98 | | |
99 | | int fuzz_recv_timeout(void *ctx, unsigned char *buf, size_t len, |
100 | | uint32_t timeout) |
101 | 9.16k | { |
102 | 9.16k | (void) timeout; |
103 | | |
104 | 9.16k | return fuzz_recv(ctx, buf, len); |
105 | 9.16k | } |