]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/recipes/20-test_mac.t
Amend references to "OpenSSL license"
[thirdparty/openssl.git] / test / recipes / 20-test_mac.t
CommitLineData
4d768e96
SL
1#! /usr/bin/env perl
2# Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3#
a6ed19dc 4# Licensed under the Apache License 2.0 (the "License"). You may not use
4d768e96
SL
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9
10use strict;
11use warnings;
12
13use OpenSSL::Test;
14use OpenSSL::Test::Utils;
15use Storable qw(dclone);
16
17setup("test_mac");
18
19my @mac_tests = (
4d768e96
SL
20 { cmd => [qw{openssl mac -macopt digest:SHA1 -macopt hexkey:000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F}],
21 type => 'HMAC',
22 input => unpack("H*", "Sample message for keylen=blocklen"),
23 expected => '5FD596EE78D5553C8FF4E72D266DFD192366DA29',
24 desc => 'HMAC SHA1' },
4d768e96
SL
25 { cmd => [qw{openssl mac -macopt cipher:AES-256-GCM -macopt hexkey:4C973DBC7364621674F8B5B89E5C15511FCED9216490FB1C1A2CAA0FFE0407E5 -macopt hexiv:7AE8E2CA4EC500012E58495C}],
26 type => 'GMAC',
27 input => '68F2E77696CE7AE8E2CA4EC588E541002E58495C08000F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D0007',
28 expected => '00BDA1B7E87608BCBF470F12157F4C07',
29 desc => 'GMAC' },
30 { cmd => [qw{openssl mac -macopt hexkey:404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F -macopt xof:0}],
31 type => 'KMAC128',
32 input => '00010203',
33 expected => 'E5780B0D3EA6F7D3A429C5706AA43A00FADBD7D49628839E3187243F456EE14E',
34 desc => 'KMAC128' },
35 { cmd => [qw{openssl mac -macopt hexkey:404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F -macopt }, 'custom:My Tagged Application'],
36 type => 'KMAC256',
37 input => '00010203',
38 expected => '20C570C31346F703C9AC36C61C03CB64C3970D0CFC787E9B79599D273A68D2F7F69D4CC3DE9D104A351689F27CF6F5951F0103F33F4F24871024D9C27773A8DD',
39 desc => 'KMAC256' },
40 { cmd => [qw{openssl mac -macopt hexkey:404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F -macopt xof:1 -macopt}, 'custom:My Tagged Application'],
41 type => 'KMAC256',
42 input => '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7',
43 expected => 'D5BE731C954ED7732846BB59DBE3A8E30F83E77A4BFF4459F2F1C2B4ECEBB8CE67BA01C62E8AB8578D2D499BD1BB276768781190020A306A97DE281DCC30305D',
44 desc => 'KMAC256 with xof len of 64' },
45);
46
5810bbd8
MC
47my @siphash_tests = (
48 { cmd => [qw{openssl mac -macopt hexkey:000102030405060708090A0B0C0D0E0F}],
4d768e96
SL
49 type => 'SipHash',
50 input => '00',
5810bbd8
MC
51 expected => 'da87c1d86b99af44347659119b22fc45',
52 desc => 'SipHash No input' }
53);
54
55my @cmac_tests = (
56 { cmd => [qw{openssl mac -macopt cipher:AES-256-CBC -macopt hexkey:0B122AC8F34ED1FE082A3625D157561454167AC145A10BBF77C6A70596D574F1}],
57 type => 'CMAC',
58 input => '498B53FDEC87EDCBF07097DCCDE93A084BAD7501A224E388DF349CE18959FE8485F8AD1537F0D896EA73BEDC7214713F',
59 expected => 'F62C46329B41085625669BAF51DEA66A',
60 desc => 'CMAC AES-256-CBC' }
61);
62
63my @poly1305_tests = (
64 { cmd => [qw{openssl mac -macopt hexkey:02000000000000000000000000000000ffffffffffffffffffffffffffffffff}],
65 type => 'Poly1305',
66 input => '02000000000000000000000000000000',
67 expected => '03000000000000000000000000000000',
68 desc => 'Poly1305 (wrap 2^128)' },
69);
70
71push @mac_tests, @siphash_tests unless disabled("siphash");
72push @mac_tests, @cmac_tests unless disabled("cmac");
73push @mac_tests, @poly1305_tests unless disabled("poly1305");
74
75my @mac_fail_tests = (
4d768e96
SL
76 { cmd => [qw{openssl mac}],
77 type => 'KMAC128',
78 input => '00',
79 err => 'EVP_MAC_Init',
80 desc => 'KMAC128 Fail no key' },
81);
82
5810bbd8
MC
83my @siphash_fail_tests = (
84 { cmd => [qw{openssl mac}],
85 type => 'SipHash',
86 input => '00',
87 err => '',
88 desc => 'SipHash Fail no key' },
89);
90
91push @mac_fail_tests, @siphash_fail_tests unless disabled("siphash");
92
4d768e96
SL
93plan tests => (scalar @mac_tests * 2) + scalar @mac_fail_tests;
94
95foreach (@mac_tests) {
96 ok(compareline($_->{cmd}, $_->{type}, $_->{input}, $_->{expected}, $_->{err}), $_->{desc});
97}
98foreach (@mac_tests) {
99 ok(comparefile($_->{cmd}, $_->{type}, $_->{input}, $_->{expected}), $_->{desc});
100}
101
102foreach (@mac_fail_tests) {
103 ok(compareline($_->{cmd}, $_->{type}, $_->{input}, $_->{expected}, $_->{err}), $_->{desc});
104}
105
106# Create a temp input file and save the input data into it, and
107# then compare the stdout output matches the expected value.
108sub compareline {
109 my $tmpfile = 'tmp.bin';
110 my ($cmdarray_orig, $type, $input, $expect, $err) = @_;
111 my $cmdarray = dclone $cmdarray_orig;
112 if (defined($expect)) {
113 $expect = uc $expect;
114 }
115 # Open a temporary input file and write $input to it
116 open(my $in, '>', $tmpfile) or die "Could not open file";
117 binmode($in);
118 my $bin = pack("H*", $input);
119 print $in $bin;
120 close $in;
121
122 # The last cmd parameter is the temporary input file we just created.
123 my @other = ('-in', $tmpfile, $type);
124 push @$cmdarray, @other;
125
126 my @lines = run(app($cmdarray), capture => 1);
127 unlink $tmpfile;
128
129 if (defined($expect)) {
130 if ($lines[1] =~ m|^\Q${expect}\E\R$|) {
131 return 1;
132 } else {
133 print "Got: $lines[1]";
134 print "Exp: $expect\n";
135 return 0;
136 }
137 }
138 if (defined($err)) {
139 if (defined($lines[0])) {
140 $lines[0] =~ s/\s+$//;
141 if ($lines[0] eq $err) {
142 return 1;
143 } else {
144 print "Got: $lines[0]";
145 print "Exp: $err\n";
146 return 0;
147 }
148 } else {
149 # expected an error
150 return 1;
151 }
152 }
153 return 0;
154}
155
156# Create a temp input file and save the input data into it, and
157# use the '-bin -out <file>' commandline options to save results out to a file.
158# Read this file back in and check its output matches the expected value.
159sub comparefile {
160 my $tmpfile = 'tmp.bin';
161 my $outfile = 'out.bin';
162 my ($cmdarray, $type, $input, $expect) = @_;
163 $expect = uc $expect;
164
165 # Open a temporary input file and write $input to it
166 open(my $in, '>', $tmpfile) or die "Could not open file";
167 binmode($in);
168 my $bin = pack("H*", $input);
169 print $in $bin;
170 close $in;
171
172 my @other = ("-binary", "-in", $tmpfile, "-out", $outfile, $type);
173 push @$cmdarray, @other;
174
175 run(app($cmdarray));
176 unlink $tmpfile;
177 open(my $out, '<', $outfile) or die "Could not open file";
178 binmode($out);
179 my $buffer;
180 my $BUFSIZE = 1024;
181 read($out, $buffer, $BUFSIZE) or die "unable to read";
182
183 my $line = uc unpack("H*", $buffer);
184 close($out);
185 unlink $outfile;
186
187 if ($line eq $expect) {
188 return 1;
189 } else {
190 print "Got: $line\n";
191 print "Exp: $expect\n";
192 return 0;
193 }
194}