LCOV - code coverage report
Current view: top level - tls - s2n_ecc_preferences.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 29 29 100.0 %
Date: 2026-11-22 08:25:05 Functions: 2 2 100.0 %
Branches: 15 16 93.8 %

           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_ecc_preferences.h"
      17                 :            : 
      18                 :            : #include "api/s2n.h"
      19                 :            : #include "crypto/s2n_ecc_evp.h"
      20                 :            : #include "tls/s2n_connection.h"
      21                 :            : #include "utils/s2n_safety.h"
      22                 :            : 
      23                 :            : /* Chosen based on AWS server recommendations as of 05/24:
      24                 :            :  * - All supported curves
      25                 :            :  * - Prefer p256
      26                 :            :  */
      27                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20240501[] = {
      28                 :            :     &s2n_ecc_curve_secp256r1,
      29                 :            : #if EVP_APIS_SUPPORTED
      30                 :            :     &s2n_ecc_curve_x25519,
      31                 :            : #endif
      32                 :            :     &s2n_ecc_curve_secp384r1,
      33                 :            :     &s2n_ecc_curve_secp521r1,
      34                 :            : };
      35                 :            : 
      36                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20140601[] = {
      37                 :            :     &s2n_ecc_curve_secp256r1,
      38                 :            :     &s2n_ecc_curve_secp384r1,
      39                 :            : };
      40                 :            : 
      41                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20200310[] = {
      42                 :            : #if EVP_APIS_SUPPORTED
      43                 :            :     &s2n_ecc_curve_x25519,
      44                 :            : #endif
      45                 :            :     &s2n_ecc_curve_secp256r1,
      46                 :            :     &s2n_ecc_curve_secp384r1,
      47                 :            : };
      48                 :            : 
      49                 :            : /* Curve p256 is at the top of the list in order to minimize HRR */
      50                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20230623[] = {
      51                 :            :     &s2n_ecc_curve_secp256r1,
      52                 :            : #if EVP_APIS_SUPPORTED
      53                 :            :     &s2n_ecc_curve_x25519,
      54                 :            : #endif
      55                 :            :     &s2n_ecc_curve_secp384r1,
      56                 :            : };
      57                 :            : 
      58                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20261021[] = {
      59                 :            :     &s2n_ecc_curve_secp256r1,
      60                 :            : #if EVP_APIS_SUPPORTED
      61                 :            :     &s2n_ecc_curve_x25519,
      62                 :            : #endif
      63                 :            : };
      64                 :            : 
      65                 :            : /*
      66                 :            :  * These curves were chosen based on the following specification:
      67                 :            :  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r2.pdf
      68                 :            :  */
      69                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_default_fips[] = {
      70                 :            :     &s2n_ecc_curve_secp256r1,
      71                 :            :     &s2n_ecc_curve_secp384r1,
      72                 :            : };
      73                 :            : 
      74                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20201021[] = {
      75                 :            :     &s2n_ecc_curve_secp256r1,
      76                 :            :     &s2n_ecc_curve_secp384r1,
      77                 :            :     &s2n_ecc_curve_secp521r1,
      78                 :            : };
      79                 :            : 
      80                 :            : /* Prefer x25519 over p256 for performance */
      81                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20240603[] = {
      82                 :            : #if EVP_APIS_SUPPORTED
      83                 :            :     &s2n_ecc_curve_x25519,
      84                 :            : #endif
      85                 :            :     &s2n_ecc_curve_secp256r1,
      86                 :            :     &s2n_ecc_curve_secp384r1,
      87                 :            :     &s2n_ecc_curve_secp521r1,
      88                 :            : };
      89                 :            : 
      90                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20210816[] = {
      91                 :            :     &s2n_ecc_curve_secp384r1,
      92                 :            : };
      93                 :            : 
      94                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_20251113[] = {
      95                 :            :     &s2n_ecc_curve_secp384r1,
      96                 :            :     &s2n_ecc_curve_secp256r1,
      97                 :            :     &s2n_ecc_curve_secp521r1,
      98                 :            : };
      99                 :            : 
     100                 :            : const struct s2n_ecc_named_curve *const s2n_ecc_pref_list_test_all[] = {
     101                 :            : #if EVP_APIS_SUPPORTED
     102                 :            :     &s2n_ecc_curve_x25519,
     103                 :            : #endif
     104                 :            :     &s2n_ecc_curve_secp256r1,
     105                 :            :     &s2n_ecc_curve_secp384r1,
     106                 :            :     &s2n_ecc_curve_secp521r1,
     107                 :            : };
     108                 :            : 
     109                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20240501 = {
     110                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20240501),
     111                 :            :     .ecc_curves = s2n_ecc_pref_list_20240501,
     112                 :            : };
     113                 :            : 
     114                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20140601 = {
     115                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20140601),
     116                 :            :     .ecc_curves = s2n_ecc_pref_list_20140601,
     117                 :            : };
     118                 :            : 
     119                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20200310 = {
     120                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20200310),
     121                 :            :     .ecc_curves = s2n_ecc_pref_list_20200310,
     122                 :            : };
     123                 :            : 
     124                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20230623 = {
     125                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20230623),
     126                 :            :     .ecc_curves = s2n_ecc_pref_list_20230623,
     127                 :            : };
     128                 :            : 
     129                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20261021 = {
     130                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20261021),
     131                 :            :     .ecc_curves = s2n_ecc_pref_list_20261021,
     132                 :            : };
     133                 :            : 
     134                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_default_fips = {
     135                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_default_fips),
     136                 :            :     .ecc_curves = s2n_ecc_pref_list_default_fips,
     137                 :            : };
     138                 :            : 
     139                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20201021 = {
     140                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20201021),
     141                 :            :     .ecc_curves = s2n_ecc_pref_list_20201021,
     142                 :            : };
     143                 :            : 
     144                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20210816 = {
     145                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20210816),
     146                 :            :     .ecc_curves = s2n_ecc_pref_list_20210816,
     147                 :            : };
     148                 :            : 
     149                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20240603 = {
     150                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20240603),
     151                 :            :     .ecc_curves = s2n_ecc_pref_list_20240603,
     152                 :            : };
     153                 :            : 
     154                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_20251113 = {
     155                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_20251113),
     156                 :            :     .ecc_curves = s2n_ecc_pref_list_20251113,
     157                 :            : };
     158                 :            : 
     159                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_test_all = {
     160                 :            :     .count = s2n_array_len(s2n_ecc_pref_list_test_all),
     161                 :            :     .ecc_curves = s2n_ecc_pref_list_test_all,
     162                 :            : };
     163                 :            : 
     164                 :            : const struct s2n_ecc_preferences s2n_ecc_preferences_null = {
     165                 :            :     .count = 0,
     166                 :            :     .ecc_curves = NULL,
     167                 :            : };
     168                 :            : 
     169                 :            : /* Checks if the ecc_curves present in s2n_ecc_preferences list is a subset of s2n_all_supported_curves_list
     170                 :            :  * maintained in s2n_ecc_evp.c */
     171                 :            : int s2n_check_ecc_preferences_curves_list(const struct s2n_ecc_preferences *ecc_preferences)
     172                 :      75385 : {
     173                 :      75385 :     int check = 1;
     174         [ +  + ]:     272221 :     for (int i = 0; i < ecc_preferences->count; i++) {
     175                 :     196837 :         const struct s2n_ecc_named_curve *named_curve = ecc_preferences->ecc_curves[i];
     176                 :     196837 :         int curve_found = 0;
     177         [ +  + ]:     409731 :         for (size_t j = 0; j < s2n_all_supported_curves_list_len; j++) {
     178         [ +  + ]:     409730 :             if (named_curve->iana_id == s2n_all_supported_curves_list[j]->iana_id) {
     179                 :     196836 :                 curve_found = 1;
     180                 :     196836 :                 break;
     181                 :     196836 :             }
     182                 :     409730 :         }
     183                 :     196837 :         check *= curve_found;
     184         [ +  + ]:     196837 :         if (check == 0) {
     185         [ +  - ]:          1 :             POSIX_BAIL(S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
     186                 :          1 :         }
     187                 :     196837 :     }
     188                 :      75384 :     return S2N_SUCCESS;
     189                 :      75385 : }
     190                 :            : 
     191                 :            : /* Determines if query_iana_id corresponds to a curve for these ECC preferences. */
     192                 :            : bool s2n_ecc_preferences_includes_curve(const struct s2n_ecc_preferences *ecc_preferences, uint16_t query_iana_id)
     193                 :      19343 : {
     194         [ +  + ]:      19343 :     if (ecc_preferences == NULL) {
     195                 :          1 :         return false;
     196                 :          1 :     }
     197                 :            : 
     198         [ +  + ]:      27801 :     for (size_t i = 0; i < ecc_preferences->count; i++) {
     199         [ +  + ]:      27768 :         if (query_iana_id == ecc_preferences->ecc_curves[i]->iana_id) {
     200                 :      19309 :             return true;
     201                 :      19309 :         }
     202                 :      27768 :     }
     203                 :            : 
     204                 :         33 :     return false;
     205                 :      19342 : }

Generated by: LCOV version 1.14