Skip to content

Commit 5cafaa4

Browse files
committed
Add type specific ECC public key encode/deocde handling ECC X9.63 public key format
1 parent f494d08 commit 5cafaa4

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

src/wp_ecc_kmgmt.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,28 @@ static int wp_ecc_decode_params(wp_Ecc* ecc, unsigned char* data, word32 len)
19951995
return ok;
19961996
}
19971997

1998+
static int wp_ecc_decode_x963_pub(wp_Ecc* ecc, unsigned char* data, word32 len)
1999+
{
2000+
int ok = 1;
2001+
int rc;
2002+
2003+
rc = wc_ecc_import_x963((const byte *)data, len, &ecc->key);
2004+
if (rc != 0) {
2005+
ok = 0;
2006+
}
2007+
if (ok) {
2008+
ecc->curveId = ecc->key.dp->id;
2009+
ecc->hasPub = 1;
2010+
/* Needs curveId set. */
2011+
if (!wp_ecc_set_bits(ecc)) {
2012+
ok = 0;
2013+
}
2014+
}
2015+
2016+
WOLFPROV_LEAVE(WP_LOG_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
2017+
return ok;
2018+
}
2019+
19982020
/**
19992021
* Decode the SubjectPublicInfo DER encoded ECC key into the ECC key object.
20002022
*
@@ -2160,6 +2182,12 @@ static int wp_ecc_decode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
21602182
decoded = 0;
21612183
}
21622184
}
2185+
else if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
2186+
if (!wp_ecc_decode_x963_pub(ecc, data, len)) {
2187+
ok = 0;
2188+
decoded = 0;
2189+
}
2190+
}
21632191
else {
21642192
if (!wp_ecc_decode_params(ecc, data, len)) {
21652193
ok = 0;
@@ -2253,6 +2281,61 @@ static int wp_ecc_encode_params(const wp_Ecc *ecc, unsigned char* keyData,
22532281
return ok;
22542282
}
22552283

2284+
/**
2285+
* Get the public key encoding size.
2286+
*
2287+
* @param [in] ecc ECC key object.
2288+
* @param [out] keyLen Length of encoding in bytes.
2289+
* @return 1 on success.
2290+
* @return 0 on failure.
2291+
*/
2292+
static int wp_ecc_encode_pub_size(const wp_Ecc *ecc, size_t* keyLen)
2293+
{
2294+
int ok = 1;
2295+
int rc;
2296+
word32 len;
2297+
2298+
rc = wc_ecc_export_x963_ex((ecc_key*)&ecc->key, NULL, &len, 0);
2299+
if (rc != LENGTH_ONLY_E) {
2300+
ok = 0;
2301+
}
2302+
if (ok) {
2303+
*keyLen = len;
2304+
}
2305+
2306+
WOLFPROV_LEAVE(WP_LOG_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
2307+
return ok;
2308+
}
2309+
2310+
/**
2311+
* Encode the ECC public key.
2312+
*
2313+
* @param [in] ecc ECC key object.
2314+
* @param [out] keyData Buffer to hold encoded data.
2315+
* @param [in, out] keyLen On in, length of buffer in bytes.
2316+
* On out, length of encoding in bytes.
2317+
* @return 1 on success.
2318+
* @return 0 on failure.
2319+
*/
2320+
static int wp_ecc_encode_pub(const wp_Ecc *ecc, unsigned char* keyData,
2321+
size_t* keyLen)
2322+
{
2323+
int ok = 1;
2324+
int rc;
2325+
word32 len = (word32)*keyLen;
2326+
2327+
rc = wc_ecc_export_x963_ex((ecc_key*)&ecc->key, keyData, &len, 0);
2328+
if (rc != 0) {
2329+
ok = 0;
2330+
}
2331+
if (ok) {
2332+
*keyLen = len;
2333+
}
2334+
2335+
WOLFPROV_LEAVE(WP_LOG_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
2336+
return ok;
2337+
}
2338+
22562339
/**
22572340
* Get the PKCS#8 encoding size for the key.
22582341
*
@@ -2548,6 +2631,11 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
25482631
ok = 0;
25492632
}
25502633
}
2634+
else if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
2635+
if (!wp_ecc_encode_pub_size(key, &derLen)) {
2636+
ok = 0;
2637+
}
2638+
}
25512639
else if (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) {
25522640
if (!wp_ecc_encode_params_size(key, &derLen)) {
25532641
ok = 0;
@@ -2591,6 +2679,12 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
25912679
ok = 0;
25922680
}
25932681
}
2682+
else if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
2683+
pemType = PUBLICKEY_TYPE;
2684+
if (!wp_ecc_encode_pub(key, derData, &derLen)) {
2685+
ok = 0;
2686+
}
2687+
}
25942688
else if (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) {
25952689
pemType = DH_PARAM_TYPE;
25962690
if (!wp_ecc_encode_params(key, derData, &derLen)) {

0 commit comments

Comments
 (0)