iPXE
exchange_test.c File Reference

Key exchange self-tests. More...

#include <stdlib.h>
#include <string.h>
#include <ipxe/crypto.h>
#include "exchange_test.h"

Go to the source code of this file.

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.

Detailed Description

Key exchange self-tests.

Definition in file exchange_test.c.

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 )

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
unsigned char uint8_t
Definition stdint.h:10
static int test
Definition epic100.c:73
#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.