auth: add HMAC implementation

This allows for checking the SHA-512 implementation against Wycheproof
via the HMAC tests.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-06 12:41:44 +00:00
parent 216ef8f940
commit ad42d99e0b
9 changed files with 554 additions and 5 deletions

View file

@ -1,17 +1,19 @@
.PATH: ${.CURDIR}/..
AEAD= wycheproof_aead
MAC= wycheproof_mac
PROGS= ${AEAD}
PROGS= ${AEAD} ${MAC}
NOMAN= noman
SRCS_wycheproof_aead= wycheproof_aead.c
SRCS_wycheproof_mac= wycheproof_mac.c
DPADD+= ${.CURDIR}/../lib/obj/liblilcrypto.a
LDADD+= ${.CURDIR}/../lib/obj/liblilcrypto.a
tests: all tests-aead
tests: all tests-aead tests-mac
tests-aead:
.ifndef WYCHEPROOF_DIR
@ -23,4 +25,16 @@ tests-aead:
${WYCHEPROOF_DIR}/testvectors_v1/chacha20_poly1305_test.json
.endfor
tests-mac:
.ifndef WYCHEPROOF_DIR
@echo Undefined WYCHEPROOF_DIR; false
.endif
.for p in ${MAC}
perl ${.CURDIR}/mac.pl ${TESTOPTS} -x ./${p} \
${WYCHEPROOF_DIR}/testvectors/hmac_sha384_test.json \
${WYCHEPROOF_DIR}/testvectors_v1/hmac_sha384_test.json \
${WYCHEPROOF_DIR}/testvectors/hmac_sha512_test.json \
${WYCHEPROOF_DIR}/testvectors_v1/hmac_sha512_test.json
.endfor
.include <bsd.prog.mk>

66
wycheproof/mac.pl Normal file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env perl
use v5.38;;
use strict;
use warnings;
use Getopt::Std;
use JSON::PP;
my $progname = $0 =~ s@.*/@@r;
sub slurp ($fh) { local $/; <$fh> }
sub usage ()
{
say STDERR "Usage: $progname -x runner json_file [json_files ...]";
exit 1;
}
sub main ()
{
my %opts;
my $rc = 0;
getopts("vx:", \%opts) && @ARGV > 0 or usage;
usage unless defined $opts{"x"};
for my $f (@ARGV) {
open(my $fh, "<", $f) or die "open failed: $!";
my $json = decode_json(slurp($fh));
for my $testgroup ($json->{testGroups}->@*) {
for my $test ($testgroup->{tests}->@*) {
my @args;
push(@args, $json->{algorithm});
push(@args, "-K", $testgroup->{keySize});
push(@args, "-k", $test->{key});
push(@args, "-m", $test->{msg});
push(@args, "-T", $testgroup->{tagSize});
push(@args, "-t", $test->{tag});
push(@args, "-v") if $opts{"v"};
open(my $th, "-|", $opts{"x"}, @args) or die;
my $result = slurp($th);
close($th);
chomp($result);
if ($result ne $test->{result}) {
$rc = 1;
say STDERR "case $test->{tcId}: ",
"expected $test->{result}: ",
"$test->{comment} [",
join(",", $test->{flags}->@*),
"]";
}
}
}
close($fh);
}
say "ALL TESTS PASSED!" if $rc == 0;
return $rc;
}
exit main;