@@ -377,8 +377,8 @@ static int s2n_aead_cipher_aes_gcm_destroy_key(struct s2n_session_key *key)
377
377
378
378
#endif
379
379
380
- static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_ktls_info ( struct s2n_ktls_crypto_info_inputs * in ,
381
- struct s2n_ktls_crypto_info * out )
380
+ static S2N_RESULT s2n_tls12_aead_cipher_aes128_gcm_set_ktls_info (
381
+ struct s2n_ktls_crypto_info_inputs * in , struct s2n_ktls_crypto_info * out )
382
382
{
383
383
RESULT_ENSURE_REF (in );
384
384
RESULT_ENSURE_REF (out );
@@ -389,29 +389,48 @@ static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_ktls_info(struct s2n_ktls_crypt
389
389
390
390
RESULT_ENSURE_LTE (sizeof (crypto_info -> key ), in -> key .size );
391
391
RESULT_CHECKED_MEMCPY (crypto_info -> key , in -> key .data , sizeof (crypto_info -> key ));
392
-
393
- RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), in -> iv .size );
394
- RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> iv .data , sizeof (crypto_info -> iv ));
395
-
396
392
RESULT_ENSURE_LTE (sizeof (crypto_info -> rec_seq ), in -> seq .size );
397
393
RESULT_CHECKED_MEMCPY (crypto_info -> rec_seq , in -> seq .data , sizeof (crypto_info -> rec_seq ));
398
394
399
- /* The salt is a prefix of the IV
395
+ /* TLS1.2 uses partially explicit nonces. That means that although part of the
396
+ * nonce is still fixed and implicit (the salt), the remainder is explicit
397
+ * (written into the record) and must be unique per record. The RFC5288 suggests
398
+ * using the sequence number as the explicit part.
399
+ *
400
+ * Therefore, ktls expects the salt to contain the iv derived from the secret
401
+ * and should generate the remainder of the nonce per-record.
400
402
*
401
- *= https://www.rfc-editor.org/rfc/rfc4106#section-4
402
- *# The salt field is a four-octet value that is assigned at the
403
- *# beginning of the security association, and then remains constant
404
- *# for the life of the security association.
403
+ * See the TLS1.2 RFC:
404
+ * - https://datatracker.ietf.org/doc/html/rfc5246#section-6.2.3.3
405
+ * And RFC5288, which defines the TLS1.2 AES-GCM cipher suites:
406
+ * - https://datatracker.ietf.org/doc/html/rfc5288#section-3
405
407
*/
406
408
RESULT_ENSURE_LTE (sizeof (crypto_info -> salt ), in -> iv .size );
407
409
RESULT_CHECKED_MEMCPY (crypto_info -> salt , in -> iv .data , sizeof (crypto_info -> salt ));
408
410
411
+ /* Because TLS1.2 uses partially explicit nonces, the kernel should not
412
+ * use the iv in crypto_info but instead use a unique value for each record.
413
+ *
414
+ * As of this commit, Openssl has chosen to set the TLS1.2 IV to random
415
+ * bytes when sending and all zeroes when receiving:
416
+ * https://github.com/openssl/openssl/blob/de8e0851a1c0d22533801f081781a9f0be56c2c2/ssl/record/methods/ktls_meth.c#L197-L204
417
+ * And GnuTLS has chosen to set the TLS1.2 IV to the sequence number:
418
+ * https://github.com/gnutls/gnutls/blob/3f42ae70a1672673cb8f27c2dd3da1a34d1cbdd7/lib/system/ktls.c#L547-L550
419
+ *
420
+ * We (fairly arbitrarily) choose to also set it to the current sequence number.
421
+ */
422
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), in -> seq .size );
423
+ RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> seq .data , sizeof (crypto_info -> iv ));
424
+
409
425
RESULT_GUARD_POSIX (s2n_blob_init (& out -> value , (uint8_t * ) (void * ) crypto_info ,
410
426
sizeof (s2n_ktls_crypto_info_tls12_aes_gcm_128 )));
411
427
return S2N_RESULT_OK ;
412
428
}
413
429
414
- static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_ktls_info (
430
+ /* TLS1.2 AES256 is configured like TLS1.2 AES128, but with a larger key size.
431
+ * See TLS1.2 AES128 for details (particularly a discussion of salt + iv).
432
+ */
433
+ static S2N_RESULT s2n_tls12_aead_cipher_aes256_gcm_set_ktls_info (
415
434
struct s2n_ktls_crypto_info_inputs * in , struct s2n_ktls_crypto_info * out )
416
435
{
417
436
RESULT_ENSURE_REF (in );
@@ -423,22 +442,78 @@ static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_ktls_info(
423
442
424
443
RESULT_ENSURE_LTE (sizeof (crypto_info -> key ), in -> key .size );
425
444
RESULT_CHECKED_MEMCPY (crypto_info -> key , in -> key .data , sizeof (crypto_info -> key ));
445
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> rec_seq ), in -> seq .size );
446
+ RESULT_CHECKED_MEMCPY (crypto_info -> rec_seq , in -> seq .data , sizeof (crypto_info -> rec_seq ));
447
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> salt ), in -> iv .size );
448
+ RESULT_CHECKED_MEMCPY (crypto_info -> salt , in -> iv .data , sizeof (crypto_info -> salt ));
449
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), in -> seq .size );
450
+ RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> seq .data , sizeof (crypto_info -> iv ));
426
451
427
- RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), in -> iv .size );
428
- RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> iv .data , sizeof (crypto_info -> iv ));
452
+ RESULT_GUARD_POSIX (s2n_blob_init (& out -> value , (uint8_t * ) (void * ) crypto_info ,
453
+ sizeof (s2n_ktls_crypto_info_tls12_aes_gcm_256 )));
454
+ return S2N_RESULT_OK ;
455
+ }
456
+
457
+ static S2N_RESULT s2n_tls13_aead_cipher_aes128_gcm_set_ktls_info (
458
+ struct s2n_ktls_crypto_info_inputs * in , struct s2n_ktls_crypto_info * out )
459
+ {
460
+ RESULT_ENSURE_REF (in );
461
+ RESULT_ENSURE_REF (out );
462
+
463
+ s2n_ktls_crypto_info_tls12_aes_gcm_128 * crypto_info = & out -> ciphers .aes_gcm_128 ;
464
+ crypto_info -> info .version = TLS_1_3_VERSION ;
465
+ crypto_info -> info .cipher_type = TLS_CIPHER_AES_GCM_128 ;
429
466
467
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> key ), in -> key .size );
468
+ RESULT_CHECKED_MEMCPY (crypto_info -> key , in -> key .data , sizeof (crypto_info -> key ));
430
469
RESULT_ENSURE_LTE (sizeof (crypto_info -> rec_seq ), in -> seq .size );
431
470
RESULT_CHECKED_MEMCPY (crypto_info -> rec_seq , in -> seq .data , sizeof (crypto_info -> rec_seq ));
432
471
433
- /* The salt is a prefix of the IV
472
+ /* TLS1.3 uses fully implicit nonces. The fixed, implicit IV value derived from
473
+ * the secret is xored with the sequence number to produce a unique per-record nonce.
434
474
*
435
- *= https://www.rfc-editor.org/rfc/rfc4106#section-4
436
- *# The salt field is a four-octet value that is assigned at the
437
- *# beginning of the security association, and then remains constant
438
- *# for the life of the security association.
475
+ * See the TLS1.3 RFC:
476
+ * - https://www.rfc-editor.org/rfc/rfc8446.html#section-5.3
477
+ *
478
+ * ktls handles this with the same structure as TLS1.2 uses for its partially
479
+ * explicit nonces by splitting the implicit IV between the salt and iv fields.
439
480
*/
440
- RESULT_ENSURE_LTE (sizeof (crypto_info -> salt ), in -> iv .size );
441
- RESULT_CHECKED_MEMCPY (crypto_info -> salt , in -> iv .data , sizeof (crypto_info -> salt ));
481
+ size_t salt_size = sizeof (crypto_info -> salt );
482
+ RESULT_ENSURE_LTE (salt_size , in -> iv .size );
483
+ RESULT_CHECKED_MEMCPY (crypto_info -> salt , in -> iv .data , salt_size );
484
+ size_t iv_remainder = in -> iv .size - salt_size ;
485
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), iv_remainder );
486
+ RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> iv .data + salt_size , sizeof (crypto_info -> iv ));
487
+
488
+ RESULT_GUARD_POSIX (s2n_blob_init (& out -> value , (uint8_t * ) (void * ) crypto_info ,
489
+ sizeof (s2n_ktls_crypto_info_tls12_aes_gcm_128 )));
490
+ return S2N_RESULT_OK ;
491
+ }
492
+
493
+ /* TLS1.3 AES256 is configured like TLS1.3 AES128, but with a larger key size.
494
+ * See TLS1.3 AES128 for details (particularly a discussion of salt + iv).
495
+ */
496
+ static S2N_RESULT s2n_tls13_aead_cipher_aes256_gcm_set_ktls_info (
497
+ struct s2n_ktls_crypto_info_inputs * in , struct s2n_ktls_crypto_info * out )
498
+ {
499
+ RESULT_ENSURE_REF (in );
500
+ RESULT_ENSURE_REF (out );
501
+
502
+ s2n_ktls_crypto_info_tls12_aes_gcm_256 * crypto_info = & out -> ciphers .aes_gcm_256 ;
503
+ crypto_info -> info .version = TLS_1_3_VERSION ;
504
+ crypto_info -> info .cipher_type = TLS_CIPHER_AES_GCM_256 ;
505
+
506
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> key ), in -> key .size );
507
+ RESULT_CHECKED_MEMCPY (crypto_info -> key , in -> key .data , sizeof (crypto_info -> key ));
508
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> rec_seq ), in -> seq .size );
509
+ RESULT_CHECKED_MEMCPY (crypto_info -> rec_seq , in -> seq .data , sizeof (crypto_info -> rec_seq ));
510
+
511
+ size_t salt_size = sizeof (crypto_info -> salt );
512
+ RESULT_ENSURE_LTE (salt_size , in -> iv .size );
513
+ RESULT_CHECKED_MEMCPY (crypto_info -> salt , in -> iv .data , salt_size );
514
+ size_t iv_remainder = in -> iv .size - salt_size ;
515
+ RESULT_ENSURE_LTE (sizeof (crypto_info -> iv ), iv_remainder );
516
+ RESULT_CHECKED_MEMCPY (crypto_info -> iv , in -> iv .data + salt_size , sizeof (crypto_info -> iv ));
442
517
443
518
RESULT_GUARD_POSIX (s2n_blob_init (& out -> value , (uint8_t * ) (void * ) crypto_info ,
444
519
sizeof (s2n_ktls_crypto_info_tls12_aes_gcm_256 )));
@@ -459,7 +534,7 @@ const struct s2n_cipher s2n_aes128_gcm = {
459
534
.set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key ,
460
535
.set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key ,
461
536
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key ,
462
- .set_ktls_info = s2n_aead_cipher_aes128_gcm_set_ktls_info ,
537
+ .set_ktls_info = s2n_tls12_aead_cipher_aes128_gcm_set_ktls_info ,
463
538
};
464
539
465
540
const struct s2n_cipher s2n_aes256_gcm = {
@@ -476,7 +551,7 @@ const struct s2n_cipher s2n_aes256_gcm = {
476
551
.set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key ,
477
552
.set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key ,
478
553
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key ,
479
- .set_ktls_info = s2n_aead_cipher_aes256_gcm_set_ktls_info ,
554
+ .set_ktls_info = s2n_tls12_aead_cipher_aes256_gcm_set_ktls_info ,
480
555
};
481
556
482
557
/* TLS 1.3 GCM ciphers */
@@ -494,6 +569,7 @@ const struct s2n_cipher s2n_tls13_aes128_gcm = {
494
569
.set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13 ,
495
570
.set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13 ,
496
571
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key ,
572
+ .set_ktls_info = s2n_tls13_aead_cipher_aes128_gcm_set_ktls_info ,
497
573
};
498
574
499
575
const struct s2n_cipher s2n_tls13_aes256_gcm = {
@@ -510,4 +586,5 @@ const struct s2n_cipher s2n_tls13_aes256_gcm = {
510
586
.set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13 ,
511
587
.set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13 ,
512
588
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key ,
589
+ .set_ktls_info = s2n_tls13_aead_cipher_aes256_gcm_set_ktls_info ,
513
590
};
0 commit comments