iPXE
elliptic_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 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 * Elliptic curve self-tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <stdint.h>
36#include <string.h>
37#include <assert.h>
38#include <ipxe/bigint.h>
39#include <ipxe/crypto.h>
40#include <ipxe/test.h>
41#include "elliptic_test.h"
42
43/**
44 * Report elliptic curve sanity test result
45 *
46 * @v curve Elliptic curve
47 * @v file Test code file
48 * @v line Test code line
49 */
50void elliptic_curve_okx ( struct elliptic_curve *curve, const char *file,
51 unsigned int line ) {
52 static const uint8_t one[] = { 1 };
53 size_t pointsize = curve->pointsize;
54 size_t keysize = curve->keysize;
55 uint8_t point[pointsize];
56 uint8_t scalar[keysize];
57 struct {
60 } temp;
61
62 /* Check that curve has the required properties */
63 okx ( curve->base != NULL, file, line );
64 okx ( curve->order != NULL, file, line );
65 okx ( ( ! elliptic_is_infinity ( curve, curve->base ) ), file, line );
66
67 /* Test multiplying base point by group order. Result should
68 * be the point at infinity.
69 */
70 okx ( elliptic_multiply ( curve, curve->base, curve->order,
71 point ) == 0, file, line );
72 okx ( elliptic_is_infinity ( curve, point ), file, line );
73
74 /* Test multiplying base point by group order plus one, to get
75 * back to the base point.
76 */
77 bigint_init ( &temp.scalar, curve->order, keysize );
78 bigint_init ( &temp.one, one, sizeof ( one ) );
79 bigint_add ( &temp.one, &temp.scalar );
80 bigint_done ( &temp.scalar, scalar, sizeof ( scalar ) );
81 okx ( elliptic_multiply ( curve, curve->base, scalar, point ) == 0,
82 file, line );
83 okx ( memcmp ( point, curve->base, pointsize ) == 0, file, line );
84}
85
86/**
87 * Report elliptic curve point multiplication test result
88 *
89 * @v test Elliptic curve point multiplication test
90 * @v file Test code file
91 * @v line Test code line
92 */
94 const char *file, unsigned int line ) {
95 struct elliptic_curve *curve = test->curve;
96 size_t pointsize = curve->pointsize;
97 size_t keysize = curve->keysize;
98 uint8_t actual[pointsize];
99 const void *base;
100 int rc;
101
102 /* Sanity checks */
103 okx ( ( test->base_len == pointsize ) || ( ! test->base_len ),
104 file, line );
105 okx ( test->scalar_len == keysize, file, line );
106 okx ( ( test->expected_len == pointsize ) || ( ! test->expected_len ),
107 file, line );
108
109 /* Perform point multiplication */
110 base = ( test->base_len ? test->base : curve->base );
111 rc = elliptic_multiply ( curve, base, test->scalar, actual );
112 if ( test->expected_len ) {
113 okx ( rc == 0, file, line );
114 } else {
115 okx ( rc != 0, file, line );
116 }
117
118 /* Check expected result */
119 okx ( memcmp ( actual, test->expected, test->expected_len ) == 0,
120 file, line );
121}
122
123/**
124 * Report elliptic curve point addition test result
125 *
126 * @v test Elliptic curve point addition test
127 * @v file Test code file
128 * @v line Test code line
129 */
131 const char *file, unsigned int line ) {
132 struct elliptic_curve *curve = test->curve;
133 size_t pointsize = curve->pointsize;
134 uint8_t actual[pointsize];
135 int rc;
136
137 /* Sanity checks */
138 okx ( test->addend_len == pointsize, file, line );
139 okx ( test->augend_len == pointsize, file, line );
140 okx ( ( test->expected_len == pointsize ) || ( ! test->expected_len ),
141 file, line );
142
143 /* Perform point addition */
144 rc = elliptic_add ( curve, test->addend, test->augend, actual );
145 if ( test->expected_len ) {
146 okx ( rc == 0, file, line );
147 } else {
148 okx ( rc != 0, file, line );
149 }
150
151 /* Check expected result */
152 okx ( memcmp ( actual, test->expected, test->expected_len ) == 0,
153 file, line );
154}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
Assertions.
void elliptic_curve_okx(struct elliptic_curve *curve, const char *file, unsigned int line)
Report elliptic curve sanity test result.
void elliptic_multiply_okx(struct elliptic_multiply_test *test, const char *file, unsigned int line)
Report elliptic curve point multiplication test result.
void elliptic_add_okx(struct elliptic_add_test *test, const char *file, unsigned int line)
Report elliptic curve point addition test result.
static int test
Definition epic100.c:73
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
Big integer support.
#define bigint_t(size)
Define a big-integer type.
Definition bigint.h:20
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition bigint.h:31
#define bigint_done(value, out, len)
Finalise big integer.
Definition bigint.h:75
#define bigint_add(addend, value)
Add big integers.
Definition bigint.h:87
#define bigint_init(value, data, len)
Initialise big integer.
Definition bigint.h:62
Cryptographic API.
static int elliptic_multiply(struct elliptic_curve *curve, const void *base, const void *scalar, void *result)
Definition crypto.h:327
static int elliptic_is_infinity(struct elliptic_curve *curve, const void *point)
Definition crypto.h:322
static int elliptic_add(struct elliptic_curve *curve, const void *addend, const void *augend, void *result)
Definition crypto.h:333
String functions.
uint32_t base
Base.
Definition librm.h:3
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
An elliptic curve point addition test.
An elliptic curve.
Definition crypto.h:178
const void * order
Order of the generator (if prime)
Definition crypto.h:188
size_t keysize
Scalar (and private key) size.
Definition crypto.h:184
size_t pointsize
Point (and public key) size.
Definition crypto.h:182
const void * base
Generator base point.
Definition crypto.h:186
An elliptic curve point multiplication test.
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
u16 keysize
Length of encryption key to be used, network byte order.
Definition wpa.h:10