iPXE
elliptic_test.c File Reference

Elliptic curve self-tests. More...

#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <ipxe/bigint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>
#include "elliptic_test.h"

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
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.

Detailed Description

Elliptic curve self-tests.

Definition in file elliptic_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ elliptic_curve_okx()

void elliptic_curve_okx ( struct elliptic_curve * curve,
const char * file,
unsigned int line )

Report elliptic curve sanity test result.

Parameters
curveElliptic curve
fileTest code file
lineTest code line

Definition at line 50 of file elliptic_test.c.

51 {
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}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned char uint8_t
Definition stdint.h:10
#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
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
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
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
#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

References elliptic_curve::base, bigint_add, bigint_done, bigint_init, bigint_required_size, bigint_t, elliptic_is_infinity(), elliptic_multiply(), elliptic_curve::keysize, keysize, memcmp(), NULL, okx, elliptic_curve::order, and elliptic_curve::pointsize.

◆ elliptic_multiply_okx()

void elliptic_multiply_okx ( struct elliptic_multiply_test * test,
const char * file,
unsigned int line )

Report elliptic curve point multiplication test result.

Parameters
testElliptic curve point multiplication test
fileTest code file
lineTest code line

Definition at line 93 of file elliptic_test.c.

94 {
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}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static int test
Definition epic100.c:73
uint32_t base
Base.
Definition librm.h:3
An elliptic curve.
Definition crypto.h:178

References base, elliptic_curve::base, elliptic_multiply(), elliptic_curve::keysize, keysize, memcmp(), okx, elliptic_curve::pointsize, rc, and test.

◆ elliptic_add_okx()

void elliptic_add_okx ( struct elliptic_add_test * test,
const char * file,
unsigned int line )

Report elliptic curve point addition test result.

Parameters
testElliptic curve point addition test
fileTest code file
lineTest code line

Definition at line 130 of file elliptic_test.c.

131 {
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}
static int elliptic_add(struct elliptic_curve *curve, const void *addend, const void *augend, void *result)
Definition crypto.h:333

References elliptic_add(), memcmp(), okx, elliptic_curve::pointsize, rc, and test.