Skip to content

Commit bc2b184

Browse files
committed
Add EC_POINT_hex2point: zd #17090
1 parent 791c9e7 commit bc2b184

1 file changed

Lines changed: 22 additions & 33 deletions

File tree

src/pk.c

Lines changed: 22 additions & 33 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. */
@@ -9787,12 +9793,12 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
97879793
* odd.
97889794
*/
97899795
hex[0] = mp_isodd((mp_int*)point->Y->internal) ?
9790-
0x03 : 0x02;
9796+
ECC_POINT_COMP_ODD : ECC_POINT_COMP_EVEN;
97919797
/* No y-ordinate. */
97929798
}
97939799
else {
97949800
/* Put in uncompressed format byte. */
9795-
hex[0] = 0x04;
9801+
hex[0] = ECC_POINT_UNCOMP;
97969802
/* Calculate offset as leading zeros not encoded. */
97979803
i = 1 + 2 * sz - mp_unsigned_bin_size((mp_int*)point->Y->internal);
97989804
/* Put in y-ordinate after x-ordinate. */
@@ -9826,13 +9832,11 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
98269832
static size_t hex_to_bytes(const char *hex, unsigned char *output, size_t sz)
98279833
{
98289834
word32 i;
9829-
for (i = 0; i < sz; i++)
9830-
{
9835+
for (i = 0; i < sz; i++) {
98319836
signed char ch1, ch2;
98329837
ch1 = HexCharToByte(hex[i * 2]);
98339838
ch2 = HexCharToByte(hex[i * 2 + 1]);
9834-
if ((ch1 < 0) || (ch2 < 0))
9835-
{
9839+
if ((ch1 < 0) || (ch2 < 0)) {
98369840
WOLFSSL_MSG("hex_to_bytes: syntax error");
98379841
return 0;
98389842
}
@@ -9841,20 +9845,19 @@ static size_t hex_to_bytes(const char *hex, unsigned char *output, size_t sz)
98419845
return sz;
98429846
}
98439847

9844-
WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
9848+
WOLFSSL_EC_POINT* wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
98459849
const char *hex, WOLFSSL_EC_POINT*p, WOLFSSL_BN_CTX *ctx)
98469850
{
98479851
/* for uncompressed mode */
98489852
size_t str_sz;
98499853
BIGNUM *Gx = NULL;
98509854
BIGNUM *Gy = NULL;
9851-
char *strGx = NULL;
9855+
char strGx[MAX_ECC_BYTES * 2 + 1];
98529856

9853-
/* for compressed mode */
9857+
/* for compressed mode */
98549858
int key_sz;
9855-
byte *octGx = NULL;
9859+
byte *octGx = (byte *)strGx; /* octGx[MAX_ECC_BYTES] */
98569860

9857-
#define P_ALLOC 1
98589861
int p_alloc = 0;
98599862
int ret;
98609863

@@ -9868,24 +9871,20 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
98689871
WOLFSSL_MSG("wolfSSL_EC_POINT_new");
98699872
goto err;
98709873
}
9871-
p_alloc = P_ALLOC;
9874+
p_alloc = 1;
98729875
}
98739876

9877+
key_sz = (wolfSSL_EC_GROUP_get_degree(group) + 7) / 8;
98749878
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-
}
9879+
str_sz = key_sz * 2;
98819880

98829881
XMEMSET(strGx, 0x0, str_sz + 1);
98839882
XMEMCPY(strGx, hex + 2, str_sz);
98849883

9885-
if (BN_hex2bn(&Gx, strGx) == 0)
9884+
if (wolfSSL_BN_hex2bn(&Gx, strGx) == 0)
98869885
goto err;
98879886

9888-
if (BN_hex2bn(&Gy, hex + 2 + str_sz) == 0)
9887+
if (wolfSSL_BN_hex2bn(&Gy, hex + 2 + str_sz) == 0)
98899888
goto err;
98909889

98919890
ret = wolfSSL_EC_POINT_set_affine_coordinates_GFp
@@ -9897,16 +9896,10 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
98979896
}
98989897
}
98999898
else if (hex[0] == '0' && (hex[1] == '2' || hex[1] == '3')) {
9899+
size_t sz = XSTRLEN(hex + 2) / 2;
99009900
/* 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) {
9901+
octGx[0] = ECC_POINT_COMP_ODD;
9902+
if (hex_to_bytes(hex + 2, octGx + 1, sz) != sz) {
99109903
goto err;
99119904
}
99129905
if (wolfSSL_ECPoint_d2i(octGx, key_sz + 1, group, p)
@@ -9917,15 +9910,11 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
99179910
else
99189911
goto err;
99199912

9920-
XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
9921-
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
99229913
wolfSSL_BN_free(Gx);
99239914
wolfSSL_BN_free(Gy);
99249915
return p;
99259916

99269917
err:
9927-
XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
9928-
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
99299918
wolfSSL_BN_free(Gx);
99309919
wolfSSL_BN_free(Gy);
99319920
if (p_alloc) {

0 commit comments

Comments
 (0)