| 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 | 
|  | 23 | # with any combination of elliptic curves. To this effect, build the library | 
|  | 24 | # and run the test suite with each tested combination of elliptic curves. | 
| Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 25 | # | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 26 | # Testing all 2^n combinations would be too much, so we only test 2*n: | 
| Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 27 | # | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 28 | # 1. Test with a single curve, for each curve. This validates that the | 
|  | 29 | #    library works with any curve, and in particular that curve-specific | 
|  | 30 | #    code is guarded by the proper preprocessor conditionals. | 
|  | 31 | # 2. Test with all curves except one, for each curve. This validates that | 
|  | 32 | #    the test cases have correct dependencies. Testing with a single curve | 
|  | 33 | #    doesn't validate this for tests that require more than one curve. | 
|  | 34 |  | 
| Manuel Pégourié-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 35 | # Usage: tests/scripts/curves.pl | 
| Simon Butcher | 3000f78 | 2016-03-04 23:26:57 +0000 | [diff] [blame] | 36 | # | 
|  | 37 | # 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] | 38 | # | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 39 | # Only curves that are enabled in config.h will be tested. | 
|  | 40 | # | 
| Manuel Pégourié-Gonnard | 9ba9dfb | 2017-06-06 11:51:34 +0200 | [diff] [blame] | 41 | # For best effect, run either with cmake disabled, or cmake enabled in a mode | 
|  | 42 | # that includes -Werror. | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 43 |  | 
|  | 44 | use warnings; | 
|  | 45 | use strict; | 
|  | 46 |  | 
|  | 47 | -d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n"; | 
|  | 48 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p'; | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | my $config_h = 'include/mbedtls/config.h'; | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 51 | my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` ); | 
|  | 52 |  | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 53 | # Determine which curves support ECDSA by checking the dependencies of | 
|  | 54 | # ECDSA in check_config.h. | 
|  | 55 | my %curve_supports_ecdsa = (); | 
|  | 56 | { | 
|  | 57 | local $/ = ""; | 
|  | 58 | local *CHECK_CONFIG; | 
|  | 59 | open(CHECK_CONFIG, '<', 'include/mbedtls/check_config.h') | 
|  | 60 | or die "open include/mbedtls/check_config.h: $!"; | 
|  | 61 | while (my $stanza = <CHECK_CONFIG>) { | 
|  | 62 | if ($stanza =~ /\A#if defined\(MBEDTLS_ECDSA_C\)/) { | 
|  | 63 | for my $curve ($stanza =~ /(?<=\()MBEDTLS_ECP_DP_\w+_ENABLED(?=\))/g) { | 
|  | 64 | $curve_supports_ecdsa{$curve} = 1; | 
|  | 65 | } | 
|  | 66 | last; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 | close(CHECK_CONFIG); | 
|  | 70 | } | 
|  | 71 |  | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 72 | system( "cp $config_h $config_h.bak" ) and die; | 
|  | 73 | sub abort { | 
|  | 74 | 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] | 75 | # 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] | 76 | warn $_[0]; | 
|  | 77 | exit 1; | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 80 | # Disable all the curves. We'll then re-enable them one by one. | 
|  | 81 | for my $curve (@curves) { | 
|  | 82 | system( "scripts/config.pl unset $curve" ) | 
|  | 83 | and abort "Failed to disable $curve\n"; | 
|  | 84 | } | 
|  | 85 | # Depends on a specific curve. Also, ignore error if it wasn't enabled. | 
|  | 86 | system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" ); | 
|  | 87 |  | 
|  | 88 | # Test with only $curve enabled, for each $curve. | 
|  | 89 | for my $curve (@curves) { | 
|  | 90 | system( "make clean" ) and die; | 
|  | 91 |  | 
|  | 92 | print "\n******************************************\n"; | 
|  | 93 | print "* Testing with only curve: $curve\n"; | 
|  | 94 | print "******************************************\n"; | 
|  | 95 | $ENV{MBEDTLS_TEST_CONFIGURATION} = "$curve"; | 
|  | 96 |  | 
|  | 97 | system( "scripts/config.pl set $curve" ) | 
|  | 98 | and abort "Failed to enable $curve\n"; | 
|  | 99 |  | 
|  | 100 | my $ecdsa = $curve_supports_ecdsa{$curve} ? "set" : "unset"; | 
|  | 101 | for my $dep (qw(MBEDTLS_ECDSA_C | 
|  | 102 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED | 
|  | 103 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)) { | 
|  | 104 | system( "scripts/config.pl $ecdsa $dep" ) | 
|  | 105 | and abort "Failed to $ecdsa $dep\n"; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | system( "CFLAGS='-Werror -Wall -Wextra' make" ) | 
|  | 109 | and abort "Failed to build: only $curve\n"; | 
|  | 110 | system( "make test" ) | 
|  | 111 | and abort "Failed test suite: only $curve\n"; | 
|  | 112 |  | 
|  | 113 | system( "scripts/config.pl unset $curve" ) | 
|  | 114 | and abort "Failed to disable $curve\n"; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n"; | 
|  | 118 |  | 
|  | 119 | # Test with $curve disabled but the others enabled, for each $curve. | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 120 | for my $curve (@curves) { | 
|  | 121 | system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n"; | 
| Manuel Pégourié-Gonnard | a7c4c8a | 2017-07-12 12:15:24 +0200 | [diff] [blame] | 122 | system( "make clean" ) and die; | 
|  | 123 |  | 
| Manuel Pégourié-Gonnard | 8a7a189 | 2015-10-20 16:56:12 +0200 | [diff] [blame] | 124 | # depends on a specific curve. Also, ignore error if it wasn't enabled | 
| Gilles Peskine | 5d46f6a | 2019-07-27 23:52:53 +0200 | [diff] [blame] | 125 | system( "scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" ); | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 126 |  | 
|  | 127 | print "\n******************************************\n"; | 
|  | 128 | print "* Testing without curve: $curve\n"; | 
|  | 129 | print "******************************************\n"; | 
| Gilles Peskine | 9004a17 | 2019-09-16 15:20:36 +0200 | [diff] [blame] | 130 | $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$curve"; | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 131 |  | 
| Gilles Peskine | 5d46f6a | 2019-07-27 23:52:53 +0200 | [diff] [blame] | 132 | system( "scripts/config.py unset $curve" ) | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 133 | and abort "Failed to disable $curve\n"; | 
|  | 134 |  | 
| Gilles Peskine | a261160 | 2018-09-17 18:40:33 +0200 | [diff] [blame] | 135 | system( "CFLAGS='-Werror -Wall -Wextra' make" ) | 
|  | 136 | and abort "Failed to build: all but $curve\n"; | 
|  | 137 | system( "make test" ) | 
|  | 138 | and abort "Failed test suite: all but $curve\n"; | 
| Manuel Pégourié-Gonnard | 2727dc1 | 2014-11-19 20:02:46 +0100 | [diff] [blame] | 139 |  | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n"; | 
|  | 143 | system( "make clean" ) and die; | 
|  | 144 | exit 0; |