s2n_kem_preferences.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "tls/s2n_kem_preferences.h"
  16. const struct s2n_kem *pq_kems_r3_2021_05[] = {
  17. /* Round 3 Algorithms */
  18. &s2n_kyber_512_r3,
  19. };
  20. const struct s2n_kem_group *pq_kem_groups_r3_2021_05[] = {
  21. #if EVP_APIS_SUPPORTED
  22. &s2n_x25519_kyber_512_r3,
  23. #endif
  24. &s2n_secp256r1_kyber_512_r3,
  25. };
  26. const struct s2n_kem_group *pq_kem_groups_r3_2023_06[] = {
  27. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  28. &s2n_secp256r1_kyber_768_r3,
  29. #if EVP_APIS_SUPPORTED
  30. &s2n_x25519_kyber_768_r3,
  31. #endif
  32. &s2n_secp384r1_kyber_768_r3,
  33. &s2n_secp521r1_kyber_1024_r3,
  34. #endif
  35. &s2n_secp256r1_kyber_512_r3,
  36. #if EVP_APIS_SUPPORTED
  37. &s2n_x25519_kyber_512_r3,
  38. #endif
  39. };
  40. const struct s2n_kem_preferences kem_preferences_pq_tls_1_0_2021_05 = {
  41. .kem_count = s2n_array_len(pq_kems_r3_2021_05),
  42. .kems = pq_kems_r3_2021_05,
  43. .tls13_kem_group_count = s2n_array_len(pq_kem_groups_r3_2021_05),
  44. .tls13_kem_groups = pq_kem_groups_r3_2021_05,
  45. .tls13_pq_hybrid_draft_revision = 0
  46. };
  47. const struct s2n_kem_preferences kem_preferences_pq_tls_1_0_2023_01 = {
  48. .kem_count = s2n_array_len(pq_kems_r3_2021_05),
  49. .kems = pq_kems_r3_2021_05,
  50. .tls13_kem_group_count = s2n_array_len(pq_kem_groups_r3_2021_05),
  51. .tls13_kem_groups = pq_kem_groups_r3_2021_05,
  52. .tls13_pq_hybrid_draft_revision = 5
  53. };
  54. /* TLS 1.3 specifies KEMS via SupportedGroups extension, not TLS 1.2's KEM-specific extension. */
  55. const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_2023_06 = {
  56. .kem_count = 0,
  57. .kems = NULL,
  58. .tls13_kem_group_count = s2n_array_len(pq_kem_groups_r3_2023_06),
  59. .tls13_kem_groups = pq_kem_groups_r3_2023_06,
  60. .tls13_pq_hybrid_draft_revision = 5
  61. };
  62. const struct s2n_kem_preferences kem_preferences_all = {
  63. .kem_count = s2n_array_len(pq_kems_r3_2021_05),
  64. .kems = pq_kems_r3_2021_05,
  65. .tls13_kem_group_count = s2n_array_len(pq_kem_groups_r3_2023_06),
  66. .tls13_kem_groups = pq_kem_groups_r3_2023_06,
  67. .tls13_pq_hybrid_draft_revision = 5
  68. };
  69. const struct s2n_kem_preferences kem_preferences_null = {
  70. .kem_count = 0,
  71. .kems = NULL,
  72. .tls13_kem_group_count = 0,
  73. .tls13_kem_groups = NULL,
  74. .tls13_pq_hybrid_draft_revision = 0
  75. };
  76. /* Determines if query_iana_id corresponds to a tls13_kem_group for these KEM preferences. */
  77. bool s2n_kem_preferences_includes_tls13_kem_group(const struct s2n_kem_preferences *kem_preferences,
  78. uint16_t query_iana_id)
  79. {
  80. if (kem_preferences == NULL) {
  81. return false;
  82. }
  83. for (size_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) {
  84. if (query_iana_id == kem_preferences->tls13_kem_groups[i]->iana_id) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. /* Whether the client must include the length prefix in the PQ TLS 1.3 KEM KeyShares that it sends. Draft 0 of
  91. * the PQ TLS 1.3 standard required length prefixing, and drafts 1-5 removed this length prefix. To not break
  92. * backwards compatibility, we check what revision of the draft standard is configured to determine whether to send it. */
  93. bool s2n_tls13_client_must_use_hybrid_kem_length_prefix(const struct s2n_kem_preferences *kem_pref)
  94. {
  95. return kem_pref && (kem_pref->tls13_pq_hybrid_draft_revision == 0);
  96. }