blob: 8db44303cf515e14cb826a9551753285ece789d9 [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#
Gilles Peskinea2611602018-09-17 18:40:33 +02005# Copyright (c) 2014-2020, ARM Limited, All Rights Reserved
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#
20# This file is part of Mbed TLS (https://tls.mbed.org)
Simon Butcher3000f782016-03-04 23:26:57 +000021#
22# Purpose
23#
Gilles Peskinea2611602018-09-17 18:40:33 +020024# The purpose of this test script is to validate that the library works
25# with any combination of elliptic curves. To this effect, build the library
26# and run the test suite with each tested combination of elliptic curves.
Simon Butcher3000f782016-03-04 23:26:57 +000027#
Gilles Peskinea2611602018-09-17 18:40:33 +020028# Testing all 2^n combinations would be too much, so we only test 2*n:
Simon Butcher3000f782016-03-04 23:26:57 +000029#
Gilles Peskinea2611602018-09-17 18:40:33 +020030# 1. Test with a single curve, for each curve. This validates that the
31# library works with any curve, and in particular that curve-specific
32# code is guarded by the proper preprocessor conditionals.
33# 2. Test with all curves except one, for each curve. This validates that
34# the test cases have correct dependencies. Testing with a single curve
35# doesn't validate this for tests that require more than one curve.
36
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020037# Usage: tests/scripts/curves.pl
Simon Butcher3000f782016-03-04 23:26:57 +000038#
39# This script should be executed from the root of the project directory.
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020040#
Gilles Peskinea2611602018-09-17 18:40:33 +020041# Only curves that are enabled in config.h will be tested.
42#
Manuel Pégourié-Gonnard9ba9dfb2017-06-06 11:51:34 +020043# For best effect, run either with cmake disabled, or cmake enabled in a mode
44# that includes -Werror.
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010045
46use warnings;
47use strict;
48
49-d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p';
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052my $config_h = 'include/mbedtls/config.h';
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010053my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` );
54
Gilles Peskinea2611602018-09-17 18:40:33 +020055# Determine which curves support ECDSA by checking the dependencies of
56# ECDSA in check_config.h.
57my %curve_supports_ecdsa = ();
58{
59 local $/ = "";
60 local *CHECK_CONFIG;
61 open(CHECK_CONFIG, '<', 'include/mbedtls/check_config.h')
62 or die "open include/mbedtls/check_config.h: $!";
63 while (my $stanza = <CHECK_CONFIG>) {
64 if ($stanza =~ /\A#if defined\(MBEDTLS_ECDSA_C\)/) {
65 for my $curve ($stanza =~ /(?<=\()MBEDTLS_ECP_DP_\w+_ENABLED(?=\))/g) {
66 $curve_supports_ecdsa{$curve} = 1;
67 }
68 last;
69 }
70 }
71 close(CHECK_CONFIG);
72}
73
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010074system( "cp $config_h $config_h.bak" ) and die;
75sub abort {
76 system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
Manuel Pégourié-Gonnard254eec82017-10-26 09:47:36 +020077 # use an exit code between 1 and 124 for git bisect (die returns 255)
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +020078 warn $_[0];
79 exit 1;
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +010080}
81
Gilles Peskinea2611602018-09-17 18:40:33 +020082# Disable all the curves. We'll then re-enable them one by one.
83for my $curve (@curves) {
84 system( "scripts/config.pl unset $curve" )
85 and abort "Failed to disable $curve\n";
86}
87# Depends on a specific curve. Also, ignore error if it wasn't enabled.
88system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
89
90# Test with only $curve enabled, for each $curve.
91for my $curve (@curves) {
92 system( "make clean" ) and die;
93
94 print "\n******************************************\n";
95 print "* Testing with only curve: $curve\n";
96 print "******************************************\n";
97 $ENV{MBEDTLS_TEST_CONFIGURATION} = "$curve";
98
99 system( "scripts/config.pl set $curve" )
100 and abort "Failed to enable $curve\n";
101
102 my $ecdsa = $curve_supports_ecdsa{$curve} ? "set" : "unset";
103 for my $dep (qw(MBEDTLS_ECDSA_C
104 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
105 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)) {
106 system( "scripts/config.pl $ecdsa $dep" )
107 and abort "Failed to $ecdsa $dep\n";
108 }
109
110 system( "CFLAGS='-Werror -Wall -Wextra' make" )
111 and abort "Failed to build: only $curve\n";
112 system( "make test" )
113 and abort "Failed test suite: only $curve\n";
114
115 system( "scripts/config.pl unset $curve" )
116 and abort "Failed to disable $curve\n";
117}
118
119system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
120
121# Test with $curve disabled but the others enabled, for each $curve.
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100122for my $curve (@curves) {
123 system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
Manuel Pégourié-Gonnarda7c4c8a2017-07-12 12:15:24 +0200124 system( "make clean" ) and die;
125
Manuel Pégourié-Gonnard8a7a1892015-10-20 16:56:12 +0200126 # depends on a specific curve. Also, ignore error if it wasn't enabled
Gilles Peskine5d46f6a2019-07-27 23:52:53 +0200127 system( "scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100128
129 print "\n******************************************\n";
130 print "* Testing without curve: $curve\n";
131 print "******************************************\n";
Gilles Peskine9004a172019-09-16 15:20:36 +0200132 $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$curve";
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100133
Gilles Peskine5d46f6a2019-07-27 23:52:53 +0200134 system( "scripts/config.py unset $curve" )
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100135 and abort "Failed to disable $curve\n";
136
Gilles Peskinea2611602018-09-17 18:40:33 +0200137 system( "CFLAGS='-Werror -Wall -Wextra' make" )
138 and abort "Failed to build: all but $curve\n";
139 system( "make test" )
140 and abort "Failed test suite: all but $curve\n";
Manuel Pégourié-Gonnard2727dc12014-11-19 20:02:46 +0100141
142}
143
144system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
145system( "make clean" ) and die;
146exit 0;