gen_gcm_decrypt.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env perl
  2. #
  3. # Based on NIST gcmDecryptxxx.rsp validation files
  4. # Only first 3 of every set used for compile time saving
  5. #
  6. # Copyright The Mbed TLS Contributors
  7. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  8. #
  9. # This file is provided under the Apache License 2.0, or the
  10. # GNU General Public License v2.0 or later.
  11. #
  12. # **********
  13. # Apache License 2.0:
  14. #
  15. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  16. # not use this file except in compliance with the License.
  17. # You may obtain a copy of the License at
  18. #
  19. # http://www.apache.org/licenses/LICENSE-2.0
  20. #
  21. # Unless required by applicable law or agreed to in writing, software
  22. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. # See the License for the specific language governing permissions and
  25. # limitations under the License.
  26. #
  27. # **********
  28. #
  29. # **********
  30. # GNU General Public License v2.0 or later:
  31. #
  32. # This program is free software; you can redistribute it and/or modify
  33. # it under the terms of the GNU General Public License as published by
  34. # the Free Software Foundation; either version 2 of the License, or
  35. # (at your option) any later version.
  36. #
  37. # This program is distributed in the hope that it will be useful,
  38. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. # GNU General Public License for more details.
  41. #
  42. # You should have received a copy of the GNU General Public License along
  43. # with this program; if not, write to the Free Software Foundation, Inc.,
  44. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  45. #
  46. # **********
  47. use strict;
  48. my $file = shift;
  49. open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
  50. sub get_suite_val($)
  51. {
  52. my $name = shift;
  53. my $val = "";
  54. while(my $line = <TEST_DATA>)
  55. {
  56. next if ($line !~ /^\[/);
  57. ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
  58. last;
  59. }
  60. return $val;
  61. }
  62. sub get_val($)
  63. {
  64. my $name = shift;
  65. my $val = "";
  66. my $line;
  67. while($line = <TEST_DATA>)
  68. {
  69. next if($line !~ /=/);
  70. last;
  71. }
  72. ($val) = ($line =~ /^$name = (\w+)/);
  73. return $val;
  74. }
  75. sub get_val_or_fail($)
  76. {
  77. my $name = shift;
  78. my $val = "FAIL";
  79. my $line;
  80. while($line = <TEST_DATA>)
  81. {
  82. next if($line !~ /=/ && $line !~ /FAIL/);
  83. last;
  84. }
  85. ($val) = ($line =~ /^$name = (\w+)/) if ($line =~ /=/);
  86. return $val;
  87. }
  88. my $cnt = 1;;
  89. while (my $line = <TEST_DATA>)
  90. {
  91. my $key_len = get_suite_val("Keylen");
  92. next if ($key_len !~ /\d+/);
  93. my $iv_len = get_suite_val("IVlen");
  94. my $pt_len = get_suite_val("PTlen");
  95. my $add_len = get_suite_val("AADlen");
  96. my $tag_len = get_suite_val("Taglen");
  97. for ($cnt = 0; $cnt < 3; $cnt++)
  98. {
  99. my $Count = get_val("Count");
  100. my $key = get_val("Key");
  101. my $iv = get_val("IV");
  102. my $ct = get_val("CT");
  103. my $add = get_val("AAD");
  104. my $tag = get_val("Tag");
  105. my $pt = get_val_or_fail("PT");
  106. print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
  107. print("gcm_decrypt_and_verify");
  108. print(":\"$key\"");
  109. print(":\"$ct\"");
  110. print(":\"$iv\"");
  111. print(":\"$add\"");
  112. print(":$tag_len");
  113. print(":\"$tag\"");
  114. print(":\"$pt\"");
  115. print(":0");
  116. print("\n\n");
  117. }
  118. }
  119. print("GCM Selftest\n");
  120. print("gcm_selftest:\n\n");
  121. close(TEST_DATA);