iPXE
pubkey_test.h
Go to the documentation of this file.
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 /** A public-key encryption and decryption test */
11 struct pubkey_test {
12  /** Public-key algorithm */
14  /** Private key */
15  const struct asn1_cursor private;
16  /** Public key */
17  const struct asn1_cursor public;
18  /** Plaintext */
19  const void *plaintext;
20  /** Length of plaintext */
21  size_t plaintext_len;
22  /** Ciphertext
23  *
24  * Note that the encryption process may include some random
25  * padding, so a given plaintext will encrypt to multiple
26  * different ciphertexts.
27  */
28  const void *ciphertext;
29  /** Length of ciphertext */
31 };
32 
33 /** A public-key signature test */
35  /** Public-key algorithm */
37  /** Private key */
38  const struct asn1_cursor private;
39  /** Public key */
40  const struct asn1_cursor public;
41  /** Plaintext */
42  const void *plaintext;
43  /** Plaintext length */
44  size_t plaintext_len;
45  /** Signature algorithm */
47  /** Signature */
48  const void *signature;
49  /** Signature length */
50  size_t signature_len;
51 };
52 
53 /** Define inline private key data */
54 #define PRIVATE(...) { __VA_ARGS__ }
55 
56 /** Define inline public key data */
57 #define PUBLIC(...) { __VA_ARGS__ }
58 
59 /** Define inline plaintext data */
60 #define PLAINTEXT(...) { __VA_ARGS__ }
61 
62 /** Define inline ciphertext data */
63 #define CIPHERTEXT(...) { __VA_ARGS__ }
64 
65 /** Define inline signature data */
66 #define SIGNATURE(...) { __VA_ARGS__ }
67 
68 /**
69  * Define a public-key encryption and decryption test
70  *
71  * @v name Test name
72  * @v PUBKEY Public-key algorithm
73  * @v PRIVATE Private key
74  * @v PUBLIC Public key
75  * @v PLAINTEXT Plaintext
76  * @v CIPHERTEXT Ciphertext
77  * @ret test Encryption and decryption test
78  */
79 #define PUBKEY_TEST( name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT, \
80  CIPHERTEXT ) \
81  static const uint8_t name ## _private[] = PRIVATE; \
82  static const uint8_t name ## _public[] = PUBLIC; \
83  static const uint8_t name ## _plaintext[] = PLAINTEXT; \
84  static const uint8_t name ## _ciphertext[] = CIPHERTEXT; \
85  static struct pubkey_test name = { \
86  .pubkey = PUBKEY, \
87  .private = { \
88  .data = name ## _private, \
89  .len = sizeof ( name ## _private ), \
90  }, \
91  .public = { \
92  .data = name ## _public, \
93  .len = sizeof ( name ## _public ), \
94  }, \
95  .plaintext = name ## _plaintext, \
96  .plaintext_len = sizeof ( name ## _plaintext ), \
97  .ciphertext = name ## _ciphertext, \
98  .ciphertext_len = sizeof ( name ## _ciphertext ), \
99  }
100 
101 /**
102  * Define a public-key signature test
103  *
104  * @v name Test name
105  * @v PUBKEY Public-key algorithm
106  * @v PRIVATE Private key
107  * @v PUBLIC Public key
108  * @v PLAINTEXT Plaintext
109  * @v DIGEST Digest algorithm
110  * @v SIGNATURE Signature
111  * @ret test Signature test
112  */
113 #define PUBKEY_SIGN_TEST( name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT, \
114  DIGEST, SIGNATURE ) \
115  static const uint8_t name ## _private[] = PRIVATE; \
116  static const uint8_t name ## _public[] = PUBLIC; \
117  static const uint8_t name ## _plaintext[] = PLAINTEXT; \
118  static const uint8_t name ## _signature[] = SIGNATURE; \
119  static struct pubkey_sign_test name = { \
120  .pubkey = PUBKEY, \
121  .private = { \
122  .data = name ## _private, \
123  .len = sizeof ( name ## _private ), \
124  }, \
125  .public = { \
126  .data = name ## _public, \
127  .len = sizeof ( name ## _public ), \
128  }, \
129  .plaintext = name ## _plaintext, \
130  .plaintext_len = sizeof ( name ## _plaintext ), \
131  .digest = DIGEST, \
132  .signature = name ## _signature, \
133  .signature_len = sizeof ( name ## _signature ), \
134  }
135 
136 extern void pubkey_okx ( struct pubkey_test *test,
137  const char *file, unsigned int line );
138 extern void pubkey_sign_okx ( struct pubkey_sign_test *test,
139  const char *file, unsigned int line );
140 
141 /**
142  * Report a public key encryption and decryption test result
143  *
144  * @v test Public key encryption and decryption test
145  */
146 #define pubkey_ok( test ) \
147  pubkey_okx ( test, __FILE__, __LINE__ )
148 
149 /**
150  * Report a public key signature test result
151  *
152  * @v test Public key signature test
153  */
154 #define pubkey_sign_ok( test ) \
155  pubkey_sign_okx ( test, __FILE__, __LINE__ )
156 
157 #endif /* _PUBKEY_TEST_H */
const struct asn1_cursor public
Public key.
Definition: pubkey_test.h:40
A public-key encryption and decryption test.
Definition: pubkey_test.h:11
Self-test infrastructure.
struct pubkey_algorithm * pubkey
Public-key algorithm.
Definition: pubkey_test.h:13
Cryptographic API.
size_t signature_len
Signature length.
Definition: pubkey_test.h:50
size_t ciphertext_len
Length of ciphertext.
Definition: pubkey_test.h:30
const void * plaintext
Plaintext.
Definition: pubkey_test.h:42
A public-key signature test.
Definition: pubkey_test.h:34
const void * signature
Signature.
Definition: pubkey_test.h:48
const struct asn1_cursor public
Public key.
Definition: pubkey_test.h:17
struct pubkey_algorithm * pubkey
Public-key algorithm.
Definition: pubkey_test.h:36
void pubkey_okx(struct pubkey_test *test, const char *file, unsigned int line)
Report public key encryption and decryption test result.
Definition: pubkey_test.c:50
const struct asn1_cursor private
Private key.
Definition: pubkey_test.h:38
size_t plaintext_len
Length of plaintext.
Definition: pubkey_test.h:21
struct digest_algorithm * digest
Signature algorithm.
Definition: pubkey_test.h:46
void pubkey_sign_okx(struct pubkey_sign_test *test, const char *file, unsigned int line)
Report public key signature test result.
Definition: pubkey_test.c:97
size_t plaintext_len
Plaintext length.
Definition: pubkey_test.h:44
A message digest algorithm.
Definition: crypto.h:18
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const void * plaintext
Plaintext.
Definition: pubkey_test.h:19
const struct asn1_cursor private
Private key.
Definition: pubkey_test.h:15
const void * ciphertext
Ciphertext.
Definition: pubkey_test.h:28
An ASN.1 object cursor.
Definition: asn1.h:20
A public key algorithm.
Definition: crypto.h:121
static int test
Definition: epic100.c:73