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