iPXE
digest_test.h
Go to the documentation of this file.
1 #ifndef _DIGEST_TEST_H
2 #define _DIGEST_TEST_H
3 
4 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 
6 #include <stdint.h>
7 #include <ipxe/crypto.h>
8 #include <ipxe/test.h>
9 
10 /** A digest test */
11 struct digest_test {
12  /** Digest algorithm */
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;
22 };
23 
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 )
103 
104 /**
105  * Report a digest test result
106  *
107  * @v test Digest test
108  */
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 );
114 
115 #endif /* _DIGEST_TEST_H */
size_t expected_len
Expected digest length.
Definition: digest_test.h:21
A digest test.
Definition: digest_test.h:11
Self-test infrastructure.
Cryptographic API.
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
const void * data
Test data.
Definition: digest_test.h:15
const void * expected
Expected digest value.
Definition: digest_test.h:19
struct digest_algorithm * digest
Digest algorithm.
Definition: digest_test.h:13
size_t len
Length of test data.
Definition: digest_test.h:17
void digest_okx(struct digest_test *test, const char *file, unsigned int line)
Report a digest test result.
Definition: digest_test.c:111
unsigned long digest_cost(struct digest_algorithm *digest)
Calculate digest algorithm cost.
Definition: digest_test.c:131
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A message digest algorithm.
Definition: crypto.h:16
static int test
Definition: epic100.c:73