@@ -28072,6 +28072,141 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2807228072 }
2807328073 }
2807428074#endif /* WOLF_CRYPTO_CB_FREE */
28075+ #ifdef WOLF_CRYPTO_CB_SETKEY
28076+ else if (info->algo_type == WC_ALGO_TYPE_SETKEY) {
28077+ #ifdef DEBUG_WOLFSSL
28078+ fprintf(stderr, "test_CryptoCb_Func: SetKey Type=%d\n",
28079+ info->setkey.type);
28080+ #endif
28081+ switch (info->setkey.type) {
28082+ #ifndef NO_AES
28083+ case WC_SETKEY_AES:
28084+ {
28085+ Aes* aes = (Aes*)info->setkey.obj;
28086+ aes->devId = INVALID_DEVID;
28087+ ret = wc_AesSetKey(aes,
28088+ (const byte*)info->setkey.key, info->setkey.keySz,
28089+ (const byte*)info->setkey.aux, info->setkey.flags);
28090+ aes->devId = thisDevId;
28091+ break;
28092+ }
28093+ #endif /* !NO_AES */
28094+ #ifndef NO_HMAC
28095+ case WC_SETKEY_HMAC:
28096+ {
28097+ Hmac* hmac = (Hmac*)info->setkey.obj;
28098+ hmac->devId = INVALID_DEVID;
28099+ ret = wc_HmacSetKey(hmac, hmac->macType,
28100+ (const byte*)info->setkey.key, info->setkey.keySz);
28101+ hmac->devId = thisDevId;
28102+ break;
28103+ }
28104+ #endif /* !NO_HMAC */
28105+ #if !defined(NO_RSA) && defined(WOLFSSL_KEY_TO_DER)
28106+ case WC_SETKEY_RSA_PUB:
28107+ {
28108+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
28109+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
28110+ int derSz;
28111+ word32 idx = 0;
28112+ byte* der = NULL;
28113+
28114+ derSz = wc_RsaPublicKeyDerSize(rsaTmp, 1);
28115+ if (derSz <= 0) { ret = derSz; break; }
28116+
28117+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28118+ if (der == NULL) { ret = MEMORY_E; break; }
28119+
28120+ derSz = wc_RsaKeyToPublicDer_ex(rsaTmp, der,
28121+ (word32)derSz, 1);
28122+ if (derSz <= 0) {
28123+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28124+ ret = derSz; break;
28125+ }
28126+
28127+ rsaObj->devId = INVALID_DEVID;
28128+ ret = wc_RsaPublicKeyDecode(der, &idx, rsaObj,
28129+ (word32)derSz);
28130+ rsaObj->devId = thisDevId;
28131+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28132+ break;
28133+ }
28134+ case WC_SETKEY_RSA_PRIV:
28135+ {
28136+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
28137+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
28138+ int derSz;
28139+ word32 idx = 0;
28140+ byte* der = NULL;
28141+
28142+ derSz = wc_RsaKeyToDer(rsaTmp, NULL, 0);
28143+ if (derSz <= 0) { ret = derSz; break; }
28144+
28145+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28146+ if (der == NULL) { ret = MEMORY_E; break; }
28147+
28148+ derSz = wc_RsaKeyToDer(rsaTmp, der, (word32)derSz);
28149+ if (derSz <= 0) {
28150+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28151+ ret = derSz; break;
28152+ }
28153+
28154+ rsaObj->devId = INVALID_DEVID;
28155+ ret = wc_RsaPrivateKeyDecode(der, &idx, rsaObj,
28156+ (word32)derSz);
28157+ rsaObj->devId = thisDevId;
28158+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28159+ break;
28160+ }
28161+ #endif /* !NO_RSA && WOLFSSL_KEY_TO_DER */
28162+ #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
28163+ defined(HAVE_ECC_KEY_IMPORT)
28164+ case WC_SETKEY_ECC_PUB:
28165+ {
28166+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
28167+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
28168+ byte buf[ECC_BUFSIZE];
28169+ word32 bufSz = sizeof(buf);
28170+ int curveId;
28171+
28172+ ret = wc_ecc_export_x963(eccTmp, buf, &bufSz);
28173+ if (ret != 0) break;
28174+
28175+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
28176+ eccObj->devId = INVALID_DEVID;
28177+ ret = wc_ecc_import_x963_ex2(buf, bufSz, eccObj, curveId, 0);
28178+ eccObj->devId = thisDevId;
28179+ break;
28180+ }
28181+ case WC_SETKEY_ECC_PRIV:
28182+ {
28183+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
28184+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
28185+ byte pubBuf[ECC_BUFSIZE];
28186+ byte privBuf[MAX_ECC_BYTES];
28187+ word32 pubSz = sizeof(pubBuf);
28188+ word32 privSz = sizeof(privBuf);
28189+ int curveId;
28190+
28191+ ret = wc_ecc_export_x963(eccTmp, pubBuf, &pubSz);
28192+ if (ret != 0) break;
28193+ ret = wc_ecc_export_private_only(eccTmp, privBuf, &privSz);
28194+ if (ret != 0) break;
28195+
28196+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
28197+ eccObj->devId = INVALID_DEVID;
28198+ ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28199+ pubBuf, pubSz, eccObj, curveId);
28200+ eccObj->devId = thisDevId;
28201+ break;
28202+ }
28203+ #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT && HAVE_ECC_KEY_IMPORT */
28204+ default:
28205+ ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
28206+ break;
28207+ }
28208+ }
28209+ #endif /* WOLF_CRYPTO_CB_SETKEY */
2807528210 (void)thisDevId;
2807628211 (void)keyFormat;
2807728212
0 commit comments