@@ -506,72 +506,77 @@ static const OSSL_PARAM* wp_ecc_settable_params(WOLFPROV_CTX* provCtx)
506506}
507507
508508/**
509- * Set the public key's X and Y ordinates into ECC key object.
509+ * Set the encoded public key parameter into ECC key object.
510510 *
511511 * @param [in, out] ecc ECC key object.
512512 * @param [in] params Array of parameters and values.
513+ * @param [in] key String to look for.
513514 * @return 1 on success.
514515 * @return 0 on failure.
515516 */
516- static int wp_ecc_set_params_x_y (wp_Ecc * ecc , const OSSL_PARAM params [])
517+ static int wp_ecc_set_params_enc_pub_key (wp_Ecc * ecc , const OSSL_PARAM params [],
518+ const char * key )
517519{
518520 int ok = 1 ;
521+ unsigned char * data = NULL ;
522+ size_t len ;
519523
520- if (!wp_params_get_mp (params , OSSL_PKEY_PARAM_EC_PUB_X ,
521- ecc -> key .pubkey .x )) {
522- ok = 0 ;
523- }
524- if (ok && mp_iszero (ecc -> key .pubkey .x )) {
525- ok = 0 ;
526- }
527- if (ok && (!wp_params_get_mp (params , OSSL_PKEY_PARAM_EC_PUB_Y ,
528- ecc -> key .pubkey .y ))) {
529- ok = 0 ;
530- }
531- if (ok && mp_iszero (ecc -> key .pubkey .x )) {
524+ if (!wp_params_get_octet_string_ptr (params , key , & data , & len )) {
532525 ok = 0 ;
533526 }
534- if (ok ) {
535- ok = (mp_set (ecc -> key .pubkey .y , 1 ) == 0 );
536- }
537- if (ok ) {
538- ecc -> key .type = ECC_PUBLICKEY ;
539- ecc -> hasPub = 1 ;
527+ if (ok && (data != NULL )) {
528+ int rc = wc_ecc_import_x963_ex (data , (word32 )len , & ecc -> key ,
529+ ecc -> curveId );
530+ if (rc != 0 ) {
531+ ok = 0 ;
532+ }
533+ if (ok ) {
534+ ecc -> hasPub = 1 ;
535+ }
540536 }
541537
542538 WOLFPROV_LEAVE (WP_LOG_PK , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
543539 return ok ;
544540}
545541
546542/**
547- * Set the encoded public key parameter into ECC key object.
543+ * Set the public key values into ECC key object.
548544 *
549545 * @param [in, out] ecc ECC key object.
550546 * @param [in] params Array of parameters and values.
551- * @param [in] key String to look for.
552547 * @return 1 on success.
553548 * @return 0 on failure.
554549 */
555- static int wp_ecc_set_params_enc_pub_key (wp_Ecc * ecc , const OSSL_PARAM params [],
556- const char * key )
550+ static int wp_ecc_set_params_pub (wp_Ecc * ecc , const OSSL_PARAM params [])
557551{
558552 int ok = 1 ;
559- unsigned char * data = NULL ;
560- size_t len ;
553+ int set = 0 ;
561554
562- if (!wp_params_get_octet_string_ptr (params , key , & data , & len )) {
555+ if (!wp_params_get_mp (params , OSSL_PKEY_PARAM_EC_PUB_X ,
556+ ecc -> key .pubkey .x , & set )) {
563557 ok = 0 ;
564558 }
565- if (ok && (data != NULL )) {
566- int rc = wc_ecc_import_x963_ex (data , (word32 )len , & ecc -> key ,
567- ecc -> curveId );
568- if (rc != 0 ) {
559+ if (ok && (set == 1 )) {
560+ if (mp_iszero (ecc -> key .pubkey .x )) {
569561 ok = 0 ;
570562 }
571563 if (ok ) {
564+ ecc -> key .type = ECC_PUBLICKEY ;
572565 ecc -> hasPub = 1 ;
573566 }
574567 }
568+ if (!wp_params_get_mp (params , OSSL_PKEY_PARAM_EC_PUB_Y ,
569+ ecc -> key .pubkey .y , NULL )) {
570+ ok = 0 ;
571+ }
572+ if (wp_ecc_set_params_enc_pub_key (ecc , params ,
573+ OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY ) != 1 ) {
574+ ok = 0 ;
575+ }
576+ if (wp_ecc_set_params_enc_pub_key (ecc , params ,
577+ OSSL_PKEY_PARAM_PUB_KEY ) != 1 ) {
578+ ok = 0 ;
579+ }
575580
576581 WOLFPROV_LEAVE (WP_LOG_PK , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
577582 return ok ;
@@ -591,11 +596,8 @@ static int wp_ecc_set_params(wp_Ecc *ecc, const OSSL_PARAM params[])
591596 const OSSL_PARAM * p ;
592597
593598 if (params != NULL ) {
594- if (!wp_ecc_set_params_x_y (ecc , params )) {
595- if (!wp_ecc_set_params_enc_pub_key (ecc , params ,
596- OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY )) {
597- ok = 0 ;
598- }
599+ if (!wp_ecc_set_params_pub (ecc , params )) {
600+ ok = 0 ;
599601 }
600602 if (ok ) {
601603 p = OSSL_PARAM_locate_const (params ,
@@ -695,22 +697,61 @@ static int wp_ecc_get_params_enc_pub_key(wp_Ecc* ecc, OSSL_PARAM params[],
695697 int rc ;
696698 word32 outLen = (word32 )p -> return_size ;
697699
698- if (p -> data == NULL ) {
699- outLen = 1 + 2 * (( ecc -> bits + 7 ) / 8 ) ;
700+ if (ecc -> hasPub == 0 ) {
701+ ok = 0 ;
700702 }
701- else {
702- rc = wc_ecc_export_x963_ex (& ecc -> key , p -> data , & outLen , 0 );
703- if (rc != 0 ) {
704- ok = 0 ;
703+ if (ok ) {
704+ if (p -> data == NULL ) {
705+ outLen = 1 + 2 * ((ecc -> bits + 7 ) / 8 );
705706 }
707+ else {
708+ rc = wc_ecc_export_x963_ex (& ecc -> key , p -> data , & outLen , 0 );
709+ if (rc != 0 ) {
710+ ok = 0 ;
711+ }
712+ }
713+ p -> return_size = outLen ;
706714 }
707- p -> return_size = outLen ;
708715 }
709716
710717 WOLFPROV_LEAVE (WP_LOG_PK , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
711718 return ok ;
712719}
713720
721+ /**
722+ * Get the public key into parameters.
723+ *
724+ * @param [in] ecc ECC key object.
725+ * @param [in, out] params Array of parameters and values.
726+ * @return 1 on success.
727+ * @return 0 on failure.
728+ */
729+ static int wp_ecc_get_params_pub (wp_Ecc * ecc , OSSL_PARAM params [])
730+ {
731+ int ok = 1 ;
732+
733+ if (!wp_params_set_mp (params , OSSL_PKEY_PARAM_EC_PUB_X , ecc -> key .pubkey .x ,
734+ (ecc -> hasPub == 1 ))) {
735+ ok = 0 ;
736+ }
737+ if (!wp_params_set_mp (params , OSSL_PKEY_PARAM_EC_PUB_Y , ecc -> key .pubkey .y ,
738+ (ecc -> hasPub == 1 ))) {
739+ ok = 0 ;
740+ }
741+ /* Encoded public key. */
742+ if (ok && (!wp_ecc_get_params_enc_pub_key (ecc , params ,
743+ OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY ))) {
744+ ok = 0 ;
745+ }
746+ /* Public key. */
747+ if (ok && (!wp_ecc_get_params_enc_pub_key (ecc , params ,
748+ OSSL_PKEY_PARAM_PUB_KEY ))) {
749+ ok = 0 ;
750+ }
751+
752+ return ok ;
753+ }
754+
714755/**
715756 * Get the ECC key parameters.
716757 *
@@ -767,33 +808,17 @@ static int wp_ecc_get_params(wp_Ecc* ecc, OSSL_PARAM params[])
767808 ok = 0 ;
768809 }
769810 }
770- /* X-ordinate of public key. */
771- if (ok && (!wp_params_set_mp (params , OSSL_PKEY_PARAM_EC_PUB_X ,
772- ecc -> key .pubkey .x ))) {
773- ok = 0 ;
774- }
775- /* Y-ordinate of public key. */
776- if (ok && (!wp_params_set_mp (params , OSSL_PKEY_PARAM_EC_PUB_Y ,
777- ecc -> key .pubkey .y ))) {
778- ok = 0 ;
779- }
780- /* Encoded public key. */
781- if (ok && (!wp_ecc_get_params_enc_pub_key (ecc , params ,
782- OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY ))) {
783- ok = 0 ;
784- }
785- /* Public key. */
786- if (ok && (!wp_ecc_get_params_enc_pub_key (ecc , params ,
787- OSSL_PKEY_PARAM_PUB_KEY ))) {
788- ok = 0 ;
811+ /* Public key */
812+ if (ok ) {
813+ ok = wp_ecc_get_params_pub (ecc , params );
789814 }
790815 if (ok && (!wp_params_set_mp (params , OSSL_PKEY_PARAM_PRIV_KEY ,
791816#if (!defined (HAVE_FIPS ) || FIPS_VERSION_GE (5 ,3 )) && LIBWOLFSSL_VERSION_HEX >= 0x05006002
792- wc_ecc_key_get_priv (& ecc -> key )
817+ wc_ecc_key_get_priv (& ecc -> key ),
793818#else
794- & (ecc -> key .k )
819+ & (ecc -> key .k ),
795820#endif
796- ))) {
821+ 1 ))) {
797822 ok = 0 ;
798823 }
799824 /* Private key. */
@@ -1041,19 +1066,16 @@ static int wp_ecc_import_keypair(wp_Ecc* ecc, const OSSL_PARAM params[],
10411066{
10421067 int ok = 1 ;
10431068
1044- /* This call sets hasPub field in wp_Ecc. */
1045- if (!wp_ecc_set_params_x_y (ecc , params )) {
1046- /* Try direct import of encoded public key instead. */
1047- ok = wp_ecc_set_params_enc_pub_key (ecc , params ,
1048- OSSL_PKEY_PARAM_PUB_KEY );
1069+ if (wp_ecc_set_params_pub (ecc , params ) != 1 ) {
1070+ ok = 0 ;
10491071 }
10501072 if (ok && priv && (!wp_params_get_mp (params , OSSL_PKEY_PARAM_PRIV_KEY ,
10511073#if (!defined (HAVE_FIPS ) || FIPS_VERSION_GE (5 ,3 )) && LIBWOLFSSL_VERSION_HEX >= 0x05006002
1052- wc_ecc_key_get_priv (& ecc -> key )
1074+ wc_ecc_key_get_priv (& ecc -> key ),
10531075#else
1054- & (ecc -> key .k )
1076+ & (ecc -> key .k ),
10551077#endif
1056- ))) {
1078+ NULL ))) {
10571079 ok = 0 ;
10581080 }
10591081 if (ok &&
@@ -2027,13 +2049,21 @@ static int wp_ecc_decode_pki(wp_Ecc* ecc, unsigned char* data, word32 len)
20272049#endif
20282050 if (ok ) {
20292051 ecc -> curveId = ecc -> key .dp -> id ;
2030- /* ECC_PRIVATEKEY_ONLY when no public key data. */
2031- ecc -> hasPub = ecc -> key .type == ECC_PRIVATEKEY ;
20322052 ecc -> hasPriv = 1 ;
20332053 /* Needs curveId set. */
20342054 if (!wp_ecc_set_bits (ecc )) {
20352055 ok = 0 ;
20362056 }
2057+
2058+ /* Keys decoded from pki should always have public key */
2059+ if (ecc -> key .type == ECC_PRIVATEKEY_ONLY ) {
2060+ #ifdef ECC_TIMING_RESISTANT
2061+ rc = wc_ecc_make_pub_ex (& ecc -> key , NULL , & ecc -> rng );
2062+ #else
2063+ rc = wc_ecc_make_pub_ex (& ecc -> key , NULL , NULL );
2064+ #endif
2065+ }
2066+ ecc -> hasPub = 1 ;
20372067 }
20382068
20392069 WOLFPROV_LEAVE (WP_LOG_PK , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
0 commit comments