gen_ctr_drbg.pl 4.6 KB

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