blob: 47196f456ff6e46dbbbadeac7242144bf76934d8 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +01002
Simon Butcher3000f782016-03-04 23:26:57 +00003# curves.pl
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +01004#
Bence Szépkúti1e148272020-08-07 13:07:28 +02005# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02006# 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 Butcher3000f782016-03-04 23:26:57 +000020# Purpose
21#
Gilles Peskinea2611602018-09-17 18:40:33 +020022# 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 Butcher3000f782016-03-04 23:26:57 +000025#
Gilles Peskinea2611602018-09-17 18:40:33 +020026# Testing all 2^n combinations would be too much, so we only test 2*n:
Simon Butcher3000f782016-03-04 23:26:57 +000027#
Gilles Peskinea2611602018-09-17 18:40:33 +020028# 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é-Gonnard9ba9dfb2017-06-06 11:51:34 +020035# Usage: tests/scripts/curves.pl
Simon Butcher3000f782016-03-04 23:26:57 +000036#
37# This script should be executed from the root of the project directory.
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020038#
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020039# Only curves that are enabled in mbedtls_config.h will be tested.
Gilles Peskinea2611602018-09-17 18:40:33 +020040#
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020041# For best effect, run either with cmake disabled, or cmake enabled in a mode
42# that includes -Werror.
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010043
44use warnings;
45use strict;
46
47-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p';
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020050my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010051my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` );
52
Gilles Peskinea2611602018-09-17 18:40:33 +020053# Determine which curves support ECDSA by checking the dependencies of
54# ECDSA in check_config.h.
55my %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é-Gonnard2727dc12014-11-19 20:02:46 +010072system( "cp $config_h $config_h.bak" ) and die;
73sub abort {
74 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020075 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020076 warn $_[0];
77 exit 1;
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010078}
79
Gilles Peskinea2611602018-09-17 18:40:33 +020080# Disable all the curves. We'll then re-enable them one by one.
81for 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.
86system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
TRodziewicz85aff9f2021-04-23 10:47:26 +020087system( "scripts/config.pl unset MBEDTLS_ECJPAKE_C" );
Gilles Peskinea2611602018-09-17 18:40:33 +020088
89# Test with only $curve enabled, for each $curve.
90for my $curve (@curves) {
91 system( "make clean" ) and die;
92
93 print "\n******************************************\n";
94 print "* Testing with only curve: $curve\n";
95 print "******************************************\n";
96 $ENV{MBEDTLS_TEST_CONFIGURATION} = "$curve";
97
98 system( "scripts/config.pl set $curve" )
99 and abort "Failed to enable $curve\n";
100
101 my $ecdsa = $curve_supports_ecdsa{$curve} ? "set" : "unset";
102 for my $dep (qw(MBEDTLS_ECDSA_C
103 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
104 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)) {
105 system( "scripts/config.pl $ecdsa $dep" )
106 and abort "Failed to $ecdsa $dep\n";
107 }
108
109 system( "CFLAGS='-Werror -Wall -Wextra' make" )
110 and abort "Failed to build: only $curve\n";
111 system( "make test" )
112 and abort "Failed test suite: only $curve\n";
113
114 system( "scripts/config.pl unset $curve" )
115 and abort "Failed to disable $curve\n";
116}
117
118system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
119
120# Test with $curve disabled but the others enabled, for each $curve.
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100121for my $curve (@curves) {
122 system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +0200123 system( "make clean" ) and die;
124
Manuel Pégourié-Gonnard8a7a1892015-10-20 16:56:12 +0200125 # depends on a specific curve. Also, ignore error if it wasn't enabled
Gilles Peskine5d46f6a2019-07-27 23:52:53 +0200126 system( "scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100127
128 print "\n******************************************\n";
129 print "* Testing without curve: $curve\n";
130 print "******************************************\n";
Gilles Peskine9004a172019-09-16 15:20:36 +0200131 $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$curve";
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100132
Gilles Peskine5d46f6a2019-07-27 23:52:53 +0200133 system( "scripts/config.py unset $curve" )
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100134 and abort "Failed to disable $curve\n";
135
Gilles Peskinea2611602018-09-17 18:40:33 +0200136 system( "CFLAGS='-Werror -Wall -Wextra' make" )
137 and abort "Failed to build: all but $curve\n";
138 system( "make test" )
139 and abort "Failed test suite: all but $curve\n";
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100140
141}
142
143system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
144system( "make clean" ) and die;
145exit 0;