Skip to content

Commit 1c59926

Browse files
committed
Add export key callbacks and get ecc size and ecc sig size callbacks
1 parent cd024f7 commit 1c59926

4 files changed

Lines changed: 342 additions & 2 deletions

File tree

wolfcrypt/src/cryptocb.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static const char* GetAlgoTypeStr(int algo)
9090
#ifdef WOLF_CRYPTO_CB_SETKEY
9191
case WC_ALGO_TYPE_SETKEY: return "SetKey";
9292
#endif /* WOLF_CRYPTO_CB_SETKEY */
93+
#ifdef WOLF_CRYPTO_CB_EXPORT_KEY
94+
case WC_ALGO_TYPE_EXPORT_KEY: return "ExportKey";
95+
#endif /* WOLF_CRYPTO_CB_EXPORT_KEY */
9396
}
9497
return NULL;
9598
}
@@ -120,6 +123,8 @@ static const char* GetPkTypeStr(int pk)
120123
case WC_PK_TYPE_CURVE25519: return "CURVE25519";
121124
case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
122125
case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
126+
case WC_PK_TYPE_EC_GET_SIZE: return "ECC GetSize";
127+
case WC_PK_TYPE_EC_GET_SIG_SIZE: return "ECC GetSigSize";
123128
}
124129
return NULL;
125130
}
@@ -297,6 +302,12 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
297302
GetSetKeyTypeStr(info->setkey.type), info->setkey.keySz);
298303
}
299304
#endif /* WOLF_CRYPTO_CB_SETKEY */
305+
#ifdef WOLF_CRYPTO_CB_EXPORT_KEY
306+
else if (info->algo_type == WC_ALGO_TYPE_EXPORT_KEY) {
307+
printf("Crypto CB: %s Type=%d\n",
308+
GetAlgoTypeStr(info->algo_type), info->export_key.type);
309+
}
310+
#endif /* WOLF_CRYPTO_CB_EXPORT_KEY */
300311
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \
301312
defined(HAVE_CMAC_KDF)
302313
else if (info->algo_type == WC_ALGO_TYPE_KDF) {
@@ -787,6 +798,54 @@ int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey,
787798
return wc_CryptoCb_TranslateErrorCode(ret);
788799
}
789800
#endif
801+
802+
int wc_CryptoCb_EccGetSize(const ecc_key* key, int* keySize)
803+
{
804+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
805+
CryptoCb* dev;
806+
807+
if (key == NULL)
808+
return ret;
809+
810+
/* locate registered callback */
811+
dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
812+
if (dev && dev->cb) {
813+
wc_CryptoInfo cryptoInfo;
814+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
815+
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
816+
cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIZE;
817+
cryptoInfo.pk.ecc_get_size.key = key;
818+
cryptoInfo.pk.ecc_get_size.keySize = keySize;
819+
820+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
821+
}
822+
823+
return wc_CryptoCb_TranslateErrorCode(ret);
824+
}
825+
826+
int wc_CryptoCb_EccGetSigSize(const ecc_key* key, int* sigSize)
827+
{
828+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
829+
CryptoCb* dev;
830+
831+
if (key == NULL)
832+
return ret;
833+
834+
/* locate registered callback */
835+
dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
836+
if (dev && dev->cb) {
837+
wc_CryptoInfo cryptoInfo;
838+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
839+
cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
840+
cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIG_SIZE;
841+
cryptoInfo.pk.ecc_get_sig_size.key = key;
842+
cryptoInfo.pk.ecc_get_sig_size.sigSize = sigSize;
843+
844+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
845+
}
846+
847+
return wc_CryptoCb_TranslateErrorCode(ret);
848+
}
790849
#endif /* HAVE_ECC */
791850

792851
#ifdef HAVE_CURVE25519
@@ -2214,6 +2273,37 @@ int wc_CryptoCb_SetKey(int devId, int type, void* obj,
22142273
}
22152274
#endif /* WOLF_CRYPTO_CB_SETKEY */
22162275

2276+
#ifdef WOLF_CRYPTO_CB_EXPORT_KEY
2277+
/* Generic ExportKey callback for exporting key material from hardware.
2278+
* devId: Device ID for the registered callback
2279+
* type: enum wc_PkType (WC_PK_TYPE_RSA, WC_PK_TYPE_ECDSA, etc.)
2280+
* obj: Hardware key object (has devCtx, id[], etc.)
2281+
* out: Software key object to fill (same type as obj, caller-allocated)
2282+
* The callback exports from hardware into the software key. The caller then
2283+
* uses normal software export functions on 'out' and frees it.
2284+
* Returns: 0 on success, CRYPTOCB_UNAVAILABLE if not handled, negative on error
2285+
*/
2286+
int wc_CryptoCb_ExportKey(int devId, int type, void* obj, void* out)
2287+
{
2288+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2289+
CryptoCb* dev;
2290+
2291+
dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_EXPORT_KEY);
2292+
if (dev && dev->cb) {
2293+
wc_CryptoInfo cryptoInfo;
2294+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2295+
cryptoInfo.algo_type = WC_ALGO_TYPE_EXPORT_KEY;
2296+
cryptoInfo.export_key.type = type;
2297+
cryptoInfo.export_key.obj = obj;
2298+
cryptoInfo.export_key.out = out;
2299+
2300+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2301+
}
2302+
2303+
return wc_CryptoCb_TranslateErrorCode(ret);
2304+
}
2305+
#endif /* WOLF_CRYPTO_CB_EXPORT_KEY */
2306+
22172307
#if defined(HAVE_CMAC_KDF)
22182308
/* Crypto callback for NIST SP 800 56C two-step CMAC KDF. See software
22192309
* implementation in wc_KDA_KDF_twostep_cmac for more comments.

wolfcrypt/src/ecc.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11983,7 +11983,27 @@ static int ecc_public_key_size(ecc_key* key, word32* sz)
1198311983
WOLFSSL_ABI
1198411984
int wc_ecc_size(ecc_key* key)
1198511985
{
11986-
if (key == NULL || key->dp == NULL)
11986+
#ifdef WOLF_CRYPTO_CB
11987+
int ret;
11988+
int keySz;
11989+
#endif
11990+
11991+
if (key == NULL)
11992+
return 0;
11993+
11994+
#ifdef WOLF_CRYPTO_CB
11995+
if (key->devId != INVALID_DEVID) {
11996+
keySz = 0;
11997+
ret = wc_CryptoCb_EccGetSize(key, &keySz);
11998+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
11999+
if (ret != 0)
12000+
return 0;
12001+
return keySz;
12002+
}
12003+
}
12004+
#endif
12005+
12006+
if (key->dp == NULL)
1198712007
return 0;
1198812008

1198912009
return key->dp->size;
@@ -12013,8 +12033,27 @@ int wc_ecc_sig_size(const ecc_key* key)
1201312033
{
1201412034
int maxSigSz;
1201512035
int orderBits, keySz;
12036+
#ifdef WOLF_CRYPTO_CB
12037+
int ret;
12038+
int cbKeySz;
12039+
#endif
1201612040

12017-
if (key == NULL || key->dp == NULL)
12041+
if (key == NULL)
12042+
return 0;
12043+
12044+
#ifdef WOLF_CRYPTO_CB
12045+
if (key->devId != INVALID_DEVID) {
12046+
cbKeySz = 0;
12047+
ret = wc_CryptoCb_EccGetSigSize(key, &cbKeySz);
12048+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
12049+
if (ret != 0 || cbKeySz == 0)
12050+
return 0;
12051+
return cbKeySz;
12052+
}
12053+
}
12054+
#endif
12055+
12056+
if (key->dp == NULL)
1201812057
return 0;
1201912058

1202012059
/* the signature r and s will always be less than order */

wolfcrypt/src/rsa.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,22 @@ int wc_FreeRsaKey(RsaKey* key)
557557
return BAD_FUNC_ARG;
558558
}
559559

560+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
561+
#ifndef WOLF_CRYPTO_CB_FIND
562+
if (key->devId != INVALID_DEVID)
563+
#endif
564+
{
565+
ret = wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK,
566+
WC_PK_TYPE_RSA, key);
567+
/* If callback wants standard free, it returns CRYPTOCB_UNAVAILABLE.
568+
* Otherwise assume the callback handled cleanup. */
569+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
570+
return ret;
571+
/* fall-through to software cleanup */
572+
ret = 0;
573+
}
574+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
575+
560576
wc_RsaCleanup(key);
561577

562578
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
@@ -4411,11 +4427,46 @@ int wc_RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz, byte* n,
44114427
word32* nSz)
44124428
{
44134429
int sz, ret;
4430+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
4431+
WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL);
4432+
#endif
44144433

44154434
if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) {
44164435
return BAD_FUNC_ARG;
44174436
}
44184437

4438+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
4439+
#ifndef WOLF_CRYPTO_CB_FIND
4440+
if (key->devId != INVALID_DEVID)
4441+
#endif
4442+
{
4443+
WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap);
4444+
if (!WC_VAR_OK(tmpKey)) {
4445+
return MEMORY_E;
4446+
}
4447+
XMEMSET(tmpKey, 0, sizeof(RsaKey));
4448+
4449+
ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID);
4450+
if (ret != 0) {
4451+
WC_FREE_VAR(tmpKey, key->heap);
4452+
return ret;
4453+
}
4454+
4455+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA,
4456+
(void*)key, tmpKey);
4457+
if (ret == 0) {
4458+
/* Recursive call on software tmpKey (INVALID_DEVID) */
4459+
ret = wc_RsaFlattenPublicKey(tmpKey, e, eSz, n, nSz);
4460+
}
4461+
ForceZero(tmpKey, sizeof(RsaKey));
4462+
wc_FreeRsaKey(tmpKey);
4463+
WC_FREE_VAR(tmpKey, key->heap);
4464+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
4465+
return ret;
4466+
/* fall through to software */
4467+
}
4468+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
4469+
44194470
sz = mp_unsigned_bin_size(&key->e);
44204471
if ((word32)sz > *eSz)
44214472
return RSA_BUFFER_E;
@@ -4464,10 +4515,48 @@ int wc_RsaExportKey(const RsaKey* key,
44644515
byte* q, word32* qSz)
44654516
{
44664517
int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
4518+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
4519+
WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL);
4520+
#endif
44674521

44684522
if (key && e && eSz && n && nSz && d && dSz && p && pSz && q && qSz)
44694523
ret = 0;
44704524

4525+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
4526+
if (ret == 0) {
4527+
#ifndef WOLF_CRYPTO_CB_FIND
4528+
if (key->devId != INVALID_DEVID)
4529+
#endif
4530+
{
4531+
WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap);
4532+
if (!WC_VAR_OK(tmpKey)) {
4533+
return MEMORY_E;
4534+
}
4535+
XMEMSET(tmpKey, 0, sizeof(RsaKey));
4536+
4537+
ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID);
4538+
if (ret != 0) {
4539+
WC_FREE_VAR(tmpKey, key->heap);
4540+
return ret;
4541+
}
4542+
4543+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA,
4544+
(void*)key, tmpKey);
4545+
if (ret == 0) {
4546+
/* Recursive call on software tmpKey (INVALID_DEVID) */
4547+
ret = wc_RsaExportKey(tmpKey, e, eSz, n, nSz,
4548+
d, dSz, p, pSz, q, qSz);
4549+
}
4550+
ForceZero(tmpKey, sizeof(RsaKey));
4551+
wc_FreeRsaKey(tmpKey);
4552+
WC_FREE_VAR(tmpKey, key->heap);
4553+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
4554+
return ret;
4555+
ret = 0; /* fall through to software */
4556+
}
4557+
}
4558+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
4559+
44714560
if (ret == 0)
44724561
ret = RsaGetValue(&key->e, e, eSz);
44734562
if (ret == 0)

0 commit comments

Comments
 (0)