1#!/usr/bin/env perl
2#
3# Based on NIST gcmDecryptxxx.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
8#
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
21use strict;
22
23my $file = shift;
24
25open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
26
27sub get_suite_val($)
28{
29    my $name = shift;
30    my $val = "";
31
32    while(my $line = <TEST_DATA>)
33    {
34        next if ($line !~ /^\[/);
35        ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
36        last;
37    }
38
39    return $val;
40}
41
42sub get_val($)
43{
44    my $name = shift;
45    my $val = "";
46    my $line;
47
48    while($line = <TEST_DATA>)
49    {
50        next if($line !~ /=/);
51        last;
52    }
53
54    ($val) = ($line =~ /^$name = (\w+)/);
55
56    return $val;
57}
58
59sub get_val_or_fail($)
60{
61    my $name = shift;
62    my $val = "FAIL";
63    my $line;
64
65    while($line = <TEST_DATA>)
66    {
67        next if($line !~ /=/ && $line !~ /FAIL/);
68        last;
69    }
70
71    ($val) = ($line =~ /^$name = (\w+)/) if ($line =~ /=/);
72
73    return $val;
74}
75
76my $cnt = 1;;
77while (my $line = <TEST_DATA>)
78{
79    my $key_len = get_suite_val("Keylen");
80    next if ($key_len !~ /\d+/);
81    my $iv_len = get_suite_val("IVlen");
82    my $pt_len = get_suite_val("PTlen");
83    my $add_len = get_suite_val("AADlen");
84    my $tag_len = get_suite_val("Taglen");
85
86    for ($cnt = 0; $cnt < 3; $cnt++)
87    {
88        my $Count = get_val("Count");
89        my $key = get_val("Key");
90        my $iv = get_val("IV");
91        my $ct = get_val("CT");
92        my $add = get_val("AAD");
93        my $tag = get_val("Tag");
94        my $pt = get_val_or_fail("PT");
95
96        print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
97        print("gcm_decrypt_and_verify");
98        print(":\"$key\"");
99        print(":\"$ct\"");
100        print(":\"$iv\"");
101        print(":\"$add\"");
102        print(":$tag_len");
103        print(":\"$tag\"");
104        print(":\"$pt\"");
105        print(":0");
106        print("\n\n");
107    }
108}
109
110print("GCM Selftest\n");
111print("gcm_selftest:\n\n");
112
113close(TEST_DATA);
114