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
22use strict;
23
24my $file = shift;
25
26open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
27
28sub get_suite_val($)
29{
30    my $name = shift;
31    my $val = "";
32
33    my $line = <TEST_DATA>;
34    ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
35
36    return $val;
37}
38
39sub get_val($)
40{
41    my $name = shift;
42    my $val = "";
43    my $line;
44
45    while($line = <TEST_DATA>)
46    {
47        next if($line !~ /=/);
48        last;
49    }
50
51    ($val) = ($line =~ /^$name = (\w+)/);
52
53    return $val;
54}
55
56my $cnt = 1;;
57while (my $line = <TEST_DATA>)
58{
59    next if ($line !~ /^\[AES-256 use df/);
60
61    my $PredictionResistanceStr = get_suite_val("PredictionResistance");
62    my $PredictionResistance = 0;
63    $PredictionResistance = 1 if ($PredictionResistanceStr eq 'True');
64    my $EntropyInputLen = get_suite_val("EntropyInputLen");
65    my $NonceLen = get_suite_val("NonceLen");
66    my $PersonalizationStringLen = get_suite_val("PersonalizationStringLen");
67    my $AdditionalInputLen = get_suite_val("AdditionalInputLen");
68
69    for ($cnt = 0; $cnt < 15; $cnt++)
70    {
71        my $Count = get_val("COUNT");
72        my $EntropyInput = get_val("EntropyInput");
73        my $Nonce = get_val("Nonce");
74        my $PersonalizationString = get_val("PersonalizationString");
75        my $AdditionalInput1 = get_val("AdditionalInput");
76        my $EntropyInputPR1 = get_val("EntropyInputPR") if ($PredictionResistance == 1);
77        my $EntropyInputReseed = get_val("EntropyInputReseed") if ($PredictionResistance == 0);
78        my $AdditionalInputReseed = get_val("AdditionalInputReseed") if ($PredictionResistance == 0);
79        my $AdditionalInput2 = get_val("AdditionalInput");
80        my $EntropyInputPR2 = get_val("EntropyInputPR") if ($PredictionResistance == 1);
81        my $ReturnedBits = get_val("ReturnedBits");
82
83        if ($PredictionResistance == 1)
84        {
85            print("CTR_DRBG NIST Validation (AES-256 use df,$PredictionResistanceStr,$EntropyInputLen,$NonceLen,$PersonalizationStringLen,$AdditionalInputLen) #$Count\n");
86            print("ctr_drbg_validate_pr");
87            print(":\"$Nonce$PersonalizationString\"");
88            print(":\"$EntropyInput$EntropyInputPR1$EntropyInputPR2\"");
89            print(":\"$AdditionalInput1\"");
90            print(":\"$AdditionalInput2\"");
91            print(":\"$ReturnedBits\"");
92            print("\n\n");
93        }
94        else
95        {
96            print("CTR_DRBG NIST Validation (AES-256 use df,$PredictionResistanceStr,$EntropyInputLen,$NonceLen,$PersonalizationStringLen,$AdditionalInputLen) #$Count\n");
97            print("ctr_drbg_validate_nopr");
98            print(":\"$Nonce$PersonalizationString\"");
99            print(":\"$EntropyInput$EntropyInputReseed\"");
100            print(":\"$AdditionalInput1\"");
101            print(":\"$AdditionalInputReseed\"");
102            print(":\"$AdditionalInput2\"");
103            print(":\"$ReturnedBits\"");
104            print("\n\n");
105        }
106    }
107}
108close(TEST_DATA);
109