Branch data Line data Source code
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 : : 16 : : #include "tls/s2n_kem_preferences.h" 17 : : 18 : : #include "tls/s2n_kem.h" 19 : : 20 : : /* Includes only IETF standard KEM Groups. */ 21 : : const struct s2n_kem_group *pq_kem_groups_ietf_2024_10[] = { 22 : : &s2n_x25519_mlkem_768, 23 : : &s2n_secp256r1_mlkem_768, 24 : : }; 25 : : 26 : : const struct s2n_kem_group *pq_kem_groups_ietf_2025_07[] = { 27 : : &s2n_x25519_mlkem_768, 28 : : &s2n_secp256r1_mlkem_768, 29 : : &s2n_secp384r1_mlkem_1024, 30 : : }; 31 : : 32 : : const struct s2n_kem_group *pq_kem_groups_cnsa2_2026_02[] = { 33 : : &s2n_pure_mlkem_1024, 34 : : }; 35 : : 36 : : const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_ietf_2024_10 = { 37 : : .kem_count = 0, 38 : : .kems = NULL, 39 : : .tls13_kem_group_count = s2n_array_len(pq_kem_groups_ietf_2024_10), 40 : : .tls13_kem_groups = pq_kem_groups_ietf_2024_10, 41 : : .tls13_pq_hybrid_draft_revision = 5 42 : : }; 43 : : 44 : : const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_ietf_2025_07 = { 45 : : .kem_count = 0, 46 : : .kems = NULL, 47 : : .tls13_kem_group_count = s2n_array_len(pq_kem_groups_ietf_2025_07), 48 : : .tls13_kem_groups = pq_kem_groups_ietf_2025_07, 49 : : .tls13_pq_hybrid_draft_revision = 5 50 : : }; 51 : : 52 : : const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_cnsa2_2026_02 = { 53 : : .kem_count = 0, 54 : : .kems = NULL, 55 : : .tls13_kem_group_count = s2n_array_len(pq_kem_groups_cnsa2_2026_02), 56 : : .tls13_kem_groups = pq_kem_groups_cnsa2_2026_02, 57 : : .tls13_pq_hybrid_draft_revision = 5 58 : : }; 59 : : 60 : : const struct s2n_kem_preferences kem_preferences_all = { 61 : : .kem_count = 0, 62 : : .kems = NULL, 63 : : .tls13_kem_group_count = S2N_KEM_GROUPS_COUNT, 64 : : .tls13_kem_groups = ALL_SUPPORTED_KEM_GROUPS, 65 : : .tls13_pq_hybrid_draft_revision = 5 66 : : }; 67 : : 68 : : const struct s2n_kem_preferences kem_preferences_null = { 69 : : .kem_count = 0, 70 : : .kems = NULL, 71 : : .tls13_kem_group_count = 0, 72 : : .tls13_kem_groups = NULL, 73 : : .tls13_pq_hybrid_draft_revision = 0 74 : : }; 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 : 11 : { 80 [ - + ]: 11 : if (kem_preferences == NULL) { 81 : 0 : return false; 82 : 0 : } 83 : : 84 [ + + ]: 20 : for (size_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 85 [ + + ]: 14 : if (query_iana_id == kem_preferences->tls13_kem_groups[i]->iana_id) { 86 : 5 : return true; 87 : 5 : } 88 : 14 : } 89 : : 90 : 6 : return false; 91 : 11 : } 92 : : 93 : : /* Whether the client must include the length prefix in the PQ TLS 1.3 KEM KeyShares that it sends. Draft 0 of 94 : : * the PQ TLS 1.3 standard required length prefixing, and drafts 1-5 removed this length prefix. To not break 95 : : * backwards compatibility, we check what revision of the draft standard is configured to determine whether to send it. */ 96 : : bool s2n_tls13_client_must_use_hybrid_kem_length_prefix(const struct s2n_kem_preferences *kem_pref) 97 : 41 : { 98 [ + - ][ - + ]: 41 : return kem_pref && (kem_pref->tls13_pq_hybrid_draft_revision == 0); 99 : 41 : } 100 : : 101 : : S2N_RESULT s2n_kem_preferences_groups_available(const struct s2n_kem_preferences *kem_preferences, uint32_t *groups_available) 102 : 6622 : { 103 [ # # ][ - + ]: 6622 : RESULT_ENSURE_REF(kem_preferences); 104 [ - + ][ # # ]: 6622 : RESULT_ENSURE_REF(groups_available); 105 : : 106 : 6622 : uint32_t count = 0; 107 [ + + ]: 18349 : for (int i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 108 [ - + ]: 11727 : if (s2n_kem_group_is_available(kem_preferences->tls13_kem_groups[i])) { 109 : 0 : count++; 110 : 0 : } 111 : 11727 : } 112 : 6622 : *groups_available = count; 113 : 6622 : return S2N_RESULT_OK; 114 : 6622 : } 115 : : 116 : : const struct s2n_kem_group *s2n_kem_preferences_get_highest_priority_group(const struct s2n_kem_preferences *kem_preferences) 117 : 1 : { 118 [ - + ][ # # ]: 1 : PTR_ENSURE_REF(kem_preferences); 119 [ + + ]: 5 : for (size_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 120 [ - + ]: 4 : if (s2n_kem_group_is_available(kem_preferences->tls13_kem_groups[i])) { 121 : 0 : return kem_preferences->tls13_kem_groups[i]; 122 : 0 : } 123 : 4 : } 124 : 1 : return NULL; 125 : 1 : }