-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cyphar/hashing-test-vectors
hash functions: add tests to verify output against test vectors
- Loading branch information
Showing
6 changed files
with
2,275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); \ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.