@@ -27811,6 +27811,141 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2781127811 }
2781227812 }
2781327813#endif /* WOLF_CRYPTO_CB_FREE */
27814+ #ifdef WOLF_CRYPTO_CB_SETKEY
27815+ else if (info->algo_type == WC_ALGO_TYPE_SETKEY) {
27816+ #ifdef DEBUG_WOLFSSL
27817+ fprintf(stderr, "test_CryptoCb_Func: SetKey Type=%d\n",
27818+ info->setkey.type);
27819+ #endif
27820+ switch (info->setkey.type) {
27821+ #ifndef NO_AES
27822+ case WC_SETKEY_AES:
27823+ {
27824+ Aes* aes = (Aes*)info->setkey.obj;
27825+ aes->devId = INVALID_DEVID;
27826+ ret = wc_AesSetKey(aes,
27827+ (const byte*)info->setkey.key, info->setkey.keySz,
27828+ (const byte*)info->setkey.aux, info->setkey.flags);
27829+ aes->devId = thisDevId;
27830+ break;
27831+ }
27832+ #endif /* !NO_AES */
27833+ #ifndef NO_HMAC
27834+ case WC_SETKEY_HMAC:
27835+ {
27836+ Hmac* hmac = (Hmac*)info->setkey.obj;
27837+ hmac->devId = INVALID_DEVID;
27838+ ret = wc_HmacSetKey(hmac, hmac->macType,
27839+ (const byte*)info->setkey.key, info->setkey.keySz);
27840+ hmac->devId = thisDevId;
27841+ break;
27842+ }
27843+ #endif /* !NO_HMAC */
27844+ #if !defined(NO_RSA) && defined(WOLFSSL_KEY_TO_DER)
27845+ case WC_SETKEY_RSA_PUB:
27846+ {
27847+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
27848+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
27849+ int derSz;
27850+ word32 idx = 0;
27851+ byte* der = NULL;
27852+
27853+ derSz = wc_RsaPublicKeyDerSize(rsaTmp, 1);
27854+ if (derSz <= 0) { ret = derSz; break; }
27855+
27856+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27857+ if (der == NULL) { ret = MEMORY_E; break; }
27858+
27859+ derSz = wc_RsaKeyToPublicDer_ex(rsaTmp, der,
27860+ (word32)derSz, 1);
27861+ if (derSz <= 0) {
27862+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27863+ ret = derSz; break;
27864+ }
27865+
27866+ rsaObj->devId = INVALID_DEVID;
27867+ ret = wc_RsaPublicKeyDecode(der, &idx, rsaObj,
27868+ (word32)derSz);
27869+ rsaObj->devId = thisDevId;
27870+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27871+ break;
27872+ }
27873+ case WC_SETKEY_RSA_PRIV:
27874+ {
27875+ RsaKey* rsaObj = (RsaKey*)info->setkey.obj;
27876+ RsaKey* rsaTmp = (RsaKey*)info->setkey.key;
27877+ int derSz;
27878+ word32 idx = 0;
27879+ byte* der = NULL;
27880+
27881+ derSz = wc_RsaKeyToDer(rsaTmp, NULL, 0);
27882+ if (derSz <= 0) { ret = derSz; break; }
27883+
27884+ der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27885+ if (der == NULL) { ret = MEMORY_E; break; }
27886+
27887+ derSz = wc_RsaKeyToDer(rsaTmp, der, (word32)derSz);
27888+ if (derSz <= 0) {
27889+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27890+ ret = derSz; break;
27891+ }
27892+
27893+ rsaObj->devId = INVALID_DEVID;
27894+ ret = wc_RsaPrivateKeyDecode(der, &idx, rsaObj,
27895+ (word32)derSz);
27896+ rsaObj->devId = thisDevId;
27897+ XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
27898+ break;
27899+ }
27900+ #endif /* !NO_RSA && WOLFSSL_KEY_TO_DER */
27901+ #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
27902+ defined(HAVE_ECC_KEY_IMPORT)
27903+ case WC_SETKEY_ECC_PUB:
27904+ {
27905+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
27906+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
27907+ byte buf[ECC_BUFSIZE];
27908+ word32 bufSz = sizeof(buf);
27909+ int curveId;
27910+
27911+ ret = wc_ecc_export_x963(eccTmp, buf, &bufSz);
27912+ if (ret != 0) break;
27913+
27914+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
27915+ eccObj->devId = INVALID_DEVID;
27916+ ret = wc_ecc_import_x963_ex2(buf, bufSz, eccObj, curveId, 0);
27917+ eccObj->devId = thisDevId;
27918+ break;
27919+ }
27920+ case WC_SETKEY_ECC_PRIV:
27921+ {
27922+ ecc_key* eccObj = (ecc_key*)info->setkey.obj;
27923+ ecc_key* eccTmp = (ecc_key*)info->setkey.key;
27924+ byte pubBuf[ECC_BUFSIZE];
27925+ byte privBuf[MAX_ECC_BYTES];
27926+ word32 pubSz = sizeof(pubBuf);
27927+ word32 privSz = sizeof(privBuf);
27928+ int curveId;
27929+
27930+ ret = wc_ecc_export_x963(eccTmp, pubBuf, &pubSz);
27931+ if (ret != 0) break;
27932+ ret = wc_ecc_export_private_only(eccTmp, privBuf, &privSz);
27933+ if (ret != 0) break;
27934+
27935+ curveId = wc_ecc_get_curve_id(eccTmp->idx);
27936+ eccObj->devId = INVALID_DEVID;
27937+ ret = wc_ecc_import_private_key_ex(privBuf, privSz,
27938+ pubBuf, pubSz, eccObj, curveId);
27939+ eccObj->devId = thisDevId;
27940+ break;
27941+ }
27942+ #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT && HAVE_ECC_KEY_IMPORT */
27943+ default:
27944+ ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
27945+ break;
27946+ }
27947+ }
27948+ #endif /* WOLF_CRYPTO_CB_SETKEY */
2781427949 (void)thisDevId;
2781527950 (void)keyFormat;
2781627951
0 commit comments