[efi] Include installed protocol list in unknown handle names
[ipxe.git] / src / tests / pubkey_test.h
1 #ifndef _PUBKEY_TEST_H
2 #define _PUBKEY_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 /**
11 * Report public key decryption test result
12 *
13 * @v pubkey Public key algorithm
14 * @v key Key
15 * @v key_len Key length
16 * @v ciphertext Ciphertext
17 * @v ciphertext_len Ciphertext length
18 * @v expected Expected plaintext
19 * @v expected_len Expected plaintext length
20 */
21 #define pubkey_decrypt_ok( pubkey, key, key_len, ciphertext, \
22 ciphertext_len, expected, expected_len ) do {\
23 uint8_t ctx[ (pubkey)->ctxsize ]; \
24 \
25 ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
26 { \
27 size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
28 uint8_t decrypted[ max_len ]; \
29 int decrypted_len; \
30 \
31 decrypted_len = pubkey_decrypt ( (pubkey), ctx, \
32 (ciphertext), \
33 (ciphertext_len), \
34 decrypted ); \
35 ok ( decrypted_len == ( ( int ) (expected_len) ) ); \
36 ok ( memcmp ( decrypted, (expected), \
37 (expected_len) ) == 0 ); \
38 } \
39 pubkey_final ( (pubkey), ctx ); \
40 } while ( 0 )
41
42 /**
43 * Report public key encryption and decryption test result
44 *
45 * @v pubkey Public key algorithm
46 * @v encrypt_key Encryption key
47 * @v encrypt_key_len Encryption key length
48 * @v decrypt_key Decryption key
49 * @v decrypt_key_len Decryption key length
50 * @v plaintext Plaintext
51 * @v plaintext_len Plaintext length
52 */
53 #define pubkey_encrypt_ok( pubkey, encrypt_key, encrypt_key_len, \
54 decrypt_key, decrypt_key_len, plaintext, \
55 plaintext_len ) do { \
56 uint8_t ctx[ (pubkey)->ctxsize ]; \
57 \
58 ok ( pubkey_init ( (pubkey), ctx, (encrypt_key), \
59 (encrypt_key_len) ) == 0 ); \
60 { \
61 size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
62 uint8_t encrypted[ max_len ]; \
63 int encrypted_len; \
64 \
65 encrypted_len = pubkey_encrypt ( (pubkey), ctx, \
66 (plaintext), \
67 (plaintext_len), \
68 encrypted ); \
69 ok ( encrypted_len >= 0 ); \
70 pubkey_decrypt_ok ( (pubkey), (decrypt_key), \
71 (decrypt_key_len), encrypted, \
72 encrypted_len, (plaintext), \
73 (plaintext_len) ); \
74 } \
75 pubkey_final ( (pubkey), ctx ); \
76 } while ( 0 )
77
78 /**
79 * Report public key signature test result
80 *
81 * @v pubkey Public key algorithm
82 * @v key Key
83 * @v key_len Key length
84 * @v digest Digest algorithm
85 * @v plaintext Plaintext
86 * @v plaintext_len Plaintext length
87 * @v expected Expected signature
88 * @v expected_len Expected signature length
89 */
90 #define pubkey_sign_ok( pubkey, key, key_len, digest, plaintext, \
91 plaintext_len, expected, expected_len ) do { \
92 uint8_t ctx[ (pubkey)->ctxsize ]; \
93 uint8_t digestctx[ (digest)->ctxsize ]; \
94 uint8_t digestout[ (digest)->digestsize ]; \
95 \
96 digest_init ( (digest), digestctx ); \
97 digest_update ( (digest), digestctx, (plaintext), \
98 (plaintext_len) ); \
99 digest_final ( (digest), digestctx, digestout ); \
100 \
101 ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
102 { \
103 size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
104 uint8_t signature[ max_len ]; \
105 int signature_len; \
106 \
107 signature_len = pubkey_sign ( (pubkey), ctx, (digest), \
108 digestout, signature ); \
109 ok ( signature_len == ( ( int ) (expected_len) ) ); \
110 ok ( memcmp ( signature, (expected), \
111 (expected_len) ) == 0 ); \
112 } \
113 pubkey_final ( (pubkey), ctx ); \
114 } while ( 0 )
115
116 /**
117 * Report public key verification test result
118 *
119 * @v pubkey Public key algorithm
120 * @v key Key
121 * @v key_len Key length
122 * @v digest Digest algorithm
123 * @v plaintext Plaintext
124 * @v plaintext_len Plaintext length
125 * @v signature Signature
126 * @v signature_len Signature length
127 */
128 #define pubkey_verify_ok( pubkey, key, key_len, digest, plaintext, \
129 plaintext_len, signature, signature_len ) do {\
130 uint8_t ctx[ (pubkey)->ctxsize ]; \
131 uint8_t digestctx[ (digest)->ctxsize ]; \
132 uint8_t digestout[ (digest)->digestsize ]; \
133 \
134 digest_init ( (digest), digestctx ); \
135 digest_update ( (digest), digestctx, (plaintext), \
136 (plaintext_len) ); \
137 digest_final ( (digest), digestctx, digestout ); \
138 \
139 ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
140 ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout, \
141 (signature), (signature_len) ) == 0 ); \
142 pubkey_final ( (pubkey), ctx ); \
143 } while ( 0 )
144
145 /**
146 * Report public key verification test result
147 *
148 * @v pubkey Public key algorithm
149 * @v key Key
150 * @v key_len Key length
151 * @v digest Digest algorithm
152 * @v plaintext Plaintext
153 * @v plaintext_len Plaintext length
154 * @v signature Signature
155 * @v signature_len Signature length
156 */
157 #define pubkey_verify_fail_ok( pubkey, key, key_len, digest, plaintext, \
158 plaintext_len, signature, \
159 signature_len ) do { \
160 uint8_t ctx[ (pubkey)->ctxsize ]; \
161 uint8_t digestctx[ (digest)->ctxsize ]; \
162 uint8_t digestout[ (digest)->digestsize ]; \
163 \
164 digest_init ( (digest), digestctx ); \
165 digest_update ( (digest), digestctx, (plaintext), \
166 (plaintext_len) ); \
167 digest_final ( (digest), digestctx, digestout ); \
168 \
169 ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
170 ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout, \
171 (signature), (signature_len) ) != 0 ); \
172 pubkey_final ( (pubkey), ctx ); \
173 } while ( 0 )
174
175 #endif /* _PUBKEY_TEST_H */