From: tb Date: Mon, 3 May 2021 14:46:18 +0000 (+0000) Subject: Add a test that exercises the point conversion bit of the octet X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=ac1153bc7836374e36f23fdf49326d3d90f1edf8;p=openbsd Add a test that exercises the point conversion bit of the octet string representations of points on the binary curve sect571k1. --- diff --git a/regress/lib/libcrypto/ec/ec_point_conversion.c b/regress/lib/libcrypto/ec/ec_point_conversion.c index 97ba70034de..9e580180090 100644 --- a/regress/lib/libcrypto/ec/ec_point_conversion.c +++ b/regress/lib/libcrypto/ec/ec_point_conversion.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_point_conversion.c,v 1.1 2021/04/21 20:15:08 tb Exp $ */ +/* $OpenBSD: ec_point_conversion.c,v 1.2 2021/05/03 14:46:18 tb Exp $ */ /* * Copyright (c) 2021 Theo Buehler * Copyright (c) 2021 Joel Sing @@ -235,6 +235,366 @@ test_random_points(void) return failed; } +static const struct point_conversion { + const char *description; + int nid; + uint8_t octets[256]; + uint8_t octets_len; + int valid; +} point_conversions[] = { + { + .description = "point at infinity on sect571k1", + .nid = NID_sect571k1, + .octets = { 0x00 }, + .octets_len = 1, + .valid = 1, + }, + { + .description = "point at infinity on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { 0x01 }, + .octets_len = 1, + .valid = 0, + }, + { + .description = "zero x compressed point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x02, 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, 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, + }, + .octets_len = 73, + .valid = 1, + }, + { + .description = + "zero x compressed point on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { + 0x03, 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, 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, + }, + .octets_len = 73, + .valid = 0, + }, + { + .description = "generic compressed point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x02, 0x00, 0x5e, 0x33, 0x9f, 0xd6, 0xf1, 0xae, + 0x10, 0xbd, 0x48, 0xcf, 0xf1, 0x0d, 0x8e, 0x0e, + 0xd7, 0x83, 0xce, 0xf0, 0x3d, 0x14, 0x06, 0x41, + 0x29, 0x7d, 0x7e, 0xa3, 0x01, 0xf4, 0x9b, 0xa2, + 0x8c, 0xa6, 0xab, 0x24, 0xa0, 0x9e, 0xfd, 0xc4, + 0x2d, 0xc2, 0x95, 0xb4, 0xf9, 0xd4, 0xf4, 0x97, + 0x53, 0x5d, 0xe9, 0xe3, 0x47, 0xc3, 0xa8, 0x6b, + 0xbb, 0x27, 0x74, 0x6b, 0xfb, 0x26, 0xca, 0x96, + 0x76, 0x5b, 0x36, 0xe8, 0x87, 0xb5, 0xc5, 0x6a, + 0xc5, + }, + .octets_len = 73, + .valid = 1, + }, + { + .description = + "generic compressed point on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { + 0x03, 0x00, 0x5e, 0x33, 0x9f, 0xd6, 0xf1, 0xae, + 0x10, 0xbd, 0x48, 0xcf, 0xf1, 0x0d, 0x8e, 0x0e, + 0xd7, 0x83, 0xce, 0xf0, 0x3d, 0x14, 0x06, 0x41, + 0x29, 0x7d, 0x7e, 0xa3, 0x01, 0xf4, 0x9b, 0xa2, + 0x8c, 0xa6, 0xab, 0x24, 0xa0, 0x9e, 0xfd, 0xc4, + 0x2d, 0xc2, 0x95, 0xb4, 0xf9, 0xd4, 0xf4, 0x97, + 0x53, 0x5d, 0xe9, 0xe3, 0x47, 0xc3, 0xa8, 0x6b, + 0xbb, 0x27, 0x74, 0x6b, 0xfb, 0x26, 0xca, 0x96, + 0x76, 0x5b, 0x36, 0xe8, 0x87, 0xb5, 0xc5, 0x6a, + 0xc5, + }, + .octets_len = 73, + .valid = 1, + }, + { + .description = "zero x uncompressed point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x04, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, + }, + .octets_len = 145, + .valid = 1, + }, + { + .description = "zero x uncompressed point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x05, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, + }, + .octets_len = 145, + .valid = 0, + }, + { + .description = "generic uncompressed point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x04, 0x02, 0x2d, 0xf4, 0x72, 0x38, 0xc2, 0xbe, + 0x0d, 0xa4, 0xf1, 0xfc, 0xe8, 0x78, 0xe6, 0xad, + 0x5c, 0xaa, 0xd9, 0x7f, 0x7c, 0xa9, 0x4c, 0xe4, + 0xd2, 0xae, 0xba, 0xaa, 0x8c, 0x9d, 0x4c, 0xac, + 0x85, 0x00, 0xe1, 0xfa, 0x33, 0x73, 0x51, 0x52, + 0x5a, 0x4e, 0x75, 0x67, 0x1d, 0x0a, 0x2a, 0xd2, + 0x38, 0xb2, 0x9b, 0xe8, 0xec, 0xbe, 0x07, 0x8b, + 0xc0, 0x95, 0x77, 0xe9, 0x55, 0x0c, 0x6c, 0x0e, + 0x02, 0x3b, 0x34, 0xe2, 0xa8, 0x29, 0xd2, 0x97, + 0xd9, 0x05, 0xa2, 0x6f, 0xa8, 0x6f, 0x1c, 0x3a, + 0xf6, 0x12, 0x42, 0x1a, 0x26, 0x6e, 0x87, 0xf3, + 0x19, 0x04, 0x20, 0xa5, 0x29, 0x78, 0xee, 0xcf, + 0x91, 0x06, 0xd2, 0x5a, 0x62, 0x2a, 0x7f, 0x1d, + 0xa0, 0x7b, 0xb4, 0x31, 0x9c, 0xd2, 0x14, 0x60, + 0xf5, 0x9b, 0xea, 0x4a, 0x41, 0xad, 0x47, 0x72, + 0xf9, 0x01, 0xed, 0x7f, 0x5a, 0x27, 0x64, 0xa2, + 0x53, 0x4e, 0x18, 0x51, 0x33, 0xa8, 0x1e, 0x3a, + 0xc2, 0xe7, 0x2c, 0xe3, 0x63, 0x6d, 0x06, 0x29, + 0x28, + }, + .octets_len = 145, + .valid = 1, + }, + { + .description = + "generic uncompressed point on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { + 0x05, 0x02, 0x2d, 0xf4, 0x72, 0x38, 0xc2, 0xbe, + 0x0d, 0xa4, 0xf1, 0xfc, 0xe8, 0x78, 0xe6, 0xad, + 0x5c, 0xaa, 0xd9, 0x7f, 0x7c, 0xa9, 0x4c, 0xe4, + 0xd2, 0xae, 0xba, 0xaa, 0x8c, 0x9d, 0x4c, 0xac, + 0x85, 0x00, 0xe1, 0xfa, 0x33, 0x73, 0x51, 0x52, + 0x5a, 0x4e, 0x75, 0x67, 0x1d, 0x0a, 0x2a, 0xd2, + 0x38, 0xb2, 0x9b, 0xe8, 0xec, 0xbe, 0x07, 0x8b, + 0xc0, 0x95, 0x77, 0xe9, 0x55, 0x0c, 0x6c, 0x0e, + 0x02, 0x3b, 0x34, 0xe2, 0xa8, 0x29, 0xd2, 0x97, + 0xd9, 0x05, 0xa2, 0x6f, 0xa8, 0x6f, 0x1c, 0x3a, + 0xf6, 0x12, 0x42, 0x1a, 0x26, 0x6e, 0x87, 0xf3, + 0x19, 0x04, 0x20, 0xa5, 0x29, 0x78, 0xee, 0xcf, + 0x91, 0x06, 0xd2, 0x5a, 0x62, 0x2a, 0x7f, 0x1d, + 0xa0, 0x7b, 0xb4, 0x31, 0x9c, 0xd2, 0x14, 0x60, + 0xf5, 0x9b, 0xea, 0x4a, 0x41, 0xad, 0x47, 0x72, + 0xf9, 0x01, 0xed, 0x7f, 0x5a, 0x27, 0x64, 0xa2, + 0x53, 0x4e, 0x18, 0x51, 0x33, 0xa8, 0x1e, 0x3a, + 0xc2, 0xe7, 0x2c, 0xe3, 0x63, 0x6d, 0x06, 0x29, + 0x28, + }, + .octets_len = 145, + .valid = 0, + }, + { + .description = "zero x hybrid point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x06, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, + }, + .octets_len = 145, + .valid = 1, + }, + { + .description = + "zero x hybrid point on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { + 0x07, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, + }, + .octets_len = 145, + .valid = 0, + }, + { + .description = "generic hybrid point on sect571k1", + .nid = NID_sect571k1, + .octets = { + 0x07, 0x02, 0x2d, 0xf4, 0x72, 0x38, 0xc2, 0xbe, + 0x0d, 0xa4, 0xf1, 0xfc, 0xe8, 0x78, 0xe6, 0xad, + 0x5c, 0xaa, 0xd9, 0x7f, 0x7c, 0xa9, 0x4c, 0xe4, + 0xd2, 0xae, 0xba, 0xaa, 0x8c, 0x9d, 0x4c, 0xac, + 0x85, 0x00, 0xe1, 0xfa, 0x33, 0x73, 0x51, 0x52, + 0x5a, 0x4e, 0x75, 0x67, 0x1d, 0x0a, 0x2a, 0xd2, + 0x38, 0xb2, 0x9b, 0xe8, 0xec, 0xbe, 0x07, 0x8b, + 0xc0, 0x95, 0x77, 0xe9, 0x55, 0x0c, 0x6c, 0x0e, + 0x02, 0x3b, 0x34, 0xe2, 0xa8, 0x29, 0xd2, 0x97, + 0xd9, 0x05, 0xa2, 0x6f, 0xa8, 0x6f, 0x1c, 0x3a, + 0xf6, 0x12, 0x42, 0x1a, 0x26, 0x6e, 0x87, 0xf3, + 0x19, 0x04, 0x20, 0xa5, 0x29, 0x78, 0xee, 0xcf, + 0x91, 0x06, 0xd2, 0x5a, 0x62, 0x2a, 0x7f, 0x1d, + 0xa0, 0x7b, 0xb4, 0x31, 0x9c, 0xd2, 0x14, 0x60, + 0xf5, 0x9b, 0xea, 0x4a, 0x41, 0xad, 0x47, 0x72, + 0xf9, 0x01, 0xed, 0x7f, 0x5a, 0x27, 0x64, 0xa2, + 0x53, 0x4e, 0x18, 0x51, 0x33, 0xa8, 0x1e, 0x3a, + 0xc2, 0xe7, 0x2c, 0xe3, 0x63, 0x6d, 0x06, 0x29, + 0x28, + }, + .octets_len = 145, + .valid = 1, + }, + { + .description = + "generic hybrid point on sect571k1 (flipped y_bit)", + .nid = NID_sect571k1, + .octets = { + 0x06, 0x02, 0x2d, 0xf4, 0x72, 0x38, 0xc2, 0xbe, + 0x0d, 0xa4, 0xf1, 0xfc, 0xe8, 0x78, 0xe6, 0xad, + 0x5c, 0xaa, 0xd9, 0x7f, 0x7c, 0xa9, 0x4c, 0xe4, + 0xd2, 0xae, 0xba, 0xaa, 0x8c, 0x9d, 0x4c, 0xac, + 0x85, 0x00, 0xe1, 0xfa, 0x33, 0x73, 0x51, 0x52, + 0x5a, 0x4e, 0x75, 0x67, 0x1d, 0x0a, 0x2a, 0xd2, + 0x38, 0xb2, 0x9b, 0xe8, 0xec, 0xbe, 0x07, 0x8b, + 0xc0, 0x95, 0x77, 0xe9, 0x55, 0x0c, 0x6c, 0x0e, + 0x02, 0x3b, 0x34, 0xe2, 0xa8, 0x29, 0xd2, 0x97, + 0xd9, 0x05, 0xa2, 0x6f, 0xa8, 0x6f, 0x1c, 0x3a, + 0xf6, 0x12, 0x42, 0x1a, 0x26, 0x6e, 0x87, 0xf3, + 0x19, 0x04, 0x20, 0xa5, 0x29, 0x78, 0xee, 0xcf, + 0x91, 0x06, 0xd2, 0x5a, 0x62, 0x2a, 0x7f, 0x1d, + 0xa0, 0x7b, 0xb4, 0x31, 0x9c, 0xd2, 0x14, 0x60, + 0xf5, 0x9b, 0xea, 0x4a, 0x41, 0xad, 0x47, 0x72, + 0xf9, 0x01, 0xed, 0x7f, 0x5a, 0x27, 0x64, 0xa2, + 0x53, 0x4e, 0x18, 0x51, 0x33, 0xa8, 0x1e, 0x3a, + 0xc2, 0xe7, 0x2c, 0xe3, 0x63, 0x6d, 0x06, 0x29, + 0x28, + }, + .octets_len = 145, + .valid = 0, + }, +}; + +static const size_t N_POINT_CONVERSIONS = + sizeof(point_conversions) / sizeof(point_conversions[0]); + +static int +point_conversion_form_y_bit(const struct point_conversion *test) +{ + EC_GROUP *group = NULL; + EC_POINT *point = NULL; + int ret; + int failed = 0; + + if ((group = EC_GROUP_new_by_curve_name(test->nid)) == NULL) + errx(1, "group"); + if ((point = EC_POINT_new(group)) == NULL) + errx(1, "point"); + + ret = EC_POINT_oct2point(group, point, test->octets, test->octets_len, + NULL); + if (ret != test->valid) { + fprintf(stderr, "%s want %d got %d\n", test->description, + test->valid, ret); + failed |= 1; + } + + EC_GROUP_free(group); + EC_POINT_free(point); + + return failed; +} + +static int +test_point_conversions(void) +{ + size_t i; + int failed = 0; + + for (i = 0; i < N_POINT_CONVERSIONS; i++) + failed |= point_conversion_form_y_bit(&point_conversions[i]); + + fprintf(stderr, "%s: %s\n", __func__, failed ? "FAILED" : "SUCCESS"); + + return failed; +} + int main(int argc, char **argv) { @@ -242,6 +602,7 @@ main(int argc, char **argv) failed |= test_random_points(); failed |= hybrid_corner_case(); + failed |= test_point_conversions(); fprintf(stderr, "%s\n", failed ? "FAILED" : "SUCCESS");