4141#include "ngtcp2_macro.h"
4242#include "shared.h"
4343
44- static int crypto_initialized ;
44+ #if defined(OPENSSL_NO_CHACHA ) || defined(OPENSSL_NO_POLY1305 )
45+ # define NGTCP2_NO_CHACHA_POLY1305
46+ #endif /* defined(OPENSSL_NO_CHACHA) || \
47+ defined(OPENSSL_NO_POLY1305) */
48+
4549static EVP_CIPHER * crypto_aes_128_gcm ;
4650static EVP_CIPHER * crypto_aes_256_gcm ;
47- static EVP_CIPHER * crypto_chacha20_poly1305 ;
4851static EVP_CIPHER * crypto_aes_128_ccm ;
4952static EVP_CIPHER * crypto_aes_128_ctr ;
5053static EVP_CIPHER * crypto_aes_256_ctr ;
54+ #ifndef NGTCP2_NO_CHACHA_POLY1305
55+ static EVP_CIPHER * crypto_chacha20_poly1305 ;
5156static EVP_CIPHER * crypto_chacha20 ;
57+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
5258static EVP_MD * crypto_sha256 ;
5359static EVP_MD * crypto_sha384 ;
5460static EVP_KDF * crypto_hkdf ;
5561
5662int ngtcp2_crypto_ossl_init (void ) {
63+ /* We do not care whether the pre-fetch succeeds or not. If it
64+ fails, it returns NULL, which is still the default value, and our
65+ code should still work with it. */
5766 crypto_aes_128_gcm = EVP_CIPHER_fetch (NULL , "AES-128-GCM" , NULL );
58- if (crypto_aes_128_gcm == NULL ) {
59- return -1 ;
60- }
61-
6267 crypto_aes_256_gcm = EVP_CIPHER_fetch (NULL , "AES-256-GCM" , NULL );
63- if (crypto_aes_256_gcm == NULL ) {
64- return -1 ;
65- }
66-
67- crypto_chacha20_poly1305 = EVP_CIPHER_fetch (NULL , "ChaCha20-Poly1305" , NULL );
68- if (crypto_chacha20_poly1305 == NULL ) {
69- return -1 ;
70- }
71-
7268 crypto_aes_128_ccm = EVP_CIPHER_fetch (NULL , "AES-128-CCM" , NULL );
73- if (crypto_aes_128_ccm == NULL ) {
74- return -1 ;
75- }
76-
7769 crypto_aes_128_ctr = EVP_CIPHER_fetch (NULL , "AES-128-CTR" , NULL );
78- if (crypto_aes_128_ctr == NULL ) {
79- return -1 ;
80- }
81-
8270 crypto_aes_256_ctr = EVP_CIPHER_fetch (NULL , "AES-256-CTR" , NULL );
83- if (crypto_aes_256_ctr == NULL ) {
84- return -1 ;
85- }
86-
71+ #ifndef NGTCP2_NO_CHACHA_POLY1305
72+ crypto_chacha20_poly1305 = EVP_CIPHER_fetch (NULL , "ChaCha20-Poly1305" , NULL );
8773 crypto_chacha20 = EVP_CIPHER_fetch (NULL , "ChaCha20" , NULL );
88- if (crypto_chacha20 == NULL ) {
89- return -1 ;
90- }
91-
74+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
9275 crypto_sha256 = EVP_MD_fetch (NULL , "sha256" , NULL );
93- if (crypto_sha256 == NULL ) {
94- return -1 ;
95- }
96-
9776 crypto_sha384 = EVP_MD_fetch (NULL , "sha384" , NULL );
98- if (crypto_sha384 == NULL ) {
99- return -1 ;
100- }
101-
10277 crypto_hkdf = EVP_KDF_fetch (NULL , "hkdf" , NULL );
103- if (crypto_hkdf == NULL ) {
104- return -1 ;
105- }
106-
107- crypto_initialized = 1 ;
10878
10979 return 0 ;
11080}
@@ -125,13 +95,15 @@ static const EVP_CIPHER *crypto_aead_aes_256_gcm(void) {
12595 return EVP_aes_256_gcm ();
12696}
12797
98+ #ifndef NGTCP2_NO_CHACHA_POLY1305
12899static const EVP_CIPHER * crypto_aead_chacha20_poly1305 (void ) {
129100 if (crypto_chacha20_poly1305 ) {
130101 return crypto_chacha20_poly1305 ;
131102 }
132103
133104 return EVP_chacha20_poly1305 ();
134105}
106+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
135107
136108static const EVP_CIPHER * crypto_aead_aes_128_ccm (void ) {
137109 if (crypto_aes_128_ccm ) {
@@ -157,13 +129,15 @@ static const EVP_CIPHER *crypto_cipher_aes_256_ctr(void) {
157129 return EVP_aes_256_ctr ();
158130}
159131
132+ #ifndef NGTCP2_NO_CHACHA_POLY1305
160133static const EVP_CIPHER * crypto_cipher_chacha20 (void ) {
161134 if (crypto_chacha20 ) {
162135 return crypto_chacha20 ;
163136 }
164137
165138 return EVP_chacha20 ();
166139}
140+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
167141
168142static const EVP_MD * crypto_md_sha256 (void ) {
169143 if (crypto_sha256 ) {
@@ -189,13 +163,21 @@ static EVP_KDF *crypto_kdf_hkdf(void) {
189163 return EVP_KDF_fetch (NULL , "hkdf" , NULL );
190164}
191165
166+ static void crypto_kdf_hkdf_free (EVP_KDF * kdf ) {
167+ if (kdf && crypto_hkdf != kdf ) {
168+ EVP_KDF_free (kdf );
169+ }
170+ }
171+
192172static size_t crypto_aead_max_overhead (const EVP_CIPHER * aead ) {
193173 switch (EVP_CIPHER_nid (aead )) {
194174 case NID_aes_128_gcm :
195175 case NID_aes_256_gcm :
196176 return EVP_GCM_TLS_TAG_LEN ;
177+ #ifndef NGTCP2_NO_CHACHA_POLY1305
197178 case NID_chacha20_poly1305 :
198179 return EVP_CHACHAPOLY_TLS_TAG_LEN ;
180+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
199181 case NID_aes_128_ccm :
200182 return EVP_CCM_TLS_TAG_LEN ;
201183 default :
@@ -239,8 +221,10 @@ static const EVP_CIPHER *crypto_cipher_id_get_aead(uint32_t cipher_id) {
239221 return crypto_aead_aes_128_gcm ();
240222 case TLS1_3_CK_AES_256_GCM_SHA384 :
241223 return crypto_aead_aes_256_gcm ();
224+ #ifndef NGTCP2_NO_CHACHA_POLY1305
242225 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
243226 return crypto_aead_chacha20_poly1305 ();
227+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
244228 case TLS1_3_CK_AES_128_CCM_SHA256 :
245229 return crypto_aead_aes_128_ccm ();
246230 default :
@@ -253,8 +237,10 @@ static uint64_t crypto_cipher_id_get_aead_max_encryption(uint32_t cipher_id) {
253237 case TLS1_3_CK_AES_128_GCM_SHA256 :
254238 case TLS1_3_CK_AES_256_GCM_SHA384 :
255239 return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM ;
240+ #ifndef NGTCP2_NO_CHACHA_POLY1305
256241 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
257242 return NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305 ;
243+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
258244 case TLS1_3_CK_AES_128_CCM_SHA256 :
259245 return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_CCM ;
260246 default :
@@ -268,8 +254,10 @@ crypto_cipher_id_get_aead_max_decryption_failure(uint32_t cipher_id) {
268254 case TLS1_3_CK_AES_128_GCM_SHA256 :
269255 case TLS1_3_CK_AES_256_GCM_SHA384 :
270256 return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM ;
257+ #ifndef NGTCP2_NO_CHACHA_POLY1305
271258 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
272259 return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305 ;
260+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
273261 case TLS1_3_CK_AES_128_CCM_SHA256 :
274262 return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_CCM ;
275263 default :
@@ -284,8 +272,10 @@ static const EVP_CIPHER *crypto_cipher_id_get_hp(uint32_t cipher_id) {
284272 return crypto_cipher_aes_128_ctr ();
285273 case TLS1_3_CK_AES_256_GCM_SHA384 :
286274 return crypto_cipher_aes_256_ctr ();
275+ #ifndef NGTCP2_NO_CHACHA_POLY1305
287276 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
288277 return crypto_cipher_chacha20 ();
278+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
289279 default :
290280 return NULL ;
291281 }
@@ -294,7 +284,9 @@ static const EVP_CIPHER *crypto_cipher_id_get_hp(uint32_t cipher_id) {
294284static const EVP_MD * crypto_cipher_id_get_md (uint32_t cipher_id ) {
295285 switch (cipher_id ) {
296286 case TLS1_3_CK_AES_128_GCM_SHA256 :
287+ #ifndef NGTCP2_NO_CHACHA_POLY1305
297288 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
289+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
298290 case TLS1_3_CK_AES_128_CCM_SHA256 :
299291 return crypto_md_sha256 ();
300292 case TLS1_3_CK_AES_256_GCM_SHA384 :
@@ -308,7 +300,9 @@ static int supported_cipher_id(uint32_t cipher_id) {
308300 switch (cipher_id ) {
309301 case TLS1_3_CK_AES_128_GCM_SHA256 :
310302 case TLS1_3_CK_AES_256_GCM_SHA384 :
303+ #ifndef NGTCP2_NO_CHACHA_POLY1305
311304 case TLS1_3_CK_CHACHA20_POLY1305_SHA256 :
305+ #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */
312306 case TLS1_3_CK_AES_128_CCM_SHA256 :
313307 return 1 ;
314308 default :
@@ -697,9 +691,7 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md,
697691 };
698692 int rv = 0 ;
699693
700- if (!crypto_initialized ) {
701- EVP_KDF_free (kdf );
702- }
694+ crypto_kdf_hkdf_free (kdf );
703695
704696 if (EVP_KDF_derive (kctx , dest , (size_t )EVP_MD_size (prf ), params ) <= 0 ) {
705697 rv = -1 ;
@@ -730,9 +722,7 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
730722 };
731723 int rv = 0 ;
732724
733- if (!crypto_initialized ) {
734- EVP_KDF_free (kdf );
735- }
725+ crypto_kdf_hkdf_free (kdf );
736726
737727 if (EVP_KDF_derive (kctx , dest , destlen , params ) <= 0 ) {
738728 rv = -1 ;
@@ -763,9 +753,7 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
763753 };
764754 int rv = 0 ;
765755
766- if (!crypto_initialized ) {
767- EVP_KDF_free (kdf );
768- }
756+ crypto_kdf_hkdf_free (kdf );
769757
770758 if (EVP_KDF_derive (kctx , dest , destlen , params ) <= 0 ) {
771759 rv = -1 ;
0 commit comments