Skip to content

Commit

Permalink
Merge pull request #3 from cyphar/hashing-test-vectors
Browse files Browse the repository at this point in the history
hash functions: add tests to verify output against test vectors
  • Loading branch information
archiecobbs authored Jun 5, 2023
2 parents 2a2ff3b + 76377ed commit 48d9e0b
Show file tree
Hide file tree
Showing 6 changed files with 2,275 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ libnbcompat_la_SOURCES=
libnbcompat_la_LIBADD= $(LTLIBOBJS)
EXTRA_libnbcompat_la_DEPENDENCIES: $(LTLIBOBJS)

# Test programs.
TESTS = rmd160-test sha1-test sha2-test
check_PROGRAMS = rmd160-test sha1-test sha2-test
rmd160_test_LDADD = .libs/libnbcompat.a
sha1_test_LDADD = .libs/libnbcompat.a
sha2_test_LDADD = .libs/libnbcompat.a

# It's bad form to install your "config.h", so we tweak it to make it less harmful
install-data-hook:
find $(DESTDIR)$(includedir)/nbcompat.h $(DESTDIR)$(includedir)/nbcompat -name '*.h' -print | while read HDRFILE; do \
Expand Down Expand Up @@ -74,8 +81,11 @@ noinst_HEADERS= private/cclass.h \
private/cname.h \
private/pwcache.h \
private/regex2.h \
private/test-helpers.h \
private/utils.h

noinst_SOURCES = private/cavs2c.awk

if WITH_DB

libnbcompat_la_SOURCES+= db/btree/bt_close.c \
Expand Down
63 changes: 63 additions & 0 deletions private/cas2c.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/awk -f
# $NetBSD: cas2c.awk,v 1.0 2023/06/06 16:12:53 cyphar Exp $

# Copyright (c) 2023 Aleksa Sarai <[email protected]>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# cavs2c - convert a NIST CAVS .rsp file into test vectors to be used in this project
#
# usage: ./cavs2c.awk -v name=digestname < sha-test-vectors/DigestFooBar.rsp
#
# You can download the set of sha test vectors from the NIST website:
# <https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program>
# This script was only tested with the CAVS 11.0 files.

BEGIN {
print "int " name "(void)"
print "{"
print "\tint err = 0;"
print ""
}

/^Len =/ {
printf("\tsize_t len%d = %d;\n", ++vecnum, $3 / 8)
}

/^Msg =/ {
printf("\tconst char vector%d[] = {%s};\n", vecnum, gensub(/([0-9a-f]{2})/, "'\\\\x\\1',", "g", $3))
}

/^MD =/ {
printf("\tconst char *digest%d = \"%s\";\n", vecnum, $3)
printf("\terr |= test_%s(vector%d, len%d, digest%d);\n", name, vecnum, vecnum, vecnum)
print ""
}

END {
print "\treturn err;"
print "}"
}
53 changes: 53 additions & 0 deletions private/test-helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* $NetBSD: test-helpers.h,v 1.0 2023/06/06 16:12:53 cyphar Exp $ */

/*-
* Copyright (c) 2023 Aleksa Sarai <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <nbcompat/vis.h>

#define DEFINE_TEST_FUNC(name, DigestDataFunc) \
int test_ ## name(const char *data, size_t len, const char *expected_digest) \
{ \
int err = 0; \
char buffer[BUFSIZ] = {}; \
char *digest = DigestDataFunc(data, len, buffer); \
if ((err = strcmp(digest, expected_digest) != 0)) { \
char *encoded = malloc(len*4 + 1); \
memset(encoded, '\0', len*4 + 1); \
strvisx(encoded, data, len, VIS_TAB | VIS_NL); \
fprintf(stderr, #name "(%s) = %s != %s\n", encoded, digest, expected_digest); \
free(encoded); \
} \
return err; \
} \
\
int test_ ## name ## _str(const char *data, const char *expected_digest) \
{ \
return test_ ## name(data, strlen(data), expected_digest); \
}
69 changes: 69 additions & 0 deletions rmd160-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* $NetBSD: rmd160-test.c,v 1.0 2023/06/06 16:12:53 cyphar Exp $ */

/*-
* Copyright (c) 2023 Aleksa Sarai <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <nbcompat.h>
#include <nbcompat/stdio.h>
#include <nbcompat/rmd160.h>

#include "private/test-helpers.h"

DEFINE_TEST_FUNC(ripemd160, RMD160Data)

#define VECTOR8_PART "1234567890"
#define VECTOR8 \
VECTOR8_PART VECTOR8_PART VECTOR8_PART VECTOR8_PART \
VECTOR8_PART VECTOR8_PART VECTOR8_PART VECTOR8_PART

char *vector9(void)
{
char *vector = malloc(1000 * 1000 + 1);
memset(vector, 'a', 1000*1000);
vector[1000*1000] = '\0';
return vector;
}

int main(void)
{
int err = 0;

/* Test vectors from <https://homes.esat.kuleuven.be/~bosselae/ripemd160.html>. */
err |= test_ripemd160_str("", "9c1185a5c5e9fc54612808977ee8f548b2258d31");
err |= test_ripemd160_str("a", "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
err |= test_ripemd160_str("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
err |= test_ripemd160_str("message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36");
err |= test_ripemd160_str("abcdefghijklmnopqrstuvwxyz", "f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
err |= test_ripemd160_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "12a053384a9c0c88e405a06c27dcf49ada62eb2b");
err |= test_ripemd160_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "b0e20b6e3116640286ed3a87a5713079b21f5189");
err |= test_ripemd160_str(VECTOR8, "9b752e45573d4b39f4dbd3323cab82bf63326bfb");
err |= test_ripemd160_str(vector9(), "52783243c1697bdbe16d37f97f68f08325dc1528");

return err;
}
Loading

0 comments on commit 48d9e0b

Please sign in to comment.