| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -278,33 +278,44 @@ int ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx, | |||
| 278 | 278 | const uint8_t *key) { | |
| 279 | 279 | ngtcp2_crypto_boringssl_cipher *hp_cipher = cipher->native_handle; | |
| 280 | 280 | ngtcp2_crypto_boringssl_cipher_ctx *ctx; | |
| 281 | - int rv; | ||
| 282 | - (void)rv; | ||
| 281 | + int rv = 0; | ||
| 283 | 282 | ||
| 284 | 283 | ctx = malloc(sizeof(*ctx)); | |
| 285 | 284 | if (ctx == NULL) { | |
| 286 | 285 | return -1; | |
| 287 | 286 | } | |
| 288 | 287 | ||
| 289 | - ctx->type = hp_cipher->type; | ||
| 290 | - cipher_ctx->native_handle = ctx; | ||
| 291 | - | ||
| 292 | 288 | switch (hp_cipher->type) { | |
| 293 | 289 | case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128: | |
| 294 | - rv = AES_set_encrypt_key(key, 128, &ctx->aes_key); | ||
| 295 | - assert(0 == rv); | ||
| 296 | - return 0; | ||
| 290 | + if (AES_set_encrypt_key(key, 128, &ctx->aes_key) != 0) { | ||
| 291 | + rv = -1; | ||
| 292 | + } | ||
| 293 | + | ||
| 294 | + break; | ||
| 297 | 295 | case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256: | |
| 298 | - rv = AES_set_encrypt_key(key, 256, &ctx->aes_key); | ||
| 299 | - assert(0 == rv); | ||
| 300 | - return 0; | ||
| 296 | + if (AES_set_encrypt_key(key, 256, &ctx->aes_key) != 0) { | ||
| 297 | + rv = -1; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + break; | ||
| 301 | 301 | case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20: | |
| 302 | 302 | memcpy(ctx->key, key, sizeof(ctx->key)); | |
| 303 | - return 0; | ||
| 303 | + break; | ||
| 304 | 304 | default: | |
| 305 | 305 | assert(0); | |
| 306 | 306 | abort(); | |
| 307 | 307 | }; | |
| 308 | + | ||
| 309 | + if (rv != 0) { | ||
| 310 | + free(ctx); | ||
| 311 | + | ||
| 312 | + return rv; | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + ctx->type = hp_cipher->type; | ||
| 316 | + cipher_ctx->native_handle = ctx; | ||
| 317 | + | ||
| 318 | + return 0; | ||
| 308 | 319 | } | |
| 309 | 320 | ||
| 310 | 321 | void ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -730,8 +730,8 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, | |||
| 730 | 730 | const uint8_t *secret, size_t secretlen, | |
| 731 | 731 | const uint8_t *salt, size_t saltlen) { | |
| 732 | 732 | const EVP_MD *prf = md->native_handle; | |
| 733 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 734 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 733 | + EVP_KDF *kdf; | ||
| 734 | + EVP_KDF_CTX *kctx; | ||
| 735 | 735 | int mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY; | |
| 736 | 736 | OSSL_PARAM params[] = { | |
| 737 | 737 | OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode), | |
@@ -745,13 +745,24 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, | |||
| 745 | 745 | }; | |
| 746 | 746 | int rv = 0; | |
| 747 | 747 | ||
| 748 | - crypto_kdf_hkdf_free(kdf); | ||
| 748 | + kdf = crypto_kdf_hkdf(); | ||
| 749 | + if (!kdf) { | ||
| 750 | + return -1; | ||
| 751 | + } | ||
| 752 | + | ||
| 753 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 754 | + if (!kctx) { | ||
| 755 | + rv = -1; | ||
| 756 | + goto fail_kdf_ctx_new; | ||
| 757 | + } | ||
| 749 | 758 | ||
| 750 | 759 | if (EVP_KDF_derive(kctx, dest, (size_t)EVP_MD_size(prf), params) <= 0) { | |
| 751 | 760 | rv = -1; | |
| 752 | 761 | } | |
| 753 | 762 | ||
| 754 | 763 | EVP_KDF_CTX_free(kctx); | |
| 764 | + fail_kdf_ctx_new: | ||
| 765 | + crypto_kdf_hkdf_free(kdf); | ||
| 755 | 766 | ||
| 756 | 767 | return rv; | |
| 757 | 768 | } | |
@@ -761,8 +772,8 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen, | |||
| 761 | 772 | size_t secretlen, const uint8_t *info, | |
| 762 | 773 | size_t infolen) { | |
| 763 | 774 | const EVP_MD *prf = md->native_handle; | |
| 764 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 765 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 775 | + EVP_KDF *kdf; | ||
| 776 | + EVP_KDF_CTX *kctx; | ||
| 766 | 777 | int mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY; | |
| 767 | 778 | OSSL_PARAM params[] = { | |
| 768 | 779 | OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode), | |
@@ -776,13 +787,24 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen, | |||
| 776 | 787 | }; | |
| 777 | 788 | int rv = 0; | |
| 778 | 789 | ||
| 779 | - crypto_kdf_hkdf_free(kdf); | ||
| 790 | + kdf = crypto_kdf_hkdf(); | ||
| 791 | + if (!kdf) { | ||
| 792 | + return -1; | ||
| 793 | + } | ||
| 794 | + | ||
| 795 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 796 | + if (!kctx) { | ||
| 797 | + rv = -1; | ||
| 798 | + goto fail_kdf_ctx_new; | ||
| 799 | + } | ||
| 780 | 800 | ||
| 781 | 801 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 782 | 802 | rv = -1; | |
| 783 | 803 | } | |
| 784 | 804 | ||
| 785 | 805 | EVP_KDF_CTX_free(kctx); | |
| 806 | + fail_kdf_ctx_new: | ||
| 807 | + crypto_kdf_hkdf_free(kdf); | ||
| 786 | 808 | ||
| 787 | 809 | return rv; | |
| 788 | 810 | } | |
@@ -792,8 +814,8 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, | |||
| 792 | 814 | size_t secretlen, const uint8_t *salt, size_t saltlen, | |
| 793 | 815 | const uint8_t *info, size_t infolen) { | |
| 794 | 816 | const EVP_MD *prf = md->native_handle; | |
| 795 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 796 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 817 | + EVP_KDF *kdf; | ||
| 818 | + EVP_KDF_CTX *kctx; | ||
| 797 | 819 | OSSL_PARAM params[] = { | |
| 798 | 820 | OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, | |
| 799 | 821 | (char *)EVP_MD_get0_name(prf), 0), | |
@@ -807,13 +829,24 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, | |||
| 807 | 829 | }; | |
| 808 | 830 | int rv = 0; | |
| 809 | 831 | ||
| 810 | - crypto_kdf_hkdf_free(kdf); | ||
| 832 | + kdf = crypto_kdf_hkdf(); | ||
| 833 | + if (!kdf) { | ||
| 834 | + return -1; | ||
| 835 | + } | ||
| 836 | + | ||
| 837 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 838 | + if (!kctx) { | ||
| 839 | + rv = -1; | ||
| 840 | + goto fail_kdf_ctx_new; | ||
| 841 | + } | ||
| 811 | 842 | ||
| 812 | 843 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 813 | 844 | rv = -1; | |
| 814 | 845 | } | |
| 815 | 846 | ||
| 816 | 847 | EVP_KDF_CTX_free(kctx); | |
| 848 | + fail_kdf_ctx_new: | ||
| 849 | + crypto_kdf_hkdf_free(kdf); | ||
| 817 | 850 | ||
| 818 | 851 | return rv; | |
| 819 | 852 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,7 +44,6 @@ | |||
| 44 | 44 | #include "shared.h" | |
| 45 | 45 | ||
| 46 | 46 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | |
| 47 | - static int crypto_initialized; | ||
| 48 | 47 | static EVP_CIPHER *crypto_aes_128_gcm; | |
| 49 | 48 | static EVP_CIPHER *crypto_aes_256_gcm; | |
| 50 | 49 | static EVP_CIPHER *crypto_chacha20_poly1305; | |
@@ -57,57 +56,19 @@ static EVP_MD *crypto_sha384; | |||
| 57 | 56 | static EVP_KDF *crypto_hkdf; | |
| 58 | 57 | ||
| 59 | 58 | int ngtcp2_crypto_quictls_init(void) { | |
| 59 | + /* We do not care whether the pre-fetch succeeds or not. If it | ||
| 60 | + fails, it returns NULL, which is still the default value, and our | ||
| 61 | + code should still work with it. */ | ||
| 60 | 62 | crypto_aes_128_gcm = EVP_CIPHER_fetch(NULL, "AES-128-GCM", NULL); | |
| 61 | - if (crypto_aes_128_gcm == NULL) { | ||
| 62 | - return -1; | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | 63 | crypto_aes_256_gcm = EVP_CIPHER_fetch(NULL, "AES-256-GCM", NULL); | |
| 66 | - if (crypto_aes_256_gcm == NULL) { | ||
| 67 | - return -1; | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | 64 | crypto_chacha20_poly1305 = EVP_CIPHER_fetch(NULL, "ChaCha20-Poly1305", NULL); | |
| 71 | - if (crypto_chacha20_poly1305 == NULL) { | ||
| 72 | - return -1; | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | 65 | crypto_aes_128_ccm = EVP_CIPHER_fetch(NULL, "AES-128-CCM", NULL); | |
| 76 | - if (crypto_aes_128_ccm == NULL) { | ||
| 77 | - return -1; | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | 66 | crypto_aes_128_ecb = EVP_CIPHER_fetch(NULL, "AES-128-ECB", NULL); | |
| 81 | - if (crypto_aes_128_ecb == NULL) { | ||
| 82 | - return -1; | ||
| 83 | - } | ||
| 84 | - | ||
| 85 | 67 | crypto_aes_256_ecb = EVP_CIPHER_fetch(NULL, "AES-256-ECB", NULL); | |
| 86 | - if (crypto_aes_256_ecb == NULL) { | ||
| 87 | - return -1; | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | 68 | crypto_chacha20 = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL); | |
| 91 | - if (crypto_chacha20 == NULL) { | ||
| 92 | - return -1; | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | 69 | crypto_sha256 = EVP_MD_fetch(NULL, "sha256", NULL); | |
| 96 | - if (crypto_sha256 == NULL) { | ||
| 97 | - return -1; | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | 70 | crypto_sha384 = EVP_MD_fetch(NULL, "sha384", NULL); | |
| 101 | - if (crypto_sha384 == NULL) { | ||
| 102 | - return -1; | ||
| 103 | - } | ||
| 104 | - | ||
| 105 | 71 | crypto_hkdf = EVP_KDF_fetch(NULL, "hkdf", NULL); | |
| 106 | - if (crypto_hkdf == NULL) { | ||
| 107 | - return -1; | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | - crypto_initialized = 1; | ||
| 111 | 72 | ||
| 112 | 73 | return 0; | |
| 113 | 74 | } | |
@@ -191,6 +152,12 @@ static EVP_KDF *crypto_kdf_hkdf(void) { | |||
| 191 | 152 | ||
| 192 | 153 | return EVP_KDF_fetch(NULL, "hkdf", NULL); | |
| 193 | 154 | } | |
| 155 | + | ||
| 156 | + static void crypto_kdf_hkdf_free(EVP_KDF *kdf) { | ||
| 157 | + if (kdf && crypto_hkdf != kdf) { | ||
| 158 | + EVP_KDF_free(kdf); | ||
| 159 | + } | ||
| 160 | + } | ||
| 194 | 161 | #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */ | |
| 195 | 162 | # define crypto_aead_aes_128_gcm EVP_aes_128_gcm | |
| 196 | 163 | # define crypto_aead_aes_256_gcm EVP_aes_256_gcm | |
@@ -524,8 +491,8 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, | |||
| 524 | 491 | const uint8_t *salt, size_t saltlen) { | |
| 525 | 492 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | |
| 526 | 493 | const EVP_MD *prf = md->native_handle; | |
| 527 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 528 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 494 | + EVP_KDF *kdf; | ||
| 495 | + EVP_KDF_CTX *kctx; | ||
| 529 | 496 | int mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY; | |
| 530 | 497 | OSSL_PARAM params[] = { | |
| 531 | 498 | OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode), | |
@@ -539,15 +506,24 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, | |||
| 539 | 506 | }; | |
| 540 | 507 | int rv = 0; | |
| 541 | 508 | ||
| 542 | - if (!crypto_initialized) { | ||
| 543 | - EVP_KDF_free(kdf); | ||
| 509 | + kdf = crypto_kdf_hkdf(); | ||
| 510 | + if (!kdf) { | ||
| 511 | + return -1; | ||
| 512 | + } | ||
| 513 | + | ||
| 514 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 515 | + if (!kctx) { | ||
| 516 | + rv = -1; | ||
| 517 | + goto fail_kdf_ctx_new; | ||
| 544 | 518 | } | |
| 545 | 519 | ||
| 546 | 520 | if (EVP_KDF_derive(kctx, dest, (size_t)EVP_MD_size(prf), params) <= 0) { | |
| 547 | 521 | rv = -1; | |
| 548 | 522 | } | |
| 549 | 523 | ||
| 550 | 524 | EVP_KDF_CTX_free(kctx); | |
| 525 | + fail_kdf_ctx_new: | ||
| 526 | + crypto_kdf_hkdf_free(kdf); | ||
| 551 | 527 | ||
| 552 | 528 | return rv; | |
| 553 | 529 | #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */ | |
@@ -581,8 +557,8 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen, | |||
| 581 | 557 | size_t infolen) { | |
| 582 | 558 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | |
| 583 | 559 | const EVP_MD *prf = md->native_handle; | |
| 584 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 585 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 560 | + EVP_KDF *kdf; | ||
| 561 | + EVP_KDF_CTX *kctx; | ||
| 586 | 562 | int mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY; | |
| 587 | 563 | OSSL_PARAM params[] = { | |
| 588 | 564 | OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode), | |
@@ -596,15 +572,24 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen, | |||
| 596 | 572 | }; | |
| 597 | 573 | int rv = 0; | |
| 598 | 574 | ||
| 599 | - if (!crypto_initialized) { | ||
| 600 | - EVP_KDF_free(kdf); | ||
| 575 | + kdf = crypto_kdf_hkdf(); | ||
| 576 | + if (!kdf) { | ||
| 577 | + return -1; | ||
| 578 | + } | ||
| 579 | + | ||
| 580 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 581 | + if (!kctx) { | ||
| 582 | + rv = -1; | ||
| 583 | + goto fail_kdf_ctx_new; | ||
| 601 | 584 | } | |
| 602 | 585 | ||
| 603 | 586 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 604 | 587 | rv = -1; | |
| 605 | 588 | } | |
| 606 | 589 | ||
| 607 | 590 | EVP_KDF_CTX_free(kctx); | |
| 591 | + fail_kdf_ctx_new: | ||
| 592 | + crypto_kdf_hkdf_free(kdf); | ||
| 608 | 593 | ||
| 609 | 594 | return rv; | |
| 610 | 595 | #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */ | |
@@ -637,8 +622,8 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, | |||
| 637 | 622 | const uint8_t *info, size_t infolen) { | |
| 638 | 623 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | |
| 639 | 624 | const EVP_MD *prf = md->native_handle; | |
| 640 | - EVP_KDF *kdf = crypto_kdf_hkdf(); | ||
| 641 | - EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf); | ||
| 625 | + EVP_KDF *kdf; | ||
| 626 | + EVP_KDF_CTX *kctx; | ||
| 642 | 627 | OSSL_PARAM params[] = { | |
| 643 | 628 | OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, | |
| 644 | 629 | (char *)EVP_MD_get0_name(prf), 0), | |
@@ -652,15 +637,24 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, | |||
| 652 | 637 | }; | |
| 653 | 638 | int rv = 0; | |
| 654 | 639 | ||
| 655 | - if (!crypto_initialized) { | ||
| 656 | - EVP_KDF_free(kdf); | ||
| 640 | + kdf = crypto_kdf_hkdf(); | ||
| 641 | + if (!kdf) { | ||
| 642 | + return -1; | ||
| 643 | + } | ||
| 644 | + | ||
| 645 | + kctx = EVP_KDF_CTX_new(kdf); | ||
| 646 | + if (!kctx) { | ||
| 647 | + rv = -1; | ||
| 648 | + goto fail_kdf_ctx_new; | ||
| 657 | 649 | } | |
| 658 | 650 | ||
| 659 | 651 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 660 | 652 | rv = -1; | |
| 661 | 653 | } | |
| 662 | 654 | ||
| 663 | 655 | EVP_KDF_CTX_free(kctx); | |
| 656 | + fail_kdf_ctx_new: | ||
| 657 | + crypto_kdf_hkdf_free(kdf); | ||
| 664 | 658 | ||
| 665 | 659 | return rv; | |
| 666 | 660 | #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments