iPXE
pubkey_test.c File Reference

Public key self-tests. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>
#include "pubkey_test.h"

Go to the source code of this file.

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.

Variables

static const uint8_tpubkey_random
 Random data input.
static size_t pubkey_random_len
 Length of random data input.

Detailed Description

Public key self-tests.

Definition in file pubkey_test.c.

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 )

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 )

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 test
Definition epic100.c:73
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 )

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
unsigned char uint8_t
Definition stdint.h:10
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.

Variable Documentation

◆ pubkey_random

const uint8_t* pubkey_random
static

Random data input.

Definition at line 44 of file pubkey_test.c.

Referenced by pubkey_okx(), pubkey_sign_okx(), and pubkey_test_get_random().

◆ pubkey_random_len

size_t pubkey_random_len
static

Length of random data input.

Definition at line 47 of file pubkey_test.c.

Referenced by pubkey_okx(), pubkey_sign_okx(), and pubkey_test_get_random().