summaryrefslogtreecommitdiffstats
path: root/src/tests/digest_test.h
blob: abf1b834f50c48938d492ff5c6741ac64772e046 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef _DIGEST_TEST_H
#define _DIGEST_TEST_H

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

/** 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;
};

/** 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 a digest test result
 *
 * @v test		Digest test
 */
#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 */