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/crypto.h>
39 #include <ipxe/test.h>
40 #include "elliptic_test.h"
41 
42 /**
43  * Report elliptic curve point multiplication test result
44  *
45  * @v test Elliptic curve point multiplication test
46  * @v file Test code file
47  * @v line Test code line
48  */
49 void elliptic_okx ( struct elliptic_test *test, const char *file,
50  unsigned int line ) {
51  struct elliptic_curve *curve = test->curve;
52  size_t pointsize = curve->pointsize;
53  size_t keysize = curve->keysize;
54  uint8_t actual[pointsize];
55  int rc;
56 
57  /* Sanity checks */
58  okx ( ( test->base_len == pointsize ) || ( ! test->base_len ),
59  file, line );
60  okx ( test->scalar_len == keysize, file, line );
61  okx ( ( test->expected_len == pointsize ) || ( ! test->expected_len ),
62  file, line );
63 
64  /* Perform point multiplication */
65  rc = elliptic_multiply ( curve, ( test->base_len ? test->base : NULL ),
66  test->scalar, actual );
67  if ( test->expected_len ) {
68  okx ( rc == 0, file, line );
69  } else {
70  okx ( rc != 0, file, line );
71  }
72 
73  /* Check expected result */
74  okx ( memcmp ( actual, test->expected, test->expected_len ) == 0,
75  file, line );
76 }
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:310
Self-test infrastructure.
Cryptographic API.
An elliptic curve point multiplication test.
Definition: elliptic_test.h:11
#define okx(success, file, line)
Report test result.
Definition: test.h:44
Assertions.
size_t keysize
Scalar (and private key) size.
Definition: crypto.h:190
void elliptic_okx(struct elliptic_test *test, const char *file, unsigned int line)
Report elliptic curve point multiplication test result.
Definition: elliptic_test.c:49
unsigned char uint8_t
Definition: stdint.h:10
u16 keysize
Length of encryption key to be used, network byte order.
Definition: wpa.h:37
An elliptic curve.
Definition: crypto.h:184
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
size_t pointsize
Point (and public key) size.
Definition: crypto.h:188
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