@@ -28306,6 +28306,141 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2830628306 }
2830728307 }
2830828308#endif /* WOLF_CRYPTO_CB_FREE */
28309+ #ifdef WOLF_CRYPTO_CB_SETKEY
28310+ else if (info->algo_type == WC_ALGO_TYPE_SETKEY) {
28311+ #ifdef DEBUG_WOLFSSL
28312+ fprintf(stderr, "test_CryptoCb_Func: SetKey Type=%d\n",
28313+ info->setkey.type);
28314+ #endif
28315+ switch (info->setkey.type) {
28316+ #ifndef NO_AES
28317+ case WC_SETKEY_AES:
28318+ {
28319+ Aes* aes = (Aes*)info->setkey.obj;
28320+ aes->devId = INVALID_DEVID;
28321+ ret = wc_AesSetKey(aes,
28322+ (const byte*)info->setkey.key, info->setkey.keySz,
28323+ (const byte*)info->setkey.aux, info->setkey.flags);
28324+ aes->devId = thisDevId;
28325+ break;
28326+ }
28327+ #endif /* !NO_AES */
28328+ #ifndef NO_HMAC
28329+ case WC_SETKEY_HMAC:
28330+ {
28331+ Hmac* hmac = (Hmac*)info->setkey.obj;
28332+ hmac->devId = INVALID_DEVID;
28333+ ret = wc_HmacSetKey(hmac, hmac->macType,
28334+ (const byte*)info->setkey.key, info->setkey.keySz);
28335+ hmac->devId = thisDevId;
28336+ break;
28337+ }
28338+ #endif /* !NO_HMAC */
28339+ #if !defined(NO_RSA) && defined(WOLFSSL_KEY_TO_DER)
28340+ case WC_SETKEY_RSA_PUB:
28341+ {
28342+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
28343+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
28344+ int derSz;
28345+ word32 idx = 0;
28346+ byte* der = NULL;
28347+
28348+ derSz = wc_RsaPublicKeyDerSize(rsaTmp, 1);
28349+ if (derSz <= 0) { ret = derSz; break; }
28350+
28351+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28352+ if (der == NULL) { ret = MEMORY_E; break; }
28353+
28354+ derSz = wc_RsaKeyToPublicDer_ex(rsaTmp, der,
28355+ (word32)derSz, 1);
28356+ if (derSz <= 0) {
28357+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28358+ ret = derSz; break;
28359+ }
28360+
28361+ rsaObj->devId = INVALID_DEVID;
28362+ ret = wc_RsaPublicKeyDecode(der, &idx, rsaObj,
28363+ (word32)derSz);
28364+ rsaObj->devId = thisDevId;
28365+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28366+ break;
28367+ }
28368+ case WC_SETKEY_RSA_PRIV:
28369+ {
28370+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
28371+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
28372+ int derSz;
28373+ word32 idx = 0;
28374+ byte* der = NULL;
28375+
28376+ derSz = wc_RsaKeyToDer(rsaTmp, NULL, 0);
28377+ if (derSz <= 0) { ret = derSz; break; }
28378+
28379+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28380+ if (der == NULL) { ret = MEMORY_E; break; }
28381+
28382+ derSz = wc_RsaKeyToDer(rsaTmp, der, (word32)derSz);
28383+ if (derSz <= 0) {
28384+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28385+ ret = derSz; break;
28386+ }
28387+
28388+ rsaObj->devId = INVALID_DEVID;
28389+ ret = wc_RsaPrivateKeyDecode(der, &idx, rsaObj,
28390+ (word32)derSz);
28391+ rsaObj->devId = thisDevId;
28392+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
28393+ break;
28394+ }
28395+ #endif /* !NO_RSA && WOLFSSL_KEY_TO_DER */
28396+ #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
28397+ defined(HAVE_ECC_KEY_IMPORT)
28398+ case WC_SETKEY_ECC_PUB:
28399+ {
28400+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
28401+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
28402+ byte buf[ECC_BUFSIZE];
28403+ word32 bufSz = sizeof(buf);
28404+ int curveId;
28405+
28406+ ret = wc_ecc_export_x963(eccTmp, buf, &bufSz);
28407+ if (ret != 0) break;
28408+
28409+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
28410+ eccObj->devId = INVALID_DEVID;
28411+ ret = wc_ecc_import_x963_ex2(buf, bufSz, eccObj, curveId, 0);
28412+ eccObj->devId = thisDevId;
28413+ break;
28414+ }
28415+ case WC_SETKEY_ECC_PRIV:
28416+ {
28417+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
28418+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
28419+ byte pubBuf[ECC_BUFSIZE];
28420+ byte privBuf[MAX_ECC_BYTES];
28421+ word32 pubSz = sizeof(pubBuf);
28422+ word32 privSz = sizeof(privBuf);
28423+ int curveId;
28424+
28425+ ret = wc_ecc_export_x963(eccTmp, pubBuf, &pubSz);
28426+ if (ret != 0) break;
28427+ ret = wc_ecc_export_private_only(eccTmp, privBuf, &privSz);
28428+ if (ret != 0) break;
28429+
28430+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
28431+ eccObj->devId = INVALID_DEVID;
28432+ ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28433+ pubBuf, pubSz, eccObj, curveId);
28434+ eccObj->devId = thisDevId;
28435+ break;
28436+ }
28437+ #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT && HAVE_ECC_KEY_IMPORT */
28438+ default:
28439+ ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
28440+ break;
28441+ }
28442+ }
28443+ #endif /* WOLF_CRYPTO_CB_SETKEY */
2830928444 (void)thisDevId;
2831028445 (void)keyFormat;
2831128446
0 commit comments