aboutsummaryrefslogtreecommitdiff
path: root/src/tests/pubkey_test.h
blob: 20bb94355c8298469df819b5e9255e734253b66b (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
#ifndef _PUBKEY_TEST_H
#define _PUBKEY_TEST_H

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

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

/** A public-key encryption and decryption test */
struct pubkey_test {
	/** Public-key algorithm */
	struct pubkey_algorithm *pubkey;
	/** Private key */
	const struct asn1_cursor private;
	/** Public key */
	const struct asn1_cursor public;
	/** Plaintext */
	const void *plaintext;
	/** Length of plaintext */
	size_t plaintext_len;
	/** Ciphertext
	 *
	 * Note that the encryption process may include some random
	 * padding, so a given plaintext will encrypt to multiple
	 * different ciphertexts.
	 */
	const void *ciphertext;
	/** Length of ciphertext */
	size_t ciphertext_len;
};

/** A public-key signature test */
struct pubkey_sign_test {
	/** Public-key algorithm */
	struct pubkey_algorithm *pubkey;
	/** Private key */
	const struct asn1_cursor private;
	/** Public key */
	const struct asn1_cursor public;
	/** Plaintext */
	const void *plaintext;
	/** Plaintext length */
	size_t plaintext_len;
	/** Signature algorithm */
	struct digest_algorithm *digest;
	/** Signature */
	const void *signature;
	/** Signature length */
	size_t signature_len;
};

/** Define inline private key data */
#define PRIVATE(...) { __VA_ARGS__ }

/** Define inline public key data */
#define PUBLIC(...) { __VA_ARGS__ }

/** Define inline plaintext data */
#define PLAINTEXT(...) { __VA_ARGS__ }

/** Define inline ciphertext data */
#define CIPHERTEXT(...) { __VA_ARGS__ }

/** Define inline signature data */
#define SIGNATURE(...) { __VA_ARGS__ }

/**
 * Define a public-key encryption and decryption test
 *
 * @v name		Test name
 * @v PUBKEY		Public-key algorithm
 * @v PRIVATE		Private key
 * @v PUBLIC		Public key
 * @v PLAINTEXT		Plaintext
 * @v CIPHERTEXT	Ciphertext
 * @ret test		Encryption and decryption test
 */
#define PUBKEY_TEST( name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT,		\
			   CIPHERTEXT )					\
	static const uint8_t name ## _private[] = PRIVATE;		\
	static const uint8_t name ## _public[] = PUBLIC;		\
	static const uint8_t name ## _plaintext[] = PLAINTEXT;		\
	static const uint8_t name ## _ciphertext[] = CIPHERTEXT;	\
	static struct pubkey_test name = {				\
		.pubkey = PUBKEY,					\
		.private = {						\
			.data = name ## _private,			\
			.len = sizeof ( name ## _private ),		\
		},							\
		.public = {						\
			.data = name ## _public,			\
			.len = sizeof ( name ## _public ),		\
		},							\
		.plaintext = name ## _plaintext,			\
		.plaintext_len = sizeof ( name ## _plaintext ),		\
		.ciphertext = name ## _ciphertext,			\
		.ciphertext_len = sizeof ( name ## _ciphertext ),	\
	}

/**
 * Define a public-key signature test
 *
 * @v name		Test name
 * @v PUBKEY		Public-key algorithm
 * @v PRIVATE		Private key
 * @v PUBLIC		Public key
 * @v PLAINTEXT		Plaintext
 * @v DIGEST		Digest algorithm
 * @v SIGNATURE		Signature
 * @ret test		Signature test
 */
#define PUBKEY_SIGN_TEST( name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT,	\
			  DIGEST, SIGNATURE )				\
	static const uint8_t name ## _private[] = PRIVATE;		\
	static const uint8_t name ## _public[] = PUBLIC;		\
	static const uint8_t name ## _plaintext[] = PLAINTEXT;		\
	static const uint8_t name ## _signature[] = SIGNATURE;		\
	static struct pubkey_sign_test name = {				\
		.pubkey = PUBKEY,					\
		.private = {						\
			.data = name ## _private,			\
			.len = sizeof ( name ## _private ),		\
		},							\
		.public = {						\
			.data = name ## _public,			\
			.len = sizeof ( name ## _public ),		\
		},							\
		.plaintext = name ## _plaintext,			\
		.plaintext_len = sizeof ( name ## _plaintext ),		\
		.digest = DIGEST,					\
		.signature = name ## _signature,			\
		.signature_len = sizeof ( name ## _signature ),		\
	}

extern void pubkey_okx ( struct pubkey_test *test,
			 const char *file, unsigned int line );
extern void pubkey_sign_okx ( struct pubkey_sign_test *test,
			      const char *file, unsigned int line );

/**
 * Report a public key encryption and decryption test result
 *
 * @v test		Public key encryption and decryption test
 */
#define pubkey_ok( test ) \
	pubkey_okx ( test, __FILE__, __LINE__ )

/**
 * Report a public key signature test result
 *
 * @v test		Public key signature test
 */
#define pubkey_sign_ok( test ) \
	pubkey_sign_okx ( test, __FILE__, __LINE__ )

#endif /* _PUBKEY_TEST_H */