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/digest_test.h | |
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/digest_test.h')
-rw-r--r-- | src/tests/digest_test.h | 118 |
1 files changed, 98 insertions, 20 deletions
diff --git a/src/tests/digest_test.h b/src/tests/digest_test.h index 8bee353..abf1b83 100644 --- a/src/tests/digest_test.h +++ b/src/tests/digest_test.h @@ -7,31 +7,109 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include <ipxe/crypto.h> #include <ipxe/test.h> -/** Maximum number of digest test fragments */ -#define NUM_DIGEST_TEST_FRAG 8 - -/** A digest test fragment list */ -struct digest_test_fragments { - /** Fragment lengths */ - size_t len[NUM_DIGEST_TEST_FRAG]; +/** A digest test */ +struct digest_test { + /** Digest algorithm */ + struct digest_algorithm *digest; + /** Test data */ + const void *data; + /** Length of test data */ + size_t len; + /** Expected digest value */ + const void *expected; + /** Expected digest length */ + size_t expected_len; }; -extern int digest_test ( struct digest_algorithm *digest, - struct digest_test_fragments *fragments, - void *data, size_t len, void *expected ); -extern unsigned long digest_cost ( struct digest_algorithm *digest ); +/** Define inline test data */ +#define DATA(...) { __VA_ARGS__ } + +/** Define inline expected digest value */ +#define DIGEST(...) { __VA_ARGS__ } + +/** + * Define a digest test + * + * @v name Test name + * @v DIGEST Digest algorithm + * @v DATA Test data + * @v EXPECTED Expected digest value + * @ret test Digest test + */ +#define DIGEST_TEST( name, DIGEST, DATA, EXPECTED ) \ + static const uint8_t name ## _data[] = DATA; \ + static const uint8_t name ## _expected[] = EXPECTED; \ + static struct digest_test name = { \ + .digest = DIGEST, \ + .data = name ## _data, \ + .len = sizeof ( name ## _data ), \ + .expected = name ## _expected, \ + .expected_len = sizeof ( name ## _expected ), \ + }; + +/** Standard test vector: empty data */ +#define DIGEST_EMPTY DATA() + +/** Standard test vector: NIST string "abc" + * + * The NIST Cryptographic Toolkit examples for all digest algorithms + * include a test vector which is the unterminated string + * + * "abc" + */ +#define DIGEST_NIST_ABC \ + DATA ( 0x61, 0x62, 0x63 ) + +/** Standard test vector: NIST string "abc...opq" + * + * The NIST Cryptographic Toolkit examples for all 32-bit digest + * algorithms (SHA-1 and the SHA-256 family) include a test vector + * which is the unterminated string + * + * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + */ +#define DIGEST_NIST_ABC_OPQ \ + DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, \ + 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66, \ + 0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69, \ + 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c, \ + 0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, \ + 0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, \ + 0x70, 0x71 ) + +/** Standard test vector: NIST string "abc...stu" + * + * The NIST Cryptographic Toolkit examples for all 64-bit digest + * algorithms (SHA-512 family) include a test vector which is the + * unterminated string + * + * "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + * "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" + */ +#define DIGEST_NIST_ABC_STU \ + DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, \ + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64, \ + 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66, \ + 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68, \ + 0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, \ + 0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, \ + 0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, \ + 0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, \ + 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, \ + 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, \ + 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f, \ + 0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71, \ + 0x72, 0x73, 0x74, 0x75 ) /** - * Report digest test result + * Report a digest test result * - * @v digest Digest algorithm - * @v fragments Digest test fragment list, or NULL - * @v data Test data - * @v len Length of test data - * @v expected Expected digest value + * @v test Digest test */ -#define digest_ok( digest, fragments, data, len, expected ) do { \ - ok ( digest_test ( digest, fragments, data, len, expected ) ); \ - } while ( 0 ) +#define digest_ok(test) digest_okx ( test, __FILE__, __LINE__ ) + +extern void digest_okx ( struct digest_test *test, const char *file, + unsigned int line ); +extern unsigned long digest_cost ( struct digest_algorithm *digest ); #endif /* _DIGEST_TEST_H */ |