@@ -218,11 +218,12 @@ const ngtcp2_crypto_ctx* GetCryptoContext(ngtcp2_conn* conn, SSL* ssl) {
218
218
return ctx;
219
219
}
220
220
221
- const ngtcp2_crypto_ctx* GetInitialCryptoContext (ngtcp2_conn* conn {
221
+ const ngtcp2_crypto_ctx* GetInitialCryptoContext (ngtcp2_conn* conn) {
222
222
const ngtcp2_crypto_ctx* ctx = ngtcp2_conn_get_initial_crypto_ctx (conn);
223
223
// ctx will always be non-null, so check members
224
224
if (ctx->aead .native_handle == nullptr ) {
225
225
ngtcp2_crypto_ctx context;
226
+ ngtcp2_crypto_ctx_initial (&context);
226
227
ngtcp2_conn_set_initial_crypto_ctx (conn, &context);
227
228
ctx = ngtcp2_conn_get_initial_crypto_ctx (conn);
228
229
}
@@ -324,32 +325,62 @@ bool Encrypt(
324
325
size_t taglen = aead_tag_length (aead);
325
326
const EVP_CIPHER* cipher =
326
327
static_cast <const EVP_CIPHER*>(aead->native_handle );
327
- DCHECK_NOT_NULL (cipher);
328
+ CHECK_NOT_NULL (cipher);
329
+
328
330
329
331
CipherCtxPointer actx (EVP_CIPHER_CTX_new ());
330
332
CHECK (actx);
331
333
332
- size_t outlen = 0 ;
333
334
int len;
334
335
335
- if (EVP_EncryptInit_ex (actx.get (), cipher, nullptr , nullptr , nullptr ) != 1 ||
336
- EVP_CIPHER_CTX_ctrl (actx.get (), EVP_CTRL_AEAD_SET_IVLEN,
337
- noncelen, nullptr ) != 1 ) ||
338
- EVP_EncryptInit_ex (actx.get (), nullptr , nullptr , key, nonce) != 1 ||
339
- EVP_EncryptUpdate (actx.get (), nullptr , &len, ad, adlen) != 1 ||
340
- EVP_EncryptUpdate (actx.get (), dest, &len, plaintext, plaintextlen) != 1 ) {
336
+ if (EVP_EncryptInit_ex (
337
+ actx.get (),
338
+ cipher,
339
+ nullptr ,
340
+ nullptr ,
341
+ nullptr ) != 1 ||
342
+ EVP_CIPHER_CTX_ctrl (
343
+ actx.get (),
344
+ EVP_CTRL_AEAD_SET_IVLEN,
345
+ noncelen,
346
+ nullptr ) != 1 ||
347
+ EVP_EncryptInit_ex (
348
+ actx.get (),
349
+ nullptr ,
350
+ nullptr ,
351
+ key,
352
+ nonce) != 1 ||
353
+ EVP_EncryptUpdate (
354
+ actx.get (),
355
+ nullptr ,
356
+ &len,
357
+ ad,
358
+ adlen) != 1 ||
359
+ EVP_EncryptUpdate (
360
+ actx.get (),
361
+ dest,
362
+ &len,
363
+ plaintext,
364
+ plaintextlen) != 1 ) {
341
365
return false ;
342
366
}
343
367
344
- outlen = len;
368
+ dest + = len;
345
369
346
- if (EVP_EncryptFinal_ex (actx.get (), dest + outlen, &len) != 1 )
370
+ if (EVP_EncryptFinal_ex (
371
+ actx.get (),
372
+ dest,
373
+ &len) != 1 ) {
347
374
return false ;
375
+ }
348
376
349
- outlen += len;
377
+ dest += len;
350
378
351
- if (EVP_CIPHER_CTX_ctrl (actx.get (), EVP_CTRL_AEAD_GET_TAG, taglen,
352
- dest + outlen) != 1 ) {
379
+ if (EVP_CIPHER_CTX_ctrl (
380
+ actx.get (),
381
+ EVP_CTRL_AEAD_GET_TAG,
382
+ taglen,
383
+ dest) != 1 ) {
353
384
return false ;
354
385
}
355
386
@@ -535,6 +566,11 @@ bool DerivePacketProtectionKey(
535
566
static const uint8_t KEY_LABEL[] = " quic key" ;
536
567
static const uint8_t IV_LABEL[] = " quic iv" ;
537
568
static const uint8_t HP_KEY_LABEL[] = " quic hp" ;
569
+
570
+ CHECK_NOT_NULL (ctx);
571
+ CHECK_NOT_NULL (ctx->aead .native_handle );
572
+ CHECK_NOT_NULL (ctx->md .native_handle );
573
+
538
574
size_t keylen = aead_key_length (&ctx->aead );
539
575
size_t ivlen = packet_protection_ivlen (ctx);
540
576
@@ -569,9 +605,10 @@ bool DerivePacketProtectionKey(
569
605
// TODO(@jasnell): Replace with ngtcp2_crypto_derive_and_install_initial_key
570
606
// once we move to ngtcp2_crypto
571
607
bool DeriveAndInstallInitialKey (
572
- ngtcp2_conn* conn,
573
- const ngtcp2_cid* dcid,
574
- ngtcp2_crypto_side side) {
608
+ ngtcp2_conn* conn,
609
+ const ngtcp2_crypto_ctx* ctx,
610
+ const ngtcp2_cid* dcid,
611
+ ngtcp2_crypto_side side) {
575
612
InitialSecret rx_secret;
576
613
InitialSecret tx_secret;
577
614
InitialKey rx_key;
@@ -581,8 +618,7 @@ bool DeriveAndInstallInitialKey(
581
618
InitialKey rx_hp;
582
619
InitialKey tx_hp;
583
620
584
- ngtcp2_crypto_ctx ctx;
585
- ngtcp2_crypto_ctx_initial (&ctx);
621
+ CHECK_NOT_NULL (ctx);
586
622
587
623
return
588
624
DeriveInitialSecrets (
@@ -595,14 +631,14 @@ bool DeriveAndInstallInitialKey(
595
631
rx_key.data (),
596
632
rx_iv.data (),
597
633
rx_hp.data (),
598
- & ctx,
634
+ ctx,
599
635
rx_secret.data (),
600
636
NGTCP2_CRYPTO_INITIAL_SECRETLEN) &&
601
637
DerivePacketProtectionKey (
602
638
tx_key.data (),
603
639
tx_iv.data (),
604
640
tx_hp.data (),
605
- & ctx,
641
+ ctx,
606
642
tx_secret.data (),
607
643
NGTCP2_CRYPTO_INITIAL_SECRETLEN) &&
608
644
ngtcp2_conn_install_initial_key (
@@ -1254,7 +1290,6 @@ bool GenerateRetryToken(
1254
1290
ngtcp2_crypto_ctx_initial (&ctx);
1255
1291
1256
1292
const size_t addrlen = SocketAddress::GetAddressLen (addr);
1257
- size_t keylen = aead_key_length (&ctx.aead );
1258
1293
size_t ivlen = packet_protection_ivlen (&ctx);
1259
1294
1260
1295
uint64_t now = uv_hrtime ();
@@ -1312,7 +1347,6 @@ bool InvalidRetryToken(
1312
1347
ngtcp2_crypto_ctx ctx;
1313
1348
ngtcp2_crypto_ctx_initial (&ctx);
1314
1349
1315
- size_t keylen = aead_key_length (&ctx.aead );
1316
1350
size_t ivlen = packet_protection_ivlen (&ctx);
1317
1351
const size_t addrlen = SocketAddress::GetAddressLen (addr);
1318
1352
0 commit comments