1 /* 2 * Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * https://www.openssl.org/source/license.html 8 * or in the file LICENSE in the source distribution. 9 */ 10 #include <string.h> 11 #include <openssl/types.h> 12 #include <openssl/crypto.h> 13 #include <openssl/core_names.h> 14 #include <openssl/kdf.h> 15 #include <openssl/evp.h> 16 #include <openssl/provider.h> 17 #include "fuzzer.h" 18 19 #define DEFINE_ALGORITHMS(name, evp) \ 20 DEFINE_STACK_OF(evp) \ 21 static int cmp_##evp(const evp *const *a, const evp *const *b); \ 22 static void collect_##evp(evp *obj, void *stack); \ 23 static void init_##name(OSSL_LIB_CTX *libctx); \ 24 static void cleanup_##name(void); \ 25 static STACK_OF(evp) *name##_collection; \ 26 static int cmp_##evp(const evp *const *a, const evp *const *b) \ 27 { \ 28 return strcmp(OSSL_PROVIDER_get0_name(evp##_get0_provider(*a)), \ 29 OSSL_PROVIDER_get0_name(evp##_get0_provider(*b))); \ 30 } \ 31 static void collect_##evp(evp *obj, void *stack) \ 32 { \ 33 STACK_OF(evp) *obj_stack = stack; \ 34 \ 35 if (!evp##_up_ref(obj)) \ 36 return; \ 37 \ 38 if (sk_##evp##_push(obj_stack, obj) <= 0) \ 39 evp##_free(obj); \ 40 } \ 41 static void init_##name(OSSL_LIB_CTX *libctx) \ 42 { \ 43 name##_collection = sk_##evp##_new(cmp_##evp); \ 44 evp##_do_all_provided(libctx, collect_##evp, name##_collection); \ 45 } \ 46 static void cleanup_##name(void) \ 47 { \ 48 sk_##evp##_pop_free(name##_collection, evp##_free); \ 49 } 50 51 DEFINE_ALGORITHMS(digests, EVP_MD) 52 53 DEFINE_ALGORITHMS(kdf, EVP_KDF) 54 55 DEFINE_ALGORITHMS(cipher, EVP_CIPHER) 56 57 DEFINE_ALGORITHMS(kem, EVP_KEM) 58 59 DEFINE_ALGORITHMS(keyexch, EVP_KEYEXCH) 60 61 DEFINE_ALGORITHMS(rand, EVP_RAND) 62 63 DEFINE_ALGORITHMS(mac, EVP_MAC) 64 65 DEFINE_ALGORITHMS(keymgmt, EVP_KEYMGMT) 66 67 DEFINE_ALGORITHMS(signature, EVP_SIGNATURE) 68 69 DEFINE_ALGORITHMS(asym_ciphers, EVP_ASYM_CIPHER) 70 71 static OSSL_LIB_CTX *libctx = NULL; 72 73 int FuzzerInitialize(int *argc, char ***argv) 74 { 75 libctx = OSSL_LIB_CTX_new(); 76 if (libctx == NULL) 77 return 0; 78 79 init_digests(libctx); 80 init_kdf(libctx); 81 init_cipher(libctx); 82 init_kem(libctx); 83 init_keyexch(libctx); 84 init_rand(libctx); 85 init_mac(libctx); 86 init_keymgmt(libctx); 87 init_signature(libctx); 88 init_asym_ciphers(libctx); 89 return 1; 90 } 91 92 void FuzzerCleanup(void) 93 { 94 cleanup_digests(); 95 cleanup_kdf(); 96 cleanup_cipher(); 97 cleanup_kem(); 98 cleanup_keyexch(); 99 cleanup_rand(); 100 cleanup_mac(); 101 cleanup_keymgmt(); 102 cleanup_signature(); 103 cleanup_asym_ciphers(); 104 105 OSSL_LIB_CTX_free(libctx); 106 } 107 108 static int read_uint(const uint8_t **buf, size_t *len, uint64_t **res) 109 { 110 int r = 1; 111 112 if (*len < sizeof(uint64_t)) { 113 r = 0; 114 goto end; 115 } 116 117 *res = OPENSSL_malloc(sizeof(uint64_t)); 118 **res = (uint64_t)**buf; 119 120 *buf += sizeof(uint64_t); 121 *len -= sizeof(uint64_t); 122 end: 123 return r; 124 } 125 126 static int read_int(const uint8_t **buf, size_t *len, int64_t **res) 127 { 128 int r = 1; 129 130 if (*len < sizeof(int64_t)) { 131 r = 0; 132 goto end; 133 } 134 135 *res = OPENSSL_malloc(sizeof(int64_t)); 136 **res = (int64_t)**buf; 137 138 *buf += sizeof(int64_t); 139 *len -= sizeof(int64_t); 140 end: 141 return r; 142 } 143 144 static int read_double(const uint8_t **buf, size_t *len, double **res) 145 { 146 int r = 1; 147 148 if (*len < sizeof(double)) { 149 r = 0; 150 goto end; 151 } 152 153 *res = OPENSSL_malloc(sizeof(double)); 154 **res = (double)**buf; 155 156 *buf += sizeof(double); 157 *len -= sizeof(double); 158 end: 159 return r; 160 } 161 162 static int read_utf8_string(const uint8_t **buf, size_t *len, char **res) 163 { 164 size_t found_len; 165 int r; 166 167 found_len = OPENSSL_strnlen((const char *)*buf, *len); 168 169 if (found_len == *len) { 170 r = -1; 171 goto end; 172 } 173 174 found_len++; /* skip over the \0 byte */ 175 176 r = (int)found_len; 177 178 *res = (char *)*buf; 179 *len -= found_len; 180 *buf = *buf + found_len; /* continue after the \0 byte */ 181 end: 182 return r; 183 } 184 185 static int read_utf8_ptr(const uint8_t **buf, size_t *len, char **res) 186 { 187 if (*len > 0 && **buf == 0xFF) { 188 /* represent NULL somehow */ 189 *res = NULL; 190 *buf += 1; 191 *len -= 1; 192 return 0; 193 } 194 return read_utf8_string(buf, len, res); 195 } 196 197 static int read_octet_string(const uint8_t **buf, size_t *len, char **res) 198 { 199 int r; 200 size_t i; 201 const uint8_t *ptr = *buf; 202 int found = 0; 203 204 for (i = 0; i < *len; ++i) { 205 if (*ptr == 0xFF && (i + 1 < *len && *(ptr + 1) == 0xFF)) { 206 ptr++; 207 found = 1; 208 break; 209 } 210 ptr++; 211 } 212 213 if (!found) { 214 r = -1; 215 goto end; 216 } 217 218 *res = (char *)*buf; 219 220 r = ptr - *buf; 221 *len -= r; 222 *buf = ptr; 223 224 end: 225 return r; 226 } 227 228 static int read_octet_ptr(const uint8_t **buf, size_t *len, char **res) 229 { 230 /* TODO: This representation could need an improvement potentially. */ 231 if (*len > 1 && **buf == 0xFF && *(*buf + 1) == 0xFF) { 232 /* represent NULL somehow */ 233 *res = NULL; 234 *buf += 2; 235 *len -= 2; 236 return 0; 237 } 238 return read_octet_string(buf, len, res); 239 } 240 241 static char *DFLT_STR = ""; 242 static char *DFLT_UTF8_PTR = NULL; 243 static char *DFLT_OCTET_STRING = ""; 244 static char *DFLT_OCTET_PTR = NULL; 245 246 static int64_t ITERS = 1; 247 static uint64_t UITERS = 1; 248 static int64_t BLOCKSIZE = 8; 249 static uint64_t UBLOCKSIZE = 8; 250 251 static void free_params(OSSL_PARAM *param) 252 { 253 for (; param != NULL && param->key != NULL; param++) { 254 switch (param->data_type) { 255 case OSSL_PARAM_INTEGER: 256 case OSSL_PARAM_UNSIGNED_INTEGER: 257 case OSSL_PARAM_REAL: 258 if (param->data != NULL) { 259 OPENSSL_free(param->data); 260 } 261 break; 262 } 263 } 264 } 265 266 static OSSL_PARAM *fuzz_params(OSSL_PARAM *param, const uint8_t **buf, size_t *len) 267 { 268 OSSL_PARAM *p; 269 OSSL_PARAM *fuzzed_parameters; 270 int p_num = 0; 271 272 for (p = param; p != NULL && p->key != NULL; p++) 273 p_num++; 274 275 fuzzed_parameters = OPENSSL_zalloc(sizeof(OSSL_PARAM) * (p_num + 1)); 276 p = fuzzed_parameters; 277 278 for (; param != NULL && param->key != NULL; param++) { 279 int64_t *use_param = NULL; 280 int64_t *p_value_int = NULL; 281 uint64_t *p_value_uint = NULL; 282 double *p_value_double = NULL; 283 char *p_value_utf8_str = DFLT_STR; 284 char *p_value_octet_str = DFLT_OCTET_STRING; 285 char *p_value_utf8_ptr = DFLT_UTF8_PTR; 286 char *p_value_octet_ptr = DFLT_OCTET_PTR; 287 288 int data_len = 0; 289 290 if (!read_int(buf, len, &use_param)) { 291 use_param = OPENSSL_malloc(sizeof(uint64_t)); 292 *use_param = 0; 293 } 294 295 switch (param->data_type) { 296 case OSSL_PARAM_INTEGER: 297 if (strcmp(param->key, OSSL_KDF_PARAM_ITER) == 0) { 298 p_value_int = OPENSSL_malloc(sizeof(ITERS)); 299 *p_value_int = ITERS; 300 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_N) == 0) { 301 p_value_int = OPENSSL_malloc(sizeof(ITERS)); 302 *p_value_int = ITERS; 303 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_R) == 0) { 304 p_value_int = OPENSSL_malloc(sizeof(BLOCKSIZE)); 305 *p_value_int = BLOCKSIZE; 306 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_P) == 0) { 307 p_value_int = OPENSSL_malloc(sizeof(BLOCKSIZE)); 308 *p_value_int = BLOCKSIZE; 309 } else if (!*use_param || !read_int(buf, len, &p_value_int)) { 310 p_value_int = OPENSSL_malloc(sizeof(int64_t)); 311 *p_value_int = 0; 312 } 313 314 *p = *param; 315 p->data = p_value_int; 316 p++; 317 break; 318 case OSSL_PARAM_UNSIGNED_INTEGER: 319 if (strcmp(param->key, OSSL_KDF_PARAM_ITER) == 0) { 320 p_value_uint = OPENSSL_malloc(sizeof(UITERS)); 321 *p_value_uint = UITERS; 322 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_N) == 0) { 323 p_value_uint = OPENSSL_malloc(sizeof(UITERS)); 324 *p_value_uint = UITERS; 325 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_R) == 0) { 326 p_value_uint = OPENSSL_malloc(sizeof(UBLOCKSIZE)); 327 *p_value_uint = UBLOCKSIZE; 328 } else if (strcmp(param->key, OSSL_KDF_PARAM_SCRYPT_P) == 0) { 329 p_value_uint = OPENSSL_malloc(sizeof(UBLOCKSIZE)); 330 *p_value_uint = UBLOCKSIZE; 331 } else if (!*use_param || !read_uint(buf, len, &p_value_uint)) { 332 p_value_uint = OPENSSL_malloc(sizeof(uint64_t)); 333 *p_value_uint = 0; 334 } 335 336 *p = *param; 337 p->data = p_value_uint; 338 p++; 339 break; 340 case OSSL_PARAM_REAL: 341 if (!*use_param || !read_double(buf, len, &p_value_double)) { 342 p_value_double = OPENSSL_malloc(sizeof(double)); 343 *p_value_double = 0; 344 } 345 346 *p = *param; 347 p->data = p_value_double; 348 p++; 349 break; 350 case OSSL_PARAM_UTF8_STRING: 351 if (*use_param && (data_len = read_utf8_string(buf, len, &p_value_utf8_str)) < 0) 352 data_len = 0; 353 *p = *param; 354 p->data = p_value_utf8_str; 355 p->data_size = data_len; 356 p++; 357 break; 358 case OSSL_PARAM_OCTET_STRING: 359 if (*use_param && (data_len = read_octet_string(buf, len, &p_value_octet_str)) < 0) 360 data_len = 0; 361 *p = *param; 362 p->data = p_value_octet_str; 363 p->data_size = data_len; 364 p++; 365 break; 366 case OSSL_PARAM_UTF8_PTR: 367 if (*use_param && (data_len = read_utf8_ptr(buf, len, &p_value_utf8_ptr)) < 0) 368 data_len = 0; 369 *p = *param; 370 p->data = p_value_utf8_ptr; 371 p->data_size = data_len; 372 p++; 373 break; 374 case OSSL_PARAM_OCTET_PTR: 375 if (*use_param && (data_len = read_octet_ptr(buf, len, &p_value_octet_ptr)) < 0) 376 data_len = 0; 377 *p = *param; 378 p->data = p_value_octet_ptr; 379 p->data_size = data_len; 380 p++; 381 break; 382 default: 383 break; 384 } 385 386 OPENSSL_free(use_param); 387 } 388 389 return fuzzed_parameters; 390 } 391 392 static int do_evp_cipher(const EVP_CIPHER *evp_cipher, const OSSL_PARAM param[]) 393 { 394 unsigned char outbuf[1024]; 395 int outlen, tmplen; 396 unsigned char key[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; 397 unsigned char iv[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 398 const char intext[] = "text"; 399 EVP_CIPHER_CTX *ctx; 400 401 ctx = EVP_CIPHER_CTX_new(); 402 403 if (!EVP_CIPHER_CTX_set_params(ctx, param)) { 404 EVP_CIPHER_CTX_free(ctx); 405 return 0; 406 } 407 408 if (!EVP_EncryptInit_ex2(ctx, evp_cipher, key, iv, NULL)) { 409 /* Error */ 410 EVP_CIPHER_CTX_free(ctx); 411 return 0; 412 } 413 414 if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, (const unsigned char *)intext, strlen(intext))) { 415 /* Error */ 416 EVP_CIPHER_CTX_free(ctx); 417 return 0; 418 } 419 /* 420 * Buffer passed to EVP_EncryptFinal() must be after data just 421 * encrypted to avoid overwriting it. 422 */ 423 if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) { 424 /* Error */ 425 EVP_CIPHER_CTX_free(ctx); 426 return 0; 427 } 428 outlen += tmplen; 429 EVP_CIPHER_CTX_free(ctx); 430 return 1; 431 } 432 433 static int do_evp_kdf(EVP_KDF *evp_kdf, const OSSL_PARAM params[]) 434 { 435 int r = 1; 436 EVP_KDF_CTX *kctx = NULL; 437 unsigned char derived[32]; 438 439 kctx = EVP_KDF_CTX_new(evp_kdf); 440 441 if (kctx == NULL) { 442 r = 0; 443 goto end; 444 } 445 446 if (EVP_KDF_CTX_set_params(kctx, params) <= 0) { 447 r = 0; 448 goto end; 449 } 450 451 if (EVP_KDF_derive(kctx, derived, sizeof(derived), NULL) <= 0) { 452 r = 0; 453 goto end; 454 } 455 456 end: 457 EVP_KDF_CTX_free(kctx); 458 return r; 459 } 460 461 static int do_evp_mac(EVP_MAC *evp_mac, const OSSL_PARAM params[]) 462 { 463 int r = 1; 464 const char *key = "mac_key"; 465 char text[] = "Some Crypto Text"; 466 EVP_MAC_CTX *ctx = NULL; 467 unsigned char buf[4096]; 468 size_t final_l; 469 470 if ((ctx = EVP_MAC_CTX_new(evp_mac)) == NULL 471 || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key), 472 params)) { 473 r = 0; 474 goto end; 475 } 476 477 if (EVP_MAC_CTX_set_params(ctx, params) <= 0) { 478 r = 0; 479 goto end; 480 } 481 482 if (!EVP_MAC_update(ctx, (unsigned char *)text, sizeof(text))) { 483 r = 0; 484 goto end; 485 } 486 487 if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf))) { 488 r = 0; 489 goto end; 490 } 491 492 end: 493 EVP_MAC_CTX_free(ctx); 494 return r; 495 } 496 497 static int do_evp_rand(EVP_RAND *evp_rand, const OSSL_PARAM params[]) 498 { 499 int r = 1; 500 EVP_RAND_CTX *ctx = NULL; 501 unsigned char buf[4096]; 502 503 if (!(ctx = EVP_RAND_CTX_new(evp_rand, NULL))) { 504 r = 0; 505 goto end; 506 } 507 508 if (EVP_RAND_CTX_set_params(ctx, params) <= 0) { 509 r = 0; 510 goto end; 511 } 512 513 if (!EVP_RAND_generate(ctx, buf, sizeof(buf), 0, 0, NULL, 0)) { 514 r = 0; 515 goto end; 516 } 517 518 if (!EVP_RAND_reseed(ctx, 0, 0, 0, NULL, 0)) { 519 r = 0; 520 goto end; 521 } 522 523 end: 524 EVP_RAND_CTX_free(ctx); 525 return r; 526 } 527 528 static int do_evp_sig(EVP_SIGNATURE *evp_sig, const OSSL_PARAM params[]) 529 { 530 return 0; 531 } 532 533 static int do_evp_asym_cipher(EVP_ASYM_CIPHER *evp_asym_cipher, const OSSL_PARAM params[]) 534 { 535 return 0; 536 } 537 538 static int do_evp_kem(EVP_KEM *evp_kem, const OSSL_PARAM params[]) 539 { 540 return 0; 541 } 542 543 static int do_evp_key_exch(EVP_KEYEXCH *evp_kdf, const OSSL_PARAM params[]) 544 { 545 return 0; 546 } 547 548 static int do_evp_md(EVP_MD *evp_md, const OSSL_PARAM params[]) 549 { 550 int r = 1; 551 unsigned char md_value[EVP_MAX_MD_SIZE]; 552 unsigned int md_len; 553 EVP_MD_CTX *mdctx = NULL; 554 555 if (!(mdctx = EVP_MD_CTX_new())) { 556 r = 0; 557 goto end; 558 } 559 560 if (!EVP_MD_CTX_set_params(mdctx, params)) { 561 r = 0; 562 goto end; 563 } 564 565 if (!EVP_DigestInit_ex2(mdctx, evp_md, NULL)) { 566 r = 0; 567 goto end; 568 } 569 if (!EVP_DigestUpdate(mdctx, "Test", strlen("Test"))) { 570 r = 0; 571 goto end; 572 } 573 if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) { 574 r = 0; 575 goto end; 576 } 577 578 end: 579 EVP_MD_CTX_free(mdctx); 580 return r; 581 } 582 583 #define EVP_FUZZ(source, evp, f) \ 584 do { \ 585 evp *alg = sk_##evp##_value(source, *algorithm % sk_##evp##_num(source)); \ 586 OSSL_PARAM *fuzzed_params; \ 587 \ 588 if (alg == NULL) \ 589 break; \ 590 fuzzed_params = fuzz_params((OSSL_PARAM *)evp##_settable_ctx_params(alg), &buf, &len); \ 591 if (fuzzed_params != NULL) \ 592 f(alg, fuzzed_params); \ 593 free_params(fuzzed_params); \ 594 OSSL_PARAM_free(fuzzed_params); \ 595 } while (0); 596 597 int FuzzerTestOneInput(const uint8_t *buf, size_t len) 598 { 599 int r = 1; 600 uint64_t *operation = NULL; 601 int64_t *algorithm = NULL; 602 603 if (!read_uint(&buf, &len, &operation)) { 604 r = 0; 605 goto end; 606 } 607 608 if (!read_int(&buf, &len, &algorithm)) { 609 r = 0; 610 goto end; 611 } 612 613 switch (*operation % 10) { 614 case 0: 615 EVP_FUZZ(digests_collection, EVP_MD, do_evp_md); 616 break; 617 case 1: 618 EVP_FUZZ(cipher_collection, EVP_CIPHER, do_evp_cipher); 619 break; 620 case 2: 621 EVP_FUZZ(kdf_collection, EVP_KDF, do_evp_kdf); 622 break; 623 case 3: 624 EVP_FUZZ(mac_collection, EVP_MAC, do_evp_mac); 625 break; 626 case 4: 627 EVP_FUZZ(kem_collection, EVP_KEM, do_evp_kem); 628 break; 629 case 5: 630 EVP_FUZZ(rand_collection, EVP_RAND, do_evp_rand); 631 break; 632 case 6: 633 EVP_FUZZ(asym_ciphers_collection, EVP_ASYM_CIPHER, do_evp_asym_cipher); 634 break; 635 case 7: 636 EVP_FUZZ(signature_collection, EVP_SIGNATURE, do_evp_sig); 637 break; 638 case 8: 639 EVP_FUZZ(keyexch_collection, EVP_KEYEXCH, do_evp_key_exch); 640 break; 641 case 9: 642 /* 643 Implement and call: 644 static int do_evp_keymgmt(EVP_KEYMGMT *evp_kdf, const OSSL_PARAM params[]) 645 { 646 return 0; 647 } 648 */ 649 /* not yet implemented */ 650 break; 651 default: 652 r = 0; 653 goto end; 654 } 655 656 end: 657 OPENSSL_free(operation); 658 OPENSSL_free(algorithm); 659 return r; 660 } 661