@@ -27848,6 +27848,141 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2784827848 }
2784927849 }
2785027850#endif /* WOLF_CRYPTO_CB_FREE */
27851+ #ifdef WOLF_CRYPTO_CB_SETKEY
27852+ else if (info->algo_type == WC_ALGO_TYPE_SETKEY) {
27853+ #ifdef DEBUG_WOLFSSL
27854+ fprintf(stderr, "test_CryptoCb_Func: SetKey Type=%d\n",
27855+ info->setkey.type);
27856+ #endif
27857+ switch (info->setkey.type) {
27858+ #ifndef NO_AES
27859+ case WC_SETKEY_AES:
27860+ {
27861+ Aes* aes = (Aes*)info->setkey.obj;
27862+ aes->devId = INVALID_DEVID;
27863+ ret = wc_AesSetKey(aes,
27864+ (const byte*)info->setkey.key, info->setkey.keySz,
27865+ (const byte*)info->setkey.aux, info->setkey.flags);
27866+ aes->devId = thisDevId;
27867+ break;
27868+ }
27869+ #endif /* !NO_AES */
27870+ #ifndef NO_HMAC
27871+ case WC_SETKEY_HMAC:
27872+ {
27873+ Hmac* hmac = (Hmac*)info->setkey.obj;
27874+ hmac->devId = INVALID_DEVID;
27875+ ret = wc_HmacSetKey(hmac, hmac->macType,
27876+ (const byte*)info->setkey.key, info->setkey.keySz);
27877+ hmac->devId = thisDevId;
27878+ break;
27879+ }
27880+ #endif /* !NO_HMAC */
27881+ #if !defined(NO_RSA) && defined(WOLFSSL_KEY_TO_DER)
27882+ case WC_SETKEY_RSA_PUB:
27883+ {
27884+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
27885+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
27886+ int derSz;
27887+ word32 idx = 0;
27888+ byte* der = NULL;
27889+
27890+ derSz = wc_RsaPublicKeyDerSize(rsaTmp, 1);
27891+ if (derSz <= 0) { ret = derSz; break; }
27892+
27893+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27894+ if (der == NULL) { ret = MEMORY_E; break; }
27895+
27896+ derSz = wc_RsaKeyToPublicDer_ex(rsaTmp, der,
27897+ (word32)derSz, 1);
27898+ if (derSz <= 0) {
27899+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27900+ ret = derSz; break;
27901+ }
27902+
27903+ rsaObj->devId = INVALID_DEVID;
27904+ ret = wc_RsaPublicKeyDecode(der, &idx, rsaObj,
27905+ (word32)derSz);
27906+ rsaObj->devId = thisDevId;
27907+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27908+ break;
27909+ }
27910+ case WC_SETKEY_RSA_PRIV:
27911+ {
27912+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
27913+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
27914+ int derSz;
27915+ word32 idx = 0;
27916+ byte* der = NULL;
27917+
27918+ derSz = wc_RsaKeyToDer(rsaTmp, NULL, 0);
27919+ if (derSz <= 0) { ret = derSz; break; }
27920+
27921+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27922+ if (der == NULL) { ret = MEMORY_E; break; }
27923+
27924+ derSz = wc_RsaKeyToDer(rsaTmp, der, (word32)derSz);
27925+ if (derSz <= 0) {
27926+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27927+ ret = derSz; break;
27928+ }
27929+
27930+ rsaObj->devId = INVALID_DEVID;
27931+ ret = wc_RsaPrivateKeyDecode(der, &idx, rsaObj,
27932+ (word32)derSz);
27933+ rsaObj->devId = thisDevId;
27934+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27935+ break;
27936+ }
27937+ #endif /* !NO_RSA && WOLFSSL_KEY_TO_DER */
27938+ #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
27939+ defined(HAVE_ECC_KEY_IMPORT)
27940+ case WC_SETKEY_ECC_PUB:
27941+ {
27942+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
27943+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
27944+ byte buf[ECC_BUFSIZE];
27945+ word32 bufSz = sizeof(buf);
27946+ int curveId;
27947+
27948+ ret = wc_ecc_export_x963(eccTmp, buf, &bufSz);
27949+ if (ret != 0) break;
27950+
27951+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
27952+ eccObj->devId = INVALID_DEVID;
27953+ ret = wc_ecc_import_x963_ex2(buf, bufSz, eccObj, curveId, 0);
27954+ eccObj->devId = thisDevId;
27955+ break;
27956+ }
27957+ case WC_SETKEY_ECC_PRIV:
27958+ {
27959+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
27960+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
27961+ byte pubBuf[ECC_BUFSIZE];
27962+ byte privBuf[MAX_ECC_BYTES];
27963+ word32 pubSz = sizeof(pubBuf);
27964+ word32 privSz = sizeof(privBuf);
27965+ int curveId;
27966+
27967+ ret = wc_ecc_export_x963(eccTmp, pubBuf, &pubSz);
27968+ if (ret != 0) break;
27969+ ret = wc_ecc_export_private_only(eccTmp, privBuf, &privSz);
27970+ if (ret != 0) break;
27971+
27972+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
27973+ eccObj->devId = INVALID_DEVID;
27974+ ret = wc_ecc_import_private_key_ex(privBuf, privSz,
27975+ pubBuf, pubSz, eccObj, curveId);
27976+ eccObj->devId = thisDevId;
27977+ break;
27978+ }
27979+ #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT && HAVE_ECC_KEY_IMPORT */
27980+ default:
27981+ ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
27982+ break;
27983+ }
27984+ }
27985+ #endif /* WOLF_CRYPTO_CB_SETKEY */
2785127986 (void)thisDevId;
2785227987 (void)keyFormat;
2785327988
0 commit comments