iPXE
pubkey_test.h File Reference
#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  pubkey_test
 A public-key encryption and decryption test. More...
struct  pubkey_sign_test
 A public-key signature test. More...

Macros

#define PRIVATE(...)
 Define inline private key data.
#define PUBLIC(...)
 Define inline public key data.
#define RANDOM(...)
 Define inline random data.
#define PLAINTEXT(...)
 Define inline plaintext data.
#define CIPHERTEXT(...)
 Define inline ciphertext data.
#define SIGNATURE(...)
 Define inline signature data.
#define PUBKEY_TEST(name, PUBKEY, PRIVATE, PUBLIC, RANDOM, PLAINTEXT, CIPHERTEXT)
 Define a public-key encryption and decryption test.
#define PUBKEY_SIGN_TEST(name, PUBKEY, PRIVATE, PUBLIC, RANDOM, PLAINTEXT, DIGEST, SIGNATURE)
 Define a public-key signature test.
#define pubkey_ok(test)
 Report a public key encryption and decryption test result.
#define pubkey_sign_ok(test)
 Report a public key signature test result.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int pubkey_test_get_random (void *data, size_t len)
 Get random data input.
void pubkey_okx (struct pubkey_test *test, const char *file, unsigned int line)
 Report public key encryption and decryption test result.
void pubkey_sign_okx (struct pubkey_sign_test *test, const char *file, unsigned int line)
 Report public key signature test result.

Macro Definition Documentation

◆ PRIVATE

#define PRIVATE ( ...)
Value:
{ __VA_ARGS__ }

Define inline private key data.

Definition at line 51 of file pubkey_test.h.

◆ PUBLIC

#define PUBLIC ( ...)
Value:
{ __VA_ARGS__ }

Define inline public key data.

Definition at line 54 of file pubkey_test.h.

◆ RANDOM

#define RANDOM ( ...)
Value:
{ __VA_ARGS__ }

Define inline random data.

Definition at line 57 of file pubkey_test.h.

Referenced by PUBKEY_SIGN_TEST(), PUBKEY_SIGN_TEST(), PUBKEY_SIGN_TEST(), PUBKEY_TEST(), and PUBKEY_TEST().

◆ PLAINTEXT

#define PLAINTEXT ( ...)
Value:
{ __VA_ARGS__ }

Define inline plaintext data.

Definition at line 60 of file pubkey_test.h.

◆ CIPHERTEXT

#define CIPHERTEXT ( ...)
Value:
{ __VA_ARGS__ }

Define inline ciphertext data.

Definition at line 63 of file pubkey_test.h.

◆ SIGNATURE

#define SIGNATURE ( ...)

◆ PUBKEY_TEST

#define PUBKEY_TEST ( name,
PUBKEY,
PRIVATE,
PUBLIC,
RANDOM,
PLAINTEXT,
CIPHERTEXT )
Value:
static const uint8_t name ## _private[] = PRIVATE; \
static const uint8_t name ## _public[] = PUBLIC; \
static const uint8_t name ## _random[] = RANDOM; \
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 ), \
}, \
.random = name ## _random, \
.random_len = sizeof ( name ## _random ), \
.plaintext = { \
.data = name ## _plaintext, \
.len = sizeof ( name ## _plaintext ), \
}, \
.ciphertext = { \
.data = name ## _ciphertext, \
.len = sizeof ( name ## _ciphertext ), \
}, \
}
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
#define CIPHERTEXT(...)
Define inline ciphertext data.
Definition cipher_test.h:57
#define PLAINTEXT(...)
Define inline plaintext data.
Definition cipher_test.h:54
#define PUBLIC(...)
Define inline expected public key.
#define PRIVATE(...)
Define inline private key.
#define RANDOM(...)
Define inline random data.
Definition pubkey_test.h:57
A public-key encryption and decryption test.
Definition pubkey_test.h:11

Define a public-key encryption and decryption test.

Parameters
nameTest name
PUBKEYPublic-key algorithm
PRIVATEPrivate key
PUBLICPublic key
RANDOMRandom data
PLAINTEXTPlaintext
CIPHERTEXTCiphertext
Return values
testEncryption and decryption test

Definition at line 80 of file pubkey_test.h.

◆ PUBKEY_SIGN_TEST

#define PUBKEY_SIGN_TEST ( name,
PUBKEY,
PRIVATE,
PUBLIC,
RANDOM,
PLAINTEXT,
DIGEST,
SIGNATURE )
Value:
static const uint8_t name ## _private[] = PRIVATE; \
static const uint8_t name ## _public[] = PUBLIC; \
static const uint8_t name ## _random[] = RANDOM; \
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 ), \
}, \
.random = name ## _random, \
.random_len = sizeof ( name ## _random ), \
.plaintext = name ## _plaintext, \
.plaintext_len = sizeof ( name ## _plaintext ), \
.digest = DIGEST, \
.signature = { \
.data = name ## _signature, \
.len = sizeof ( name ## _signature ), \
}, \
}
#define DIGEST(...)
Define inline expected digest.
Definition der_test.c:45
#define SIGNATURE(...)
Define inline signature data.
Definition pubkey_test.h:66
A public-key signature test.
Definition pubkey_test.h:29

Define a public-key signature test.

Parameters
nameTest name
PUBKEYPublic-key algorithm
PRIVATEPrivate key
PUBLICPublic key
RANDOMRandom data
PLAINTEXTPlaintext
DIGESTDigest algorithm
SIGNATURESignature
Return values
testSignature test

Definition at line 122 of file pubkey_test.h.

◆ pubkey_ok

#define pubkey_ok ( test)
Value:
pubkey_okx ( test, __FILE__, __LINE__ )
static int test
Definition epic100.c:73
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:78

Report a public key encryption and decryption test result.

Parameters
testPublic key encryption and decryption test

Definition at line 161 of file pubkey_test.h.

161#define pubkey_ok( test ) \
162 pubkey_okx ( test, __FILE__, __LINE__ )

Referenced by rsa_test_exec().

◆ pubkey_sign_ok

#define pubkey_sign_ok ( test)
Value:
pubkey_sign_okx ( test, __FILE__, __LINE__ )
void pubkey_sign_okx(struct pubkey_sign_test *test, const char *file, unsigned int line)
Report public key signature test result.

Report a public key signature test result.

Parameters
testPublic key signature test

Definition at line 169 of file pubkey_test.h.

169#define pubkey_sign_ok( test ) \
170 pubkey_sign_okx ( test, __FILE__, __LINE__ )

Referenced by ecdsa_test_exec(), and rsa_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ pubkey_test_get_random()

int pubkey_test_get_random ( void * data,
size_t len )
extern

Get random data input.

Parameters
dataOutput buffer
lenLength of output buffer
Return values
rcReturn status code

Definition at line 56 of file pubkey_test.c.

56 {
57
58 /* Sanity check */
60
61 /* Return random data */
63
64 /* Consume random data */
67
68 return 0;
69}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static size_t pubkey_random_len
Length of random data input.
Definition pubkey_test.c:47
static const uint8_t * pubkey_random
Random data input.
Definition pubkey_test.c:44

References assert, data, len, memcpy(), pubkey_random, and pubkey_random_len.

Referenced by rsa_test_exec().

◆ pubkey_okx()

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

Report public key encryption and decryption test result.

Parameters
testPublic key encryption and decryption test
fileTest code file
lineTest code line

Definition at line 78 of file pubkey_test.c.

79 {
80 struct pubkey_algorithm *pubkey = test->pubkey;
81 struct asn1_builder plaintext;
82 struct asn1_builder ciphertext;
83
84 /* Test key matching */
85 okx ( pubkey_match ( pubkey, &test->private, &test->public ) == 0,
86 file, line );
87
88 /* Test encrypting with public key to obtain known ciphertext */
89 ciphertext.data = NULL;
90 ciphertext.len = 0;
91 pubkey_random = test->random;
92 pubkey_random_len = test->random_len;
93 okx ( pubkey_encrypt ( pubkey, &test->public, &test->plaintext,
94 &ciphertext ) == 0, file, line );
95 okx ( pubkey_random_len == 0, file, line );
96 okx ( asn1_compare ( asn1_built ( &ciphertext ),
97 &test->ciphertext ) == 0, file, line );
98 free ( ciphertext.data );
99
100 /* Test decrypting with private key to obtain known plaintext */
101 plaintext.data = NULL;
102 plaintext.len = 0;
103 okx ( pubkey_decrypt ( pubkey, &test->private, &test->ciphertext,
104 &plaintext ) == 0, file, line );
105 okx ( asn1_compare ( asn1_built ( &plaintext ),
106 &test->plaintext ) == 0, file, line );
107 free ( plaintext.data );
108
109 /* Test encrypting with private key and decrypting with public key */
110 ciphertext.data = NULL;
111 ciphertext.len = 0;
112 plaintext.data = NULL;
113 plaintext.len = 0;
114 pubkey_random = test->random;
115 pubkey_random_len = test->random_len;
116 okx ( pubkey_encrypt ( pubkey, &test->private, &test->plaintext,
117 &ciphertext ) == 0, file, line );
118 okx ( pubkey_random_len == 0, file, line );
119 okx ( pubkey_decrypt ( pubkey, &test->public,
120 asn1_built ( &ciphertext ),
121 &plaintext ) == 0, file, line );
122 okx ( asn1_compare ( asn1_built ( &plaintext ),
123 &test->plaintext ) == 0, file, line );
124 free ( ciphertext.data );
125 free ( plaintext.data );
126
127 /* Test encrypting with public key and decrypting with private key */
128 ciphertext.data = NULL;
129 ciphertext.len = 0;
130 plaintext.data = NULL;
131 plaintext.len = 0;
132 pubkey_random = test->random;
133 pubkey_random_len = test->random_len;
134 okx ( pubkey_encrypt ( pubkey, &test->public, &test->plaintext,
135 &ciphertext ) == 0, file, line );
136 okx ( pubkey_random_len == 0, file, line );
137 okx ( pubkey_decrypt ( pubkey, &test->private,
138 asn1_built ( &ciphertext ),
139 &plaintext ) == 0, file, line );
140 okx ( asn1_compare ( asn1_built ( &plaintext ),
141 &test->plaintext ) == 0, file, line );
142 free ( ciphertext.data );
143 free ( plaintext.data );
144}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition asn1.c:528
static struct asn1_cursor * asn1_built(struct asn1_builder *builder)
Get cursor for built object.
Definition asn1.h:499
static int pubkey_match(struct pubkey_algorithm *pubkey, const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Definition crypto.h:390
static int pubkey_encrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext)
Definition crypto.h:362
static int pubkey_decrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext)
Definition crypto.h:369
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
An ASN.1 object builder.
Definition asn1.h:29
A public key algorithm.
Definition crypto.h:142
#define okx(success, file, line)
Report test result.
Definition test.h:44

References asn1_built(), asn1_compare(), asn1_builder::data, free, asn1_builder::len, NULL, okx, pubkey_decrypt(), pubkey_encrypt(), pubkey_match(), pubkey_random, pubkey_random_len, and test.

◆ pubkey_sign_okx()

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

Report public key signature test result.

Parameters
testPublic key signature test
fileTest code file
lineTest code line

Definition at line 153 of file pubkey_test.c.

154 {
155 struct pubkey_algorithm *pubkey = test->pubkey;
156 struct digest_algorithm *digest = test->digest;
157 uint8_t digestctx[digest->ctxsize];
158 uint8_t digestout[digest->digestsize];
159 uint8_t signature[test->signature.len];
160 struct asn1_cursor cursor = { signature, sizeof ( signature ) };
161 struct asn1_builder builder = { NULL, 0 };
162 uint8_t *bad;
163
164 /* Test key matching */
165 okx ( pubkey_match ( pubkey, &test->private, &test->public ) == 0,
166 file, line );
167
168 /* Construct digest over plaintext */
169 digest_init ( digest, digestctx );
170 digest_update ( digest, digestctx, test->plaintext,
171 test->plaintext_len );
172 digest_final ( digest, digestctx, digestout );
173
174 /* Test verification using public key */
175 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
176 &test->signature ) == 0, file, line );
177
178 /* Test verification failure of modified signature */
179 memcpy ( signature, test->signature.data, sizeof ( signature ) );
180 bad = ( signature + ( sizeof ( signature ) / 2 ) );
181 *bad ^= 0x40;
182 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
183 &cursor ) != 0, file, line );
184 *bad ^= 0x40;
185 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
186 &cursor ) == 0, file, line );
187
188 /* Test signing using private key */
189 pubkey_random = test->random;
190 pubkey_random_len = test->random_len;
191 okx ( pubkey_sign ( pubkey, &test->private, digest, digestout,
192 &builder ) == 0, file, line );
193 okx ( pubkey_random_len == 0, file, line );
194 okx ( builder.len != 0, file, line );
195 okx ( asn1_compare ( asn1_built ( &builder ), &test->signature ) == 0,
196 file, line );
197
198 /* Test verification of constructed signature */
199 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
200 asn1_built ( &builder ) ) == 0, file, line );
201
202 /* Free signature */
203 free ( builder.data );
204}
u8 signature
CPU signature.
Definition CIB_PRM.h:7
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:294
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:305
static int pubkey_verify(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature)
Definition crypto.h:383
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:299
static int pubkey_sign(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *signature)
Definition crypto.h:376
void * data
Data.
Definition asn1.h:36
size_t len
Length of data.
Definition asn1.h:38
An ASN.1 object cursor.
Definition asn1.h:21
A message digest algorithm.
Definition crypto.h:19
size_t digestsize
Digest size.
Definition crypto.h:27
size_t ctxsize
Context size.
Definition crypto.h:23

References asn1_built(), asn1_compare(), digest_algorithm::ctxsize, asn1_builder::data, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, free, asn1_builder::len, memcpy(), NULL, okx, pubkey_match(), pubkey_random, pubkey_random_len, pubkey_sign(), pubkey_verify(), signature, and test.