#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) || \
defined(WOLFSSL_RENESAS_TSIP_TLS) || \
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
#include <wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h>
#define cmn_hw_lock wc_fspsm_hw_lock
#define cmn_hw_unlock wc_fspsm_hw_unlock
#elif defined(WOLFSSL_RENESAS_TSIP_TLS) || \
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
#include <wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h>
#define cmn_hw_lock tsip_hw_lock
#define cmn_hw_unlock tsip_hw_unlock
#define FSPSM_ST TsipUserCtx
#define FSPSM_ST_Internal TsipUserCtx_Internal
#endif
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/asn.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/internal.h>
#include <wolfssl/error-ssl.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
#define INITIAL_DEVID 7890
uint32_t g_CAscm_Idx = (uint32_t)-1;
static int gdevId = INITIAL_DEVID;
#ifdef WOLF_CRYPTO_CB
#define MAX_FSPSM_CBINDEX 5
FSPSM_ST *gCbCtx[MAX_FSPSM_CBINDEX];
#include <wolfssl/wolfcrypt/cryptocb.h>
WOLFSSL_LOCAL int Renesas_cmn_Cleanup(struct WOLFSSL* ssl)
{
int ret = 0;
WOLFSSL_ENTER("Renesas_cmn_Cleanup");
(void) ssl;
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = tsip_TlsCleanup(ssl);
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_TlsCleanup(ssl);
#endif
WOLFSSL_LEAVE("Renesas_cmn_Cleanup", ret);
return ret;
}
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
WOLFSSL_LOCAL int Renesas_cmn_RsaSignCb(WOLFSSL* ssl,
const unsigned char* in, unsigned int inSz,
unsigned char* out, word32* outSz,
const unsigned char* keyDer, unsigned int keySz,
void* ctx)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
WOLFSSL_ENTER("Renesas_cmn_RsaSignCb");
WOLFSSL_LEAVE("Renesas_cmn_RsaSignCb", ret);
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_RsaSignCheckCb(WOLFSSL* ssl,
unsigned char* sig, unsigned int sigSz,
unsigned char** out,
const unsigned char* keyDer, unsigned int keySz,
void* ctx)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
WOLFSSL_ENTER("Renesas_cmn_RsaSignCheckCb");
#if defined(WOLFSSL_RENESAS_TSIP)
ret = tsip_VerifyRsaPkcsCb(ssl, sig, sigSz, out, keyDer, keySz, ctx);
#endif
WOLFSSL_LEAVE("Renesas_cmn_RsaSignCheckCb", ret);
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_EccSignCb(WOLFSSL* ssl,
const unsigned char* in, unsigned int inSz,
unsigned char* out, word32* outSz,
const unsigned char* keyDer, unsigned int keySz,
void* ctx)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
WOLFSSL_ENTER("Renesas_cmn_EccSignCb");
WOLFSSL_LEAVE("Renesas_cmn_EccSignCb", ret);
return ret;
}
#endif
static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
WOLFSSL_ENTER("Renesas_cmn_CryptoDevCb");
#if defined(WOLFSSL_RENESAS_TSIP_TLS) || \
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
(void)cbInfo;
#endif
if (info == NULL || ctx == NULL)
return BAD_FUNC_ARG;
#if defined(DEBUG_CRYPTOCB)
wc_CryptoCb_InfoString(info);
#endif
#if defined(WOLFSSL_RENESAS_TSIP) || \
defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
ret = CRYPTOCB_UNAVAILABLE;
if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
#if !defined(NO_AES)
ret = wc_tsip_AesCipher(devIdArg, info, ctx);
#endif
}
if (info->algo_type == WC_ALGO_TYPE_PK) {
#if !defined(NO_RSA)
#if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
ret = wc_tsip_MakeRsaKey(info->pk.rsakg.size, (void*)ctx);
if (ret == 0) {
TsipUserCtx* tsipCtx = (TsipUserCtx*)ctx;
RsaKey* key = info->pk.rsakg.key;
#if (defined(TSIP_RSAES_1024) && TSIP_RSAES_1024 == 1) || \
(defined(TSIP_RSASSA_1024) && TSIP_RSASSA_1024 == 1)
if (info->pk.rsakg.size == 1024) {
ret = wc_RsaPublicKeyDecodeRaw(
tsipCtx->rsa1024pub_keyIdx->value.key_n,
R_TSIP_RSA_1024_KEY_N_LENGTH_BYTE_SIZE,
tsipCtx->rsa1024pub_keyIdx->value.key_e,
R_TSIP_RSA_1024_KEY_E_LENGTH_BYTE_SIZE,
key
);
}
#endif
#if (defined(TSIP_RSAES_2048) && TSIP_RSAES_2048 == 1) || \
(defined(TSIP_RSASSA_2048) && TSIP_RSASSA_2048 == 1)
if (info->pk.rsakg.size == 2048) {
ret = wc_RsaPublicKeyDecodeRaw(
tsipCtx->rsa2048pub_keyIdx->value.key_n,
R_TSIP_RSA_2048_KEY_N_LENGTH_BYTE_SIZE,
tsipCtx->rsa2048pub_keyIdx->value.key_e,
R_TSIP_RSA_2048_KEY_E_LENGTH_BYTE_SIZE,
key
);
}
#endif
}
}
#endif
if (info->pk.type == WC_PK_TYPE_RSA_PKCS) {
RsaPadding* pad = info->pk.rsa.padding;
if (pad && pad->pad_value == RSA_BLOCK_TYPE_1) {
if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT ||
info->pk.rsa.type == RSA_PRIVATE_DECRYPT) {
ret = tsip_SignRsaPkcs(info, cbInfo);
}
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
else {
ret = wc_tsip_RsaVerifyPkcs(info, cbInfo);
}
#endif
}
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
else if (pad && pad->pad_value == RSA_BLOCK_TYPE_2) {
ret = wc_tsip_RsaFunction(info, cbInfo);
}
#endif
}
if (info->pk.type == WC_PK_TYPE_RSA_GET_SIZE) {
if (cbInfo->wrappedKeyType == TSIP_KEY_TYPE_RSA2048) {
*info->pk.rsa_get_size.keySize = 256;
ret = 0;
}
#ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
else if (cbInfo->wrappedKeyType == TSIP_KEY_TYPE_RSA1024) {
*info->pk.rsa_get_size.keySize = 128;
ret = 0;
}
#endif
}
#endif
#if defined(HAVE_ECC)
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
ret = tsip_SignEcdsa(info, cbInfo);
}
#endif
#if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
ret = tsip_VerifyEcdsa(info, cbInfo);
}
#endif
#endif
}
#elif (defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY))\
&& !defined(NO_WOLFSSL_RENESAS_FSPSM_AES)
if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
#if !defined(NO_AES)
ret = wc_fspsm_AesCipher(devIdArg, info, ctx);
#endif
}
#if !defined(NO_RSA) && defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
else if (info->algo_type == WC_ALGO_TYPE_PK) {
if (info->pk.type == WC_PK_TYPE_RSA_GET_SIZE) {
if (cbInfo->keyflgs_crypt.bits.rsapri2048_installedkey_set ||
cbInfo->keyflgs_crypt.bits.rsapub2048_installedkey_set )
{
*info->pk.rsa_get_size.keySize = 256;
ret = 0;
} else if (
cbInfo->keyflgs_crypt.bits.rsapri1024_installedkey_set ||
cbInfo->keyflgs_crypt.bits.rsapub1024_installedkey_set )
{
*info->pk.rsa_get_size.keySize = 128;
ret = 0;
}
}
#if defined(WOLFSSL_KEY_GEN)
if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
ret = wc_fspsm_MakeRsaKey(info->pk.rsakg.key,
info->pk.rsakg.size, (void*)ctx);
}
#endif
if (info->pk.type == WC_PK_TYPE_RSA_PKCS) {
if (cbInfo->keyflgs_crypt.bits.rsapri2048_installedkey_set ||
cbInfo->keyflgs_crypt.bits.rsapub2048_installedkey_set ||
cbInfo->keyflgs_crypt.bits.rsapri1024_installedkey_set ||
cbInfo->keyflgs_crypt.bits.rsapub1024_installedkey_set ) {
if (info->pk.rsa.type == RSA_PRIVATE_DECRYPT ||
info->pk.rsa.type == RSA_PUBLIC_ENCRYPT )
{
ret = wc_fspsm_RsaFunction(info->pk.rsa.in,
info->pk.rsa.inLen,
info->pk.rsa.out,
info->pk.rsa.outLen,
info->pk.rsa.type,
info->pk.rsa.key,
info->pk.rsa.rng);
}
else if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT ){
ret = wc_fspsm_RsaSign(info->pk.rsa.in,
info->pk.rsa.inLen,
info->pk.rsa.out,
info->pk.rsa.outLen,
info->pk.rsa.key,
(void*)ctx);
}
else if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT ) {
ret = wc_fspsm_RsaVerify(info->pk.rsa.in,
info->pk.rsa.inLen,
info->pk.rsa.out,
info->pk.rsa.outLen,
info->pk.rsa.key,
(void*)ctx);
}
}
else {
WOLFSSL_MSG(
"SCE can handle 1024 or 2048 bit key size. "
"key size is not either 1024 or 2048. "
"Or wrapped key is not installed. "
"RSA operation falls through to SW operation.");
}
}
}
#endif
#endif
(void)devIdArg;
(void)ctx;
WOLFSSL_LEAVE("Renesas_cmn_CryptoDevCb", ret);
return ret;
}
int Renesas_cmn_usable(const struct WOLFSSL* ssl, byte session_key_generated)
{
int ret = 0;
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = tsip_usable(ssl, session_key_generated);
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\
defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
ret = wc_fspsm_usable(ssl, session_key_generated);
#endif
(void)ssl;
(void)session_key_generated;
return ret;
}
WOLFSSL_LOCAL void *Renesas_cmn_GetCbCtxBydevId(int devId)
{
if (devId >= INITIAL_DEVID && devId <= (MAX_FSPSM_CBINDEX + INITIAL_DEVID))
return gCbCtx[devId - INITIAL_DEVID];
else
return NULL;
}
int wc_CryptoCb_CryptInitRenesasCmn(struct WOLFSSL* ssl, void* ctx)
{
(void)ssl;
(void)ctx;
FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
size_t internal_sz = sizeof(FSPSM_ST_Internal);
if (cbInfo == NULL
#if (!defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
!defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
!defined(HAVE_RENESAS_SYNC)
|| ssl == NULL
#endif
) {
WOLFSSL_MSG("Invalid devId\n");
return INVALID_DEVID;
}
if (cbInfo->internal == NULL) {
if (ssl)
cbInfo->internal =
(FSPSM_ST_Internal*)XMALLOC(internal_sz, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
else
cbInfo->internal = (FSPSM_ST_Internal*)XMALLOC(internal_sz, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cbInfo->internal == NULL) {
return MEMORY_E;
}
ForceZero(cbInfo->internal, internal_sz);
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\
defined(WOLFSSL_RENESAS_TSIP_TLS)
if (ssl)
cbInfo->internal->heap = ssl->heap;
#endif
}
if ((cmn_hw_lock()) == 0) {
if (gdevId < 0) {
gdevId = INITIAL_DEVID;
}
cbInfo->devId = gdevId++;
cmn_hw_unlock();
}
else {
WOLFSSL_MSG("Failed to lock tsip hw");
return INVALID_DEVID;
}
if (wc_CryptoCb_RegisterDevice(cbInfo->devId,
Renesas_cmn_CryptoDevCb, cbInfo) < 0) {
gdevId--;
return INVALID_DEVID;
}
#if !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
!defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) && \
!defined(HAVE_RENESAS_SYNC)
if (ssl)
wolfSSL_SetDevId(ssl, cbInfo->devId);
#endif
gCbCtx[cbInfo->devId - INITIAL_DEVID] = (void*)cbInfo;
return cbInfo->devId;
}
void wc_CryptoCb_CleanupRenesasCmn(int* id)
{
FSPSM_ST* cbInfo = NULL;
if (*id < INITIAL_DEVID ||
(*id - INITIAL_DEVID) > MAX_FSPSM_CBINDEX)
return;
cbInfo = (FSPSM_ST*)gCbCtx[*id - INITIAL_DEVID];
if (cbInfo != NULL && cbInfo->internal != NULL) {
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) && \
!defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
XFREE(cbInfo->internal, cbInfo->internal->heap,
DYNAMIC_TYPE_TMP_BUFFER);
#else
XFREE(cbInfo->internal, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
cbInfo->internal = NULL;
}
wc_CryptoCb_UnRegisterDevice(*id);
}
#endif
#endif
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) || defined(WOLFSSL_RENESAS_TSIP_TLS)
WOLFSSL_LOCAL byte Renesas_cmn_checkCA(word32 cmIdx)
{
WOLFSSL_ENTER("Renesas_cmn_checkCA");
return (cmIdx == g_CAscm_Idx? 1:0);
}
static byte fspsm_tsip_rootCAverified(void)
{
WOLFSSL_ENTER("fspsm_tsip_rootCAverified");
return (g_CAscm_Idx != (uint32_t)-1 ? 1:0);
}
WOLFSSL_LOCAL int Renesas_cmn_RsaVerify(WOLFSSL* ssl, unsigned char* sig,
unsigned int sigSz, unsigned char** out,
const unsigned char* key, unsigned int keySz, void* ctx)
{
int ret = 0;
WOLFSSL_ENTER("Renesas_cmn_RsaVerify");
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = wc_tsip_RsaVerify(ssl, sig, sigSz, out, key, keySz, ctx);
if (ret == 0) {
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
}
else {
WOLFSSL_MSG("failed wc_tsip_RsaVerify");
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
}
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_RsaVerifyTLS(ssl, sig, sigSz, out,key, keySz, ctx);
if (ret == 0) {
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
}
else {
WOLFSSL_MSG("failed R_XXX_TLS_ServerKeyExchangeVerify");
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
}
#endif
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_EccVerify(WOLFSSL* ssl, const unsigned char* sig,
unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
const unsigned char* key, unsigned int keySz, int* result, void* ctx)
{
int ret = 0;
WOLFSSL_ENTER("Renesas_cmn_EccVerify");
#if defined(WOLFSSL_RENESAS_TSIP)
ret = wc_tsip_EccVerify(ssl, sig, sigSz, hash, hashSz, key, keySz,
result, ctx);
if (ret == 0 && *result == 1) {
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
}
else {
WOLFSSL_MSG("failed wc_tsip_EccVerify");
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
}
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_EccVerifyTLS(ssl, sig, sigSz, hash, hashSz, key, keySz,
result, ctx);
if (ret == 0 && *result == 1) {
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
}
else {
WOLFSSL_MSG("failed R_XXXX_TLS_ServerKeyExchangeVerify");
wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
}
#endif
return ret;
}
int wc_Renesas_cmn_RootCertVerify(const byte* cert, word32 cert_len,
word32 key_n_start, word32 key_n_len, word32 key_e_start,
word32 key_e_len, word32 cm_row)
{
int ret;
WOLFSSL_ENTER("wc_Renesas_cmn_RootCertVerify");
if (fspsm_tsip_rootCAverified() == 0) {
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = wc_tsip_tls_RootCertVerify(cert, cert_len, key_n_start,
key_n_len, key_e_start, key_e_len, cm_row);
if (ret != TSIP_SUCCESS) {
ret = WOLFSSL_FATAL_ERROR;
}
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_tls_RootCertVerify(cert, cert_len, key_n_start,
key_n_len, key_e_start, key_e_len, cm_row);
if (ret != FSP_SUCCESS) {
ret = WOLFSSL_FATAL_ERROR;
}
#endif
}
else {
ret = 0;
}
WOLFSSL_LEAVE("wc_Renesas_cmn_RootCertVerify", ret);
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_TlsFinished(WOLFSSL* ssl, const byte *side,
const byte *handshake_hash, word32 hashSz,
byte *hashes, void* ctx)
{
int ret = -1;
(void)hashSz;
(void)ctx;
WOLFSSL_ENTER("Renesas_cmn_TlsFinished");
if (Renesas_cmn_usable(ssl, 1)) {
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = wc_tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
side, handshake_hash, hashes);
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_generateVerifyData(ssl->arrays->fspsm_masterSecret,
side, handshake_hash, hashes);
#endif
}
else
ret = PROTOCOLCB_UNAVAILABLE;
return ret;
}
static int Renesas_cmn_EncryptKeys(WOLFSSL* ssl, void* ctx)
{
int ret;
WOLFSSL_ENTER("Renesas_cmn_EncryptKeys");
if (ssl == NULL || ctx == NULL)
return BAD_FUNC_ARG;
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
if (cbInfo->internal->session_key_set == 1) {
switch(cbInfo->internal->key_side) {
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
if (cbInfo != NULL && cbInfo->internal != NULL &&
cbInfo->internal->keyflgs_tls.bits.session_key_set == 1) {
switch(cbInfo->internal->side) {
#endif
case 1:
ssl->encrypt.setup = 1;
break;
case 2:
ssl->decrypt.setup = 1;
break;
case 3:
ssl->decrypt.setup = 1;
ssl->encrypt.setup = 1;
break;
default:break;
}
ret = 0;
wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, Renesas_cmn_TlsFinished);
wolfSSL_SetTlsFinishedCtx(ssl, cbInfo);
}
else {
wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, NULL);
wolfSSL_SetTlsFinishedCtx(ssl, NULL);
ret = -1;
}
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_generateSessionKey(WOLFSSL* ssl, void* ctx)
{
int ret = -1;
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
#endif
(void)ctx;
WOLFSSL_ENTER("Renesas_cmn_generateSessionKey");
if (Renesas_cmn_usable(ssl, 0)) {
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
ret = wc_tsip_generateSessionKey(ssl, cbInfo,
cbInfo->devId);
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
ret = wc_fspsm_generateSessionKey(ssl, ctx, cbInfo->devId);
#endif
}
else {
ret = PROTOCOLCB_UNAVAILABLE;
}
if (ret == 0) {
wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, Renesas_cmn_EncryptKeys);
wolfSSL_SetEncryptKeysCtx(ssl, ctx);
}
else {
wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, NULL);
wolfSSL_SetEncryptKeysCtx(ssl, NULL);
}
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_generatePremasterSecret(WOLFSSL* ssl,
byte *premaster, word32 preSz, void* ctx)
{
int ret;
(void) ctx;
(void) ssl;
WOLFSSL_ENTER("Renesas_cmn_generatePremasterSecret");
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (Renesas_cmn_usable(ssl, 0)) {
ret = wc_tsip_generatePremasterSecret(premaster, preSz);
ssl->arrays->preMasterSz = preSz;
}
else
ret = PROTOCOLCB_UNAVAILABLE;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (Renesas_cmn_usable(ssl, 0)) {
ret = wc_fspsm_generatePremasterSecret(premaster, preSz);
ssl->arrays->preMasterSz = preSz;
}
else
ret = PROTOCOLCB_UNAVAILABLE;
#endif
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_genMasterSecret(struct WOLFSSL* ssl, void* ctx)
{
int ret = WC_NO_ERR_TRACE(WOLFSSL_NOT_IMPLEMENTED);
(void) ret;
(void) ctx;
WOLFSSL_ENTER("Renesas_cmn_genMasterSecret");
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (Renesas_cmn_usable(ssl, 0)) {
#if (WOLFSSL_RENESAS_TSIP_VER >= 109)
ret = wc_tsip_generateMasterSecretEx(
ssl->options.cipherSuite0,
ssl->options.cipherSuite,
ssl->arrays->preMasterSecret,
ssl->arrays->clientRandom,
ssl->arrays->serverRandom,
ssl->arrays->tsip_masterSecret);
#else
ret = wc_tsip_generateMasterSecret(
ssl->arrays->preMasterSecret,
ssl->arrays->clientRandom,
ssl->arrays->serverRandom,
ssl->arrays->tsip_masterSecret);
#endif
if (ret == 0) {
wc_tsip_storeKeyCtx(ssl, (TsipUserCtx*)ctx);
wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
Renesas_cmn_generateSessionKey);
wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
}
else {
wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
}
}
else
ret = PROTOCOLCB_UNAVAILABLE;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (Renesas_cmn_usable(ssl, 0)) {
ret = wc_fspsm_generateMasterSecret(
ssl->options.cipherSuite0,
ssl->options.cipherSuite,
ssl->arrays->preMasterSecret,
ssl->arrays->clientRandom,
ssl->arrays->serverRandom,
ssl->arrays->fspsm_masterSecret);
if (ret == 0) {
wc_fspsm_storeKeyCtx(ssl, ctx);
wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
Renesas_cmn_generateSessionKey);
wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
}
else {
wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
}
}
else {
WOLFSSL_MSG("PROTOCOLCB_UNAVAILABLE\n");
ret = PROTOCOLCB_UNAVAILABLE;
}
#endif
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_RsaEnc(WOLFSSL* ssl, const unsigned char* in,
unsigned int inSz, unsigned char* out, word32* outSz,
const unsigned char* keyDer, unsigned int keySz, void* ctx)
{
int ret;
int EncSz;
(void)ctx;
(void)in;
(void)inSz;
(void)keyDer;
(void)keySz;
(void)EncSz;
WOLFSSL_ENTER("Renesas_cmn_RsaEnc");
if (ssl == NULL || in == NULL || out == NULL || keyDer == NULL ||
ctx == NULL) {
return BAD_FUNC_ARG;
}
EncSz = wc_RsaEncryptSize(ssl->peerRsaKey);
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (tsip_usable(ssl, 0)) {
if (EncSz == 256) {
ret = wc_tsip_generateEncryptPreMasterSecret(ssl, out, outSz);
}
else {
WOLFSSL_MSG("TSIP can only handle 256 bytes for RSA encrypt size.");
ret = CRYPTOCB_UNAVAILABLE;
}
}
else {
ret = CRYPTOCB_UNAVAILABLE;
}
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (wc_fspsm_usable(ssl, 0) && EncSz == 256) {
ret = wc_fspsm_generateEncryptPreMasterSecret(ssl, out,
(uint32_t*)outSz);
}
else {
if (EncSz != 256)
WOLFSSL_MSG("SCE cannot use"
"because Rsa Encrypt Size isn't 256 bytes(2048 bits).");
ret = CRYPTOCB_UNAVAILABLE;
}
#endif
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_VerifyHmac(WOLFSSL *ssl, const byte* message,
word32 messageSz, word32 macSz, word32 content, void* ctx)
{
int ret;
(void)ctx;
WOLFSSL_ENTER("Renesas_cmn_VerifyHmac");
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (tsip_usable(ssl, 1)) {
ret = wc_tsip_ShaXHmacVerify(ssl, message, messageSz, macSz, content);
}
else
ret = PROTOCOLCB_UNAVAILABLE;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (wc_fspsm_usable(ssl, 1)) {
ret = wc_fspsm_Sha256VerifyHmac(ssl, message,
messageSz, macSz, content);
}
else
ret = PROTOCOLCB_UNAVAILABLE;
#endif
return ret;
}
#ifndef WOLFSSL_AEAD_ONLY
WOLFSSL_LOCAL int Renesas_cmn_TLS_hmac(WOLFSSL* ssl, byte* digest,
const byte* in, word32 sz, int padSz, int content, int verify, int epochOrder)
{
int ret;
byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
WOLFSSL_ENTER("Renesas_cmn_TLS_hmac");
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (Renesas_cmn_usable(ssl, 1)) {
wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE) {
ret = wc_tsip_Sha1HmacGenerate(ssl, myInner,
WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
}
else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
ret = wc_tsip_Sha256HmacGenerate(ssl, myInner,
WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
}
else {
ret = TSIP_MAC_DIGSZ_E;
}
}
else {
WOLFSSL_MSG("TLS_hmac is used instead of TSIP");
ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
}
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (Renesas_cmn_usable(ssl, 1)) {
if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
ret = wc_fspsm_Sha256GenerateHmac(ssl, myInner,
WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
}
else
ret = TSIP_MAC_DIGSZ_E;
}
else {
ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
}
#endif
return ret;
}
#endif
WOLFSSL_LOCAL int Renesas_cmn_SigPkCbRsaVerify(unsigned char* sig,
unsigned int sigSz, unsigned char** out, const unsigned char* keyDer,
unsigned int keySz, void* ctx)
{
int ret;
CertAttribute* CertAtt;
(void)out;
(void)keyDer;
(void)keySz;
WOLFSSL_ENTER("Renesas_cmn_SigPkCbRsaVerify");
if (sig == NULL || out == NULL || keyDer == NULL || ctx == NULL)
return BAD_FUNC_ARG;
CertAtt = (CertAttribute*)ctx;
if (!CertAtt) {
return CRYPTOCB_UNAVAILABLE;
}
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (CertAtt->keyIndex != NULL) {
ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
CertAtt->pubkey_n_start - CertAtt->certBegin,
CertAtt->pubkey_n_len - 1,
CertAtt->pubkey_e_start - CertAtt->certBegin,
CertAtt->pubkey_e_len -1,
(uint8_t*)CertAtt->keyIndex);
if (ret == 0) {
CertAtt->verifyByTSIP_SCE = 1;
}
else {
WOLFSSL_MSG("RSA Verify by TSIP didn't match");
ret = ASN_SIG_CONFIRM_E;
}
}
else
ret = CRYPTOCB_UNAVAILABLE;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (CertAtt->keyIndex != NULL) {
ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
sig, sigSz,
CertAtt->pubkey_n_start - CertAtt->certBegin,
CertAtt->pubkey_n_len - 1,
CertAtt->pubkey_e_start - CertAtt->certBegin,
CertAtt->pubkey_e_len -1,
(uint8_t*)CertAtt->keyIndex);
if (ret == 0) {
CertAtt->verifyByTSIP_SCE = 1;
}
else {
WOLFSSL_MSG("RSA Verify by SCE didn't match");
ret = ASN_SIG_CONFIRM_E;
}
}
else
ret = CRYPTOCB_UNAVAILABLE;
#endif
return ret;
}
WOLFSSL_LOCAL int Renesas_cmn_SigPkCbEccVerify(const unsigned char* sig,
unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
const unsigned char* keyDer, unsigned int keySz,
int* result, void* ctx)
{
int ret;
CertAttribute* CertAtt;
(void)result;
(void)keyDer;
(void)keySz;
(void)hash;
(void)hashSz;
WOLFSSL_ENTER("Renesas_cmn_SigPkCbEccVerify");
if (sig == NULL || keyDer == NULL || hash == NULL || ctx == NULL ||
result == NULL)
return BAD_FUNC_ARG;
CertAtt = (CertAttribute*)ctx;
if (!CertAtt) {
return CRYPTOCB_UNAVAILABLE;
}
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
if (CertAtt->keyIndex != NULL) {
ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
CertAtt->pubkey_n_start - CertAtt->certBegin,
CertAtt->pubkey_n_len - 1,
CertAtt->pubkey_e_start - CertAtt->certBegin,
CertAtt->pubkey_e_len -1,
(uint8_t*)CertAtt->keyIndex);
if (ret == 0) {
CertAtt->verifyByTSIP_SCE = 1;
*result = 1;
}
else {
WOLFSSL_MSG("RSA Verify by TSIP didn't match");
ret = ASN_SIG_CONFIRM_E;
}
}
else
ret = CRYPTOCB_UNAVAILABLE;
#elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
if (CertAtt->keyIndex != NULL) {
ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
sig, sigSz,
CertAtt->pubkey_n_start - CertAtt->certBegin,
CertAtt->pubkey_n_len - 1,
CertAtt->pubkey_e_start - CertAtt->certBegin,
CertAtt->pubkey_e_len -1,
(uint8_t*)CertAtt->keyIndex);
if (ret == 0) {
CertAtt->verifyByTSIP_SCE = 1;
*result = 1;
}
else {
WOLFSSL_MSG("RSA Verify by SCE didn't match");
ret = ASN_SIG_CONFIRM_E;
}
}
else
ret = CRYPTOCB_UNAVAILABLE;
#endif
return ret;
}
#endif