4444#include "shared.h"
4545
4646#if OPENSSL_VERSION_NUMBER >= 0x30000000L
47- static int crypto_initialized ;
4847static EVP_CIPHER * crypto_aes_128_gcm ;
4948static EVP_CIPHER * crypto_aes_256_gcm ;
5049static EVP_CIPHER * crypto_chacha20_poly1305 ;
@@ -57,57 +56,19 @@ static EVP_MD *crypto_sha384;
5756static EVP_KDF * crypto_hkdf ;
5857
5958int 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. */
6062 crypto_aes_128_gcm = EVP_CIPHER_fetch (NULL , "AES-128-GCM" , NULL );
61- if (crypto_aes_128_gcm == NULL ) {
62- return -1 ;
63- }
64-
6563 crypto_aes_256_gcm = EVP_CIPHER_fetch (NULL , "AES-256-GCM" , NULL );
66- if (crypto_aes_256_gcm == NULL ) {
67- return -1 ;
68- }
69-
7064 crypto_chacha20_poly1305 = EVP_CIPHER_fetch (NULL , "ChaCha20-Poly1305" , NULL );
71- if (crypto_chacha20_poly1305 == NULL ) {
72- return -1 ;
73- }
74-
7565 crypto_aes_128_ccm = EVP_CIPHER_fetch (NULL , "AES-128-CCM" , NULL );
76- if (crypto_aes_128_ccm == NULL ) {
77- return -1 ;
78- }
79-
8066 crypto_aes_128_ecb = EVP_CIPHER_fetch (NULL , "AES-128-ECB" , NULL );
81- if (crypto_aes_128_ecb == NULL ) {
82- return -1 ;
83- }
84-
8567 crypto_aes_256_ecb = EVP_CIPHER_fetch (NULL , "AES-256-ECB" , NULL );
86- if (crypto_aes_256_ecb == NULL ) {
87- return -1 ;
88- }
89-
9068 crypto_chacha20 = EVP_CIPHER_fetch (NULL , "ChaCha20" , NULL );
91- if (crypto_chacha20 == NULL ) {
92- return -1 ;
93- }
94-
9569 crypto_sha256 = EVP_MD_fetch (NULL , "sha256" , NULL );
96- if (crypto_sha256 == NULL ) {
97- return -1 ;
98- }
99-
10070 crypto_sha384 = EVP_MD_fetch (NULL , "sha384" , NULL );
101- if (crypto_sha384 == NULL ) {
102- return -1 ;
103- }
104-
10571 crypto_hkdf = EVP_KDF_fetch (NULL , "hkdf" , NULL );
106- if (crypto_hkdf == NULL ) {
107- return -1 ;
108- }
109-
110- crypto_initialized = 1 ;
11172
11273 return 0 ;
11374}
@@ -191,6 +152,12 @@ static EVP_KDF *crypto_kdf_hkdf(void) {
191152
192153 return EVP_KDF_fetch (NULL , "hkdf" , NULL );
193154}
155+
156+ static void crypto_kdf_hkdf_free (EVP_KDF * kdf ) {
157+ if (kdf && crypto_hkdf != kdf ) {
158+ EVP_KDF_free (kdf );
159+ }
160+ }
194161#else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
195162# define crypto_aead_aes_128_gcm EVP_aes_128_gcm
196163# 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,
524491 const uint8_t * salt , size_t saltlen ) {
525492#if OPENSSL_VERSION_NUMBER >= 0x30000000L
526493 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 ;
529496 int mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY ;
530497 OSSL_PARAM params [] = {
531498 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,
539506 };
540507 int rv = 0 ;
541508
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 ;
544518 }
545519
546520 if (EVP_KDF_derive (kctx , dest , (size_t )EVP_MD_size (prf ), params ) <= 0 ) {
547521 rv = -1 ;
548522 }
549523
550524 EVP_KDF_CTX_free (kctx );
525+ fail_kdf_ctx_new :
526+ crypto_kdf_hkdf_free (kdf );
551527
552528 return rv ;
553529#else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
@@ -581,8 +557,8 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
581557 size_t infolen ) {
582558#if OPENSSL_VERSION_NUMBER >= 0x30000000L
583559 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 ;
586562 int mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY ;
587563 OSSL_PARAM params [] = {
588564 OSSL_PARAM_construct_int (OSSL_KDF_PARAM_MODE , & mode ),
@@ -596,15 +572,24 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
596572 };
597573 int rv = 0 ;
598574
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 ;
601584 }
602585
603586 if (EVP_KDF_derive (kctx , dest , destlen , params ) <= 0 ) {
604587 rv = -1 ;
605588 }
606589
607590 EVP_KDF_CTX_free (kctx );
591+ fail_kdf_ctx_new :
592+ crypto_kdf_hkdf_free (kdf );
608593
609594 return rv ;
610595#else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
@@ -637,8 +622,8 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
637622 const uint8_t * info , size_t infolen ) {
638623#if OPENSSL_VERSION_NUMBER >= 0x30000000L
639624 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 ;
642627 OSSL_PARAM params [] = {
643628 OSSL_PARAM_construct_utf8_string (OSSL_KDF_PARAM_DIGEST ,
644629 (char * )EVP_MD_get0_name (prf ), 0 ),
@@ -652,15 +637,24 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
652637 };
653638 int rv = 0 ;
654639
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 ;
657649 }
658650
659651 if (EVP_KDF_derive (kctx , dest , destlen , params ) <= 0 ) {
660652 rv = -1 ;
661653 }
662654
663655 EVP_KDF_CTX_free (kctx );
656+ fail_kdf_ctx_new :
657+ crypto_kdf_hkdf_free (kdf );
664658
665659 return rv ;
666660#else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
0 commit comments