gen_gcm_decrypt.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. # not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. use strict;
  21. my $file = shift;
  22. open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
  23. sub get_suite_val($)
  24. {
  25. my $name = shift;
  26. my $val = "";
  27. while(my $line = <TEST_DATA>)
  28. {
  29. next if ($line !~ /^\[/);
  30. ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
  31. last;
  32. }
  33. return $val;
  34. }
  35. sub get_val($)
  36. {
  37. my $name = shift;
  38. my $val = "";
  39. my $line;
  40. while($line = <TEST_DATA>)
  41. {
  42. next if($line !~ /=/);
  43. last;
  44. }
  45. ($val) = ($line =~ /^$name = (\w+)/);
  46. return $val;
  47. }
  48. sub get_val_or_fail($)
  49. {
  50. my $name = shift;
  51. my $val = "FAIL";
  52. my $line;
  53. while($line = <TEST_DATA>)
  54. {
  55. next if($line !~ /=/ && $line !~ /FAIL/);
  56. last;
  57. }
  58. ($val) = ($line =~ /^$name = (\w+)/) if ($line =~ /=/);
  59. return $val;
  60. }
  61. my $cnt = 1;;
  62. while (my $line = <TEST_DATA>)
  63. {
  64. my $key_len = get_suite_val("Keylen");
  65. next if ($key_len !~ /\d+/);
  66. my $iv_len = get_suite_val("IVlen");
  67. my $pt_len = get_suite_val("PTlen");
  68. my $add_len = get_suite_val("AADlen");
  69. my $tag_len = get_suite_val("Taglen");
  70. for ($cnt = 0; $cnt < 3; $cnt++)
  71. {
  72. my $Count = get_val("Count");
  73. my $key = get_val("Key");
  74. my $iv = get_val("IV");
  75. my $ct = get_val("CT");
  76. my $add = get_val("AAD");
  77. my $tag = get_val("Tag");
  78. my $pt = get_val_or_fail("PT");
  79. print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
  80. print("gcm_decrypt_and_verify");
  81. print(":\"$key\"");
  82. print(":\"$ct\"");
  83. print(":\"$iv\"");
  84. print(":\"$add\"");
  85. print(":$tag_len");
  86. print(":\"$tag\"");
  87. print(":\"$pt\"");
  88. print(":0");
  89. print("\n\n");
  90. }
  91. }
  92. print("GCM Selftest\n");
  93. print("gcm_selftest:\n\n");
  94. close(TEST_DATA);