gen_gcm_encrypt.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env perl
  2. #
  3. # Based on NIST gcmEncryptIntIVxxx.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. my $cnt = 1;;
  49. while (my $line = <TEST_DATA>)
  50. {
  51. my $key_len = get_suite_val("Keylen");
  52. next if ($key_len !~ /\d+/);
  53. my $iv_len = get_suite_val("IVlen");
  54. my $pt_len = get_suite_val("PTlen");
  55. my $add_len = get_suite_val("AADlen");
  56. my $tag_len = get_suite_val("Taglen");
  57. for ($cnt = 0; $cnt < 3; $cnt++)
  58. {
  59. my $Count = get_val("Count");
  60. my $key = get_val("Key");
  61. my $pt = get_val("PT");
  62. my $add = get_val("AAD");
  63. my $iv = get_val("IV");
  64. my $ct = get_val("CT");
  65. my $tag = get_val("Tag");
  66. print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
  67. print("gcm_encrypt_and_tag");
  68. print(":\"$key\"");
  69. print(":\"$pt\"");
  70. print(":\"$iv\"");
  71. print(":\"$add\"");
  72. print(":\"$ct\"");
  73. print(":$tag_len");
  74. print(":\"$tag\"");
  75. print(":0");
  76. print("\n\n");
  77. }
  78. }
  79. print("GCM Selftest\n");
  80. print("gcm_selftest:\n\n");
  81. close(TEST_DATA);