1#!/usr/bin/perl
2#
3# Based on NIST gcmDecryptxxx.rsp validation files
4# Only first 3 of every set used for compile time saving
5
6use strict;
7
8my $file = shift;
9
10open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
11
12sub get_suite_val($)
13{
14    my $name = shift;
15    my $val = "";
16
17    while(my $line = <TEST_DATA>)
18    {
19        next if ($line !~ /^\[/);
20        ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
21        last;
22    }
23
24    return $val;
25}
26
27sub get_val($)
28{
29    my $name = shift;
30    my $val = "";
31    my $line;
32
33    while($line = <TEST_DATA>)
34    {
35        next if($line !~ /=/);
36        last;
37    }
38
39    ($val) = ($line =~ /^$name = (\w+)/);
40
41    return $val;
42}
43
44sub get_val_or_fail($)
45{
46    my $name = shift;
47    my $val = "FAIL";
48    my $line;
49
50    while($line = <TEST_DATA>)
51    {
52        next if($line !~ /=/ && $line !~ /FAIL/);
53        last;
54    }
55
56    ($val) = ($line =~ /^$name = (\w+)/) if ($line =~ /=/);
57
58    return $val;
59}
60
61my $cnt = 1;;
62while (my $line = <TEST_DATA>)
63{
64    my $key_len = get_suite_val("Keylen");
65    next if ($key_len !~ /\d+/);
66    my $iv_len = get_suite_val("IVlen");
67    my $pt_len = get_suite_val("PTlen");
68    my $add_len = get_suite_val("AADlen");
69    my $tag_len = get_suite_val("Taglen");
70
71    for ($cnt = 0; $cnt < 3; $cnt++)
72    {
73        my $Count = get_val("Count");
74        my $key = get_val("Key");
75        my $iv = get_val("IV");
76        my $ct = get_val("CT");
77        my $add = get_val("AAD");
78        my $tag = get_val("Tag");
79        my $pt = get_val_or_fail("PT");
80
81        print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
82        print("gcm_decrypt_and_verify");
83        print(":\"$key\"");
84        print(":\"$ct\"");
85        print(":\"$iv\"");
86        print(":\"$add\"");
87        print(":$tag_len");
88        print(":\"$tag\"");
89        print(":\"$pt\"");
90        print(":0");
91        print("\n\n");
92    }
93}
94
95print("GCM Selftest\n");
96print("gcm_selftest:\n\n");
97
98close(TEST_DATA);
99