Skip to content

Commit 3975af8

Browse files
authored
Merge pull request #7191 from kojo1/ecpoint-h2p
Add EC_POINT_hex2point
2 parents 26284e2 + bc2b184 commit 3975af8

3 files changed

Lines changed: 124 additions & 14 deletions

File tree

src/pk.c

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
#ifdef HAVE_ECC
3434
#include <wolfssl/wolfcrypt/ecc.h>
35+
#ifdef HAVE_SELFTEST
36+
/* point compression types. */
37+
#define ECC_POINT_COMP_EVEN 0x02
38+
#define ECC_POINT_COMP_ODD 0x03
39+
#define ECC_POINT_UNCOMP 0x04
40+
#endif
3541
#endif
3642
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
3743
/* FIPS build has replaced ecc.h. */
@@ -9870,7 +9876,6 @@ void wolfSSL_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *point)
98709876
#endif
98719877
}
98729878

9873-
#ifndef HAVE_SELFTEST
98749879
/* Convert EC point to hex string that as either uncompressed or compressed.
98759880
*
98769881
* ECC point compression types were not included in selftest ecc.h
@@ -9983,7 +9988,100 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
99839988
return hex;
99849989
}
99859990

9986-
#endif /* HAVE_SELFTEST */
9991+
static size_t hex_to_bytes(const char *hex, unsigned char *output, size_t sz)
9992+
{
9993+
word32 i;
9994+
for (i = 0; i < sz; i++) {
9995+
signed char ch1, ch2;
9996+
ch1 = HexCharToByte(hex[i * 2]);
9997+
ch2 = HexCharToByte(hex[i * 2 + 1]);
9998+
if ((ch1 < 0) || (ch2 < 0)) {
9999+
WOLFSSL_MSG("hex_to_bytes: syntax error");
10000+
return 0;
10001+
}
10002+
output[i] = (unsigned char)((ch1 << 4) + ch2);
10003+
}
10004+
return sz;
10005+
}
10006+
10007+
WOLFSSL_EC_POINT* wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
10008+
const char *hex, WOLFSSL_EC_POINT*p, WOLFSSL_BN_CTX *ctx)
10009+
{
10010+
/* for uncompressed mode */
10011+
size_t str_sz;
10012+
BIGNUM *Gx = NULL;
10013+
BIGNUM *Gy = NULL;
10014+
char strGx[MAX_ECC_BYTES * 2 + 1];
10015+
10016+
/* for compressed mode */
10017+
int key_sz;
10018+
byte *octGx = (byte *)strGx; /* octGx[MAX_ECC_BYTES] */
10019+
10020+
int p_alloc = 0;
10021+
int ret;
10022+
10023+
WOLFSSL_ENTER("wolfSSL_EC_POINT_hex2point");
10024+
10025+
if (group == NULL || hex == NULL || ctx == NULL)
10026+
return NULL;
10027+
10028+
if (p == NULL) {
10029+
if ((p = wolfSSL_EC_POINT_new(group)) == NULL) {
10030+
WOLFSSL_MSG("wolfSSL_EC_POINT_new");
10031+
goto err;
10032+
}
10033+
p_alloc = 1;
10034+
}
10035+
10036+
key_sz = (wolfSSL_EC_GROUP_get_degree(group) + 7) / 8;
10037+
if (hex[0] == '0' && hex[1] == '4') { /* uncompressed mode */
10038+
str_sz = key_sz * 2;
10039+
10040+
XMEMSET(strGx, 0x0, str_sz + 1);
10041+
XMEMCPY(strGx, hex + 2, str_sz);
10042+
10043+
if (wolfSSL_BN_hex2bn(&Gx, strGx) == 0)
10044+
goto err;
10045+
10046+
if (wolfSSL_BN_hex2bn(&Gy, hex + 2 + str_sz) == 0)
10047+
goto err;
10048+
10049+
ret = wolfSSL_EC_POINT_set_affine_coordinates_GFp
10050+
(group, p, Gx, Gy, ctx);
10051+
10052+
if (ret != WOLFSSL_SUCCESS) {
10053+
WOLFSSL_MSG("wolfSSL_EC_POINT_set_affine_coordinates_GFp");
10054+
goto err;
10055+
}
10056+
}
10057+
else if (hex[0] == '0' && (hex[1] == '2' || hex[1] == '3')) {
10058+
size_t sz = XSTRLEN(hex + 2) / 2;
10059+
/* compressed mode */
10060+
octGx[0] = ECC_POINT_COMP_ODD;
10061+
if (hex_to_bytes(hex + 2, octGx + 1, sz) != sz) {
10062+
goto err;
10063+
}
10064+
if (wolfSSL_ECPoint_d2i(octGx, key_sz + 1, group, p)
10065+
!= WOLFSSL_SUCCESS) {
10066+
goto err;
10067+
}
10068+
}
10069+
else
10070+
goto err;
10071+
10072+
wolfSSL_BN_free(Gx);
10073+
wolfSSL_BN_free(Gy);
10074+
return p;
10075+
10076+
err:
10077+
wolfSSL_BN_free(Gx);
10078+
wolfSSL_BN_free(Gy);
10079+
if (p_alloc) {
10080+
EC_POINT_free(p);
10081+
}
10082+
return NULL;
10083+
10084+
}
998710085

998810086
/* Encode the EC point as an uncompressed point in DER.
998910087
*

tests/api.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61767,6 +61767,7 @@ static int test_wolfSSL_EC_POINT(void)
6176761767
EC_POINT* Gxy = NULL;
6176861768
EC_POINT* new_point = NULL;
6176961769
EC_POINT* set_point = NULL;
61770+
EC_POINT* get_point = NULL;
6177061771
EC_POINT* infinity = NULL;
6177161772
BIGNUM* k = NULL;
6177261773
BIGNUM* Gx = NULL;
@@ -61784,6 +61785,14 @@ static int test_wolfSSL_EC_POINT(void)
6178461785
"77037D812DEB33A0F4A13945D898C296";
6178561786
const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
6178661787
"2BCE33576B315ECECBB6406837BF51F5";
61788+
const char* uncompG
61789+
= "046B17D1F2E12C4247F8BCE6E563A440F2"
61790+
"77037D812DEB33A0F4A13945D898C296"
61791+
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
61792+
"2BCE33576B315ECECBB6406837BF51F5";
61793+
const char* compG
61794+
= "036B17D1F2E12C4247F8BCE6E563A440F2"
61795+
"77037D812DEB33A0F4A13945D898C296";
6178761796

6178861797
#ifndef HAVE_SELFTEST
6178961798
EC_POINT *tmp = NULL;
@@ -61792,10 +61801,6 @@ static int test_wolfSSL_EC_POINT(void)
6179261801
unsigned char* buf = NULL;
6179361802
unsigned char bufInf[1] = { 0x00 };
6179461803

61795-
const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
61796-
"77037D812DEB33A0F4A13945D898C296"
61797-
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
61798-
"2BCE33576B315ECECBB6406837BF51F5";
6179961804
const unsigned char binUncompG[] = {
6180061805
0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
6180161806
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
@@ -61813,8 +61818,6 @@ static int test_wolfSSL_EC_POINT(void)
6181361818
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
6181461819
};
6181561820

61816-
const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
61817-
"77037D812DEB33A0F4A13945D898C296";
6181861821
#ifdef HAVE_COMP_KEY
6181961822
const unsigned char binCompG[] = {
6182061823
0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
@@ -62039,7 +62042,6 @@ static int test_wolfSSL_EC_POINT(void)
6203962042
#endif
6204062043
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
6204162044

62042-
#ifndef HAVE_SELFTEST
6204362045
/* Test point to hex */
6204462046
ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
6204562047
ctx));
@@ -62056,13 +62058,22 @@ static int test_wolfSSL_EC_POINT(void)
6205662058
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
6205762059
ExpectNotNull(hexStr);
6205862060
ExpectStrEQ(hexStr, uncompG);
62061+
AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
62062+
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
6205962063
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
6206062064

6206162065
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
6206262066
ExpectNotNull(hexStr);
6206362067
ExpectStrEQ(hexStr, compG);
62068+
#ifdef HAVE_COMP_KEY
62069+
AssertNotNull(get_point = EC_POINT_hex2point
62070+
(group, hexStr, get_point, ctx));
62071+
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
62072+
#endif
6206462073
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
62074+
EC_POINT_free(get_point);
6206562075

62076+
#ifndef HAVE_SELFTEST
6206662077
/* Test point to oct */
6206762078
ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
6206862079
NULL, 0, ctx), 0);

wolfssl/openssl/ec.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,14 @@ WOLFSSL_API
318318
int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
319319
const WOLFSSL_EC_POINT *a);
320320

321-
#ifndef HAVE_SELFTEST
322321
WOLFSSL_API
323322
char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
324323
const WOLFSSL_EC_POINT* point, int form,
325324
WOLFSSL_BN_CTX* ctx);
326-
#endif
325+
WOLFSSL_API
326+
WOLFSSL_EC_POINT *wolfSSL_EC_POINT_hex2point
327+
(const WOLFSSL_EC_GROUP *group, const char *hex,
328+
WOLFSSL_EC_POINT *p, WOLFSSL_BN_CTX *ctx);
327329

328330
WOLFSSL_API const WOLFSSL_EC_KEY_METHOD *wolfSSL_EC_KEY_OpenSSL(void);
329331
WOLFSSL_API WOLFSSL_EC_KEY_METHOD *wolfSSL_EC_KEY_METHOD_new(
@@ -423,9 +425,8 @@ typedef WOLFSSL_EC_KEY_METHOD EC_KEY_METHOD;
423425
#define EC_KEY_set_conv_form wolfSSL_EC_KEY_set_conv_form
424426
#define EC_KEY_get_conv_form wolfSSL_EC_KEY_get_conv_form
425427

426-
#ifndef HAVE_SELFTEST
427-
#define EC_POINT_point2hex wolfSSL_EC_POINT_point2hex
428-
#endif
428+
#define EC_POINT_point2hex wolfSSL_EC_POINT_point2hex
429+
#define EC_POINT_hex2point wolfSSL_EC_POINT_hex2point
429430

430431
#define EC_POINT_dump wolfSSL_EC_POINT_dump
431432
#define EC_get_builtin_curves wolfSSL_EC_get_builtin_curves

0 commit comments

Comments
 (0)