gen_ctr_drbg.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. # not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. use strict;
  22. my $file = shift;
  23. open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
  24. sub get_suite_val($)
  25. {
  26. my $name = shift;
  27. my $val = "";
  28. my $line = <TEST_DATA>;
  29. ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
  30. return $val;
  31. }
  32. sub get_val($)
  33. {
  34. my $name = shift;
  35. my $val = "";
  36. my $line;
  37. while($line = <TEST_DATA>)
  38. {
  39. next if($line !~ /=/);
  40. last;
  41. }
  42. ($val) = ($line =~ /^$name = (\w+)/);
  43. return $val;
  44. }
  45. my $cnt = 1;;
  46. while (my $line = <TEST_DATA>)
  47. {
  48. next if ($line !~ /^\[AES-256 use df/);
  49. my $PredictionResistanceStr = get_suite_val("PredictionResistance");
  50. my $PredictionResistance = 0;
  51. $PredictionResistance = 1 if ($PredictionResistanceStr eq 'True');
  52. my $EntropyInputLen = get_suite_val("EntropyInputLen");
  53. my $NonceLen = get_suite_val("NonceLen");
  54. my $PersonalizationStringLen = get_suite_val("PersonalizationStringLen");
  55. my $AdditionalInputLen = get_suite_val("AdditionalInputLen");
  56. for ($cnt = 0; $cnt < 15; $cnt++)
  57. {
  58. my $Count = get_val("COUNT");
  59. my $EntropyInput = get_val("EntropyInput");
  60. my $Nonce = get_val("Nonce");
  61. my $PersonalizationString = get_val("PersonalizationString");
  62. my $AdditionalInput1 = get_val("AdditionalInput");
  63. my $EntropyInputPR1 = get_val("EntropyInputPR") if ($PredictionResistance == 1);
  64. my $EntropyInputReseed = get_val("EntropyInputReseed") if ($PredictionResistance == 0);
  65. my $AdditionalInputReseed = get_val("AdditionalInputReseed") if ($PredictionResistance == 0);
  66. my $AdditionalInput2 = get_val("AdditionalInput");
  67. my $EntropyInputPR2 = get_val("EntropyInputPR") if ($PredictionResistance == 1);
  68. my $ReturnedBits = get_val("ReturnedBits");
  69. if ($PredictionResistance == 1)
  70. {
  71. print("CTR_DRBG NIST Validation (AES-256 use df,$PredictionResistanceStr,$EntropyInputLen,$NonceLen,$PersonalizationStringLen,$AdditionalInputLen) #$Count\n");
  72. print("ctr_drbg_validate_pr");
  73. print(":\"$Nonce$PersonalizationString\"");
  74. print(":\"$EntropyInput$EntropyInputPR1$EntropyInputPR2\"");
  75. print(":\"$AdditionalInput1\"");
  76. print(":\"$AdditionalInput2\"");
  77. print(":\"$ReturnedBits\"");
  78. print("\n\n");
  79. }
  80. else
  81. {
  82. print("CTR_DRBG NIST Validation (AES-256 use df,$PredictionResistanceStr,$EntropyInputLen,$NonceLen,$PersonalizationStringLen,$AdditionalInputLen) #$Count\n");
  83. print("ctr_drbg_validate_nopr");
  84. print(":\"$Nonce$PersonalizationString\"");
  85. print(":\"$EntropyInput$EntropyInputReseed\"");
  86. print(":\"$AdditionalInput1\"");
  87. print(":\"$AdditionalInputReseed\"");
  88. print(":\"$AdditionalInput2\"");
  89. print(":\"$ReturnedBits\"");
  90. print("\n\n");
  91. }
  92. }
  93. }
  94. close(TEST_DATA);