mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +02:00
Fix for KCAPI KcapiEcc_LoadKey
parameter to kcapi_kpp_keygen
. Added option to release handle on load. Fixes for KCAPI sign output length. Added additional argument checking.
This commit is contained in:
@@ -5572,7 +5572,7 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen,
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#elif defined(WOLFSSL_KCAPI_ECC)
|
#elif defined(WOLFSSL_KCAPI_ECC)
|
||||||
err = KcapiEcc_Sign(key, in, inlen, out, outlen);
|
err = KcapiEcc_Sign(key, in, inlen, out, *outlen);
|
||||||
if (err != MP_OKAY) {
|
if (err != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -8673,7 +8673,7 @@ static int ecc_check_privkey_gen(ecc_key* key, mp_int* a, mp_int* prime)
|
|||||||
byte pubkey_raw[MAX_ECC_BYTES * 2];
|
byte pubkey_raw[MAX_ECC_BYTES * 2];
|
||||||
word32 pubkey_sz = (word32)sizeof(pubkey_raw);
|
word32 pubkey_sz = (word32)sizeof(pubkey_raw);
|
||||||
|
|
||||||
err = KcapiEcc_LoadKey(key, pubkey_raw, &pubkey_sz);
|
err = KcapiEcc_LoadKey(key, pubkey_raw, &pubkey_sz, 1);
|
||||||
if (err == 0) {
|
if (err == 0) {
|
||||||
err = mp_read_unsigned_bin(res->x, pubkey_raw,
|
err = mp_read_unsigned_bin(res->x, pubkey_raw,
|
||||||
pubkey_sz/2);
|
pubkey_sz/2);
|
||||||
|
@@ -82,16 +82,19 @@ static int KcapiEcc_CurveId(int curve_id, word32* kcapiCurveId)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz)
|
int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz,
|
||||||
|
int release_handle)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
word32 kcapiCurveId = 0;
|
word32 kcapiCurveId = 0;
|
||||||
|
word32 keySz;
|
||||||
|
|
||||||
if (key == NULL || key->dp == NULL) {
|
if (key == NULL || key->dp == NULL) {
|
||||||
ret = BAD_FUNC_ARG;
|
ret = BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
|
keySz = key->dp->size;
|
||||||
ret = KcapiEcc_CurveId(key->dp->id, &kcapiCurveId);
|
ret = KcapiEcc_CurveId(key->dp->id, &kcapiCurveId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +113,6 @@ int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz)
|
|||||||
* otherwise use existing key->handle */
|
* otherwise use existing key->handle */
|
||||||
if (ret == 0 && mp_iszero(&key->k) != MP_YES) {
|
if (ret == 0 && mp_iszero(&key->k) != MP_YES) {
|
||||||
byte priv[MAX_ECC_BYTES];
|
byte priv[MAX_ECC_BYTES];
|
||||||
word32 keySz = key->dp->size;
|
|
||||||
ret = wc_export_int(&key->k, priv, &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
|
ret = wc_export_int(&key->k, priv, &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = kcapi_kpp_setkey(key->handle, priv, keySz);
|
ret = kcapi_kpp_setkey(key->handle, priv, keySz);
|
||||||
@@ -122,7 +124,7 @@ int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz)
|
|||||||
|
|
||||||
/* optionally load public key */
|
/* optionally load public key */
|
||||||
if (ret == 0 && pubkey_raw != NULL && pubkey_sz != NULL) {
|
if (ret == 0 && pubkey_raw != NULL && pubkey_sz != NULL) {
|
||||||
ret = (int)kcapi_kpp_keygen(key->handle, pubkey_raw, *pubkey_sz,
|
ret = (int)kcapi_kpp_keygen(key->handle, pubkey_raw, keySz*2,
|
||||||
KCAPI_ACCESS_HEURISTIC);
|
KCAPI_ACCESS_HEURISTIC);
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
*pubkey_sz = ret;
|
*pubkey_sz = ret;
|
||||||
@@ -130,6 +132,11 @@ int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (release_handle && key != NULL && key->handle != NULL) {
|
||||||
|
kcapi_kpp_destroy(key->handle);
|
||||||
|
key->handle = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +156,7 @@ int KcapiEcc_MakeKey(ecc_key* key, int keysize, int curve_id)
|
|||||||
ret = BAD_FUNC_ARG;
|
ret = BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = KcapiEcc_LoadKey(key, key->pubkey_raw, &pubkey_sz);
|
ret = KcapiEcc_LoadKey(key, key->pubkey_raw, &pubkey_sz, 0);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = mp_read_unsigned_bin(key->pubkey.x,
|
ret = mp_read_unsigned_bin(key->pubkey.x,
|
||||||
key->pubkey_raw, pubkey_sz / 2);
|
key->pubkey_raw, pubkey_sz / 2);
|
||||||
@@ -179,15 +186,24 @@ int KcapiEcc_MakeKey(ecc_key* key, int keysize, int curve_id)
|
|||||||
int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
||||||
word32* outlen)
|
word32* outlen)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret = 0;
|
||||||
word32 kcapiCurveId = 0;
|
word32 kcapiCurveId = 0;
|
||||||
byte* buf_aligned = NULL;
|
byte* buf_aligned = NULL;
|
||||||
byte* pub_aligned = NULL;
|
byte* pub_aligned = NULL;
|
||||||
byte* out_aligned = NULL;
|
byte* out_aligned = NULL;
|
||||||
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
||||||
byte* pub = public_key->pubkey_raw;
|
byte* pub;
|
||||||
|
word32 keySz;
|
||||||
|
|
||||||
|
if (private_key == NULL || private_key->dp == NULL || public_key == NULL) {
|
||||||
|
ret = BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
|
pub = public_key->pubkey_raw;
|
||||||
|
keySz = private_key->dp->size;
|
||||||
ret = KcapiEcc_CurveId(private_key->dp->id, &kcapiCurveId);
|
ret = KcapiEcc_CurveId(private_key->dp->id, &kcapiCurveId);
|
||||||
|
}
|
||||||
if (ret == 0 && private_key->handle == NULL) {
|
if (ret == 0 && private_key->handle == NULL) {
|
||||||
ret = kcapi_kpp_init(&private_key->handle, WC_NAME_ECDH, 0);
|
ret = kcapi_kpp_init(&private_key->handle, WC_NAME_ECDH, 0);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
@@ -201,7 +217,6 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
|||||||
/* if a private key value is set, load and use it */
|
/* if a private key value is set, load and use it */
|
||||||
if (ret == 0 && mp_iszero(&private_key->k) != MP_YES) {
|
if (ret == 0 && mp_iszero(&private_key->k) != MP_YES) {
|
||||||
byte priv[MAX_ECC_BYTES];
|
byte priv[MAX_ECC_BYTES];
|
||||||
word32 keySz = private_key->dp->size;
|
|
||||||
ret = wc_export_int(&private_key->k, priv, &keySz, keySz,
|
ret = wc_export_int(&private_key->k, priv, &keySz, keySz,
|
||||||
WC_TYPE_UNSIGNED_BIN);
|
WC_TYPE_UNSIGNED_BIN);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
@@ -221,6 +236,12 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ret == 0) {
|
||||||
|
/* validate the output is large enough */
|
||||||
|
if (*outlen < keySz*2) {
|
||||||
|
ret = BUFFER_E;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
out_aligned = ((size_t)out % pageSz == 0) ? out : buf_aligned;
|
out_aligned = ((size_t)out % pageSz == 0) ? out : buf_aligned;
|
||||||
if ((size_t)pub % pageSz == 0) {
|
if ((size_t)pub % pageSz == 0) {
|
||||||
@@ -228,12 +249,11 @@ int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pub_aligned = buf_aligned + pageSz;
|
pub_aligned = buf_aligned + pageSz;
|
||||||
XMEMCPY(pub_aligned, pub, public_key->dp->size * 2);
|
XMEMCPY(pub_aligned, pub, keySz * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = (int)kcapi_kpp_ssgen(private_key->handle, pub_aligned,
|
ret = (int)kcapi_kpp_ssgen(private_key->handle, pub_aligned,
|
||||||
public_key->dp->size * 2, out_aligned,
|
keySz * 2, out_aligned, keySz * 2, KCAPI_ACCESS_HEURISTIC);
|
||||||
*outlen, KCAPI_ACCESS_HEURISTIC);
|
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
*outlen = ret / 2;
|
*outlen = ret / 2;
|
||||||
if (out_aligned != out) {
|
if (out_aligned != out) {
|
||||||
@@ -279,7 +299,7 @@ static int KcapiEcc_SetPrivKey(ecc_key* key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
||||||
word32* sigLen)
|
word32 sigLen)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
byte* buf_aligned = NULL;
|
byte* buf_aligned = NULL;
|
||||||
@@ -287,8 +307,13 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
|||||||
byte* sig_aligned = NULL;
|
byte* sig_aligned = NULL;
|
||||||
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
||||||
int handleInit = 0;
|
int handleInit = 0;
|
||||||
|
word32 keySz;
|
||||||
|
|
||||||
if (key->handle == NULL) {
|
if (key == NULL || key->dp == NULL) {
|
||||||
|
ret = BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == 0 && key->handle == NULL) {
|
||||||
ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
|
ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
WOLFSSL_MSG("KcapiEcc_Sign: Failed to initialize");
|
WOLFSSL_MSG("KcapiEcc_Sign: Failed to initialize");
|
||||||
@@ -306,6 +331,13 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ret == 0) {
|
||||||
|
/* make sure signature output is large enough */
|
||||||
|
keySz = key->dp->size;
|
||||||
|
if (sigLen < keySz*2) {
|
||||||
|
ret = BUFFER_E;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
sig_aligned = ((size_t)sig % pageSz == 0) ? sig : buf_aligned;
|
sig_aligned = ((size_t)sig % pageSz == 0) ? sig : buf_aligned;
|
||||||
if ((size_t)hash % pageSz == 0) {
|
if ((size_t)hash % pageSz == 0) {
|
||||||
@@ -316,9 +348,8 @@ int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
|||||||
XMEMCPY(hash_aligned, hash, hashLen);
|
XMEMCPY(hash_aligned, hash, hashLen);
|
||||||
}
|
}
|
||||||
ret = (int)kcapi_akcipher_sign(key->handle, hash_aligned, hashLen,
|
ret = (int)kcapi_akcipher_sign(key->handle, hash_aligned, hashLen,
|
||||||
sig_aligned, *sigLen, KCAPI_ACCESS_HEURISTIC);
|
sig_aligned, keySz*2, KCAPI_ACCESS_HEURISTIC);
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
*sigLen = ret;
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if (sig_aligned != sig) {
|
if (sig_aligned != sig) {
|
||||||
XMEMCPY(sig, sig_aligned, ret);
|
XMEMCPY(sig, sig_aligned, ret);
|
||||||
@@ -370,7 +401,11 @@ int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
|
|||||||
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
|
||||||
int handleInit = 0;
|
int handleInit = 0;
|
||||||
|
|
||||||
if (key->handle == NULL) {
|
if (key == NULL || key->dp == NULL) {
|
||||||
|
ret = BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == 0 && key->handle == NULL) {
|
||||||
ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
|
ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
WOLFSSL_MSG("KcapiEcc_Verify: Failed to initialize");
|
WOLFSSL_MSG("KcapiEcc_Verify: Failed to initialize");
|
||||||
|
@@ -41,11 +41,11 @@ WOLFSSL_LOCAL int KcapiEcc_SetPubKey(ecc_key* key);
|
|||||||
WOLFSSL_LOCAL void KcapiEcc_Free(ecc_key* key);
|
WOLFSSL_LOCAL void KcapiEcc_Free(ecc_key* key);
|
||||||
WOLFSSL_LOCAL int KcapiEcc_MakeKey(ecc_key* key, int keysize, int curve_id);
|
WOLFSSL_LOCAL int KcapiEcc_MakeKey(ecc_key* key, int keysize, int curve_id);
|
||||||
WOLFSSL_LOCAL int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw,
|
WOLFSSL_LOCAL int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw,
|
||||||
word32* pubkey_sz);
|
word32* pubkey_sz, int release_handle);
|
||||||
WOLFSSL_LOCAL int KcapiEcc_SharedSecret(ecc_key* private_key,
|
WOLFSSL_LOCAL int KcapiEcc_SharedSecret(ecc_key* private_key,
|
||||||
ecc_key* public_key, byte* out, word32* outlen);
|
ecc_key* public_key, byte* out, word32* outlen);
|
||||||
WOLFSSL_LOCAL int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen,
|
WOLFSSL_LOCAL int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen,
|
||||||
byte* sig, word32* sigLen);
|
byte* sig, word32 sigLen);
|
||||||
WOLFSSL_LOCAL int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen,
|
WOLFSSL_LOCAL int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen,
|
||||||
byte* sig, word32 sigLen);
|
byte* sig, word32 sigLen);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user