s2n_cipher_suites.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <stdint.h>
  17. #include "crypto/s2n_certificate.h"
  18. #include "crypto/s2n_cipher.h"
  19. #include "crypto/s2n_hmac.h"
  20. #include "tls/s2n_connection.h"
  21. #include "tls/s2n_crypto.h"
  22. #include "tls/s2n_kem_preferences.h"
  23. #include "tls/s2n_tls_parameters.h"
  24. /* Key exchange flags that can be OR'ed */
  25. #define S2N_KEY_EXCHANGE_DH 0x01 /* Diffie-Hellman key exchange, including ephemeral */
  26. #define S2N_KEY_EXCHANGE_EPH 0x02 /* Ephemeral key exchange */
  27. #define S2N_KEY_EXCHANGE_ECC 0x04 /* Elliptic curve cryptography */
  28. #define S2N_MAX_POSSIBLE_RECORD_ALGS 2
  29. /* Kept up-to-date by s2n_cipher_suite_test */
  30. #define S2N_CIPHER_SUITE_COUNT 37
  31. /* Record algorithm flags that can be OR'ed */
  32. #define S2N_TLS12_AES_GCM_AEAD_NONCE 0x01
  33. #define S2N_TLS12_CHACHA_POLY_AEAD_NONCE 0x02
  34. #define S2N_TLS13_RECORD_AEAD_NONCE 0x04
  35. /* From RFC: https://tools.ietf.org/html/rfc8446#section-5.5
  36. * For AES-GCM, up to 2^24.5 full-size records (about 24 million) may be
  37. * encrypted on a given connection while keeping a safety margin of
  38. * approximately 2^-57 for Authenticated Encryption (AE) security.
  39. * S2N_TLS13_MAXIMUM_RECORD_NUMBER is 2^24.5 rounded down to the nearest whole number
  40. * minus 1 for the key update message.
  41. */
  42. #define S2N_TLS13_AES_GCM_MAXIMUM_RECORD_NUMBER ((uint64_t) 23726565)
  43. typedef enum {
  44. S2N_AUTHENTICATION_RSA = 0,
  45. S2N_AUTHENTICATION_ECDSA,
  46. S2N_AUTHENTICATION_METHOD_SENTINEL
  47. } s2n_authentication_method;
  48. /* Used by TLS 1.3 CipherSuites (Eg TLS_AES_128_GCM_SHA256 "0x1301") where the Auth method will be specified by the
  49. * SignatureScheme Extension, not the CipherSuite. */
  50. #define S2N_AUTHENTICATION_METHOD_TLS13 S2N_AUTHENTICATION_METHOD_SENTINEL
  51. struct s2n_record_algorithm {
  52. const struct s2n_cipher *cipher;
  53. s2n_hmac_algorithm hmac_alg;
  54. uint32_t flags;
  55. uint64_t encryption_limit;
  56. };
  57. /* Verbose names to avoid confusion with s2n_cipher. Exposed for unit tests */
  58. extern const struct s2n_record_algorithm s2n_record_alg_null;
  59. extern const struct s2n_record_algorithm s2n_record_alg_rc4_md5;
  60. extern const struct s2n_record_algorithm s2n_record_alg_rc4_sha;
  61. extern const struct s2n_record_algorithm s2n_record_alg_3des_sha;
  62. extern const struct s2n_record_algorithm s2n_record_alg_aes128_sha;
  63. extern const struct s2n_record_algorithm s2n_record_alg_aes128_sha_composite;
  64. extern const struct s2n_record_algorithm s2n_record_alg_aes128_sha256;
  65. extern const struct s2n_record_algorithm s2n_record_alg_aes128_sha256_composite;
  66. extern const struct s2n_record_algorithm s2n_record_alg_aes256_sha;
  67. extern const struct s2n_record_algorithm s2n_record_alg_aes256_sha_composite;
  68. extern const struct s2n_record_algorithm s2n_record_alg_aes256_sha256;
  69. extern const struct s2n_record_algorithm s2n_record_alg_aes256_sha256_composite;
  70. extern const struct s2n_record_algorithm s2n_record_alg_aes256_sha384;
  71. extern const struct s2n_record_algorithm s2n_record_alg_aes128_gcm;
  72. extern const struct s2n_record_algorithm s2n_record_alg_aes256_gcm;
  73. extern const struct s2n_record_algorithm s2n_record_alg_chacha20_poly1305;
  74. extern const struct s2n_record_algorithm s2n_tls13_record_alg_aes128_gcm;
  75. extern const struct s2n_record_algorithm s2n_tls13_record_alg_chacha20_poly1305;
  76. struct s2n_cipher_suite {
  77. /* Is there an implementation available? Set in s2n_cipher_suites_init() */
  78. unsigned int available : 1;
  79. /* Cipher name in Openssl format */
  80. const char *name;
  81. const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN];
  82. const struct s2n_kex *key_exchange_alg;
  83. const s2n_authentication_method auth_method;
  84. /* Algorithms used for per-record security. Set in s2n_cipher_suites_init() */
  85. const struct s2n_record_algorithm *record_alg;
  86. /* List of all possible record alg implementations in descending priority */
  87. const struct s2n_record_algorithm *all_record_algs[S2N_MAX_POSSIBLE_RECORD_ALGS];
  88. const uint8_t num_record_algs;
  89. /* SSLv3 utilizes HMAC differently from TLS */
  90. const struct s2n_record_algorithm *sslv3_record_alg;
  91. struct s2n_cipher_suite *sslv3_cipher_suite;
  92. /* RFC 5426(TLS1.2) allows cipher suite defined PRFs. Cipher suites defined in and before TLS1.2 will use
  93. * P_hash with SHA256 when TLS1.2 is negotiated.
  94. */
  95. const s2n_hmac_algorithm prf_alg;
  96. const uint8_t minimum_required_tls_version;
  97. };
  98. /* Never negotiated */
  99. extern struct s2n_cipher_suite s2n_null_cipher_suite;
  100. extern struct s2n_cipher_suite s2n_rsa_with_rc4_128_md5;
  101. extern struct s2n_cipher_suite s2n_rsa_with_rc4_128_sha;
  102. extern struct s2n_cipher_suite s2n_rsa_with_3des_ede_cbc_sha;
  103. extern struct s2n_cipher_suite s2n_dhe_rsa_with_3des_ede_cbc_sha;
  104. extern struct s2n_cipher_suite s2n_rsa_with_aes_128_cbc_sha;
  105. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_cbc_sha;
  106. extern struct s2n_cipher_suite s2n_rsa_with_aes_256_cbc_sha;
  107. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_cbc_sha;
  108. extern struct s2n_cipher_suite s2n_rsa_with_aes_128_cbc_sha256;
  109. extern struct s2n_cipher_suite s2n_rsa_with_aes_256_cbc_sha256;
  110. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_cbc_sha256;
  111. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_cbc_sha256;
  112. extern struct s2n_cipher_suite s2n_rsa_with_aes_128_gcm_sha256;
  113. extern struct s2n_cipher_suite s2n_rsa_with_aes_256_gcm_sha384;
  114. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_gcm_sha256;
  115. extern struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_gcm_sha384;
  116. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_cbc_sha;
  117. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_cbc_sha;
  118. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_3des_ede_cbc_sha;
  119. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_cbc_sha;
  120. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_cbc_sha;
  121. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256;
  122. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384;
  123. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_cbc_sha256;
  124. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_cbc_sha384;
  125. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
  126. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384;
  127. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_gcm_sha256;
  128. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_gcm_sha384;
  129. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_chacha20_poly1305_sha256;
  130. extern struct s2n_cipher_suite s2n_dhe_rsa_with_chacha20_poly1305_sha256;
  131. extern struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256;
  132. extern struct s2n_cipher_suite s2n_ecdhe_rsa_with_rc4_128_sha;
  133. extern struct s2n_cipher_suite s2n_ecdhe_kyber_rsa_with_aes_256_gcm_sha384;
  134. extern struct s2n_cipher_suite s2n_tls13_aes_256_gcm_sha384;
  135. extern struct s2n_cipher_suite s2n_tls13_aes_128_gcm_sha256;
  136. extern struct s2n_cipher_suite s2n_tls13_chacha20_poly1305_sha256;
  137. int s2n_cipher_suites_init(void);
  138. S2N_RESULT s2n_cipher_suites_cleanup(void);
  139. S2N_RESULT s2n_cipher_suite_from_iana(const uint8_t *iana, size_t iana_len, struct s2n_cipher_suite **cipher_suite);
  140. bool s2n_cipher_suite_uses_chacha20_alg(struct s2n_cipher_suite *cipher_suite);
  141. int s2n_set_cipher_as_client(struct s2n_connection *conn, uint8_t wire[S2N_TLS_CIPHER_SUITE_LEN]);
  142. int s2n_set_cipher_as_sslv2_server(struct s2n_connection *conn, uint8_t *wire, uint16_t count);
  143. int s2n_set_cipher_as_tls_server(struct s2n_connection *conn, uint8_t *wire, uint16_t count);
  144. bool s2n_cipher_suite_requires_ecc_extension(struct s2n_cipher_suite *cipher);
  145. bool s2n_cipher_suite_requires_pq_extension(struct s2n_cipher_suite *cipher);