#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WOLFSSL_USER_SETTINGS
#include <user_settings.h>
#endif
#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_packet.h>
#include <wolftpm/tpm2_wrap.h>
#include <assert.h>
#include <string.h>
#ifdef WOLFTPM_LINUX_DEV
#include <wolftpm/tpm2_linux.h>
#define RS_SEND_COMMAND TPM2_LINUX_SendCommand
#elif defined(WOLFTPM_LINUX_DEV_AUTODETECT)
#include <wolftpm/tpm2_linux.h>
#define RS_SEND_COMMAND TPM2_LINUX_AUTODETECT_SendCommand
#elif defined(WOLFTPM_SWTPM)
#include <wolftpm/tpm2_swtpm.h>
#define RS_SEND_COMMAND TPM2_SWTPM_SendCommand
#else
#include <wolftpm/tpm2_tis.h>
#define RS_SEND_COMMAND TPM2_TIS_SendCommand
#endif
_Static_assert(sizeof(TPM2_CTX) > sizeof(void*),
"TPM2_CTX appears undersized — check wolfTPM version compatibility");
_Static_assert(sizeof(WOLFTPM2_DEV) >= sizeof(TPM2_CTX),
"WOLFTPM2_DEV appears undersized — check wolfTPM version compatibility");
int wolftpm_rs_transact(
WOLFTPM2_DEV* dev,
const byte* cmd,
int cmd_sz,
byte* rsp,
int rsp_buf_sz,
int* rsp_sz_out)
{
TPM2_Packet packet;
int rc;
int resp_len;
if (dev == NULL || cmd == NULL || rsp == NULL || rsp_sz_out == NULL)
return BAD_FUNC_ARG;
if (cmd_sz <= 0 || cmd_sz > (int)sizeof(dev->ctx.cmdBuf))
return BAD_FUNC_ARG;
if (rsp_buf_sz <= 0)
return BAD_FUNC_ARG;
XMEMCPY(dev->ctx.cmdBuf, cmd, cmd_sz);
packet.buf = dev->ctx.cmdBuf;
packet.pos = cmd_sz;
packet.size = (int)sizeof(dev->ctx.cmdBuf);
rc = RS_SEND_COMMAND(&dev->ctx, &packet);
if (rc != 0)
return rc;
resp_len = (int)(
((unsigned int)(unsigned char)dev->ctx.cmdBuf[2] << 24) |
((unsigned int)(unsigned char)dev->ctx.cmdBuf[3] << 16) |
((unsigned int)(unsigned char)dev->ctx.cmdBuf[4] << 8) |
((unsigned int)(unsigned char)dev->ctx.cmdBuf[5] )
);
if (resp_len < TPM2_HEADER_SIZE || resp_len > packet.size)
return TPM_RC_FAILURE;
if (resp_len > rsp_buf_sz)
return TPM_RC_SIZE;
XMEMCPY(rsp, dev->ctx.cmdBuf, (size_t)resp_len);
*rsp_sz_out = resp_len;
return TPM_RC_SUCCESS;
}