2
0

20-test_mac.t 7.1 KB

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