iPXE
exchange_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * Key exchange self-tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <stdlib.h>
36#include <string.h>
37#include <ipxe/crypto.h>
38#include "exchange_test.h"
39
40/**
41 * Report a key exchange test result
42 *
43 * @v test Key exchange test
44 * @v file Test code file
45 * @v line Test code line
46 */
47void exchange_okx ( struct exchange_test *test, const char *file,
48 unsigned int line ) {
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
void exchange_okx(struct exchange_test *test, const char *file, unsigned int line)
Report a key exchange test result.
#define DBGC(...)
Definition compiler.h:530
#define DBGC_HDA(...)
Definition compiler.h:531
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
Cryptographic API.
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
String functions.
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
A key exchange test.
#define okx(success, file, line)
Report test result.
Definition test.h:44