Browse Source

[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>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
a9da129122
5 changed files with 235 additions and 200 deletions
  1. 52
    11
      src/tests/digest_test.c
  2. 98
    20
      src/tests/digest_test.h
  3. 24
    52
      src/tests/md5_test.c
  4. 29
    57
      src/tests/sha1_test.c
  5. 32
    60
      src/tests/sha256_test.c

+ 52
- 11
src/tests/digest_test.c View File

@@ -38,27 +38,47 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
38 38
 #include <ipxe/profile.h>
39 39
 #include "digest_test.h"
40 40
 
41
+/** Maximum number of digest test fragments */
42
+#define NUM_DIGEST_TEST_FRAG 8
43
+
44
+/** A digest test fragment list */
45
+struct digest_test_fragments {
46
+	/** Fragment lengths */
47
+	size_t len[NUM_DIGEST_TEST_FRAG];
48
+};
49
+
50
+/** Digest test fragment lists */
51
+static struct digest_test_fragments digest_test_fragments[] = {
52
+	{ { 0, -1UL, } },
53
+	{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
54
+	{ { 2, 0, 23, 4, 6, 1, 0 } },
55
+};
56
+
41 57
 /** Number of sample iterations for profiling */
42 58
 #define PROFILE_COUNT 16
43 59
 
44 60
 /**
45
- * Test digest algorithm
61
+ * Report a digest fragmented test result
46 62
  *
47
- * @v digest		Digest algorithm
48
- * @v fragments		Digest test fragment list, or NULL
49
- * @v data		Test data
50
- * @v len		Length of test data
51
- * @v expected		Expected digest value
52
- * @ret ok		Digest value is as expected
63
+ * @v test		Digest test
64
+ * @v fragments		Fragment list
65
+ * @v file		Test code file
66
+ * @v line		Test code line
53 67
  */
54
-int digest_test ( struct digest_algorithm *digest,
55
-		  struct digest_test_fragments *fragments,
56
-		  void *data, size_t len, void *expected ) {
68
+void digest_frag_okx ( struct digest_test *test,
69
+		       struct digest_test_fragments *fragments,
70
+		       const char *file, unsigned int line ) {
71
+	struct digest_algorithm *digest = test->digest;
57 72
 	uint8_t ctx[digest->ctxsize];
58 73
 	uint8_t out[digest->digestsize];
74
+	const void *data = test->data;
75
+	size_t len = test->len;
59 76
 	size_t frag_len = 0;
60 77
 	unsigned int i;
61 78
 
79
+	/* Sanity check */
80
+	okx ( test->expected_len == sizeof ( out ), file, line );
81
+
62 82
 	/* Initialise digest */
63 83
 	digest_init ( digest, ctx );
64 84
 
@@ -78,7 +98,28 @@ int digest_test ( struct digest_algorithm *digest,
78 98
 	digest_final ( digest, ctx, out );
79 99
 
80 100
 	/* Compare against expected output */
81
-	return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
101
+	okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
102
+}
103
+
104
+/**
105
+ * Report a digest test result
106
+ *
107
+ * @v test		Digest test
108
+ * @v file		Test code file
109
+ * @v line		Test code line
110
+ */
111
+void digest_okx ( struct digest_test *test, const char *file,
112
+		  unsigned int line ) {
113
+	unsigned int i;
114
+
115
+	/* Test with a single pass */
116
+	digest_frag_okx ( test, NULL, file, line );
117
+
118
+	/* Test with fragment lists */
119
+	for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
120
+			    sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
121
+		digest_frag_okx ( test, &digest_test_fragments[i], file, line );
122
+	}
82 123
 }
83 124
 
84 125
 /**

+ 98
- 20
src/tests/digest_test.h View File

@@ -7,31 +7,109 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
7 7
 #include <ipxe/crypto.h>
8 8
 #include <ipxe/test.h>
9 9
 
10
-/** Maximum number of digest test fragments */
11
-#define NUM_DIGEST_TEST_FRAG 8
12
-
13
-/** A digest test fragment list */
14
-struct digest_test_fragments {
15
-	/** Fragment lengths */
16
-	size_t len[NUM_DIGEST_TEST_FRAG];
10
+/** A digest test */
11
+struct digest_test {
12
+	/** Digest algorithm */
13
+	struct digest_algorithm *digest;
14
+	/** Test data */
15
+	const void *data;
16
+	/** Length of test data */
17
+	size_t len;
18
+	/** Expected digest value */
19
+	const void *expected;
20
+	/** Expected digest length */
21
+	size_t expected_len;
17 22
 };
18 23
 
19
-extern int digest_test ( struct digest_algorithm *digest,
20
-			 struct digest_test_fragments *fragments,
21
-			 void *data, size_t len, void *expected );
22
-extern unsigned long digest_cost ( struct digest_algorithm *digest );
24
+/** Define inline test data */
25
+#define DATA(...) { __VA_ARGS__ }
26
+
27
+/** Define inline expected digest value */
28
+#define DIGEST(...) { __VA_ARGS__ }
29
+
30
+/**
31
+ * Define a digest test
32
+ *
33
+ * @v name		Test name
34
+ * @v DIGEST		Digest algorithm
35
+ * @v DATA		Test data
36
+ * @v EXPECTED		Expected digest value
37
+ * @ret test		Digest test
38
+ */
39
+#define DIGEST_TEST( name, DIGEST, DATA, EXPECTED )			\
40
+	static const uint8_t name ## _data[] = DATA;			\
41
+	static const uint8_t name ## _expected[] = EXPECTED;		\
42
+	static struct digest_test name = {				\
43
+		.digest = DIGEST,					\
44
+		.data = name ## _data,					\
45
+		.len = sizeof ( name ## _data ),			\
46
+		.expected = name ## _expected,				\
47
+		.expected_len = sizeof ( name ## _expected ),		\
48
+	};
49
+
50
+/** Standard test vector: empty data */
51
+#define DIGEST_EMPTY DATA()
52
+
53
+/** Standard test vector: NIST string "abc"
54
+ *
55
+ * The NIST Cryptographic Toolkit examples for all digest algorithms
56
+ * include a test vector which is the unterminated string
57
+ *
58
+ *   "abc"
59
+ */
60
+#define DIGEST_NIST_ABC							\
61
+	DATA ( 0x61, 0x62, 0x63 )
62
+
63
+/** Standard test vector: NIST string "abc...opq"
64
+ *
65
+ * The NIST Cryptographic Toolkit examples for all 32-bit digest
66
+ * algorithms (SHA-1 and the SHA-256 family) include a test vector
67
+ * which is the unterminated string
68
+ *
69
+ *   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
70
+ */
71
+#define DIGEST_NIST_ABC_OPQ						\
72
+	DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63,	\
73
+	       0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66,	\
74
+	       0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69,	\
75
+	       0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,	\
76
+	       0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c,	\
77
+	       0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f,	\
78
+	       0x70, 0x71 )
79
+
80
+/** Standard test vector: NIST string "abc...stu"
81
+ *
82
+ * The NIST Cryptographic Toolkit examples for all 64-bit digest
83
+ * algorithms (SHA-512 family) include a test vector which is the
84
+ * unterminated string
85
+ *
86
+ *   "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
87
+ *   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
88
+ */
89
+#define DIGEST_NIST_ABC_STU						\
90
+	DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62,	\
91
+	       0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64,	\
92
+	       0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66,	\
93
+	       0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68,	\
94
+	       0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a,	\
95
+	       0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,	\
96
+	       0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,	\
97
+	       0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,	\
98
+	       0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b,	\
99
+	       0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d,	\
100
+	       0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f,	\
101
+	       0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71,	\
102
+	       0x72, 0x73, 0x74, 0x75 )
23 103
 
24 104
 /**
25
- * Report digest test result
105
+ * Report a digest test result
26 106
  *
27
- * @v digest		Digest algorithm
28
- * @v fragments		Digest test fragment list, or NULL
29
- * @v data		Test data
30
- * @v len		Length of test data
31
- * @v expected		Expected digest value
107
+ * @v test		Digest test
32 108
  */
33
-#define digest_ok( digest, fragments, data, len, expected ) do {	\
34
-	ok ( digest_test ( digest, fragments, data, len, expected ) );	\
35
-	} while ( 0 )
109
+#define digest_ok(test) digest_okx ( test, __FILE__, __LINE__ )
110
+
111
+extern void digest_okx ( struct digest_test *test, const char *file,
112
+			 unsigned int line );
113
+extern unsigned long digest_cost ( struct digest_algorithm *digest );
36 114
 
37 115
 #endif /* _DIGEST_TEST_H */

+ 24
- 52
src/tests/md5_test.c View File

@@ -27,74 +27,46 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 27
  *
28 28
  * MD5 tests
29 29
  *
30
+ * Test inputs borrowed from NIST SHA-1 tests, with results calculated
31
+ * using md5sum.
30 32
  */
31 33
 
32
-#include <stdint.h>
34
+/* Forcibly enable assertions */
35
+#undef NDEBUG
36
+
33 37
 #include <ipxe/md5.h>
34 38
 #include <ipxe/test.h>
35 39
 #include "digest_test.h"
36 40
 
37
-/** An MD5 test vector */
38
-struct md5_test_vector {
39
-	/** Test data */
40
-	void *data;
41
-	/** Test data length */
42
-	size_t len;
43
-	/** Expected digest */
44
-	uint8_t digest[MD5_DIGEST_SIZE];
45
-};
41
+/* Empty test vector (digest obtained from "md5sum /dev/null") */
42
+DIGEST_TEST ( md5_empty, &md5_algorithm, DIGEST_EMPTY,
43
+	      DIGEST ( 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9,
44
+		       0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e ) );
46 45
 
47
-/** MD5 test vectors */
48
-static struct md5_test_vector md5_test_vectors[] = {
49
-	/* Test inputs borrowed from SHA-1 tests, with results
50
-	 * calculated using md5sum.
51
-	 */
52
-	{ NULL, 0,
53
-	  { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
54
-	    0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
55
-	{ "abc", 3,
56
-	  { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
57
-	    0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
58
-	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
59
-	  { 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca,
60
-	    0xaa, 0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a } },
61
-};
46
+/* NIST test vector "abc" (digest obtained from "md5sum <data>") */
47
+DIGEST_TEST ( md5_nist_abc, &md5_algorithm, DIGEST_NIST_ABC,
48
+	      DIGEST ( 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6,
49
+		       0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 ) );
62 50
 
63
-/** MD5 test fragment lists */
64
-static struct digest_test_fragments md5_test_fragments[] = {
65
-	{ { 0, -1UL, } },
66
-	{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
67
-	{ { 2, 0, 23, 4, 6, 1, 0 } },
68
-};
51
+/* NIST test vector "abc...opq" (digest obtained from "md5sum <data>") */
52
+DIGEST_TEST ( md5_nist_abc_opq, &md5_algorithm, DIGEST_NIST_ABC_OPQ,
53
+	      DIGEST ( 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca, 0xaa,
54
+		       0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a ) );
69 55
 
70 56
 /**
71 57
  * Perform MD5 self-test
72 58
  *
73 59
  */
74 60
 static void md5_test_exec ( void ) {
75
-	struct digest_algorithm *digest = &md5_algorithm;
76
-	struct md5_test_vector *test;
77
-	unsigned long cost;
78
-	unsigned int i;
79
-	unsigned int j;
80 61
 
81
-	/* Correctness test */
82
-	for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
83
-			    sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
84
-		test = &md5_test_vectors[i];
85
-		/* Test with a single pass */
86
-		digest_ok ( digest, NULL, test->data, test->len, test->digest );
87
-		/* Test with fragment lists */
88
-		for ( j = 0 ; j < ( sizeof ( md5_test_fragments ) /
89
-				    sizeof ( md5_test_fragments[0] ) ) ; j++ ){
90
-			digest_ok ( digest, &md5_test_fragments[j],
91
-				    test->data, test->len, test->digest );
92
-		}
93
-	}
62
+	/* Correctness tests */
63
+	digest_ok ( &md5_empty );
64
+	digest_ok ( &md5_nist_abc );
65
+	digest_ok ( &md5_nist_abc_opq );
94 66
 
95
-	/* Speed test */
96
-	cost = digest_cost ( digest );
97
-	DBG ( "MD5 required %ld cycles per byte\n", cost );
67
+	/* Speed tests */
68
+	DBG ( "MD5 required %ld cycles per byte\n",
69
+	      digest_cost ( &md5_algorithm ) );
98 70
 }
99 71
 
100 72
 /** MD5 self-test */

+ 29
- 57
src/tests/sha1_test.c View File

@@ -27,79 +27,51 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 27
  *
28 28
  * SHA-1 tests
29 29
  *
30
+ * NIST test vectors are taken from
31
+ *
32
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
33
+ *
30 34
  */
31 35
 
32
-#include <stdint.h>
36
+/* Forcibly enable assertions */
37
+#undef NDEBUG
38
+
33 39
 #include <ipxe/sha1.h>
34 40
 #include <ipxe/test.h>
35 41
 #include "digest_test.h"
36 42
 
37
-/** An SHA-1 test vector */
38
-struct sha1_test_vector {
39
-	/** Test data */
40
-	void *data;
41
-	/** Test data length */
42
-	size_t len;
43
-	/** Expected digest */
44
-	uint8_t digest[SHA1_DIGEST_SIZE];
45
-};
43
+/* Empty test vector (digest obtained from "sha1sum /dev/null") */
44
+DIGEST_TEST ( sha1_empty, &sha1_algorithm, DIGEST_EMPTY,
45
+	      DIGEST ( 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32,
46
+		       0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8,
47
+		       0x07, 0x09 ) );
46 48
 
47
-/** SHA-1 test vectors */
48
-static struct sha1_test_vector sha1_test_vectors[] = {
49
-	/* Empty test data
50
-	 *
51
-	 * Expected digest value obtained from "sha1sum /dev/null"
52
-	 */
53
-	{ NULL, 0,
54
-	  { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
55
-	    0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 } },
56
-	/* Test data and expected digests taken from the NIST
57
-	 * Cryptographic Toolkit Algorithm Examples at
58
-	 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
59
-	 */
60
-	{ "abc", 3,
61
-	  { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
62
-	    0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d } },
63
-	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
64
-	  { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
65
-	    0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 } },
66
-};
49
+/* NIST test vector "abc" */
50
+DIGEST_TEST ( sha1_nist_abc, &sha1_algorithm, DIGEST_NIST_ABC,
51
+	      DIGEST ( 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba,
52
+		       0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0,
53
+		       0xd8, 0x9d ) );
67 54
 
68
-/** SHA-1 test fragment lists */
69
-static struct digest_test_fragments sha1_test_fragments[] = {
70
-	{ { 0, -1UL, } },
71
-	{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
72
-	{ { 2, 0, 23, 4, 6, 1, 0 } },
73
-};
55
+/* NIST test vector "abc...opq" */
56
+DIGEST_TEST ( sha1_nist_abc_opq, &sha1_algorithm, DIGEST_NIST_ABC_OPQ,
57
+	      DIGEST ( 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba,
58
+		       0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46,
59
+		       0x70, 0xf1 ) );
74 60
 
75 61
 /**
76 62
  * Perform SHA-1 self-test
77 63
  *
78 64
  */
79 65
 static void sha1_test_exec ( void ) {
80
-	struct digest_algorithm *digest = &sha1_algorithm;
81
-	struct sha1_test_vector *test;
82
-	unsigned long cost;
83
-	unsigned int i;
84
-	unsigned int j;
85 66
 
86
-	/* Correctness test */
87
-	for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
88
-			    sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
89
-		test = &sha1_test_vectors[i];
90
-		/* Test with a single pass */
91
-		digest_ok ( digest, NULL, test->data, test->len, test->digest );
92
-		/* Test with fragment lists */
93
-		for ( j = 0 ; j < ( sizeof ( sha1_test_fragments ) /
94
-				    sizeof ( sha1_test_fragments[0] ) ) ; j++ ){
95
-			digest_ok ( digest, &sha1_test_fragments[j],
96
-				    test->data, test->len, test->digest );
97
-		}
98
-	}
67
+	/* Correctness tests */
68
+	digest_ok ( &sha1_empty );
69
+	digest_ok ( &sha1_nist_abc );
70
+	digest_ok ( &sha1_nist_abc_opq );
99 71
 
100
-	/* Speed test */
101
-	cost = digest_cost ( digest );
102
-	DBG ( "SHA1 required %ld cycles per byte\n", cost );
72
+	/* Speed tests */
73
+	DBG ( "SHA1 required %ld cycles per byte\n",
74
+	      digest_cost ( &sha1_algorithm ) );
103 75
 }
104 76
 
105 77
 /** SHA-1 self-test */

+ 32
- 60
src/tests/sha256_test.c View File

@@ -27,82 +27,54 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 27
  *
28 28
  * SHA-256 tests
29 29
  *
30
+ * NIST test vectors are taken from
31
+ *
32
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
33
+ *
30 34
  */
31 35
 
32
-#include <stdint.h>
36
+/* Forcibly enable assertions */
37
+#undef NDEBUG
38
+
33 39
 #include <ipxe/sha256.h>
34 40
 #include <ipxe/test.h>
35 41
 #include "digest_test.h"
36 42
 
37
-/** An SHA-256 test vector */
38
-struct sha256_test_vector {
39
-	/** Test data */
40
-	void *data;
41
-	/** Test data length */
42
-	size_t len;
43
-	/** Expected digest */
44
-	uint8_t digest[SHA256_DIGEST_SIZE];
45
-};
43
+/* Empty test vector (digest obtained from "sha256sum /dev/null") */
44
+DIGEST_TEST ( sha256_empty, &sha256_algorithm, DIGEST_EMPTY,
45
+	      DIGEST ( 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a,
46
+		       0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae,
47
+		       0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99,
48
+		       0x1b, 0x78, 0x52, 0xb8, 0x55 ) );
46 49
 
47
-/** SHA-256 test vectors */
48
-static struct sha256_test_vector sha256_test_vectors[] = {
49
-	/* Empty test data
50
-	 *
51
-	 * Expected digest value obtained from "sha256sum /dev/null"
52
-	 */
53
-	{ NULL, 0,
54
-	  { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
55
-	    0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
56
-	    0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 } },
57
-	/* Test data and expected digests taken from the NIST
58
-	 * Cryptographic Toolkit Algorithm Examples at
59
-	 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
60
-	 */
61
-	{ "abc", 3,
62
-	  { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
63
-	    0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
64
-	    0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } },
65
-	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
66
-	  { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
67
-	    0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
68
-	    0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } },
69
-};
50
+/* NIST test vector "abc" */
51
+DIGEST_TEST ( sha256_nist_abc, &sha256_algorithm, DIGEST_NIST_ABC,
52
+	      DIGEST ( 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41,
53
+		       0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
54
+		       0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff,
55
+		       0x61, 0xf2, 0x00, 0x15, 0xad ) );
70 56
 
71
-/** SHA-256 test fragment lists */
72
-static struct digest_test_fragments sha256_test_fragments[] = {
73
-	{ { 0, -1UL, } },
74
-	{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
75
-	{ { 2, 0, 23, 4, 6, 1, 0 } },
76
-};
57
+/* NIST test vector "abc...opq" */
58
+DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
59
+	      DIGEST ( 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5,
60
+		       0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c,
61
+		       0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
62
+		       0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
77 63
 
78 64
 /**
79 65
  * Perform SHA-256 self-test
80 66
  *
81 67
  */
82 68
 static void sha256_test_exec ( void ) {
83
-	struct digest_algorithm *digest = &sha256_algorithm;
84
-	struct sha256_test_vector *test;
85
-	unsigned long cost;
86
-	unsigned int i;
87
-	unsigned int j;
88 69
 
89
-	/* Correctness test */
90
-	for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
91
-			    sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
92
-		test = &sha256_test_vectors[i];
93
-		/* Test with a single pass */
94
-		digest_ok ( digest, NULL, test->data, test->len, test->digest );
95
-		/* Test with fragment lists */
96
-		for ( j = 0 ; j < ( sizeof ( sha256_test_fragments ) /
97
-				    sizeof ( sha256_test_fragments[0] )); j++ ){
98
-			digest_ok ( digest, &sha256_test_fragments[j],
99
-				    test->data, test->len, test->digest );
100
-		}
101
-	}
70
+	/* Correctness tests */
71
+	digest_ok ( &sha256_empty );
72
+	digest_ok ( &sha256_nist_abc );
73
+	digest_ok ( &sha256_nist_abc_opq );
102 74
 
103
-	/* Speed test */
104
-	cost = digest_cost ( digest );
105
-	DBG ( "SHA256 required %ld cycles per byte\n", cost );
75
+	/* Speed tests */
76
+	DBG ( "SHA256 required %ld cycles per byte\n",
77
+	      digest_cost ( &sha256_algorithm ) );
106 78
 }
107 79
 
108 80
 /** SHA-256 self-test */

Loading…
Cancel
Save