fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 2 | |
Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 3 | # curves.pl |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 4 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | # |
Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 20 | # Purpose |
| 21 | # |
Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 22 | # The purpose of this test script is to validate that the library works |
Gilles Peskine | e2c342b | 2022-04-14 12:00:17 +0200 | [diff] [blame] | 23 | # when only a single curve is enabled. In particular, this validates that |
| 24 | # curve-specific code is guarded by the proper preprocessor conditionals, |
| 25 | # both in the library and in tests. |
Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 26 | # |
Gilles Peskine | e2c342b | 2022-04-14 12:00:17 +0200 | [diff] [blame] | 27 | # Since this script only tests builds with a single curve, it can't detect |
| 28 | # bugs that are only triggered when multiple curves are present. We do |
| 29 | # also test in many configurations where all curves are enabled, as well |
| 30 | # as a few configurations in configs/*.h with a restricted subset of curves. |
Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 31 | # |
Gilles Peskine | e2c342b | 2022-04-14 12:00:17 +0200 | [diff] [blame] | 32 | # Here are some known test gaps that could be addressed by testing all |
| 33 | # 2^n combinations of support for n curves, which is impractical: |
| 34 | # * There could be product bugs when curves A and B are enabled but not C. |
| 35 | # For example, a MAX_SIZE calculation that forgets B, where |
| 36 | # size(A) < size(B) < size(C). |
| 37 | # * For test cases that require three or more curves, validate that they're |
| 38 | # not missing dependencies. This is extremely rare. (For test cases that |
| 39 | # require curves A and B but are missing a dependency on B, this is |
| 40 | # detected in the A-only build.) |
Manuel Pégourié-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 41 | # Usage: tests/scripts/curves.pl |
Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 42 | # |
| 43 | # This script should be executed from the root of the project directory. |
Manuel Pégourié-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 44 | # |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 45 | # Only curves that are enabled in mbedtls_config.h will be tested. |
Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 46 | # |
Manuel Pégourié-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 47 | # For best effect, run either with cmake disabled, or cmake enabled in a mode |
| 48 | # that includes -Werror. |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 49 | |
| 50 | use warnings; |
| 51 | use strict; |
| 52 | |
| 53 | -d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n"; |
| 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p'; |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 56 | my $config_h = 'include/mbedtls/mbedtls_config.h'; |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 57 | my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` ); |
| 58 | |
Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 59 | # Determine which curves support ECDSA by checking the dependencies of |
| 60 | # ECDSA in check_config.h. |
| 61 | my %curve_supports_ecdsa = (); |
| 62 | { |
| 63 | local $/ = ""; |
| 64 | local *CHECK_CONFIG; |
| 65 | open(CHECK_CONFIG, '<', 'include/mbedtls/check_config.h') |
| 66 | or die "open include/mbedtls/check_config.h: $!"; |
| 67 | while (my $stanza = <CHECK_CONFIG>) { |
| 68 | if ($stanza =~ /\A#if defined\(MBEDTLS_ECDSA_C\)/) { |
| 69 | for my $curve ($stanza =~ /(?<=\()MBEDTLS_ECP_DP_\w+_ENABLED(?=\))/g) { |
| 70 | $curve_supports_ecdsa{$curve} = 1; |
| 71 | } |
| 72 | last; |
| 73 | } |
| 74 | } |
| 75 | close(CHECK_CONFIG); |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 78 | system( "cp $config_h $config_h.bak" ) and die; |
| 79 | sub abort { |
| 80 | system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n"; |
Manuel Pégourié-Gonnard | 254eec8 | 2017-10-26 09:47:36 +0200 | [diff] [blame] | 81 | # use an exit code between 1 and 124 for git bisect (die returns 255) |
Manuel Pégourié-Gonnard | a7c4c8a | 2017-07-12 12:15:24 +0200 | [diff] [blame] | 82 | warn $_[0]; |
| 83 | exit 1; |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 86 | # Disable all the curves. We'll then re-enable them one by one. |
| 87 | for my $curve (@curves) { |
| 88 | system( "scripts/config.pl unset $curve" ) |
| 89 | and abort "Failed to disable $curve\n"; |
| 90 | } |
| 91 | # Depends on a specific curve. Also, ignore error if it wasn't enabled. |
| 92 | system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" ); |
TRodziewicz | 85aff9f | 2021-04-23 10:47:26 +0200 | [diff] [blame] | 93 | system( "scripts/config.pl unset MBEDTLS_ECJPAKE_C" ); |
Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 94 | |
| 95 | # Test with only $curve enabled, for each $curve. |
| 96 | for my $curve (@curves) { |
| 97 | system( "make clean" ) and die; |
| 98 | |
| 99 | print "\n******************************************\n"; |
| 100 | print "* Testing with only curve: $curve\n"; |
| 101 | print "******************************************\n"; |
| 102 | $ENV{MBEDTLS_TEST_CONFIGURATION} = "$curve"; |
| 103 | |
| 104 | system( "scripts/config.pl set $curve" ) |
| 105 | and abort "Failed to enable $curve\n"; |
| 106 | |
| 107 | my $ecdsa = $curve_supports_ecdsa{$curve} ? "set" : "unset"; |
| 108 | for my $dep (qw(MBEDTLS_ECDSA_C |
| 109 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED |
| 110 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)) { |
| 111 | system( "scripts/config.pl $ecdsa $dep" ) |
| 112 | and abort "Failed to $ecdsa $dep\n"; |
| 113 | } |
| 114 | |
| 115 | system( "CFLAGS='-Werror -Wall -Wextra' make" ) |
| 116 | and abort "Failed to build: only $curve\n"; |
| 117 | system( "make test" ) |
| 118 | and abort "Failed test suite: only $curve\n"; |
| 119 | |
| 120 | system( "scripts/config.pl unset $curve" ) |
| 121 | and abort "Failed to disable $curve\n"; |
| 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 124 | system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n"; |
| 125 | system( "make clean" ) and die; |
| 126 | exit 0; |