Skip to content

Commit 791c9e7

Browse files
committed
Add EC_POINT_hex2point
1 parent 3db58af commit 791c9e7

3 files changed

Lines changed: 137 additions & 16 deletions

File tree

src/pk.c

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9711,7 +9711,6 @@ void wolfSSL_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *point)
97119711
#endif
97129712
}
97139713

9714-
#ifndef HAVE_SELFTEST
97159714
/* Convert EC point to hex string that as either uncompressed or compressed.
97169715
*
97179716
* ECC point compression types were not included in selftest ecc.h
@@ -9788,12 +9787,12 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
97889787
* odd.
97899788
*/
97909789
hex[0] = mp_isodd((mp_int*)point->Y->internal) ?
9791-
ECC_POINT_COMP_ODD : ECC_POINT_COMP_EVEN;
9790+
0x03 : 0x02;
97929791
/* No y-ordinate. */
97939792
}
97949793
else {
97959794
/* Put in uncompressed format byte. */
9796-
hex[0] = ECC_POINT_UNCOMP;
9795+
hex[0] = 0x04;
97979796
/* Calculate offset as leading zeros not encoded. */
97989797
i = 1 + 2 * sz - mp_unsigned_bin_size((mp_int*)point->Y->internal);
97999798
/* Put in y-ordinate after x-ordinate. */
@@ -9824,7 +9823,117 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
98249823
return hex;
98259824
}
98269825

9827-
#endif /* HAVE_SELFTEST */
9826+
static size_t hex_to_bytes(const char *hex, unsigned char *output, size_t sz)
9827+
{
9828+
word32 i;
9829+
for (i = 0; i < sz; i++)
9830+
{
9831+
signed char ch1, ch2;
9832+
ch1 = HexCharToByte(hex[i * 2]);
9833+
ch2 = HexCharToByte(hex[i * 2 + 1]);
9834+
if ((ch1 < 0) || (ch2 < 0))
9835+
{
9836+
WOLFSSL_MSG("hex_to_bytes: syntax error");
9837+
return 0;
9838+
}
9839+
output[i] = (unsigned char)((ch1 << 4) + ch2);
9840+
}
9841+
return sz;
9842+
}
9843+
9844+
WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
9845+
const char *hex, WOLFSSL_EC_POINT*p, WOLFSSL_BN_CTX *ctx)
9846+
{
9847+
/* for uncompressed mode */
9848+
size_t str_sz;
9849+
BIGNUM *Gx = NULL;
9850+
BIGNUM *Gy = NULL;
9851+
char *strGx = NULL;
9852+
9853+
/* for compressed mode */
9854+
int key_sz;
9855+
byte *octGx = NULL;
9856+
9857+
#define P_ALLOC 1
9858+
int p_alloc = 0;
9859+
int ret;
9860+
9861+
WOLFSSL_ENTER("wolfSSL_EC_POINT_hex2point");
9862+
9863+
if (group == NULL || hex == NULL || ctx == NULL)
9864+
return NULL;
9865+
9866+
if (p == NULL) {
9867+
if ((p = wolfSSL_EC_POINT_new(group)) == NULL) {
9868+
WOLFSSL_MSG("wolfSSL_EC_POINT_new");
9869+
goto err;
9870+
}
9871+
p_alloc = P_ALLOC;
9872+
}
9873+
9874+
if (hex[0] == '0' && hex[1] == '4') { /* uncompressed mode */
9875+
str_sz = ((wolfSSL_EC_GROUP_get_degree(group) + 7) / 8) * 2;
9876+
strGx = (char *)XMALLOC(str_sz + 1, NULL, DYNAMIC_TYPE_ECC);
9877+
if (strGx == NULL) {
9878+
WOLFSSL_MSG("malloc error");
9879+
goto err;
9880+
}
9881+
9882+
XMEMSET(strGx, 0x0, str_sz + 1);
9883+
XMEMCPY(strGx, hex + 2, str_sz);
9884+
9885+
if (BN_hex2bn(&Gx, strGx) == 0)
9886+
goto err;
9887+
9888+
if (BN_hex2bn(&Gy, hex + 2 + str_sz) == 0)
9889+
goto err;
9890+
9891+
ret = wolfSSL_EC_POINT_set_affine_coordinates_GFp
9892+
(group, p, Gx, Gy, ctx);
9893+
9894+
if (ret != WOLFSSL_SUCCESS) {
9895+
WOLFSSL_MSG("wolfSSL_EC_POINT_set_affine_coordinates_GFp");
9896+
goto err;
9897+
}
9898+
}
9899+
else if (hex[0] == '0' && (hex[1] == '2' || hex[1] == '3')) {
9900+
/* compressed mode */
9901+
key_sz = ((wolfSSL_EC_GROUP_get_degree(group) + 7) / 8);
9902+
octGx = (byte *)XMALLOC(key_sz + 1, NULL, DYNAMIC_TYPE_ECC);
9903+
if (octGx == NULL) {
9904+
WOLFSSL_MSG("EEC_KEY_get_byte_size, XMALLOC");
9905+
goto err;
9906+
}
9907+
octGx[0] = 0x03;
9908+
if (hex_to_bytes(hex + 2, octGx + 1, XSTRLEN(hex + 2) / 2)
9909+
!= XSTRLEN(hex + 2) / 2) {
9910+
goto err;
9911+
}
9912+
if (wolfSSL_ECPoint_d2i(octGx, key_sz + 1, group, p)
9913+
!= WOLFSSL_SUCCESS) {
9914+
goto err;
9915+
}
9916+
}
9917+
else
9918+
goto err;
9919+
9920+
XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
9921+
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
9922+
wolfSSL_BN_free(Gx);
9923+
wolfSSL_BN_free(Gy);
9924+
return p;
9925+
9926+
err:
9927+
XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
9928+
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
9929+
wolfSSL_BN_free(Gx);
9930+
wolfSSL_BN_free(Gy);
9931+
if (p_alloc) {
9932+
EC_POINT_free(p);
9933+
}
9934+
return NULL;
9935+
9936+
}
98289937

98299938
/* Encode the EC point as an uncompressed point in DER.
98309939
*

tests/api.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59640,6 +59640,7 @@ static int test_wolfSSL_EC_POINT(void)
5964059640
EC_POINT* Gxy = NULL;
5964159641
EC_POINT* new_point = NULL;
5964259642
EC_POINT* set_point = NULL;
59643+
EC_POINT* get_point = NULL;
5964359644
EC_POINT* infinity = NULL;
5964459645
BIGNUM* k = NULL;
5964559646
BIGNUM* Gx = NULL;
@@ -59657,6 +59658,14 @@ static int test_wolfSSL_EC_POINT(void)
5965759658
"77037D812DEB33A0F4A13945D898C296";
5965859659
const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
5965959660
"2BCE33576B315ECECBB6406837BF51F5";
59661+
const char* uncompG
59662+
= "046B17D1F2E12C4247F8BCE6E563A440F2"
59663+
"77037D812DEB33A0F4A13945D898C296"
59664+
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
59665+
"2BCE33576B315ECECBB6406837BF51F5";
59666+
const char* compG
59667+
= "036B17D1F2E12C4247F8BCE6E563A440F2"
59668+
"77037D812DEB33A0F4A13945D898C296";
5966059669

5966159670
#ifndef HAVE_SELFTEST
5966259671
EC_POINT *tmp = NULL;
@@ -59665,10 +59674,6 @@ static int test_wolfSSL_EC_POINT(void)
5966559674
unsigned char* buf = NULL;
5966659675
unsigned char bufInf[1] = { 0x00 };
5966759676

59668-
const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
59669-
"77037D812DEB33A0F4A13945D898C296"
59670-
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
59671-
"2BCE33576B315ECECBB6406837BF51F5";
5967259677
const unsigned char binUncompG[] = {
5967359678
0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
5967459679
0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
@@ -59686,8 +59691,6 @@ static int test_wolfSSL_EC_POINT(void)
5968659691
0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
5968759692
};
5968859693

59689-
const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
59690-
"77037D812DEB33A0F4A13945D898C296";
5969159694
#ifdef HAVE_COMP_KEY
5969259695
const unsigned char binCompG[] = {
5969359696
0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
@@ -59912,7 +59915,6 @@ static int test_wolfSSL_EC_POINT(void)
5991259915
#endif
5991359916
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
5991459917

59915-
#ifndef HAVE_SELFTEST
5991659918
/* Test point to hex */
5991759919
ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
5991859920
ctx));
@@ -59929,13 +59931,22 @@ static int test_wolfSSL_EC_POINT(void)
5992959931
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
5993059932
ExpectNotNull(hexStr);
5993159933
ExpectStrEQ(hexStr, uncompG);
59934+
AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
59935+
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
5993259936
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
5993359937

5993459938
hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
5993559939
ExpectNotNull(hexStr);
5993659940
ExpectStrEQ(hexStr, compG);
59941+
#ifdef HAVE_COMP_KEY
59942+
AssertNotNull(get_point = EC_POINT_hex2point
59943+
(group, hexStr, get_point, ctx));
59944+
AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
59945+
#endif
5993759946
XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
59947+
EC_POINT_free(get_point);
5993859948

59949+
#ifndef HAVE_SELFTEST
5993959950
/* Test point to oct */
5994059951
ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
5994159952
NULL, 0, ctx), 0);

wolfssl/openssl/ec.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,14 @@ WOLFSSL_API
306306
int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
307307
const WOLFSSL_EC_POINT *a);
308308

309-
#ifndef HAVE_SELFTEST
310309
WOLFSSL_API
311310
char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
312311
const WOLFSSL_EC_POINT* point, int form,
313312
WOLFSSL_BN_CTX* ctx);
314-
#endif
313+
WOLFSSL_API
314+
WOLFSSL_EC_POINT *wolfSSL_EC_POINT_hex2point
315+
(const WOLFSSL_EC_GROUP *group, const char *hex,
316+
WOLFSSL_EC_POINT *p, WOLFSSL_BN_CTX *ctx);
315317

316318
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
317319

@@ -395,9 +397,8 @@ typedef WOLFSSL_EC_BUILTIN_CURVE EC_builtin_curve;
395397
#define EC_KEY_set_conv_form wolfSSL_EC_KEY_set_conv_form
396398
#define EC_KEY_get_conv_form wolfSSL_EC_KEY_get_conv_form
397399

398-
#ifndef HAVE_SELFTEST
399-
#define EC_POINT_point2hex wolfSSL_EC_POINT_point2hex
400-
#endif
400+
#define EC_POINT_point2hex wolfSSL_EC_POINT_point2hex
401+
#define EC_POINT_hex2point wolfSSL_EC_POINT_hex2point
401402

402403
#define EC_POINT_dump wolfSSL_EC_POINT_dump
403404
#define EC_get_builtin_curves wolfSSL_EC_get_builtin_curves

0 commit comments

Comments
 (0)