diff options
Diffstat (limited to 'fuzz/wrap.c')
-rw-r--r-- | fuzz/wrap.c | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/fuzz/wrap.c b/fuzz/wrap.c new file mode 100644 index 0000000..8ff7ee7 --- /dev/null +++ b/fuzz/wrap.c | |||
@@ -0,0 +1,419 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2019 Yubico AB. All rights reserved. | ||
3 | * Use of this source code is governed by a BSD-style | ||
4 | * license that can be found in the LICENSE file. | ||
5 | */ | ||
6 | |||
7 | #include <openssl/bn.h> | ||
8 | #include <openssl/evp.h> | ||
9 | #include <openssl/sha.h> | ||
10 | |||
11 | #include <cbor.h> | ||
12 | #include <fido.h> | ||
13 | |||
14 | #include <stdbool.h> | ||
15 | #include <stdint.h> | ||
16 | #include <stdio.h> | ||
17 | #include <stdlib.h> | ||
18 | |||
19 | #include "mutator_aux.h" | ||
20 | |||
21 | /* | ||
22 | * Build wrappers around functions of interest, and have them fail | ||
23 | * in a pseudo-random manner. | ||
24 | */ | ||
25 | |||
26 | #define WRAP(type, name, args, retval, param, prob) \ | ||
27 | extern type __wrap_##name args; \ | ||
28 | extern type __real_##name args; \ | ||
29 | type __wrap_##name args { \ | ||
30 | if (uniform_random(400) < (prob)) { \ | ||
31 | return (retval); \ | ||
32 | } \ | ||
33 | \ | ||
34 | return (__real_##name param); \ | ||
35 | } | ||
36 | |||
37 | WRAP(void *, | ||
38 | malloc, | ||
39 | (size_t size), | ||
40 | NULL, | ||
41 | (size), | ||
42 | 1 | ||
43 | ) | ||
44 | |||
45 | WRAP(void *, | ||
46 | calloc, | ||
47 | (size_t nmemb, size_t size), | ||
48 | NULL, | ||
49 | (nmemb, size), | ||
50 | 1 | ||
51 | ) | ||
52 | |||
53 | WRAP(char *, | ||
54 | strdup, | ||
55 | (const char *s), | ||
56 | NULL, | ||
57 | (s), | ||
58 | 1 | ||
59 | ) | ||
60 | |||
61 | WRAP(EVP_CIPHER_CTX *, | ||
62 | EVP_CIPHER_CTX_new, | ||
63 | (void), | ||
64 | NULL, | ||
65 | (), | ||
66 | 1 | ||
67 | ) | ||
68 | |||
69 | WRAP(int, EVP_EncryptInit_ex, | ||
70 | (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, | ||
71 | const unsigned char *key, const unsigned char *iv), | ||
72 | 0, | ||
73 | (ctx, type, impl, key, iv), | ||
74 | 1 | ||
75 | ) | ||
76 | |||
77 | WRAP(int, | ||
78 | EVP_CIPHER_CTX_set_padding, | ||
79 | (EVP_CIPHER_CTX *x, int padding), | ||
80 | 0, | ||
81 | (x, padding), | ||
82 | 1 | ||
83 | ) | ||
84 | |||
85 | WRAP(int, | ||
86 | EVP_EncryptUpdate, | ||
87 | (EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | ||
88 | const unsigned char *in, int inl), | ||
89 | 0, | ||
90 | (ctx, out, outl, in, inl), | ||
91 | 1 | ||
92 | ) | ||
93 | |||
94 | WRAP(int, | ||
95 | EVP_DecryptInit_ex, | ||
96 | (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, | ||
97 | const unsigned char *key, const unsigned char *iv), | ||
98 | 0, | ||
99 | (ctx, type, impl, key, iv), | ||
100 | 1 | ||
101 | ) | ||
102 | |||
103 | WRAP(int, | ||
104 | EVP_DecryptUpdate, | ||
105 | (EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | ||
106 | const unsigned char *in, int inl), | ||
107 | 0, | ||
108 | (ctx, out, outl, in, inl), | ||
109 | 1 | ||
110 | ) | ||
111 | |||
112 | WRAP(int, | ||
113 | SHA256_Init, | ||
114 | (SHA256_CTX *c), | ||
115 | 0, | ||
116 | (c), | ||
117 | 1 | ||
118 | ) | ||
119 | |||
120 | WRAP(int, | ||
121 | SHA256_Update, | ||
122 | (SHA256_CTX *c, const void *data, size_t len), | ||
123 | 0, | ||
124 | (c, data, len), | ||
125 | 1 | ||
126 | ) | ||
127 | |||
128 | WRAP(int, | ||
129 | SHA256_Final, | ||
130 | (unsigned char *md, SHA256_CTX *c), | ||
131 | 0, | ||
132 | (md, c), | ||
133 | 1 | ||
134 | ) | ||
135 | |||
136 | WRAP(RSA *, | ||
137 | EVP_PKEY_get0_RSA, | ||
138 | (EVP_PKEY *pkey), | ||
139 | NULL, | ||
140 | (pkey), | ||
141 | 1 | ||
142 | ) | ||
143 | |||
144 | WRAP(EVP_MD_CTX *, | ||
145 | EVP_MD_CTX_new, | ||
146 | (void), | ||
147 | NULL, | ||
148 | (), | ||
149 | 1 | ||
150 | ) | ||
151 | |||
152 | WRAP(int, | ||
153 | EVP_DigestVerifyInit, | ||
154 | (EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, | ||
155 | EVP_PKEY *pkey), | ||
156 | 0, | ||
157 | (ctx, pctx, type, e, pkey), | ||
158 | 1 | ||
159 | ) | ||
160 | |||
161 | WRAP(BIGNUM *, | ||
162 | BN_bin2bn, | ||
163 | (const unsigned char *s, int len, BIGNUM *ret), | ||
164 | NULL, | ||
165 | (s, len, ret), | ||
166 | 1 | ||
167 | ) | ||
168 | |||
169 | WRAP(BIGNUM *, | ||
170 | BN_CTX_get, | ||
171 | (BN_CTX *ctx), | ||
172 | NULL, | ||
173 | (ctx), | ||
174 | 1 | ||
175 | ) | ||
176 | |||
177 | WRAP(BN_CTX *, | ||
178 | BN_CTX_new, | ||
179 | (void), | ||
180 | NULL, | ||
181 | (), | ||
182 | 1 | ||
183 | ) | ||
184 | |||
185 | WRAP(BIGNUM *, | ||
186 | BN_new, | ||
187 | (void), | ||
188 | NULL, | ||
189 | (), | ||
190 | 1 | ||
191 | ) | ||
192 | |||
193 | WRAP(int, | ||
194 | RSA_set0_key, | ||
195 | (RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d), | ||
196 | 0, | ||
197 | (r, n, e, d), | ||
198 | 1 | ||
199 | ) | ||
200 | |||
201 | WRAP(EC_KEY *, | ||
202 | EC_KEY_new_by_curve_name, | ||
203 | (int nid), | ||
204 | NULL, | ||
205 | (nid), | ||
206 | 1 | ||
207 | ) | ||
208 | |||
209 | WRAP(const EC_GROUP *, | ||
210 | EC_KEY_get0_group, | ||
211 | (const EC_KEY *key), | ||
212 | NULL, | ||
213 | (key), | ||
214 | 1 | ||
215 | ) | ||
216 | |||
217 | WRAP(EC_POINT *, | ||
218 | EC_POINT_new, | ||
219 | (const EC_GROUP *group), | ||
220 | NULL, | ||
221 | (group), | ||
222 | 1 | ||
223 | ) | ||
224 | |||
225 | WRAP(EVP_PKEY *, | ||
226 | EVP_PKEY_new, | ||
227 | (void), | ||
228 | NULL, | ||
229 | (), | ||
230 | 1 | ||
231 | ) | ||
232 | |||
233 | WRAP(int, | ||
234 | EVP_PKEY_assign, | ||
235 | (EVP_PKEY *pkey, int type, void *key), | ||
236 | 0, | ||
237 | (pkey, type, key), | ||
238 | 1 | ||
239 | ) | ||
240 | |||
241 | WRAP(EVP_PKEY *, | ||
242 | EVP_PKEY_new_raw_public_key, | ||
243 | (int type, ENGINE *e, const unsigned char *key, size_t keylen), | ||
244 | NULL, | ||
245 | (type, e, key, keylen), | ||
246 | 1 | ||
247 | ) | ||
248 | |||
249 | WRAP(EVP_PKEY_CTX *, | ||
250 | EVP_PKEY_CTX_new, | ||
251 | (EVP_PKEY *pkey, ENGINE *e), | ||
252 | NULL, | ||
253 | (pkey, e), | ||
254 | 1 | ||
255 | ) | ||
256 | |||
257 | WRAP(int, | ||
258 | EVP_PKEY_derive_init, | ||
259 | (EVP_PKEY_CTX *ctx), | ||
260 | 0, | ||
261 | (ctx), | ||
262 | 1 | ||
263 | ) | ||
264 | |||
265 | WRAP(int, | ||
266 | EVP_PKEY_derive_set_peer, | ||
267 | (EVP_PKEY_CTX *ctx, EVP_PKEY *peer), | ||
268 | 0, | ||
269 | (ctx, peer), | ||
270 | 1 | ||
271 | ) | ||
272 | |||
273 | WRAP(const EVP_MD *, | ||
274 | EVP_sha256, | ||
275 | (void), | ||
276 | NULL, | ||
277 | (), | ||
278 | 1 | ||
279 | ) | ||
280 | |||
281 | WRAP(unsigned char *, | ||
282 | HMAC, | ||
283 | (const EVP_MD *evp_md, const void *key, int key_len, | ||
284 | const unsigned char *d, int n, unsigned char *md, | ||
285 | unsigned int *md_len), | ||
286 | NULL, | ||
287 | (evp_md, key, key_len, d, n, md, md_len), | ||
288 | 1 | ||
289 | ) | ||
290 | |||
291 | WRAP(HMAC_CTX *, | ||
292 | HMAC_CTX_new, | ||
293 | (void), | ||
294 | NULL, | ||
295 | (), | ||
296 | 1 | ||
297 | ) | ||
298 | |||
299 | WRAP(int, | ||
300 | HMAC_Init_ex, | ||
301 | (HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, | ||
302 | ENGINE *impl), | ||
303 | 0, | ||
304 | (ctx, key, key_len, md, impl), | ||
305 | 1 | ||
306 | ) | ||
307 | |||
308 | WRAP(int, | ||
309 | HMAC_Update, | ||
310 | (HMAC_CTX *ctx, const unsigned char *data, int len), | ||
311 | 0, | ||
312 | (ctx, data, len), | ||
313 | 1 | ||
314 | ) | ||
315 | |||
316 | WRAP(int, | ||
317 | HMAC_Final, | ||
318 | (HMAC_CTX *ctx, unsigned char *md, unsigned int *len), | ||
319 | 0, | ||
320 | (ctx, md, len), | ||
321 | 1 | ||
322 | ) | ||
323 | |||
324 | WRAP(unsigned char *, | ||
325 | SHA256, | ||
326 | (const unsigned char *d, size_t n, unsigned char *md), | ||
327 | NULL, | ||
328 | (d, n, md), | ||
329 | 1 | ||
330 | ) | ||
331 | |||
332 | WRAP(cbor_item_t *, | ||
333 | cbor_build_string, | ||
334 | (const char *val), | ||
335 | NULL, | ||
336 | (val), | ||
337 | 1 | ||
338 | ) | ||
339 | |||
340 | WRAP(cbor_item_t *, | ||
341 | cbor_build_bytestring, | ||
342 | (cbor_data handle, size_t length), | ||
343 | NULL, | ||
344 | (handle, length), | ||
345 | 1 | ||
346 | ) | ||
347 | |||
348 | WRAP(cbor_item_t *, | ||
349 | cbor_load, | ||
350 | (cbor_data source, size_t source_size, struct cbor_load_result *result), | ||
351 | NULL, | ||
352 | (source, source_size, result), | ||
353 | 1 | ||
354 | ) | ||
355 | |||
356 | WRAP(cbor_item_t *, | ||
357 | cbor_build_uint8, | ||
358 | (uint8_t value), | ||
359 | NULL, | ||
360 | (value), | ||
361 | 1 | ||
362 | ) | ||
363 | |||
364 | WRAP(struct cbor_pair *, | ||
365 | cbor_map_handle, | ||
366 | (const cbor_item_t *item), | ||
367 | NULL, | ||
368 | (item), | ||
369 | 1 | ||
370 | ) | ||
371 | |||
372 | WRAP(cbor_item_t **, | ||
373 | cbor_array_handle, | ||
374 | (const cbor_item_t *item), | ||
375 | NULL, | ||
376 | (item), | ||
377 | 1 | ||
378 | ) | ||
379 | |||
380 | WRAP(bool, | ||
381 | cbor_map_add, | ||
382 | (cbor_item_t *item, struct cbor_pair pair), | ||
383 | false, | ||
384 | (item, pair), | ||
385 | 1 | ||
386 | ) | ||
387 | |||
388 | WRAP(cbor_item_t *, | ||
389 | cbor_new_definite_map, | ||
390 | (size_t size), | ||
391 | NULL, | ||
392 | (size), | ||
393 | 1 | ||
394 | ) | ||
395 | |||
396 | WRAP(size_t, | ||
397 | cbor_serialize_alloc, | ||
398 | (const cbor_item_t *item, cbor_mutable_data *buffer, | ||
399 | size_t *buffer_size), | ||
400 | 0, | ||
401 | (item, buffer, buffer_size), | ||
402 | 1 | ||
403 | ) | ||
404 | |||
405 | WRAP(int, | ||
406 | fido_tx, | ||
407 | (fido_dev_t *d, uint8_t cmd, const void *buf, size_t count), | ||
408 | -1, | ||
409 | (d, cmd, buf, count), | ||
410 | 1 | ||
411 | ) | ||
412 | |||
413 | WRAP(int, | ||
414 | usleep, | ||
415 | (unsigned int usec), | ||
416 | -1, | ||
417 | (usec), | ||
418 | 1 | ||
419 | ) | ||