blob: fcaca9141b5f3da4c37968bd4647caad7c33b606 [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
Gilles Peskinee2c342b2022-04-14 12:00:17 +020023# 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 Butcher3000f782016-03-04 23:26:57 +000026#
Gilles Peskinee2c342b2022-04-14 12:00:17 +020027# 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 Butcher3000f782016-03-04 23:26:57 +000031#
Gilles Peskinee2c342b2022-04-14 12:00:17 +020032# 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é-Gonnard9ba9dfb2017-06-06 11:51:34 +020041# Usage: tests/scripts/curves.pl
Simon Butcher3000f782016-03-04 23:26:57 +000042#
43# This script should be executed from the root of the project directory.
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020044#
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020045# Only curves that are enabled in mbedtls_config.h will be tested.
Gilles Peskinea2611602018-09-17 18:40:33 +020046#
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020047# For best effect, run either with cmake disabled, or cmake enabled in a mode
48# that includes -Werror.
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010049
50use warnings;
51use strict;
52
53-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p';
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020056my $config_h = 'include/mbedtls/mbedtls_config.h';
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010057my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` );
58
Gilles Peskinea2611602018-09-17 18:40:33 +020059# Determine which curves support ECDSA by checking the dependencies of
60# ECDSA in check_config.h.
61my %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é-Gonnard2727dc12014-11-19 20:02:46 +010078system( "cp $config_h $config_h.bak" ) and die;
79sub abort {
80 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020081 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020082 warn $_[0];
83 exit 1;
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010084}
85
Gilles Peskinea2611602018-09-17 18:40:33 +020086# Disable all the curves. We'll then re-enable them one by one.
87for 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.
92system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
TRodziewicz85aff9f2021-04-23 10:47:26 +020093system( "scripts/config.pl unset MBEDTLS_ECJPAKE_C" );
Gilles Peskinea2611602018-09-17 18:40:33 +020094
95# Test with only $curve enabled, for each $curve.
96for 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é-Gonnard2727dc12014-11-19 20:02:46 +0100124system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
125system( "make clean" ) and die;
126exit 0;