• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OR GPL-2.0-or-later
8#
9# This file is provided under the Apache License 2.0, or the
10# GNU General Public License v2.0 or later.
11#
12# **********
13# Apache License 2.0:
14#
15# Licensed under the Apache License, Version 2.0 (the "License"); you may
16# not use this file except in compliance with the License.
17# You may obtain a copy of the License at
18#
19# http://www.apache.org/licenses/LICENSE-2.0
20#
21# Unless required by applicable law or agreed to in writing, software
22# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24# See the License for the specific language governing permissions and
25# limitations under the License.
26#
27# **********
28#
29# **********
30# GNU General Public License v2.0 or later:
31#
32# This program is free software; you can redistribute it and/or modify
33# it under the terms of the GNU General Public License as published by
34# the Free Software Foundation; either version 2 of the License, or
35# (at your option) any later version.
36#
37# This program is distributed in the hope that it will be useful,
38# but WITHOUT ANY WARRANTY; without even the implied warranty of
39# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40# GNU General Public License for more details.
41#
42# You should have received a copy of the GNU General Public License along
43# with this program; if not, write to the Free Software Foundation, Inc.,
44# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45#
46# **********
47
48use strict;
49
50my $file = shift;
51
52open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
53
54sub get_suite_val($)
55{
56    my $name = shift;
57    my $val = "";
58
59    while(my $line = <TEST_DATA>)
60    {
61        next if ($line !~ /^\[/);
62        ($val) = ($line =~ /\[$name\s\=\s(\w+)\]/);
63        last;
64    }
65
66    return $val;
67}
68
69sub get_val($)
70{
71    my $name = shift;
72    my $val = "";
73    my $line;
74
75    while($line = <TEST_DATA>)
76    {
77        next if($line !~ /=/);
78        last;
79    }
80
81    ($val) = ($line =~ /^$name = (\w+)/);
82
83    return $val;
84}
85
86sub get_val_or_fail($)
87{
88    my $name = shift;
89    my $val = "FAIL";
90    my $line;
91
92    while($line = <TEST_DATA>)
93    {
94        next if($line !~ /=/ && $line !~ /FAIL/);
95        last;
96    }
97
98    ($val) = ($line =~ /^$name = (\w+)/) if ($line =~ /=/);
99
100    return $val;
101}
102
103my $cnt = 1;;
104while (my $line = <TEST_DATA>)
105{
106    my $key_len = get_suite_val("Keylen");
107    next if ($key_len !~ /\d+/);
108    my $iv_len = get_suite_val("IVlen");
109    my $pt_len = get_suite_val("PTlen");
110    my $add_len = get_suite_val("AADlen");
111    my $tag_len = get_suite_val("Taglen");
112
113    for ($cnt = 0; $cnt < 3; $cnt++)
114    {
115        my $Count = get_val("Count");
116        my $key = get_val("Key");
117        my $iv = get_val("IV");
118        my $ct = get_val("CT");
119        my $add = get_val("AAD");
120        my $tag = get_val("Tag");
121        my $pt = get_val_or_fail("PT");
122
123        print("GCM NIST Validation (AES-$key_len,$iv_len,$pt_len,$add_len,$tag_len) #$Count\n");
124        print("gcm_decrypt_and_verify");
125        print(":\"$key\"");
126        print(":\"$ct\"");
127        print(":\"$iv\"");
128        print(":\"$add\"");
129        print(":$tag_len");
130        print(":\"$tag\"");
131        print(":\"$pt\"");
132        print(":0");
133        print("\n\n");
134    }
135}
136
137print("GCM Selftest\n");
138print("gcm_selftest:\n\n");
139
140close(TEST_DATA);
141