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 : : }; 42 : : 43 : : const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_ietf_2025_07 = { 44 : : .kem_count = 0, 45 : : .kems = NULL, 46 : : .tls13_kem_group_count = s2n_array_len(pq_kem_groups_ietf_2025_07), 47 : : .tls13_kem_groups = pq_kem_groups_ietf_2025_07 48 : : }; 49 : : 50 : : const struct s2n_kem_preferences kem_preferences_pq_tls_1_3_cnsa2_2026_02 = { 51 : : .kem_count = 0, 52 : : .kems = NULL, 53 : : .tls13_kem_group_count = s2n_array_len(pq_kem_groups_cnsa2_2026_02), 54 : : .tls13_kem_groups = pq_kem_groups_cnsa2_2026_02 55 : : }; 56 : : 57 : : const struct s2n_kem_preferences kem_preferences_all = { 58 : : .kem_count = 0, 59 : : .kems = NULL, 60 : : .tls13_kem_group_count = S2N_KEM_GROUPS_COUNT, 61 : : .tls13_kem_groups = ALL_SUPPORTED_KEM_GROUPS 62 : : }; 63 : : 64 : : const struct s2n_kem_preferences kem_preferences_null = { 65 : : .kem_count = 0, 66 : : .kems = NULL, 67 : : .tls13_kem_group_count = 0, 68 : : .tls13_kem_groups = NULL 69 : : }; 70 : : 71 : : /* Determines if query_iana_id corresponds to a tls13_kem_group for these KEM preferences. */ 72 : : bool s2n_kem_preferences_includes_tls13_kem_group(const struct s2n_kem_preferences *kem_preferences, 73 : : uint16_t query_iana_id) 74 : 11 : { 75 [ - + ]: 11 : if (kem_preferences == NULL) { 76 : 0 : return false; 77 : 0 : } 78 : : 79 [ + + ]: 20 : for (size_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 80 [ + + ]: 14 : if (query_iana_id == kem_preferences->tls13_kem_groups[i]->iana_id) { 81 : 5 : return true; 82 : 5 : } 83 : 14 : } 84 : : 85 : 6 : return false; 86 : 11 : } 87 : : 88 : : S2N_RESULT s2n_kem_preferences_groups_available(const struct s2n_kem_preferences *kem_preferences, uint32_t *groups_available) 89 : 6346 : { 90 [ - + ][ # # ]: 6346 : RESULT_ENSURE_REF(kem_preferences); 91 [ - + ][ # # ]: 6346 : RESULT_ENSURE_REF(groups_available); 92 : : 93 : 6346 : uint32_t count = 0; 94 [ + + ]: 17253 : for (int i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 95 [ - + ]: 10907 : if (s2n_kem_group_is_available(kem_preferences->tls13_kem_groups[i])) { 96 : 0 : count++; 97 : 0 : } 98 : 10907 : } 99 : 6346 : *groups_available = count; 100 : 6346 : return S2N_RESULT_OK; 101 : 6346 : } 102 : : 103 : : const struct s2n_kem_group *s2n_kem_preferences_get_highest_priority_group(const struct s2n_kem_preferences *kem_preferences) 104 : 1 : { 105 [ - + ][ # # ]: 1 : PTR_ENSURE_REF(kem_preferences); 106 [ + + ]: 5 : for (size_t i = 0; i < kem_preferences->tls13_kem_group_count; i++) { 107 [ - + ]: 4 : if (s2n_kem_group_is_available(kem_preferences->tls13_kem_groups[i])) { 108 : 0 : return kem_preferences->tls13_kem_groups[i]; 109 : 0 : } 110 : 4 : } 111 : 1 : return NULL; 112 : 1 : }