diff options
author | Michael Brown <mcb30@ipxe.org> | 2015-04-12 12:41:08 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2015-04-12 17:01:10 +0100 |
commit | a9da1291227735fdae475e21b02b2a5fa29dd956 (patch) | |
tree | 8d689b12395549809ebf408b9afe0fadaa0459c2 /src/tests/sha256_test.c | |
parent | b12b1b620fffc89e86af3879a945e7ffaa7c141d (diff) | |
download | ipxe-a9da1291227735fdae475e21b02b2a5fa29dd956.zip ipxe-a9da1291227735fdae475e21b02b2a5fa29dd956.tar.gz ipxe-a9da1291227735fdae475e21b02b2a5fa29dd956.tar.bz2 |
[test] Simplify digest algorithm self-tests
Update the digest self-tests to use okx(), and centralise concepts and
data shared between tests for multiple algorithms to reduce duplicated
code.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests/sha256_test.c')
-rw-r--r-- | src/tests/sha256_test.c | 92 |
1 files changed, 32 insertions, 60 deletions
diff --git a/src/tests/sha256_test.c b/src/tests/sha256_test.c index f8ba3a0..8075646 100644 --- a/src/tests/sha256_test.c +++ b/src/tests/sha256_test.c @@ -27,82 +27,54 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * SHA-256 tests * + * NIST test vectors are taken from + * + * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf + * */ -#include <stdint.h> +/* Forcibly enable assertions */ +#undef NDEBUG + #include <ipxe/sha256.h> #include <ipxe/test.h> #include "digest_test.h" -/** An SHA-256 test vector */ -struct sha256_test_vector { - /** Test data */ - void *data; - /** Test data length */ - size_t len; - /** Expected digest */ - uint8_t digest[SHA256_DIGEST_SIZE]; -}; +/* Empty test vector (digest obtained from "sha256sum /dev/null") */ +DIGEST_TEST ( sha256_empty, &sha256_algorithm, DIGEST_EMPTY, + DIGEST ( 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, + 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, + 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, + 0x1b, 0x78, 0x52, 0xb8, 0x55 ) ); -/** SHA-256 test vectors */ -static struct sha256_test_vector sha256_test_vectors[] = { - /* Empty test data - * - * Expected digest value obtained from "sha256sum /dev/null" - */ - { NULL, 0, - { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, - 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, - 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 } }, - /* Test data and expected digests taken from the NIST - * Cryptographic Toolkit Algorithm Examples at - * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf - */ - { "abc", 3, - { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, - 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, - 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, - { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, - 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, - 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } }, -}; +/* NIST test vector "abc" */ +DIGEST_TEST ( sha256_nist_abc, &sha256_algorithm, DIGEST_NIST_ABC, + DIGEST ( 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, + 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, + 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, + 0x61, 0xf2, 0x00, 0x15, 0xad ) ); -/** SHA-256 test fragment lists */ -static struct digest_test_fragments sha256_test_fragments[] = { - { { 0, -1UL, } }, - { { 1, 1, 1, 1, 1, 1, 1, 1 } }, - { { 2, 0, 23, 4, 6, 1, 0 } }, -}; +/* NIST test vector "abc...opq" */ +DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ, + DIGEST ( 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, + 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, + 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, + 0xd4, 0x19, 0xdb, 0x06, 0xc1 ) ); /** * Perform SHA-256 self-test * */ static void sha256_test_exec ( void ) { - struct digest_algorithm *digest = &sha256_algorithm; - struct sha256_test_vector *test; - unsigned long cost; - unsigned int i; - unsigned int j; - /* Correctness test */ - for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) / - sizeof ( sha256_test_vectors[0] ) ) ; i++ ) { - test = &sha256_test_vectors[i]; - /* Test with a single pass */ - digest_ok ( digest, NULL, test->data, test->len, test->digest ); - /* Test with fragment lists */ - for ( j = 0 ; j < ( sizeof ( sha256_test_fragments ) / - sizeof ( sha256_test_fragments[0] )); j++ ){ - digest_ok ( digest, &sha256_test_fragments[j], - test->data, test->len, test->digest ); - } - } + /* Correctness tests */ + digest_ok ( &sha256_empty ); + digest_ok ( &sha256_nist_abc ); + digest_ok ( &sha256_nist_abc_opq ); - /* Speed test */ - cost = digest_cost ( digest ); - DBG ( "SHA256 required %ld cycles per byte\n", cost ); + /* Speed tests */ + DBG ( "SHA256 required %ld cycles per byte\n", + digest_cost ( &sha256_algorithm ) ); } /** SHA-256 self-test */ |