iPXE
Data Structures | Macros | Functions
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(...)   { __VA_ARGS__ }
 Define inline private key data. More...
 
#define PUBLIC(...)   { __VA_ARGS__ }
 Define inline public key data. More...
 
#define PLAINTEXT(...)   { __VA_ARGS__ }
 Define inline plaintext data. More...
 
#define CIPHERTEXT(...)   { __VA_ARGS__ }
 Define inline ciphertext data. More...
 
#define SIGNATURE(...)   { __VA_ARGS__ }
 Define inline signature data. More...
 
#define PUBKEY_TEST(name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT, CIPHERTEXT)
 Define a public-key encryption and decryption test. More...
 
#define PUBKEY_SIGN_TEST(name, PUBKEY, PRIVATE, PUBLIC, PLAINTEXT, DIGEST, SIGNATURE)
 Define a public-key signature test. More...
 
#define pubkey_ok(test)   pubkey_okx ( test, __FILE__, __LINE__ )
 Report a public key encryption and decryption test result. More...
 
#define pubkey_sign_ok(test)   pubkey_sign_okx ( test, __FILE__, __LINE__ )
 Report a public key signature test result. More...
 

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. More...
 
void pubkey_sign_okx (struct pubkey_sign_test *test, const char *file, unsigned int line)
 Report public key signature test result. More...
 

Macro Definition Documentation

◆ PRIVATE

#define PRIVATE (   ...)    { __VA_ARGS__ }

Define inline private key data.

Definition at line 54 of file pubkey_test.h.

◆ PUBLIC

#define PUBLIC (   ...)    { __VA_ARGS__ }

Define inline public key data.

Definition at line 57 of file pubkey_test.h.

◆ PLAINTEXT

#define PLAINTEXT (   ...)    { __VA_ARGS__ }

Define inline plaintext data.

Definition at line 60 of file pubkey_test.h.

◆ CIPHERTEXT

#define CIPHERTEXT (   ...)    { __VA_ARGS__ }

Define inline ciphertext data.

Definition at line 63 of file pubkey_test.h.

◆ SIGNATURE

#define SIGNATURE (   ...)    { __VA_ARGS__ }

Define inline signature data.

Definition at line 66 of file pubkey_test.h.

◆ 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 = name ## _plaintext, \
.plaintext_len = sizeof ( name ## _plaintext ), \
.ciphertext = name ## _ciphertext, \
.ciphertext_len = sizeof ( name ## _ciphertext ), \
}
const char * name
Definition: ath9k_hw.c:1984
#define CIPHERTEXT(...)
Define inline ciphertext data.
Definition: pubkey_test.h:63
A public-key encryption and decryption test.
Definition: pubkey_test.h:11
#define PUBLIC(...)
Define inline public key data.
Definition: pubkey_test.h:57
size_t ciphertext_len
Length of ciphertext.
Definition: pubkey_test.h:30
#define PLAINTEXT(...)
Define inline plaintext data.
Definition: pubkey_test.h:60
unsigned char uint8_t
Definition: stdint.h:10
#define PRIVATE(...)
Define inline private key data.
Definition: pubkey_test.h:54
const void * ciphertext
Ciphertext.
Definition: pubkey_test.h:28

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 79 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 = name ## _signature, \
.signature_len = sizeof ( name ## _signature ), \
}
const char * name
Definition: ath9k_hw.c:1984
#define SIGNATURE(...)
Define inline signature data.
Definition: pubkey_test.h:66
#define PUBLIC(...)
Define inline public key data.
Definition: pubkey_test.h:57
size_t signature_len
Signature length.
Definition: pubkey_test.h:50
A public-key signature test.
Definition: pubkey_test.h:34
#define PLAINTEXT(...)
Define inline plaintext data.
Definition: pubkey_test.h:60
unsigned char uint8_t
Definition: stdint.h:10
struct digest_algorithm * digest
Signature algorithm.
Definition: pubkey_test.h:46
#define PRIVATE(...)
Define inline private key data.
Definition: pubkey_test.h:54
#define DIGEST(...)
Define inline expected digest.
Definition: der_test.c:45
u8 signature
CPU signature.
Definition: CIB_PRM.h:35

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 113 of file pubkey_test.h.

◆ pubkey_ok

#define pubkey_ok (   test)    pubkey_okx ( test, __FILE__, __LINE__ )

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.

◆ pubkey_sign_ok

#define pubkey_sign_ok (   test)    pubkey_sign_okx ( test, __FILE__, __LINE__ )

Report a public key signature test result.

Parameters
testPublic key signature test

Definition at line 154 of file pubkey_test.h.

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 
)

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  size_t max_len = pubkey_max_len ( pubkey, &test->private );
54  uint8_t encrypted[max_len];
55  uint8_t decrypted[max_len];
56  int encrypted_len;
57  int decrypted_len;
58 
59  /* Test decrypting with private key to obtain known plaintext */
60  decrypted_len = pubkey_decrypt ( pubkey, &test->private,
61  test->ciphertext, test->ciphertext_len,
62  decrypted );
63  okx ( decrypted_len == ( ( int ) test->plaintext_len ), file, line );
64  okx ( memcmp ( decrypted, test->plaintext, test->plaintext_len ) == 0,
65  file, line );
66 
67  /* Test encrypting with private key and decrypting with public key */
68  encrypted_len = pubkey_encrypt ( pubkey, &test->private,
69  test->plaintext, test->plaintext_len,
70  encrypted );
71  okx ( encrypted_len >= 0, file, line );
72  decrypted_len = pubkey_decrypt ( pubkey, &test->public, encrypted,
73  encrypted_len, decrypted );
74  okx ( decrypted_len == ( ( int ) test->plaintext_len ), file, line );
75  okx ( memcmp ( decrypted, test->plaintext, test->plaintext_len ) == 0,
76  file, line );
77 
78  /* Test encrypting with public key and decrypting with private key */
79  encrypted_len = pubkey_encrypt ( pubkey, &test->public,
80  test->plaintext, test->plaintext_len,
81  encrypted );
82  okx ( encrypted_len >= 0, file, line );
83  decrypted_len = pubkey_decrypt ( pubkey, &test->private, encrypted,
84  encrypted_len, decrypted );
85  okx ( decrypted_len == ( ( int ) test->plaintext_len ), file, line );
86  okx ( memcmp ( decrypted, test->plaintext, test->plaintext_len ) == 0,
87  file, line );
88 }
static int pubkey_encrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const void *data, size_t len, void *out)
Definition: crypto.h:275
static int pubkey_decrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const void *data, size_t len, void *out)
Definition: crypto.h:281
#define okx(success, file, line)
Report test result.
Definition: test.h:44
unsigned char uint8_t
Definition: stdint.h:10
static size_t pubkey_max_len(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key)
Definition: crypto.h:269
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
A public key algorithm.
Definition: crypto.h:121
size_t(* max_len)(const struct asn1_cursor *key)
Calculate maximum output length.
Definition: crypto.h:129
static int test
Definition: epic100.c:73

References pubkey_algorithm::max_len, memcmp(), okx, pubkey_decrypt(), pubkey_encrypt(), pubkey_max_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 97 of file pubkey_test.c.

98  {
99  struct pubkey_algorithm *pubkey = test->pubkey;
100  struct digest_algorithm *digest = test->digest;
101  size_t max_len = pubkey_max_len ( pubkey, &test->private );
102  uint8_t bad[test->signature_len];
103  uint8_t digestctx[digest->ctxsize ];
104  uint8_t digestout[digest->digestsize];
105  uint8_t signature[max_len];
106  int signature_len;
107 
108  /* Construct digest over plaintext */
109  digest_init ( digest, digestctx );
110  digest_update ( digest, digestctx, test->plaintext,
111  test->plaintext_len );
112  digest_final ( digest, digestctx, digestout );
113 
114  /* Test signing using private key */
115  signature_len = pubkey_sign ( pubkey, &test->private, digest,
116  digestout, signature );
117  okx ( signature_len == ( ( int ) test->signature_len ), file, line );
118  okx ( memcmp ( signature, test->signature, test->signature_len ) == 0,
119  file, line );
120 
121  /* Test verification using public key */
122  okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
123  test->signature, test->signature_len ) == 0,
124  file, line );
125 
126  /* Test verification failure of modified signature */
127  memcpy ( bad, test->signature, test->signature_len );
128  bad[ test->signature_len / 2 ] ^= 0x40;
129  okx ( pubkey_verify ( pubkey, &test->public, digest, digestout,
130  bad, sizeof ( bad ) ) != 0, file, line );
131 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:206
static int pubkey_verify(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Definition: crypto.h:294
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:212
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define okx(success, file, line)
Report test result.
Definition: test.h:44
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:201
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:22
size_t digestsize
Digest size.
Definition: crypto.h:26
A message digest algorithm.
Definition: crypto.h:18
static size_t pubkey_max_len(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key)
Definition: crypto.h:269
static int pubkey_sign(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, void *signature)
Definition: crypto.h:287
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
A public key algorithm.
Definition: crypto.h:121
static int test
Definition: epic100.c:73

References digest_algorithm::ctxsize, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, memcmp(), memcpy(), okx, pubkey_max_len(), pubkey_sign(), pubkey_verify(), signature, and test.