1 /* 2 * Copyright (c) 2019-2021 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 <stdbool.h> 13 #include <stdint.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 #include "mutator_aux.h" 18 19 extern int prng_up; 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 (prng_up && 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(int, 62 EVP_Cipher, 63 (EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, 64 unsigned int inl), 65 -1, 66 (ctx, out, in, inl), 67 1 68 ) 69 70 WRAP(int, 71 EVP_CIPHER_CTX_ctrl, 72 (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr), 73 0, 74 (ctx, type, arg, ptr), 75 1 76 ) 77 78 WRAP(EVP_CIPHER_CTX *, 79 EVP_CIPHER_CTX_new, 80 (void), 81 NULL, 82 (), 83 1 84 ) 85 86 WRAP(int, 87 EVP_EncryptInit_ex, 88 (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, 89 const unsigned char *key, const unsigned char *iv), 90 0, 91 (ctx, type, impl, key, iv), 92 1 93 ) 94 95 WRAP(int, 96 EVP_CIPHER_CTX_set_padding, 97 (EVP_CIPHER_CTX *x, int padding), 98 0, 99 (x, padding), 100 1 101 ) 102 103 WRAP(int, 104 EVP_EncryptUpdate, 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 EVP_CipherInit, 114 (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 115 const unsigned char *key, const unsigned char *iv, int enc), 116 0, 117 (ctx, cipher, key, iv, enc), 118 1 119 ) 120 121 WRAP(int, 122 EVP_DecryptInit_ex, 123 (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, 124 const unsigned char *key, const unsigned char *iv), 125 0, 126 (ctx, type, impl, key, iv), 127 1 128 ) 129 130 WRAP(int, 131 EVP_DecryptUpdate, 132 (EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, 133 const unsigned char *in, int inl), 134 0, 135 (ctx, out, outl, in, inl), 136 1 137 ) 138 139 WRAP(int, 140 SHA256_Init, 141 (SHA256_CTX *c), 142 0, 143 (c), 144 1 145 ) 146 147 WRAP(int, 148 SHA256_Update, 149 (SHA256_CTX *c, const void *data, size_t len), 150 0, 151 (c, data, len), 152 1 153 ) 154 155 WRAP(int, 156 SHA256_Final, 157 (unsigned char *md, SHA256_CTX *c), 158 0, 159 (md, c), 160 1 161 ) 162 163 WRAP(RSA *, 164 EVP_PKEY_get0_RSA, 165 (EVP_PKEY *pkey), 166 NULL, 167 (pkey), 168 1 169 ) 170 171 WRAP(EC_KEY *, 172 EVP_PKEY_get0_EC_KEY, 173 (EVP_PKEY *pkey), 174 NULL, 175 (pkey), 176 1 177 ) 178 179 WRAP(int, 180 EVP_PKEY_get_raw_public_key, 181 (const EVP_PKEY *pkey, unsigned char *pub, size_t *len), 182 0, 183 (pkey, pub, len), 184 1 185 ) 186 187 WRAP(EVP_MD_CTX *, 188 EVP_MD_CTX_new, 189 (void), 190 NULL, 191 (), 192 1 193 ) 194 195 WRAP(int, 196 EVP_DigestVerifyInit, 197 (EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, 198 EVP_PKEY *pkey), 199 0, 200 (ctx, pctx, type, e, pkey), 201 1 202 ) 203 204 WRAP(BIGNUM *, 205 BN_bin2bn, 206 (const unsigned char *s, int len, BIGNUM *ret), 207 NULL, 208 (s, len, ret), 209 1 210 ) 211 212 WRAP(int, 213 BN_bn2bin, 214 (const BIGNUM *a, unsigned char *to), 215 -1, 216 (a, to), 217 1 218 ) 219 220 WRAP(BIGNUM *, 221 BN_CTX_get, 222 (BN_CTX *ctx), 223 NULL, 224 (ctx), 225 1 226 ) 227 228 WRAP(BN_CTX *, 229 BN_CTX_new, 230 (void), 231 NULL, 232 (), 233 1 234 ) 235 236 WRAP(BIGNUM *, 237 BN_new, 238 (void), 239 NULL, 240 (), 241 1 242 ) 243 244 WRAP(int, 245 RSA_set0_key, 246 (RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d), 247 0, 248 (r, n, e, d), 249 1 250 ) 251 252 WRAP(EC_KEY *, 253 EC_KEY_new_by_curve_name, 254 (int nid), 255 NULL, 256 (nid), 257 1 258 ) 259 260 WRAP(const EC_GROUP *, 261 EC_KEY_get0_group, 262 (const EC_KEY *key), 263 NULL, 264 (key), 265 1 266 ) 267 268 WRAP(const BIGNUM *, 269 EC_KEY_get0_private_key, 270 (const EC_KEY *key), 271 NULL, 272 (key), 273 1 274 ) 275 276 WRAP(EC_POINT *, 277 EC_POINT_new, 278 (const EC_GROUP *group), 279 NULL, 280 (group), 281 1 282 ) 283 284 WRAP(int, 285 EC_POINT_get_affine_coordinates_GFp, 286 (const EC_GROUP *group, const EC_POINT *p, BIGNUM *x, BIGNUM *y, BN_CTX *ctx), 287 0, 288 (group, p, x, y, ctx), 289 1 290 ) 291 292 WRAP(EVP_PKEY *, 293 EVP_PKEY_new, 294 (void), 295 NULL, 296 (), 297 1 298 ) 299 300 WRAP(int, 301 EVP_PKEY_assign, 302 (EVP_PKEY *pkey, int type, void *key), 303 0, 304 (pkey, type, key), 305 1 306 ) 307 308 WRAP(int, 309 EVP_PKEY_keygen_init, 310 (EVP_PKEY_CTX *ctx), 311 0, 312 (ctx), 313 1 314 ) 315 316 WRAP(int, 317 EVP_PKEY_keygen, 318 (EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey), 319 0, 320 (ctx, ppkey), 321 1 322 ) 323 324 WRAP(int, 325 EVP_PKEY_paramgen_init, 326 (EVP_PKEY_CTX *ctx), 327 0, 328 (ctx), 329 1 330 ) 331 332 WRAP(int, 333 EVP_PKEY_paramgen, 334 (EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey), 335 0, 336 (ctx, ppkey), 337 1 338 ) 339 340 WRAP(EVP_PKEY *, 341 EVP_PKEY_new_raw_public_key, 342 (int type, ENGINE *e, const unsigned char *key, size_t keylen), 343 NULL, 344 (type, e, key, keylen), 345 1 346 ) 347 348 WRAP(EVP_PKEY_CTX *, 349 EVP_PKEY_CTX_new, 350 (EVP_PKEY *pkey, ENGINE *e), 351 NULL, 352 (pkey, e), 353 1 354 ) 355 356 WRAP(EVP_PKEY_CTX *, 357 EVP_PKEY_CTX_new_id, 358 (int id, ENGINE *e), 359 NULL, 360 (id, e), 361 1 362 ) 363 364 WRAP(int, 365 EVP_PKEY_derive, 366 (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen), 367 0, 368 (ctx, key, pkeylen), 369 1 370 ) 371 372 WRAP(int, 373 EVP_PKEY_derive_init, 374 (EVP_PKEY_CTX *ctx), 375 0, 376 (ctx), 377 1 378 ) 379 380 WRAP(int, 381 EVP_PKEY_derive_set_peer, 382 (EVP_PKEY_CTX *ctx, EVP_PKEY *peer), 383 0, 384 (ctx, peer), 385 1 386 ) 387 388 WRAP(const EVP_MD *, 389 EVP_sha256, 390 (void), 391 NULL, 392 (), 393 1 394 ) 395 396 WRAP(unsigned char *, 397 HMAC, 398 (const EVP_MD *evp_md, const void *key, int key_len, 399 const unsigned char *d, int n, unsigned char *md, 400 unsigned int *md_len), 401 NULL, 402 (evp_md, key, key_len, d, n, md, md_len), 403 1 404 ) 405 406 WRAP(HMAC_CTX *, 407 HMAC_CTX_new, 408 (void), 409 NULL, 410 (), 411 1 412 ) 413 414 WRAP(int, 415 HMAC_Init_ex, 416 (HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md, 417 ENGINE *impl), 418 0, 419 (ctx, key, key_len, md, impl), 420 1 421 ) 422 423 WRAP(int, 424 HMAC_Update, 425 (HMAC_CTX *ctx, const unsigned char *data, int len), 426 0, 427 (ctx, data, len), 428 1 429 ) 430 431 WRAP(int, 432 HMAC_Final, 433 (HMAC_CTX *ctx, unsigned char *md, unsigned int *len), 434 0, 435 (ctx, md, len), 436 1 437 ) 438 439 WRAP(unsigned char *, 440 SHA256, 441 (const unsigned char *d, size_t n, unsigned char *md), 442 NULL, 443 (d, n, md), 444 1 445 ) 446 447 WRAP(cbor_item_t *, 448 cbor_build_string, 449 (const char *val), 450 NULL, 451 (val), 452 1 453 ) 454 455 WRAP(cbor_item_t *, 456 cbor_build_bytestring, 457 (cbor_data handle, size_t length), 458 NULL, 459 (handle, length), 460 1 461 ) 462 463 WRAP(cbor_item_t *, 464 cbor_build_bool, 465 (bool value), 466 NULL, 467 (value), 468 1 469 ) 470 471 WRAP(cbor_item_t *, 472 cbor_build_negint8, 473 (uint8_t value), 474 NULL, 475 (value), 476 1 477 ) 478 479 WRAP(cbor_item_t *, 480 cbor_build_negint16, 481 (uint16_t value), 482 NULL, 483 (value), 484 1 485 ) 486 487 WRAP(cbor_item_t *, 488 cbor_load, 489 (cbor_data source, size_t source_size, struct cbor_load_result *result), 490 NULL, 491 (source, source_size, result), 492 1 493 ) 494 495 WRAP(cbor_item_t *, 496 cbor_build_uint8, 497 (uint8_t value), 498 NULL, 499 (value), 500 1 501 ) 502 503 WRAP(cbor_item_t *, 504 cbor_build_uint32, 505 (uint32_t value), 506 NULL, 507 (value), 508 1 509 ) 510 511 WRAP(struct cbor_pair *, 512 cbor_map_handle, 513 (const cbor_item_t *item), 514 NULL, 515 (item), 516 1 517 ) 518 519 WRAP(cbor_item_t **, 520 cbor_array_handle, 521 (const cbor_item_t *item), 522 NULL, 523 (item), 524 1 525 ) 526 527 WRAP(bool, 528 cbor_array_push, 529 (cbor_item_t *array, cbor_item_t *pushee), 530 false, 531 (array, pushee), 532 1 533 ) 534 535 WRAP(bool, 536 cbor_map_add, 537 (cbor_item_t *item, struct cbor_pair pair), 538 false, 539 (item, pair), 540 1 541 ) 542 543 WRAP(cbor_item_t *, 544 cbor_new_definite_map, 545 (size_t size), 546 NULL, 547 (size), 548 1 549 ) 550 551 WRAP(cbor_item_t *, 552 cbor_new_definite_array, 553 (size_t size), 554 NULL, 555 (size), 556 1 557 ) 558 559 WRAP(size_t, 560 cbor_serialize_alloc, 561 (const cbor_item_t *item, cbor_mutable_data *buffer, 562 size_t *buffer_size), 563 0, 564 (item, buffer, buffer_size), 565 1 566 ) 567 568 WRAP(int, 569 fido_tx, 570 (fido_dev_t *d, uint8_t cmd, const void *buf, size_t count), 571 -1, 572 (d, cmd, buf, count), 573 1 574 ) 575 576 WRAP(int, 577 usleep, 578 (unsigned int usec), 579 -1, 580 (usec), 581 1 582 ) 583