Skip to content

Commit e1761e4

Browse files
committed
Modify ECC pub param handling per review comments
1 parent ad49f68 commit e1761e4

File tree

4 files changed

+53
-68
lines changed

4 files changed

+53
-68
lines changed

include/wolfprovider/wp_params.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ void wp_param_set_mp_buf(OSSL_PARAM* p, const char* key, unsigned char* num,
5151

5252
int wp_params_get_digest(const OSSL_PARAM* params, char* name,
5353
OSSL_LIB_CTX* libCtx, enum wc_HashType* type, size_t* len);
54-
int wp_params_get_mp(const OSSL_PARAM* params, const char* key, mp_int* mp);
54+
int wp_params_get_mp(const OSSL_PARAM* params, const char* key, mp_int* mp,
55+
int *set);
5556
int wp_params_get_octet_string(const OSSL_PARAM* params, const char* key,
5657
unsigned char** data, size_t* len, int secure);
5758
int wp_params_get_bn_be(const OSSL_PARAM* params, const char* key,
@@ -70,7 +71,8 @@ int wp_params_get_int(const OSSL_PARAM* params, const char* key, int* val);
7071
int wp_params_get_uint(const OSSL_PARAM* params, const char* key,
7172
unsigned int* val, int* set);
7273

73-
int wp_params_set_mp(OSSL_PARAM params[], const char* key, mp_int* mp);
74+
int wp_params_set_mp(OSSL_PARAM params[], const char* key, mp_int* mp,
75+
int allow);
7476
int wp_params_set_octet_string_be(OSSL_PARAM params[], const char* key,
7577
unsigned char* data, size_t len);
7678

src/wp_dh_kmgmt.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,12 @@ static int wp_dh_get_params(wp_Dh* dh, OSSL_PARAM params[])
737737
ok = 0;
738738
}
739739
}
740-
if (ok && (!wp_params_set_mp(params, OSSL_PKEY_PARAM_FFC_P, &dh->key.p))) {
740+
if (ok && (!wp_params_set_mp(params, OSSL_PKEY_PARAM_FFC_P,
741+
&dh->key.p, 1))) {
741742
ok = 0;
742743
}
743-
if (ok && (!wp_params_set_mp(params, OSSL_PKEY_PARAM_FFC_G, &dh->key.g))) {
744+
if (ok && (!wp_params_set_mp(params, OSSL_PKEY_PARAM_FFC_G,
745+
&dh->key.g, 1))) {
744746
ok = 0;
745747
}
746748
if (ok && (!wp_params_set_octet_string_be(params, OSSL_PKEY_PARAM_PUB_KEY,

src/wp_ecc_kmgmt.c

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -550,31 +550,24 @@ static int wp_ecc_set_params_enc_pub_key(wp_Ecc *ecc, const OSSL_PARAM params[],
550550
static int wp_ecc_set_params_pub(wp_Ecc *ecc, const OSSL_PARAM params[])
551551
{
552552
int ok = 1;
553-
const OSSL_PARAM *p = NULL;
553+
int set = 0;
554554

555-
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_PUB_X);
556-
if (p != NULL) {
557-
if (!wp_params_get_mp(params, OSSL_PKEY_PARAM_EC_PUB_X,
558-
ecc->key.pubkey.x)) {
559-
ok = 0;
560-
}
561-
if (ok && mp_iszero(ecc->key.pubkey.x)) {
555+
if (!wp_params_get_mp(params, OSSL_PKEY_PARAM_EC_PUB_X,
556+
ecc->key.pubkey.x, &set)) {
557+
ok = 0;
558+
}
559+
if (ok && (set == 1)) {
560+
if (mp_iszero(ecc->key.pubkey.x)) {
562561
ok = 0;
563562
}
564563
if (ok) {
565564
ecc->key.type = ECC_PUBLICKEY;
566565
ecc->hasPub = 1;
567566
}
568567
}
569-
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_PUB_Y);
570-
if (p != NULL) {
571-
if (!wp_params_get_mp(params, OSSL_PKEY_PARAM_EC_PUB_Y,
572-
ecc->key.pubkey.y)) {
573-
ok = 0;
574-
}
575-
if (ok && mp_iszero(ecc->key.pubkey.y)) {
576-
ok = 0;
577-
}
568+
if (!wp_params_get_mp(params, OSSL_PKEY_PARAM_EC_PUB_Y,
569+
ecc->key.pubkey.y, NULL)) {
570+
ok = 0;
578571
}
579572
if (wp_ecc_set_params_enc_pub_key(ecc, params,
580573
OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY) != 1) {
@@ -736,43 +729,14 @@ static int wp_ecc_get_params_enc_pub_key(wp_Ecc* ecc, OSSL_PARAM params[],
736729
static int wp_ecc_get_params_pub(wp_Ecc* ecc, OSSL_PARAM params[])
737730
{
738731
int ok = 1;
739-
OSSL_PARAM* p;
740732

741-
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
742-
if ((p != NULL) && (ecc->hasPub == 0)) {
733+
if (!wp_params_set_mp(params, OSSL_PKEY_PARAM_EC_PUB_X, ecc->key.pubkey.x,
734+
(ecc->hasPub == 1))) {
743735
ok = 0;
744736
}
745-
if (ok && p != NULL) {
746-
size_t outLen = mp_unsigned_bin_size(ecc->key.pubkey.x);
747-
if (p->data != NULL) {
748-
if (p->data_size < outLen) {
749-
ok = 0;
750-
}
751-
if (ok && !wp_mp_to_unsigned_bin_le(ecc->key.pubkey.x,
752-
p->data, outLen)) {
753-
ok = 0;
754-
}
755-
}
756-
p->return_size = outLen;
757-
}
758-
if (ok) {
759-
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
760-
if ((p != NULL) && (ecc->hasPub == 0)) {
761-
ok = 0;
762-
}
763-
}
764-
if (ok && p != NULL) {
765-
size_t outLen = mp_unsigned_bin_size(ecc->key.pubkey.y);
766-
if (p->data != NULL) {
767-
if (p->data_size < outLen) {
768-
ok = 0;
769-
}
770-
if (ok && !wp_mp_to_unsigned_bin_le(ecc->key.pubkey.y,
771-
p->data, outLen)) {
772-
ok = 0;
773-
}
774-
}
775-
p->return_size = outLen;
737+
if (!wp_params_set_mp(params, OSSL_PKEY_PARAM_EC_PUB_Y, ecc->key.pubkey.y,
738+
(ecc->hasPub == 1))) {
739+
ok = 0;
776740
}
777741
/* Encoded public key. */
778742
if (ok && (!wp_ecc_get_params_enc_pub_key(ecc, params,
@@ -850,11 +814,11 @@ static int wp_ecc_get_params(wp_Ecc* ecc, OSSL_PARAM params[])
850814
}
851815
if (ok && (!wp_params_set_mp(params, OSSL_PKEY_PARAM_PRIV_KEY,
852816
#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) && LIBWOLFSSL_VERSION_HEX >= 0x05006002
853-
wc_ecc_key_get_priv(&ecc->key)
817+
wc_ecc_key_get_priv(&ecc->key),
854818
#else
855-
&(ecc->key.k)
819+
&(ecc->key.k),
856820
#endif
857-
))) {
821+
1))) {
858822
ok = 0;
859823
}
860824
/* Private key. */
@@ -1107,11 +1071,11 @@ static int wp_ecc_import_keypair(wp_Ecc* ecc, const OSSL_PARAM params[],
11071071
}
11081072
if (ok && priv && (!wp_params_get_mp(params, OSSL_PKEY_PARAM_PRIV_KEY,
11091073
#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)) && LIBWOLFSSL_VERSION_HEX >= 0x05006002
1110-
wc_ecc_key_get_priv(&ecc->key)
1074+
wc_ecc_key_get_priv(&ecc->key),
11111075
#else
1112-
&(ecc->key.k)
1076+
&(ecc->key.k),
11131077
#endif
1114-
))) {
1078+
NULL))) {
11151079
ok = 0;
11161080
}
11171081
if (ok &&

src/wp_params.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,34 @@ int wp_params_get_digest(const OSSL_PARAM* params, char* name,
254254
* @param [in] params Array of parameters.
255255
* @param [in] key String key to look for.
256256
* @param [out] mp Multi-precision number.
257+
* @param [out] set Indicates if mp has been set.
257258
* @return 1 on success.
258259
* @return 0 on failure.
259260
*/
260-
int wp_params_get_mp(const OSSL_PARAM* params, const char* key, mp_int* mp)
261+
int wp_params_get_mp(const OSSL_PARAM* params, const char* key, mp_int* mp,
262+
int *set)
261263
{
262264
int ok = 1;
263265
const OSSL_PARAM* p;
264266

267+
if (set != NULL) {
268+
*set = 0;
269+
}
270+
265271
p = OSSL_PARAM_locate_const(params, key);
266272
if ((p != NULL) && (p->data_type != OSSL_PARAM_UNSIGNED_INTEGER)) {
267-
ok = 0;
268-
}
269-
if ((p != NULL) && ok && (!wp_mp_read_unsigned_bin_le(mp, p->data,
270-
p->data_size))) {
271273
ok = 0;
272274
}
275+
if (ok && (p != NULL)) {
276+
if (!wp_mp_read_unsigned_bin_le(mp, p->data, p->data_size)) {
277+
ok = 0;
278+
}
279+
else {
280+
if (set != NULL) {
281+
*set = 1;
282+
}
283+
}
284+
}
273285

274286
WOLFPROV_LEAVE(WP_LOG_PROVIDER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
275287
return ok;
@@ -577,16 +589,21 @@ int wp_params_get_uint(const OSSL_PARAM* params, const char* key,
577589
* @param [in, out] params Array of parameters.
578590
* @param [in] key String key to look for.
579591
* @param [in] mp Multi-precision number.
592+
* @param [in] allow This mp is allowed to be set.
580593
* @return 1 on success.
581594
* @return 0 on failure.
582595
*/
583-
int wp_params_set_mp(OSSL_PARAM params[], const char* key, mp_int* mp)
596+
int wp_params_set_mp(OSSL_PARAM params[], const char* key, mp_int* mp,
597+
int allow)
584598
{
585599
int ok = 1;
586600
OSSL_PARAM* p;
587601

588602
p = OSSL_PARAM_locate(params, key);
589-
if (p != NULL) {
603+
if ((p != NULL) && (allow != 1)) {
604+
ok = 0;
605+
}
606+
if (ok && (p != NULL)) {
590607
size_t outLen = mp_unsigned_bin_size(mp);
591608
if (p->data != NULL) {
592609
if (p->data_size < outLen) {

0 commit comments

Comments
 (0)