瀏覽代碼

[test] Add self-tests for SHA-1 algorithm

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 年之前
父節點
當前提交
18d2887281
共有 4 個文件被更改,包括 204 次插入0 次删除
  1. 69
    0
      src/tests/digest_test.c
  2. 36
    0
      src/tests/digest_test.h
  3. 98
    0
      src/tests/sha1_test.c
  4. 1
    0
      src/tests/test.c

+ 69
- 0
src/tests/digest_test.c 查看文件

@@ -0,0 +1,69 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * Digest self-tests
24
+ *
25
+ */
26
+
27
+#include <string.h>
28
+#include <ipxe/crypto.h>
29
+#include "digest_test.h"
30
+
31
+/**
32
+ * Test digest algorithm
33
+ *
34
+ * @v digest		Digest algorithm
35
+ * @v fragments		Digest test fragment list, or NULL
36
+ * @v data		Test data
37
+ * @v len		Length of test data
38
+ * @v expected		Expected digest value
39
+ * @ret ok		Digest value is as expected
40
+ */
41
+int digest_test ( struct digest_algorithm *digest,
42
+		  struct digest_test_fragments *fragments,
43
+		  void *data, size_t len, void *expected ) {
44
+	uint8_t ctx[digest->ctxsize];
45
+	uint8_t out[digest->digestsize];
46
+	size_t frag_len = 0;
47
+	unsigned int i;
48
+
49
+	/* Initialise digest */
50
+	digest_init ( digest, ctx );
51
+
52
+	/* Update digest fragment-by-fragment */
53
+	for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
54
+				     sizeof ( fragments->len[0] ) ) ) ; i++ ) {
55
+		if ( fragments )
56
+			frag_len = fragments->len[i];
57
+		if ( ( frag_len == 0 ) || ( frag_len < len ) )
58
+			frag_len = len;
59
+		digest_update ( digest, ctx, data, frag_len );
60
+		data += frag_len;
61
+		len -= frag_len;
62
+	}
63
+
64
+	/* Finalise digest */
65
+	digest_final ( digest, ctx, out );
66
+
67
+	/* Compare against expected output */
68
+	return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
69
+}

+ 36
- 0
src/tests/digest_test.h 查看文件

@@ -0,0 +1,36 @@
1
+#ifndef _DIGEST_TEST_H
2
+#define _DIGEST_TEST_H
3
+
4
+FILE_LICENCE ( GPL2_OR_LATER );
5
+
6
+#include <stdint.h>
7
+#include <ipxe/crypto.h>
8
+#include <ipxe/test.h>
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];
17
+};
18
+
19
+extern int digest_test ( struct digest_algorithm *digest,
20
+			 struct digest_test_fragments *fragments,
21
+			 void *data, size_t len, void *expected );
22
+
23
+/**
24
+ * Report digest test result
25
+ *
26
+ * @v digest		Digest algorithm
27
+ * @v fragments		Digest test fragment list, or NULL
28
+ * @v data		Test data
29
+ * @v len		Length of test data
30
+ * @v expected		Expected digest value
31
+ */
32
+#define digest_ok( digest, fragments, data, len, expected ) do {	\
33
+	ok ( digest_test ( digest, fragments, data, len, expected ) );	\
34
+	} while ( 0 )
35
+
36
+#endif /* _DIGEST_TEST_H */

+ 98
- 0
src/tests/sha1_test.c 查看文件

@@ -0,0 +1,98 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * SHA-1 tests
24
+ *
25
+ */
26
+
27
+#include <stdint.h>
28
+#include <ipxe/sha1.h>
29
+#include <ipxe/test.h>
30
+#include "digest_test.h"
31
+
32
+/** An SHA-1 test vector */
33
+struct sha1_test_vector {
34
+	/** Test data */
35
+	void *data;
36
+	/** Test data length */
37
+	size_t len;
38
+	/** Expected digest */
39
+	uint8_t digest[SHA1_DIGEST_SIZE];
40
+};
41
+
42
+/** SHA-1 test vectors */
43
+static struct sha1_test_vector sha1_test_vectors[] = {
44
+	/* Empty test data
45
+	 *
46
+	 * Expected digest value obtained from "sha1sum /dev/null"
47
+	 */
48
+	{ NULL, 0,
49
+	  { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
50
+	    0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 } },
51
+	/* Test data and expected digests taken from the NIST
52
+	 * Cryptographic Toolkit Algorithm Examples at
53
+	 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
54
+	 */
55
+	{ "abc", 3,
56
+	  { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
57
+	    0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d } },
58
+	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
59
+	  { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
60
+	    0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 } },
61
+};
62
+
63
+/** SHA-1 test fragment lists */
64
+static struct digest_test_fragments sha1_test_fragments[] = {
65
+	{ { 0, -1UL, } },
66
+	{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
67
+	{ { 2, 0, 23, 4, 6, 1, 0 } },
68
+};
69
+
70
+/**
71
+ * Perform SHA-1 self-test
72
+ *
73
+ */
74
+static void sha1_test_exec ( void ) {
75
+	struct digest_algorithm *digest = &sha1_algorithm;
76
+	struct sha1_test_vector *test;
77
+	unsigned int i;
78
+	unsigned int j;
79
+
80
+	for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
81
+			    sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
82
+		test = &sha1_test_vectors[i];
83
+		/* Test with a single pass */
84
+		digest_ok ( digest, NULL, test->data, test->len, test->digest );
85
+		/* Test with fragment lists */
86
+		for ( j = 0 ; j < ( sizeof ( sha1_test_fragments ) /
87
+				    sizeof ( sha1_test_fragments[0] ) ) ; j++ ){
88
+			digest_ok ( digest, &sha1_test_fragments[j],
89
+				    test->data, test->len, test->digest );
90
+		}
91
+	}
92
+}
93
+
94
+/** SHA-1 self-test */
95
+struct self_test sha1_test __self_test = {
96
+	.name = "sha1",
97
+	.exec = sha1_test_exec,
98
+};

+ 1
- 0
src/tests/test.c 查看文件

@@ -140,3 +140,4 @@ struct init_fn test_init_fn __init_fn ( INIT_NORMAL ) = {
140 140
 
141 141
 /* Drag in all applicable self-tests */
142 142
 REQUIRE_OBJECT ( list_test );
143
+REQUIRE_OBJECT ( sha1_test );

Loading…
取消
儲存