1#!/usr/bin/env perl
2#
3# Based on NIST gcmEncryptIntIVxxx.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
59my $cnt = 1;;
60while (my $line = <TEST_DATA>)
61{
62    my $key_len = get_suite_val("Keylen");
63    next if ($key_len !~ /\d+/);
64    my $iv_len = get_suite_val("IVlen");
65    my $pt_len = get_suite_val("PTlen");
66    my $add_len = get_suite_val("AADlen");
67    my $tag_len = get_suite_val("Taglen");
68
69    for ($cnt = 0; $cnt < 3; $cnt++)
70    {
71        my $Count = get_val("Count");
72        my $key = get_val("Key");
73        my $pt = get_val("PT");
74        my $add = get_val("AAD");
75        my $iv = get_val("IV");
76        my $ct = get_val("CT");
77        my $tag = get_val("Tag");
78
79        print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
80        print("gcm_encrypt_and_tag");
81        print(":\"$key\"");
82        print(":\"$pt\"");
83        print(":\"$iv\"");
84        print(":\"$add\"");
85        print(":\"$ct\"");
86        print(":$tag_len");
87        print(":\"$tag\"");
88        print(":0");
89        print("\n\n");
90    }
91}
92
93print("GCM Selftest\n");
94print("gcm_selftest:\n\n");
95
96close(TEST_DATA);
97