[test] Allow for elliptic curve tests other than multiplication

Rename elliptic_ok() to elliptic_multiply_ok() etc, to create
namespace for tests of other elliptic curve operations.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-12-05 13:17:58 +00:00
parent e50e30a7f8
commit b362f77bdf
4 changed files with 271 additions and 270 deletions

View File

@@ -46,8 +46,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
* @v file Test code file * @v file Test code file
* @v line Test code line * @v line Test code line
*/ */
void elliptic_okx ( struct elliptic_test *test, const char *file, void elliptic_multiply_okx ( struct elliptic_multiply_test *test,
unsigned int line ) { const char *file, unsigned int line ) {
struct elliptic_curve *curve = test->curve; struct elliptic_curve *curve = test->curve;
size_t pointsize = curve->pointsize; size_t pointsize = curve->pointsize;
size_t keysize = curve->keysize; size_t keysize = curve->keysize;

View File

@@ -8,7 +8,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/test.h> #include <ipxe/test.h>
/** An elliptic curve point multiplication test */ /** An elliptic curve point multiplication test */
struct elliptic_test { struct elliptic_multiply_test {
/** Elliptic curve */ /** Elliptic curve */
struct elliptic_curve *curve; struct elliptic_curve *curve;
/** Base point */ /** Base point */
@@ -50,11 +50,11 @@ struct elliptic_test {
* @v EXPECTED Expected result point * @v EXPECTED Expected result point
* @ret test Elliptic curve point multiplication test * @ret test Elliptic curve point multiplication test
*/ */
#define ELLIPTIC_TEST( name, CURVE, BASE, SCALAR, EXPECTED ) \ #define ELLIPTIC_MULTIPLY_TEST( name, CURVE, BASE, SCALAR, EXPECTED ) \
static const uint8_t name ## _base[] = BASE; \ static const uint8_t name ## _base[] = BASE; \
static const uint8_t name ## _scalar[] = SCALAR; \ static const uint8_t name ## _scalar[] = SCALAR; \
static const uint8_t name ## _expected[] = EXPECTED; \ static const uint8_t name ## _expected[] = EXPECTED; \
static struct elliptic_test name = { \ static struct elliptic_multiply_test name = { \
.curve = CURVE, \ .curve = CURVE, \
.base = name ## _base, \ .base = name ## _base, \
.base_len = sizeof ( name ## _base ), \ .base_len = sizeof ( name ## _base ), \
@@ -64,14 +64,15 @@ struct elliptic_test {
.expected_len = sizeof ( name ## _expected ), \ .expected_len = sizeof ( name ## _expected ), \
}; };
extern void elliptic_okx ( struct elliptic_test *test, const char *file, extern void elliptic_multiply_okx ( struct elliptic_multiply_test *test,
unsigned int line ); const char *file, unsigned int line );
/** /**
* Report an elliptic curve point multiplication test result * Report an elliptic curve point multiplication test result
* *
* @v test Elliptic curve point multiplication test * @v test Elliptic curve point multiplication test
*/ */
#define elliptic_ok( test ) elliptic_okx ( test, __FILE__, __LINE__ ) #define elliptic_multiply_ok( test ) \
elliptic_multiply_okx ( test, __FILE__, __LINE__ )
#endif /* _ELLIPTIC_TEST_H */ #endif /* _ELLIPTIC_TEST_H */

View File

@@ -37,7 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include "elliptic_test.h" #include "elliptic_test.h"
/* http://point-at-infinity.org/ecc/nisttv k=1 */ /* http://point-at-infinity.org/ecc/nisttv k=1 */
ELLIPTIC_TEST ( poi_1, &p256_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_1, &p256_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -52,7 +52,7 @@ ELLIPTIC_TEST ( poi_1, &p256_curve, BASE_GENERATOR,
0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 ) ); 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 ) );
/* http://point-at-infinity.org/ecc/nisttv k=2 */ /* http://point-at-infinity.org/ecc/nisttv k=2 */
ELLIPTIC_TEST ( poi_2, &p256_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_2, &p256_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -67,7 +67,7 @@ ELLIPTIC_TEST ( poi_2, &p256_curve, BASE_GENERATOR,
0x9e, 0x04, 0xb7, 0x9d, 0x22, 0x78, 0x73, 0xd1 ) ); 0x9e, 0x04, 0xb7, 0x9d, 0x22, 0x78, 0x73, 0xd1 ) );
/* http://point-at-infinity.org/ecc/nisttv k=2 (as base) to k=20 */ /* http://point-at-infinity.org/ecc/nisttv k=2 (as base) to k=20 */
ELLIPTIC_TEST ( poi_2_20, &p256_curve, ELLIPTIC_MULTIPLY_TEST ( poi_2_20, &p256_curve,
BASE ( 0x7c, 0xf2, 0x7b, 0x18, 0x8d, 0x03, 0x4f, 0x7e, BASE ( 0x7c, 0xf2, 0x7b, 0x18, 0x8d, 0x03, 0x4f, 0x7e,
0x8a, 0x52, 0x38, 0x03, 0x04, 0xb5, 0x1a, 0xc3, 0x8a, 0x52, 0x38, 0x03, 0x04, 0xb5, 0x1a, 0xc3,
0xc0, 0x89, 0x69, 0xe2, 0x77, 0xf2, 0x1b, 0x35, 0xc0, 0x89, 0x69, 0xe2, 0x77, 0xf2, 0x1b, 0x35,
@@ -90,7 +90,7 @@ ELLIPTIC_TEST ( poi_2_20, &p256_curve,
0xf8, 0x24, 0x0a, 0xaa, 0x86, 0xf6, 0x40, 0xb8 ) ); 0xf8, 0x24, 0x0a, 0xaa, 0x86, 0xf6, 0x40, 0xb8 ) );
/* http://point-at-infinity.org/ecc/nisttv k=112233445566778899 */ /* http://point-at-infinity.org/ecc/nisttv k=112233445566778899 */
ELLIPTIC_TEST ( poi_mid, &p256_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_mid, &p256_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -105,7 +105,7 @@ ELLIPTIC_TEST ( poi_mid, &p256_curve, BASE_GENERATOR,
0x5a, 0xda, 0x38, 0xb6, 0x74, 0x33, 0x6a, 0x21 ) ); 0x5a, 0xda, 0x38, 0xb6, 0x74, 0x33, 0x6a, 0x21 ) );
/* http://point-at-infinity.org/ecc/nisttv k=<largest> */ /* http://point-at-infinity.org/ecc/nisttv k=<largest> */
ELLIPTIC_TEST ( poi_large, &p256_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_large, &p256_curve, BASE_GENERATOR,
SCALAR ( 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84,
@@ -120,7 +120,7 @@ ELLIPTIC_TEST ( poi_large, &p256_curve, BASE_GENERATOR,
0x34, 0x49, 0xbf, 0x97, 0xc8, 0x40, 0xae, 0x0a ) ); 0x34, 0x49, 0xbf, 0x97, 0xc8, 0x40, 0xae, 0x0a ) );
/* Invalid curve point zero */ /* Invalid curve point zero */
ELLIPTIC_TEST ( invalid_zero, &p256_curve, ELLIPTIC_MULTIPLY_TEST ( invalid_zero, &p256_curve,
BASE ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, BASE ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -136,7 +136,7 @@ ELLIPTIC_TEST ( invalid_zero, &p256_curve,
EXPECTED_FAIL ); EXPECTED_FAIL );
/* Invalid curve point (base_x, base_y - 1) */ /* Invalid curve point (base_x, base_y - 1) */
ELLIPTIC_TEST ( invalid_one, &p256_curve, ELLIPTIC_MULTIPLY_TEST ( invalid_one, &p256_curve,
BASE ( 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, BASE ( 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
@@ -158,15 +158,15 @@ ELLIPTIC_TEST ( invalid_one, &p256_curve,
static void p256_test_exec ( void ) { static void p256_test_exec ( void ) {
/* Tests from http://point-at-infinity.org/ecc/nisttv */ /* Tests from http://point-at-infinity.org/ecc/nisttv */
elliptic_ok ( &poi_1 ); elliptic_multiply_ok ( &poi_1 );
elliptic_ok ( &poi_2 ); elliptic_multiply_ok ( &poi_2 );
elliptic_ok ( &poi_2_20 ); elliptic_multiply_ok ( &poi_2_20 );
elliptic_ok ( &poi_mid ); elliptic_multiply_ok ( &poi_mid );
elliptic_ok ( &poi_large ); elliptic_multiply_ok ( &poi_large );
/* Invalid point tests */ /* Invalid point tests */
elliptic_ok ( &invalid_zero ); elliptic_multiply_ok ( &invalid_zero );
elliptic_ok ( &invalid_one ); elliptic_multiply_ok ( &invalid_one );
} }
/** P-256 self-test */ /** P-256 self-test */

View File

@@ -37,7 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include "elliptic_test.h" #include "elliptic_test.h"
/* http://point-at-infinity.org/ecc/nisttv k=1 */ /* http://point-at-infinity.org/ecc/nisttv k=1 */
ELLIPTIC_TEST ( poi_1, &p384_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_1, &p384_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -58,7 +58,7 @@ ELLIPTIC_TEST ( poi_1, &p384_curve, BASE_GENERATOR,
0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f ) ); 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f ) );
/* http://point-at-infinity.org/ecc/nisttv k=2 */ /* http://point-at-infinity.org/ecc/nisttv k=2 */
ELLIPTIC_TEST ( poi_2, &p384_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_2, &p384_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -79,7 +79,7 @@ ELLIPTIC_TEST ( poi_2, &p384_curve, BASE_GENERATOR,
0x61, 0x50, 0x1e, 0x70, 0x0a, 0x94, 0x0e, 0x80 ) ); 0x61, 0x50, 0x1e, 0x70, 0x0a, 0x94, 0x0e, 0x80 ) );
/* http://point-at-infinity.org/ecc/nisttv k=2 (as base) to k=20 */ /* http://point-at-infinity.org/ecc/nisttv k=2 (as base) to k=20 */
ELLIPTIC_TEST ( poi_2_20, &p384_curve, ELLIPTIC_MULTIPLY_TEST ( poi_2_20, &p384_curve,
BASE ( 0x08, 0xd9, 0x99, 0x05, 0x7b, 0xa3, 0xd2, 0xd9, BASE ( 0x08, 0xd9, 0x99, 0x05, 0x7b, 0xa3, 0xd2, 0xd9,
0x69, 0x26, 0x00, 0x45, 0xc5, 0x5b, 0x97, 0xf0, 0x69, 0x26, 0x00, 0x45, 0xc5, 0x5b, 0x97, 0xf0,
0x89, 0x02, 0x59, 0x59, 0xa6, 0xf4, 0x34, 0xd6, 0x89, 0x02, 0x59, 0x59, 0xa6, 0xf4, 0x34, 0xd6,
@@ -112,7 +112,7 @@ ELLIPTIC_TEST ( poi_2_20, &p384_curve,
0x8e, 0x23, 0x8c, 0x38, 0xda, 0xd8, 0xee, 0xdd ) ); 0x8e, 0x23, 0x8c, 0x38, 0xda, 0xd8, 0xee, 0xdd ) );
/* http://point-at-infinity.org/ecc/nisttv k=112233445566778899 */ /* http://point-at-infinity.org/ecc/nisttv k=112233445566778899 */
ELLIPTIC_TEST ( poi_mid, &p384_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_mid, &p384_curve, BASE_GENERATOR,
SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, SCALAR ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -133,7 +133,7 @@ ELLIPTIC_TEST ( poi_mid, &p384_curve, BASE_GENERATOR,
0x11, 0x31, 0xe2, 0x30, 0xb7, 0x27, 0xd3, 0x6a ) ); 0x11, 0x31, 0xe2, 0x30, 0xb7, 0x27, 0xd3, 0x6a ) );
/* http://point-at-infinity.org/ecc/nisttv k=<largest> */ /* http://point-at-infinity.org/ecc/nisttv k=<largest> */
ELLIPTIC_TEST ( poi_large, &p384_curve, BASE_GENERATOR, ELLIPTIC_MULTIPLY_TEST ( poi_large, &p384_curve, BASE_GENERATOR,
SCALAR ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, SCALAR ( 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -154,7 +154,7 @@ ELLIPTIC_TEST ( poi_large, &p384_curve, BASE_GENERATOR,
0x85, 0xbc, 0xe2, 0x84, 0x6f, 0x15, 0xf1, 0xa0 ) ); 0x85, 0xbc, 0xe2, 0x84, 0x6f, 0x15, 0xf1, 0xa0 ) );
/* Invalid curve point zero */ /* Invalid curve point zero */
ELLIPTIC_TEST ( invalid_zero, &p384_curve, ELLIPTIC_MULTIPLY_TEST ( invalid_zero, &p384_curve,
BASE ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, BASE ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -176,7 +176,7 @@ ELLIPTIC_TEST ( invalid_zero, &p384_curve,
EXPECTED_FAIL ); EXPECTED_FAIL );
/* Invalid curve point (base_x, base_y - 1) */ /* Invalid curve point (base_x, base_y - 1) */
ELLIPTIC_TEST ( invalid_one, &p384_curve, ELLIPTIC_MULTIPLY_TEST ( invalid_one, &p384_curve,
BASE ( 0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37, BASE ( 0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37,
0x8e, 0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74, 0x8e, 0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74,
0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98, 0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98,
@@ -204,15 +204,15 @@ ELLIPTIC_TEST ( invalid_one, &p384_curve,
static void p384_test_exec ( void ) { static void p384_test_exec ( void ) {
/* Tests from http://point-at-infinity.org/ecc/nisttv */ /* Tests from http://point-at-infinity.org/ecc/nisttv */
elliptic_ok ( &poi_1 ); elliptic_multiply_ok ( &poi_1 );
elliptic_ok ( &poi_2 ); elliptic_multiply_ok ( &poi_2 );
elliptic_ok ( &poi_2_20 ); elliptic_multiply_ok ( &poi_2_20 );
elliptic_ok ( &poi_mid ); elliptic_multiply_ok ( &poi_mid );
elliptic_ok ( &poi_large ); elliptic_multiply_ok ( &poi_large );
/* Invalid point tests */ /* Invalid point tests */
elliptic_ok ( &invalid_zero ); elliptic_multiply_ok ( &invalid_zero );
elliptic_ok ( &invalid_one ); elliptic_multiply_ok ( &invalid_one );
} }
/** P-384 self-test */ /** P-384 self-test */