summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/tests/pubkey_test.h
blob: 7678453a94673dd69b37c19c4695cb91e55fd7ef (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef _PUBKEY_TEST_H
#define _PUBKEY_TEST_H

FILE_LICENCE ( GPL2_OR_LATER );

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

/**
 * Report public key decryption test result
 *
 * @v pubkey		Public key algorithm
 * @v key		Key
 * @v key_len		Key length
 * @v ciphertext	Ciphertext
 * @v ciphertext_len	Ciphertext length
 * @v expected		Expected plaintext
 * @v expected_len	Expected plaintext length
 */
#define pubkey_decrypt_ok( pubkey, key, key_len, ciphertext,		\
			   ciphertext_len, expected, expected_len ) do {\
	uint8_t ctx[ (pubkey)->ctxsize ];				\
									\
	ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );	\
	{								\
		size_t max_len = pubkey_max_len ( (pubkey), ctx );	\
		uint8_t decrypted[ max_len ];				\
		int decrypted_len;					\
									\
		decrypted_len = pubkey_decrypt ( (pubkey), ctx,		\
						 (ciphertext),		\
						 (ciphertext_len),	\
						 decrypted );		\
		ok ( decrypted_len == ( ( int ) (expected_len) ) );	\
		ok ( memcmp ( decrypted, (expected),			\
			      (expected_len) ) == 0 );			\
	}								\
	pubkey_final ( (pubkey), ctx );					\
	} while ( 0 )

/**
 * Report public key encryption and decryption test result
 *
 * @v pubkey		Public key algorithm
 * @v encrypt_key	Encryption key
 * @v encrypt_key_len	Encryption key length
 * @v decrypt_key	Decryption key
 * @v decrypt_key_len	Decryption key length
 * @v plaintext		Plaintext
 * @v plaintext_len	Plaintext length
 */
#define pubkey_encrypt_ok( pubkey, encrypt_key, encrypt_key_len,	\
			   decrypt_key, decrypt_key_len, plaintext,	\
			   plaintext_len ) do {				\
	uint8_t ctx[ (pubkey)->ctxsize ];				\
									\
	ok ( pubkey_init ( (pubkey), ctx, (encrypt_key),		\
			   (encrypt_key_len) ) == 0 );			\
	{								\
		size_t max_len = pubkey_max_len ( (pubkey), ctx );	\
		uint8_t encrypted[ max_len ];				\
		int encrypted_len;					\
									\
		encrypted_len = pubkey_encrypt ( (pubkey), ctx,		\
						 (plaintext),		\
						 (plaintext_len),	\
						 encrypted );		\
		ok ( encrypted_len >= 0 );				\
		pubkey_decrypt_ok ( (pubkey), (decrypt_key),		\
				    (decrypt_key_len), encrypted,	\
				    encrypted_len, (plaintext),		\
				    (plaintext_len) );			\
	}								\
	pubkey_final ( (pubkey), ctx );					\
	} while ( 0 )

/**
 * Report public key signature test result
 *
 * @v pubkey		Public key algorithm
 * @v key		Key
 * @v key_len		Key length
 * @v digest		Digest algorithm
 * @v plaintext		Plaintext
 * @v plaintext_len	Plaintext length
 * @v expected		Expected signature
 * @v expected_len	Expected signature length
 */
#define pubkey_sign_ok( pubkey, key, key_len, digest, plaintext,	\
			plaintext_len, expected, expected_len ) do {	\
	uint8_t ctx[ (pubkey)->ctxsize ];				\
	uint8_t digestctx[ (digest)->ctxsize ];				\
	uint8_t digestout[ (digest)->digestsize ];			\
									\
	digest_init ( (digest), digestctx );				\
	digest_update ( (digest), digestctx, (plaintext),		\
			(plaintext_len) );				\
	digest_final ( (digest), digestctx, digestout );		\
									\
	ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );	\
	{								\
		size_t max_len = pubkey_max_len ( (pubkey), ctx );	\
		uint8_t signature[ max_len ];				\
		int signature_len;					\
									\
		signature_len = pubkey_sign ( (pubkey), ctx, (digest),	\
					      digestout, signature );	\
		ok ( signature_len == ( ( int ) (expected_len) ) );	\
		ok ( memcmp ( signature, (expected),			\
			      (expected_len) ) == 0 );			\
	}								\
	pubkey_final ( (pubkey), ctx );					\
	} while ( 0 )

/**
 * Report public key verification test result
 *
 * @v pubkey		Public key algorithm
 * @v key		Key
 * @v key_len		Key length
 * @v digest		Digest algorithm
 * @v plaintext		Plaintext
 * @v plaintext_len	Plaintext length
 * @v signature		Signature
 * @v signature_len	Signature length
 */
#define pubkey_verify_ok( pubkey, key, key_len, digest, plaintext,	\
			  plaintext_len, signature, signature_len ) do {\
	uint8_t ctx[ (pubkey)->ctxsize ];				\
	uint8_t digestctx[ (digest)->ctxsize ];				\
	uint8_t digestout[ (digest)->digestsize ];			\
									\
	digest_init ( (digest), digestctx );				\
	digest_update ( (digest), digestctx, (plaintext),		\
			(plaintext_len) );				\
	digest_final ( (digest), digestctx, digestout );		\
									\
	ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );	\
	ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout,	\
			     (signature), (signature_len) ) == 0 );	\
	pubkey_final ( (pubkey), ctx );					\
	} while ( 0 )

/**
 * Report public key verification test result
 *
 * @v pubkey		Public key algorithm
 * @v key		Key
 * @v key_len		Key length
 * @v digest		Digest algorithm
 * @v plaintext		Plaintext
 * @v plaintext_len	Plaintext length
 * @v signature		Signature
 * @v signature_len	Signature length
 */
#define pubkey_verify_fail_ok( pubkey, key, key_len, digest, plaintext,	\
			       plaintext_len, signature,		\
			       signature_len ) do {			\
	uint8_t ctx[ (pubkey)->ctxsize ];				\
	uint8_t digestctx[ (digest)->ctxsize ];				\
	uint8_t digestout[ (digest)->digestsize ];			\
									\
	digest_init ( (digest), digestctx );				\
	digest_update ( (digest), digestctx, (plaintext),		\
			(plaintext_len) );				\
	digest_final ( (digest), digestctx, digestout );		\
									\
	ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );	\
	ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout,	\
			     (signature), (signature_len) ) != 0 );	\
	pubkey_final ( (pubkey), ctx );					\
	} while ( 0 )

#endif /* _PUBKEY_TEST_H */