gen_pkcs1_v21_sign_verify.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. #
  6. # This file is provided under the Apache License 2.0, or the
  7. # GNU General Public License v2.0 or later.
  8. #
  9. # **********
  10. # Apache License 2.0:
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. # not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # **********
  25. #
  26. # **********
  27. # GNU General Public License v2.0 or later:
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation; either version 2 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License along
  40. # with this program; if not, write to the Free Software Foundation, Inc.,
  41. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  42. #
  43. # **********
  44. use strict;
  45. my $file = shift;
  46. open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
  47. sub get_val($$)
  48. {
  49. my $str = shift;
  50. my $name = shift;
  51. my $val = "";
  52. while(my $line = <TEST_DATA>)
  53. {
  54. next if($line !~ /^# $str/);
  55. last;
  56. }
  57. while(my $line = <TEST_DATA>)
  58. {
  59. last if($line eq "\r\n");
  60. $val .= $line;
  61. }
  62. $val =~ s/[ \r\n]//g;
  63. return $val;
  64. }
  65. my $state = 0;
  66. my $val_n = "";
  67. my $val_e = "";
  68. my $val_p = "";
  69. my $val_q = "";
  70. my $mod = 0;
  71. my $cnt = 1;
  72. while (my $line = <TEST_DATA>)
  73. {
  74. next if ($line !~ /^# Example/);
  75. ( $mod ) = ($line =~ /A (\d+)/);
  76. $val_n = get_val("RSA modulus n", "N");
  77. $val_e = get_val("RSA public exponent e", "E");
  78. $val_p = get_val("Prime p", "P");
  79. $val_q = get_val("Prime q", "Q");
  80. for(my $i = 1; $i <= 6; $i++)
  81. {
  82. my $val_m = get_val("Message to be", "M");
  83. my $val_salt = get_val("Salt", "Salt");
  84. my $val_sig = get_val("Signature", "Sig");
  85. print("RSASSA-PSS Signature Example ${cnt}_${i}\n");
  86. print("pkcs1_rsassa_pss_sign:$mod:16:\"$val_p\":16:\"$val_q\":16:\"$val_n\":16:\"$val_e\":SIG_RSA_SHA1:MBEDTLS_MD_SHA1");
  87. print(":\"$val_m\"");
  88. print(":\"$val_salt\"");
  89. print(":\"$val_sig\":0");
  90. print("\n\n");
  91. print("RSASSA-PSS Signature Example ${cnt}_${i} (verify)\n");
  92. print("pkcs1_rsassa_pss_verify:$mod:16:\"$val_n\":16:\"$val_e\":SIG_RSA_SHA1:MBEDTLS_MD_SHA1");
  93. print(":\"$val_m\"");
  94. print(":\"$val_salt\"");
  95. print(":\"$val_sig\":0");
  96. print("\n\n");
  97. }
  98. $cnt++;
  99. }
  100. close(TEST_DATA);