curves.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env perl
  2. # curves.pl
  3. #
  4. # Copyright The Mbed TLS Contributors
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # Purpose
  20. #
  21. # The purpose of this test script is to validate that the library works
  22. # with any combination of elliptic curves. To this effect, build the library
  23. # and run the test suite with each tested combination of elliptic curves.
  24. #
  25. # Testing all 2^n combinations would be too much, so we only test 2*n:
  26. #
  27. # 1. Test with a single curve, for each curve. This validates that the
  28. # library works with any curve, and in particular that curve-specific
  29. # code is guarded by the proper preprocessor conditionals.
  30. # 2. Test with all curves except one, for each curve. This validates that
  31. # the test cases have correct dependencies. Testing with a single curve
  32. # doesn't validate this for tests that require more than one curve.
  33. # Usage: tests/scripts/curves.pl
  34. #
  35. # This script should be executed from the root of the project directory.
  36. #
  37. # Only curves that are enabled in config.h will be tested.
  38. #
  39. # For best effect, run either with cmake disabled, or cmake enabled in a mode
  40. # that includes -Werror.
  41. use warnings;
  42. use strict;
  43. -d 'library' && -d 'include' && -d 'tests' or die "Must be run from root\n";
  44. my $sed_cmd = 's/^#define \(MBEDTLS_ECP_DP.*_ENABLED\)/\1/p';
  45. my $config_h = 'include/mbedtls/config.h';
  46. my @curves = split( /\s+/, `sed -n -e '$sed_cmd' $config_h` );
  47. # Determine which curves support ECDSA by checking the dependencies of
  48. # ECDSA in check_config.h.
  49. my %curve_supports_ecdsa = ();
  50. {
  51. local $/ = "";
  52. local *CHECK_CONFIG;
  53. open(CHECK_CONFIG, '<', 'include/mbedtls/check_config.h')
  54. or die "open include/mbedtls/check_config.h: $!";
  55. while (my $stanza = <CHECK_CONFIG>) {
  56. if ($stanza =~ /\A#if defined\(MBEDTLS_ECDSA_C\)/) {
  57. for my $curve ($stanza =~ /(?<=\()MBEDTLS_ECP_DP_\w+_ENABLED(?=\))/g) {
  58. $curve_supports_ecdsa{$curve} = 1;
  59. }
  60. last;
  61. }
  62. }
  63. close(CHECK_CONFIG);
  64. }
  65. system( "cp $config_h $config_h.bak" ) and die;
  66. sub abort {
  67. system( "mv $config_h.bak $config_h" ) and warn "$config_h not restored\n";
  68. # use an exit code between 1 and 124 for git bisect (die returns 255)
  69. warn $_[0];
  70. exit 1;
  71. }
  72. # Disable all the curves. We'll then re-enable them one by one.
  73. for my $curve (@curves) {
  74. system( "scripts/config.pl unset $curve" )
  75. and abort "Failed to disable $curve\n";
  76. }
  77. # Depends on a specific curve. Also, ignore error if it wasn't enabled.
  78. system( "scripts/config.pl unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
  79. # Test with only $curve enabled, for each $curve.
  80. for my $curve (@curves) {
  81. system( "make clean" ) and die;
  82. print "\n******************************************\n";
  83. print "* Testing with only curve: $curve\n";
  84. print "******************************************\n";
  85. $ENV{MBEDTLS_TEST_CONFIGURATION} = "$curve";
  86. system( "scripts/config.pl set $curve" )
  87. and abort "Failed to enable $curve\n";
  88. my $ecdsa = $curve_supports_ecdsa{$curve} ? "set" : "unset";
  89. for my $dep (qw(MBEDTLS_ECDSA_C
  90. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
  91. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)) {
  92. system( "scripts/config.pl $ecdsa $dep" )
  93. and abort "Failed to $ecdsa $dep\n";
  94. }
  95. system( "CFLAGS='-Werror -Wall -Wextra' make" )
  96. and abort "Failed to build: only $curve\n";
  97. system( "make test" )
  98. and abort "Failed test suite: only $curve\n";
  99. system( "scripts/config.pl unset $curve" )
  100. and abort "Failed to disable $curve\n";
  101. }
  102. system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
  103. # Test with $curve disabled but the others enabled, for each $curve.
  104. for my $curve (@curves) {
  105. system( "cp $config_h.bak $config_h" ) and die "$config_h not restored\n";
  106. system( "make clean" ) and die;
  107. # depends on a specific curve. Also, ignore error if it wasn't enabled
  108. system( "scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED" );
  109. print "\n******************************************\n";
  110. print "* Testing without curve: $curve\n";
  111. print "******************************************\n";
  112. $ENV{MBEDTLS_TEST_CONFIGURATION} = "-$curve";
  113. system( "scripts/config.py unset $curve" )
  114. and abort "Failed to disable $curve\n";
  115. system( "CFLAGS='-Werror -Wall -Wextra' make" )
  116. and abort "Failed to build: all but $curve\n";
  117. system( "make test" )
  118. and abort "Failed test suite: all but $curve\n";
  119. }
  120. system( "mv $config_h.bak $config_h" ) and die "$config_h not restored\n";
  121. system( "make clean" ) and die;
  122. exit 0;