Add KDF interface and HKDF implementation

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-18 14:30:06 +00:00
parent cef67c9f09
commit 5eb28b420d
9 changed files with 570 additions and 7 deletions

View file

@ -1,19 +1,21 @@
.PATH: ${.CURDIR}/..
AEAD= wycheproof_aead
HKDF= wycheproof_hkdf
MAC= wycheproof_mac
PROGS= ${AEAD} ${MAC}
PROGS= ${AEAD} ${HKDF} ${MAC}
NOMAN= noman
SRCS_wycheproof_aead= wycheproof_aead.c
SRCS_wycheproof_hkdf= wycheproof_hkdf.c
SRCS_wycheproof_mac= wycheproof_mac.c
DPADD+= ${.CURDIR}/../lib/obj/liblilcrypto.a
LDADD+= ${.CURDIR}/../lib/obj/liblilcrypto.a
tests: all tests-aead tests-mac
tests: all tests-aead tests-hkdf tests-mac
tests-aead:
.ifndef WYCHEPROOF_DIR
@ -27,6 +29,22 @@ tests-aead:
${WYCHEPROOF_DIR}/testvectors_v1/xchacha20_poly1305_test.json
.endfor
tests-hkdf:
.ifndef WYCHEPROOF_DIR
@echo Undefined WYCHEPROOF_DIR; false
.endif
.for p in ${HKDF}
perl ${.CURDIR}/hkdf.pl ${TESTOPTS} -x ./${p} \
${WYCHEPROOF_DIR}/testvectors/hkdf_sha256_test.json \
${WYCHEPROOF_DIR}/testvectors_v1/hkdf_sha256_test.json \
${WYCHEPROOF_DIR}/testvectors/hkdf_sha384_test.json \
${WYCHEPROOF_DIR}/testvectors_v1/hkdf_sha384_test.json \
${WYCHEPROOF_DIR}/testvectors/hkdf_sha512_test.json \
${WYCHEPROOF_DIR}/testvectors_v1/hkdf_sha512_test.json
.endfor
.include <bsd.prog.mk>
tests-mac:
.ifndef WYCHEPROOF_DIR
@echo Undefined WYCHEPROOF_DIR; false

69
wycheproof/hkdf.pl Normal file
View file

@ -0,0 +1,69 @@
#!/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 [-Cv] -x runner json_file ",
"[json_files ...]";
exit 1;
}
sub main ()
{
my %opts;
my $rc = 0;
getopts("Cvx:", \%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, "-i", $test->{info});
push(@args, "-K", $testgroup->{keySize});
push(@args, "-k", $test->{ikm});
push(@args, "-o", $test->{okm});
push(@args, "-s", $test->{salt});
push(@args, "-z", $test->{size});
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}->@*),
"]";
exit 1 unless $opts{"C"};
}
}
}
close($fh);
}
say "ALL TESTS PASSED!" if $rc == 0;
return $rc;
}
exit main;