gen_gcm_encrypt.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 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. my $cnt = 1;;
  76. while (my $line = <TEST_DATA>)
  77. {
  78. my $key_len = get_suite_val("Keylen");
  79. next if ($key_len !~ /\d+/);
  80. my $iv_len = get_suite_val("IVlen");
  81. my $pt_len = get_suite_val("PTlen");
  82. my $add_len = get_suite_val("AADlen");
  83. my $tag_len = get_suite_val("Taglen");
  84. for ($cnt = 0; $cnt < 3; $cnt++)
  85. {
  86. my $Count = get_val("Count");
  87. my $key = get_val("Key");
  88. my $pt = get_val("PT");
  89. my $add = get_val("AAD");
  90. my $iv = get_val("IV");
  91. my $ct = get_val("CT");
  92. my $tag = get_val("Tag");
  93. print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
  94. print("gcm_encrypt_and_tag");
  95. print(":\"$key\"");
  96. print(":\"$pt\"");
  97. print(":\"$iv\"");
  98. print(":\"$add\"");
  99. print(":\"$ct\"");
  100. print(":$tag_len");
  101. print(":\"$tag\"");
  102. print(":0");
  103. print("\n\n");
  104. }
  105. }
  106. print("GCM Selftest\n");
  107. print("gcm_selftest:\n\n");
  108. close(TEST_DATA);