-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuzz_test.cpp
181 lines (141 loc) · 4.59 KB
/
fuzz_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stddef.h>
#include <stdint.h>
#include <cifuzz/cifuzz.h>
#include <fuzzer/FuzzedDataProvider.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "crypto_1.h"
#include "crypto_2.h"
#include "gps_1.h"
#include "key_management_1.h"
#include "time_1.h"
#ifdef __cplusplus
}
#endif
void SetFDP(FuzzedDataProvider *fuzzed_data_provider);
FuzzedDataProvider *GetFDP();
void ConsumeDataAndFillRestWithZeros(void *destination, size_t num_bytes);
FUZZ_TEST_SETUP() {}
FUZZ_TEST(const uint8_t *data, size_t size) {
// Ensure a minimum data length
if (size < 100)
return;
// Setup FuzzedDataProvider and initialize the mocklib
FuzzedDataProvider fdp(data, size);
SetFDP(&fdp);
int number_of_functions = GetFDP()->ConsumeIntegralInRange<int>(1, 100);
for (int i = 0; i < number_of_functions; i++) {
int function_id = GetFDP()->ConsumeIntegralInRange<int>(0, 15);
switch (function_id) {
case 0: {
crypto_get_state();
break;
}
case 1: {
get_destination_position();
break;
}
case 2: {
crypto_key key = {};
ConsumeDataAndFillRestWithZeros(key.key, 64);
crypto_verify_key(key);
break;
}
case 3: {
current_time();
break;
}
case 4: {
crypto_nonce nonce_tmp = {};
ConsumeDataAndFillRestWithZeros(nonce_tmp.nonce, 64);
nonce_tmp.time_of_creation = GetFDP()->ConsumeIntegral<int>();
crypto_nonce *nonce = &nonce_tmp;
crypto_verify_nonce(nonce);
break;
}
case 5: {
std::vector<uint8_t> message_vec = GetFDP()->ConsumeBytes<uint8_t>(
sizeof(uint8_t) * GetFDP()->ConsumeIntegral<uint16_t>());
const uint8_t *message = (const uint8_t *)message_vec.data();
int len = message_vec.size();
crypto_hmac hmac_tmp = {};
ConsumeDataAndFillRestWithZeros(hmac_tmp.hmac, 64);
crypto_hmac *hmac = &hmac_tmp;
crypto_verify_hmac(message, len, hmac);
break;
}
case 6: {
crypto_nonce nonce = {};
ConsumeDataAndFillRestWithZeros(nonce.nonce, 64);
nonce.time_of_creation = GetFDP()->ConsumeIntegral<int>();
crypto_set_nonce(nonce);
break;
}
case 7: {
std::vector<uint8_t> message_vec = GetFDP()->ConsumeBytes<uint8_t>(
sizeof(uint8_t) * GetFDP()->ConsumeIntegral<uint16_t>());
const uint8_t *message = (const uint8_t *)message_vec.data();
int len = message_vec.size();
crypto_hmac hmac_tmp = {};
ConsumeDataAndFillRestWithZeros(hmac_tmp.hmac, 64);
crypto_hmac *hmac = &hmac_tmp;
crypto_calculate_hmac(message, len, hmac);
break;
}
case 8: {
GPS_position position = {};
position.longitude_degree = GetFDP()->ConsumeIntegral<uint8_t>();
position.longitude_minute = GetFDP()->ConsumeIntegral<uint8_t>();
position.longitude_second = GetFDP()->ConsumeIntegral<uint8_t>();
position.latitude_degree = GetFDP()->ConsumeIntegral<uint8_t>();
position.latitude_minute = GetFDP()->ConsumeIntegral<uint8_t>();
position.latitude_second = GetFDP()->ConsumeIntegral<uint8_t>();
set_destination_postition(position);
break;
}
case 9: {
crypto_key key = {};
ConsumeDataAndFillRestWithZeros(key.key, 64);
crypto_set_key(key);
break;
}
case 10: {
GPS_position position_tmp = {};
position_tmp.longitude_degree = GetFDP()->ConsumeIntegral<uint8_t>();
position_tmp.longitude_minute = GetFDP()->ConsumeIntegral<uint8_t>();
position_tmp.longitude_second = GetFDP()->ConsumeIntegral<uint8_t>();
position_tmp.latitude_degree = GetFDP()->ConsumeIntegral<uint8_t>();
position_tmp.latitude_minute = GetFDP()->ConsumeIntegral<uint8_t>();
position_tmp.latitude_second = GetFDP()->ConsumeIntegral<uint8_t>();
GPS_position *position = &position_tmp;
get_current_position(position);
break;
}
case 11: {
init_crypto_module();
break;
}
case 12: {
std::vector<uint8_t> key_vec = GetFDP()->ConsumeBytes<uint8_t>(
sizeof(uint8_t) * GetFDP()->ConsumeIntegral<uint16_t>());
uint8_t *key = (uint8_t *)key_vec.data();
uint8_t length = key_vec.size();
key_management_create_key(key, length);
break;
}
case 13: {
std::vector<uint8_t> nonce_vec = GetFDP()->ConsumeBytes<uint8_t>(
sizeof(uint8_t) * GetFDP()->ConsumeIntegral<uint16_t>());
uint8_t *nonce = (uint8_t *)nonce_vec.data();
uint8_t length = nonce_vec.size();
key_management_create_nonce(nonce, length);
break;
}
case 14: {
crypto_init();
break;
}
}
}
}