iPXE
exchange_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  exchange_test
 A key exchange test. More...

Macros

#define PRIVATE(...)
 Define inline private key.
#define PARTNER(...)
 Define inline partner public key.
#define PUBLIC(...)
 Define inline expected public key.
#define SHARED(...)
 Define inline expected shared secret.
#define SHARED_FAIL   SHARED()
 Define inline expected failure result.
#define EXCHANGE_TEST(name, EXCHANGE, PRIVATE, PARTNER, PUBLIC, SHARED)
 Define a key exchange test.
#define exchange_ok(test)
 Report a key exchange test result.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void exchange_okx (struct exchange_test *test, const char *file, unsigned int line)
 Report a key exchange test result.

Macro Definition Documentation

◆ PRIVATE

◆ PARTNER

◆ PUBLIC

◆ SHARED

◆ SHARED_FAIL

#define SHARED_FAIL   SHARED()

Define inline expected failure result.

Definition at line 45 of file exchange_test.h.

Referenced by EXCHANGE_TEST(), EXCHANGE_TEST(), EXCHANGE_TEST(), and EXCHANGE_TEST().

◆ EXCHANGE_TEST

#define EXCHANGE_TEST ( name,
EXCHANGE,
PRIVATE,
PARTNER,
PUBLIC,
SHARED )
Value:
static const uint8_t name ## _private[] = PRIVATE; \
static const uint8_t name ## _partner[] = PARTNER; \
static const uint8_t name ## _public[] = PUBLIC; \
static const uint8_t name ## _shared[] = SHARED; \
static struct exchange_test name = { \
.exchange = EXCHANGE, \
.private = name ## _private, \
.private_len = sizeof ( name ## _private ), \
.partner = name ## _partner, \
.partner_len = sizeof ( name ## _partner ), \
.public = name ## _public, \
.public_len = sizeof ( name ## _public ), \
.shared = name ## _shared, \
.shared_len = sizeof ( name ## _shared ), \
};
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
struct eth_slow_lacp_entity_tlv partner
Partner information.
Definition eth_slow.h:5
#define PUBLIC(...)
Define inline expected public key.
#define PRIVATE(...)
Define inline private key.
#define SHARED(...)
Define inline expected shared secret.
#define PARTNER(...)
Define inline partner public key.
A key exchange test.

Define a key exchange test.

Parameters
nameTest name
EXCHANGEKey exchange algorithm
PRIVATEPrivate key
PARTNERPartner public key
PUBLICExpected public key
SHAREDExpected shared secret
Return values
testKey exchange test

Definition at line 58 of file exchange_test.h.

◆ exchange_ok

#define exchange_ok ( test)
Value:
exchange_okx ( test, __FILE__, __LINE__ )
static int test
Definition epic100.c:73
void exchange_okx(struct exchange_test *test, const char *file, unsigned int line)
Report a key exchange test result.

Report a key exchange test result.

Parameters
testKey exchange test

Definition at line 81 of file exchange_test.h.

Referenced by ffdhe_test_exec(), p256_test_exec(), p384_test_exec(), and x25519_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ exchange_okx()

void exchange_okx ( struct exchange_test * test,
const char * file,
unsigned int line )
extern

Report a key exchange test result.

Parameters
testKey exchange test
fileTest code file
lineTest code line

Definition at line 47 of file exchange_test.c.

48 {
49 struct exchange_algorithm *exchange = test->exchange;
50 struct {
51 uint8_t public[ exchange->pubsize ];
52 uint8_t shared[ exchange->sharedsize ];
53 } *actual;
54 int rc;
55
56 /* Sanity checks */
57 okx ( test->private_len == exchange->privsize, file, line );
58 okx ( test->partner_len == exchange->pubsize, file, line );
59 okx ( test->public_len == exchange->pubsize, file, line );
60 okx ( ( ( test->shared_len == 0 ) ||
61 ( test->shared_len == exchange->sharedsize ) ), file, line );
62
63 /* Allocate result buffer */
64 actual = malloc ( sizeof ( *actual ) );
65 okx ( actual != NULL, file, line );
66
67 /* Verify calculation of public key */
68 DBGC ( test, "KEX %s private key:\n", exchange->name );
69 DBGC_HDA ( test, 0, test->private, exchange->privsize );
70 okx ( exchange_share ( exchange, test->private, actual->public ) == 0,
71 file, line );
72 DBGC ( test, "KEX %s public key:\n", exchange->name );
73 DBGC_HDA ( test, 0, actual->public, exchange->pubsize );
74 okx ( memcmp ( actual->public, test->public, exchange->pubsize ) == 0,
75 file, line );
76
77 /* Verify calculation of shared secret */
78 DBGC ( test, "KEX %s partner key:\n", exchange->name );
79 DBGC_HDA ( test, 0, test->partner, exchange->pubsize );
80 rc = exchange_agree ( exchange, test->private, test->partner,
81 actual->shared );
82 if ( test->shared_len ) {
83 /* Verify successful calculation */
84 okx ( rc == 0, file, line );
85 DBGC ( test, "KEX %s shared secret:\n", exchange->name );
86 DBGC_HDA ( test, 0, actual->shared, exchange->sharedsize );
87 okx ( memcmp ( actual->shared, test->shared,
88 exchange->sharedsize ) == 0, file, line );
89 } else {
90 /* Verify failure */
91 okx ( rc != 0, file, line );
92 }
93
94 /* Free result buffer */
95 free ( actual );
96}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBGC(...)
Definition compiler.h:530
#define DBGC_HDA(...)
Definition compiler.h:531
static int exchange_share(struct exchange_algorithm *exchange, const void *private, void *public)
Definition crypto.h:397
static int exchange_agree(struct exchange_algorithm *exchange, const void *private, const void *partner, void *shared)
Definition crypto.h:403
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A key exchange algorithm.
Definition crypto.h:210
size_t sharedsize
Shared secret size.
Definition crypto.h:218
size_t privsize
Private key size.
Definition crypto.h:214
size_t pubsize
Public key size.
Definition crypto.h:216
const char * name
Algorithm name.
Definition crypto.h:212
#define okx(success, file, line)
Report test result.
Definition test.h:44

References DBGC, DBGC_HDA, exchange_agree(), exchange_share(), free, malloc(), memcmp(), exchange_algorithm::name, NULL, okx, exchange_algorithm::privsize, exchange_algorithm::pubsize, rc, exchange_algorithm::sharedsize, and test.