Skip to content

Commit

Permalink
Add EC_POINT_hex2point: zd #17090
Browse files Browse the repository at this point in the history
  • Loading branch information
kojo1 committed May 29, 2024
1 parent 791c9e7 commit 7839f39
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#ifdef HAVE_SELFTEST
/* point compression types. */
#define ECC_POINT_COMP_EVEN 0x02
#define ECC_POINT_COMP_ODD 0x03
#define ECC_POINT_UNCOMP 0x04
#endif
#endif
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
/* FIPS build has replaced ecc.h. */
Expand Down Expand Up @@ -9787,12 +9793,12 @@ char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
* odd.
*/
hex[0] = mp_isodd((mp_int*)point->Y->internal) ?
0x03 : 0x02;
ECC_POINT_COMP_ODD : ECC_POINT_COMP_EVEN;
/* No y-ordinate. */
}
else {
/* Put in uncompressed format byte. */
hex[0] = 0x04;
hex[0] = ECC_POINT_UNCOMP;
/* Calculate offset as leading zeros not encoded. */
i = 1 + 2 * sz - mp_unsigned_bin_size((mp_int*)point->Y->internal);
/* Put in y-ordinate after x-ordinate. */
Expand Down Expand Up @@ -9848,13 +9854,12 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
size_t str_sz;
BIGNUM *Gx = NULL;
BIGNUM *Gy = NULL;
char *strGx = NULL;
char strGx[MAX_ECC_BYTES * 2];

/* for compressed mode */
/* for compressed mode */
int key_sz;
byte *octGx = NULL;
byte *octGx = (byte *)strGx; /* octGx[MAX_ECC_BYTES] */

#define P_ALLOC 1
int p_alloc = 0;
int ret;

Expand All @@ -9868,16 +9873,12 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
WOLFSSL_MSG("wolfSSL_EC_POINT_new");
goto err;
}
p_alloc = P_ALLOC;
p_alloc = 1;
}

key_sz = (wolfSSL_EC_GROUP_get_degree(group) + 7) / 8;
if (hex[0] == '0' && hex[1] == '4') { /* uncompressed mode */
str_sz = ((wolfSSL_EC_GROUP_get_degree(group) + 7) / 8) * 2;
strGx = (char *)XMALLOC(str_sz + 1, NULL, DYNAMIC_TYPE_ECC);
if (strGx == NULL) {
WOLFSSL_MSG("malloc error");
goto err;
}
str_sz = key_sz * 2;

XMEMSET(strGx, 0x0, str_sz + 1);
XMEMCPY(strGx, hex + 2, str_sz);
Expand All @@ -9898,13 +9899,7 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
}
else if (hex[0] == '0' && (hex[1] == '2' || hex[1] == '3')) {
/* compressed mode */
key_sz = ((wolfSSL_EC_GROUP_get_degree(group) + 7) / 8);
octGx = (byte *)XMALLOC(key_sz + 1, NULL, DYNAMIC_TYPE_ECC);
if (octGx == NULL) {
WOLFSSL_MSG("EEC_KEY_get_byte_size, XMALLOC");
goto err;
}
octGx[0] = 0x03;
octGx[0] = ECC_POINT_COMP_ODD;
if (hex_to_bytes(hex + 2, octGx + 1, XSTRLEN(hex + 2) / 2)
!= XSTRLEN(hex + 2) / 2) {
goto err;
Expand All @@ -9917,15 +9912,11 @@ WOLFSSL_EC_POINT*wolfSSL_EC_POINT_hex2point(const EC_GROUP *group,
else
goto err;

XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
wolfSSL_BN_free(Gx);
wolfSSL_BN_free(Gy);
return p;

err:
XFREE(strGx, NULL, DYNAMIC_TYPE_ECC);
XFREE(octGx, NULL, DYNAMIC_TYPE_ECC);
wolfSSL_BN_free(Gx);
wolfSSL_BN_free(Gy);
if (p_alloc) {
Expand Down

0 comments on commit 7839f39

Please sign in to comment.