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 PLAINTEXT(...)
 Define inline plaintext data.
#define CIPHERTEXT(...)
 Define inline ciphertext data.
#define SIGNATURE(...)
 Define inline signature data.
#define PUBKEY_TEST(name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT, CIPHERTEXT)
 Define a public-key encryption and decryption test.
#define PUBKEY_SIGN_TEST(name, PUBKEY, PRIVATE, PUBLIC, 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)
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 48 of file pubkey_test.h.

◆ PUBLIC

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

Define inline public key data.

Definition at line 51 of file pubkey_test.h.

◆ PLAINTEXT

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

Define inline plaintext data.

Definition at line 54 of file pubkey_test.h.

◆ CIPHERTEXT

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

Define inline ciphertext data.

Definition at line 57 of file pubkey_test.h.

◆ SIGNATURE

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

Define inline signature data.

Definition at line 60 of file pubkey_test.h.

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

◆ PUBKEY_TEST

#define PUBKEY_TEST ( name,
PUBKEY,
PRIVATE,
PUBLIC,
PLAINTEXT,
CIPHERTEXT )
Value:
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 = { \
.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 public key data.
Definition dhe_test.c:59
#define PRIVATE(...)
Define inline private key data.
Definition dhe_test.c:56
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
PLAINTEXTPlaintext
CIPHERTEXTCiphertext
Return values
testEncryption and decryption test

Definition at line 73 of file pubkey_test.h.

◆ PUBKEY_SIGN_TEST

#define PUBKEY_SIGN_TEST ( name,
PUBKEY,
PRIVATE,
PUBLIC,
PLAINTEXT,
DIGEST,
SIGNATURE )
Value:
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 = { \
.data = name ## _signature, \
.len = sizeof ( name ## _signature ), \
}, \
}
u8 signature
CPU signature.
Definition CIB_PRM.h:7
#define DIGEST(...)
Define inline expected digest.
Definition der_test.c:45
#define SIGNATURE(...)
Define inline signature data.
Definition pubkey_test.h:60
A public-key signature test.
Definition pubkey_test.h:30

Define a public-key signature test.

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

Definition at line 111 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:50

Report a public key encryption and decryption test result.

Parameters
testPublic key encryption and decryption test

Definition at line 146 of file pubkey_test.h.

146#define pubkey_ok( test ) \
147 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 154 of file pubkey_test.h.

154#define pubkey_sign_ok( test ) \
155 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_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 50 of file pubkey_test.c.

51 {
52 struct pubkey_algorithm *pubkey = test->pubkey;
53 struct asn1_builder plaintext;
54 struct asn1_builder ciphertext;
55
56 /* Test key matching */
57 okx ( pubkey_match ( pubkey, &test->private, &test->public ) == 0,
58 file, line );
59
60 /* Test decrypting with private key to obtain known plaintext */
61 plaintext.data = NULL;
62 plaintext.len = 0;
63 okx ( pubkey_decrypt ( pubkey, &test->private, &test->ciphertext,
64 &plaintext ) == 0, file, line );
65 okx ( asn1_compare ( asn1_built ( &plaintext ),
66 &test->plaintext ) == 0, file, line );
67 free ( plaintext.data );
68
69 /* Test encrypting with private key and decrypting with public key */
70 ciphertext.data = NULL;
71 ciphertext.len = 0;
72 plaintext.data = NULL;
73 plaintext.len = 0;
74 okx ( pubkey_encrypt ( pubkey, &test->private, &test->plaintext,
75 &ciphertext ) == 0, file, line );
76 okx ( pubkey_decrypt ( pubkey, &test->public,
77 asn1_built ( &ciphertext ),
78 &plaintext ) == 0, file, line );
79 okx ( asn1_compare ( asn1_built ( &plaintext ),
80 &test->plaintext ) == 0, file, line );
81 free ( ciphertext.data );
82 free ( plaintext.data );
83
84 /* Test encrypting with public key and decrypting with private key */
85 ciphertext.data = NULL;
86 ciphertext.len = 0;
87 plaintext.data = NULL;
88 plaintext.len = 0;
89 okx ( pubkey_encrypt ( pubkey, &test->public, &test->plaintext,
90 &ciphertext ) == 0, file, line );
91 okx ( pubkey_decrypt ( pubkey, &test->private,
92 asn1_built ( &ciphertext ),
93 &plaintext ) == 0, file, line );
94 okx ( asn1_compare ( asn1_built ( &plaintext ),
95 &test->plaintext ) == 0, file, line );
96 free ( ciphertext.data );
97 free ( plaintext.data );
98}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition asn1.c:458
static struct asn1_cursor * asn1_built(struct asn1_builder *builder)
Get cursor for built object.
Definition asn1.h:492
static int pubkey_match(struct pubkey_algorithm *pubkey, const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Definition crypto.h:315
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:287
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:294
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:122
#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(), 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 107 of file pubkey_test.c.

108 {
109 struct pubkey_algorithm *pubkey = test->pubkey;
110 struct digest_algorithm *digest = test->digest;
111 uint8_t digestctx[digest->ctxsize];
112 uint8_t digestout[digest->digestsize];
113 uint8_t signature[test->signature.len];
114 struct asn1_cursor cursor = { signature, sizeof ( signature ) };
115 struct asn1_builder builder = { NULL, 0 };
116 uint8_t *bad;
117
118 /* Test key matching */
119 okx ( pubkey_match ( pubkey, &test->private, &test->public ) == 0,
120 file, line );
121
122 /* Construct digest over plaintext */
123 digest_init ( digest, digestctx );
124 digest_update ( digest, digestctx, test->plaintext,
125 test->plaintext_len );
126 digest_final ( digest, digestctx, digestout );
127
128 /* Test verification using public key */
129 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
130 &test->signature ) == 0, file, line );
131
132 /* Test verification failure of modified signature */
133 memcpy ( signature, test->signature.data, sizeof ( signature ) );
134 bad = ( signature + ( sizeof ( signature ) / 2 ) );
135 *bad ^= 0x40;
136 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
137 &cursor ) != 0, file, line );
138 *bad ^= 0x40;
139 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
140 &cursor ) == 0, file, line );
141
142 /* Test signing using private key */
143 okx ( pubkey_sign ( pubkey, &test->private, digest, digestout,
144 &builder ) == 0, file, line );
145 okx ( builder.len != 0, file, line );
146 okx ( asn1_compare ( asn1_built ( &builder ), &test->signature ) == 0,
147 file, line );
148
149 /* Test verification of constructed signature */
150 okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
151 asn1_built ( &builder ) ) == 0, file, line );
152
153 /* Free signature */
154 free ( builder.data );
155}
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:219
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:230
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:308
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:224
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:301
void * memcpy(void *dest, const void *src, size_t len) __nonnull
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_sign(), pubkey_verify(), signature, and test.