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 
24 FILE_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  */
50 void 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 {
58  bigint_t ( bigint_required_size ( keysize ) ) scalar;
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 
66  /* Test multiplying base point by group order. Result should
67  * be the point at infinity, which should not be representable
68  * as a point in affine coordinates (and so should fail).
69  */
70  okx ( elliptic_multiply ( curve, curve->base, curve->order,
71  point ) != 0, file, line );
72 
73  /* Test multiplying base point by group order plus one, to get
74  * back to the base point.
75  */
76  bigint_init ( &temp.scalar, curve->order, keysize );
77  bigint_init ( &temp.one, one, sizeof ( one ) );
78  bigint_add ( &temp.one, &temp.scalar );
79  bigint_done ( &temp.scalar, scalar, sizeof ( scalar ) );
80  okx ( elliptic_multiply ( curve, curve->base, scalar, point ) == 0,
81  file, line );
82  okx ( memcmp ( point, curve->base, pointsize ) == 0, file, line );
83 }
84 
85 /**
86  * Report elliptic curve point multiplication test result
87  *
88  * @v test Elliptic curve point multiplication test
89  * @v file Test code file
90  * @v line Test code line
91  */
93  const char *file, unsigned int line ) {
94  struct elliptic_curve *curve = test->curve;
95  size_t pointsize = curve->pointsize;
96  size_t keysize = curve->keysize;
97  uint8_t actual[pointsize];
98  const void *base;
99  int rc;
100 
101  /* Sanity checks */
102  okx ( ( test->base_len == pointsize ) || ( ! test->base_len ),
103  file, line );
104  okx ( test->scalar_len == keysize, file, line );
105  okx ( ( test->expected_len == pointsize ) || ( ! test->expected_len ),
106  file, line );
107 
108  /* Perform point multiplication */
109  base = ( test->base_len ? test->base : curve->base );
110  rc = elliptic_multiply ( curve, base, test->scalar, actual );
111  if ( test->expected_len ) {
112  okx ( rc == 0, file, line );
113  } else {
114  okx ( rc != 0, file, line );
115  }
116 
117  /* Check expected result */
118  okx ( memcmp ( actual, test->expected, test->expected_len ) == 0,
119  file, line );
120 }
121 
122 /**
123  * Report elliptic curve point addition test result
124  *
125  * @v test Elliptic curve point addition test
126  * @v file Test code file
127  * @v line Test code line
128  */
130  const char *file, unsigned int line ) {
131  struct elliptic_curve *curve = test->curve;
132  size_t pointsize = curve->pointsize;
133  uint8_t actual[pointsize];
134  int rc;
135 
136  /* Sanity checks */
137  okx ( test->addend_len == pointsize, file, line );
138  okx ( test->augend_len == pointsize, file, line );
139  okx ( ( test->expected_len == pointsize ) || ( ! test->expected_len ),
140  file, line );
141 
142  /* Perform point addition */
143  rc = elliptic_add ( curve, test->addend, test->augend, actual );
144  if ( test->expected_len ) {
145  okx ( rc == 0, file, line );
146  } else {
147  okx ( rc != 0, file, line );
148  }
149 
150  /* Check expected result */
151  okx ( memcmp ( actual, test->expected, test->expected_len ) == 0,
152  file, line );
153 }
uint32_t base
Base.
Definition: librm.h:138
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int elliptic_multiply(struct elliptic_curve *curve, const void *base, const void *scalar, void *result)
Definition: crypto.h:311
const void * order
Order of the generator (if prime)
Definition: crypto.h:187
An elliptic curve point multiplication test.
Definition: elliptic_test.h:11
void elliptic_multiply_okx(struct elliptic_multiply_test *test, const char *file, unsigned int line)
Report elliptic curve point multiplication test result.
Definition: elliptic_test.c:92
#define bigint_init(value, data, len)
Initialise big integer.
Definition: bigint.h:61
Self-test infrastructure.
Cryptographic API.
Big integer support.
#define okx(success, file, line)
Report test result.
Definition: test.h:44
Assertions.
#define bigint_done(value, out, len)
Finalise big integer.
Definition: bigint.h:74
size_t keysize
Scalar (and private key) size.
Definition: crypto.h:183
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition: bigint.h:30
An elliptic curve point addition test.
Definition: elliptic_test.h:29
unsigned char uint8_t
Definition: stdint.h:10
u16 keysize
Length of encryption key to be used, network byte order.
Definition: wpa.h:37
void elliptic_add_okx(struct elliptic_add_test *test, const char *file, unsigned int line)
Report elliptic curve point addition test result.
static int elliptic_add(struct elliptic_curve *curve, const void *addend, const void *augend, void *result)
Definition: crypto.h:317
An elliptic curve.
Definition: crypto.h:177
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const void * base
Generator base point.
Definition: crypto.h:185
size_t pointsize
Point (and public key) size.
Definition: crypto.h:181
void elliptic_curve_okx(struct elliptic_curve *curve, const char *file, unsigned int line)
Report elliptic curve sanity test result.
Definition: elliptic_test.c:50
#define bigint_add(addend, value)
Add big integers.
Definition: bigint.h:86
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
static int test
Definition: epic100.c:73
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.