Skip to content

Commit 0047cce

Browse files
committed
Fixed issues with ZGen_2Phase to populate x and y. Fix for fwtpm without NV.
1 parent ad53eb9 commit 0047cce

8 files changed

Lines changed: 108 additions & 32 deletions

File tree

hal/tpm_io_fwtpm.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static int gFwtpmClientInit = 0;
5555
int FWTPM_TIS_ClientConnect(FWTPM_TIS_CLIENT_CTX* client)
5656
{
5757
int fd;
58+
int openFlags;
59+
int fdFlags;
5860
struct stat st;
5961
FWTPM_TIS_REGS* shm;
6062
sem_t* semCmd;
@@ -70,16 +72,14 @@ int FWTPM_TIS_ClientConnect(FWTPM_TIS_CLIENT_CTX* client)
7072
/* Open existing shared memory file. O_NOFOLLOW and O_CLOEXEC are not
7173
* universally available across POSIX targets — guard at compile time
7274
* and fall back to fcntl(FD_CLOEXEC) for the close-on-exec semantics. */
73-
{
74-
int openFlags = O_RDWR;
75-
#ifdef O_NOFOLLOW
76-
openFlags |= O_NOFOLLOW;
77-
#endif
78-
#ifdef O_CLOEXEC
79-
openFlags |= O_CLOEXEC;
80-
#endif
81-
fd = open(FWTPM_TIS_SHM_PATH, openFlags);
82-
}
75+
openFlags = O_RDWR;
76+
#ifdef O_NOFOLLOW
77+
openFlags |= O_NOFOLLOW;
78+
#endif
79+
#ifdef O_CLOEXEC
80+
openFlags |= O_CLOEXEC;
81+
#endif
82+
fd = open(FWTPM_TIS_SHM_PATH, openFlags);
8383
if (fd < 0) {
8484
#ifdef DEBUG_WOLFTPM
8585
printf("fwTPM HAL: open(%s) failed: %d (%s)\n",
@@ -88,8 +88,12 @@ int FWTPM_TIS_ClientConnect(FWTPM_TIS_CLIENT_CTX* client)
8888
return TPM_RC_FAILURE;
8989
}
9090
#ifndef O_CLOEXEC
91-
/* Fallback for platforms without O_CLOEXEC */
92-
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
91+
fdFlags = fcntl(fd, F_GETFD);
92+
if (fdFlags >= 0) {
93+
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
94+
}
95+
#else
96+
(void)fdFlags;
9397
#endif
9498

9599
/* Verify file is large enough before mapping */

scripts/fwtpm_emu_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ set -e
102102
# Show UART output (filter emulator noise)
103103
echo ""
104104
echo "--- fwTPM output ---"
105-
grep -E "^===|^fwTPM|^ |^Running|^ Startup|^ SelfTest|^ GetRandom|^ GetCapability|^ Random|^All self|^SELF-TEST" "$LOG"
105+
grep -E "^===|^fwTPM|^ |^Running|^ Startup|^ SelfTest|^ GetRandom|^ GetCapability|^ Random|^All self|^SELF-TEST" "$LOG" || true
106106
echo "--- end ---"
107107
echo ""
108108

src/fwtpm/fwtpm_command.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11908,8 +11908,9 @@ static TPM_RC FwCmd_ZGen_2Phase(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1190811908
FWTPM_DECLARE_VAR(privKeyA, ecc_key);
1190911909
FWTPM_DECLARE_VAR(privEph, ecc_key);
1191011910
FWTPM_DECLARE_VAR(peerPub, ecc_key);
11911-
byte z1Buf[66], z2Buf[66]; /* shared secret x-coordinates */
11912-
word32 z1Sz, z2Sz;
11911+
byte z1xBuf[66], z1yBuf[66]; /* shared point coordinates (outZ1) */
11912+
byte z2xBuf[66], z2yBuf[66]; /* shared point coordinates (outZ2) */
11913+
word32 z1xSz, z1ySz, z2xSz, z2ySz;
1191311914
int wcCurve;
1191411915
int markPos;
1191511916
int paramSzPos, paramStart;
@@ -12029,8 +12030,8 @@ static TPM_RC FwCmd_ZGen_2Phase(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1202912030
if (rc != 0) rc = TPM_RC_ECC_POINT;
1203012031
}
1203112032
if (rc == 0) {
12032-
z1Sz = (word32)sizeof(z1Buf);
12033-
rc = wc_ecc_shared_secret(privKeyA, peerPub, z1Buf, &z1Sz);
12033+
rc = FwEccSharedPoint(privKeyA, peerPub,
12034+
z1xBuf, &z1xSz, z1yBuf, &z1ySz);
1203412035
if (rc != 0) rc = TPM_RC_FAILURE;
1203512036
}
1203612037

@@ -12065,35 +12066,41 @@ static TPM_RC FwCmd_ZGen_2Phase(FWTPM_CTX* ctx, TPM2_Packet* cmd,
1206512066
if (rc != 0) rc = TPM_RC_ECC_POINT;
1206612067
}
1206712068
if (rc == 0) {
12068-
z2Sz = (word32)sizeof(z2Buf);
12069-
rc = wc_ecc_shared_secret(privEph, peerPub, z2Buf, &z2Sz);
12069+
rc = FwEccSharedPoint(privEph, peerPub,
12070+
z2xBuf, &z2xSz, z2yBuf, &z2ySz);
1207012071
if (rc != 0) rc = TPM_RC_FAILURE;
1207112072
}
1207212073

12073-
/* Build response: outZ1 + outZ2 as TPM2B_ECC_POINT (x-only) */
12074+
/* Build response: outZ1 + outZ2 as TPM2B_ECC_POINT with full (x,y).
12075+
* TPM 2.0 Part 3 §14.7: Z value is the x-coordinate; y is populated
12076+
* for spec-strictness and TPM_ALG_ECMQV compatibility. */
1207412077
if (rc == 0) {
1207512078
paramStart = FwRspParamsBegin(rsp, cmdTag, &paramSzPos);
1207612079

1207712080
/* outZ1 */
1207812081
TPM2_Packet_MarkU16(rsp, &markPos);
12079-
TPM2_Packet_AppendU16(rsp, (UINT16)z1Sz);
12080-
TPM2_Packet_AppendBytes(rsp, z1Buf, z1Sz);
12081-
TPM2_Packet_AppendU16(rsp, 0); /* y = empty */
12082+
TPM2_Packet_AppendU16(rsp, (UINT16)z1xSz);
12083+
TPM2_Packet_AppendBytes(rsp, z1xBuf, z1xSz);
12084+
TPM2_Packet_AppendU16(rsp, (UINT16)z1ySz);
12085+
TPM2_Packet_AppendBytes(rsp, z1yBuf, z1ySz);
1208212086
TPM2_Packet_PlaceU16(rsp, markPos);
1208312087

1208412088
/* outZ2 */
1208512089
TPM2_Packet_MarkU16(rsp, &markPos);
12086-
TPM2_Packet_AppendU16(rsp, (UINT16)z2Sz);
12087-
TPM2_Packet_AppendBytes(rsp, z2Buf, z2Sz);
12088-
TPM2_Packet_AppendU16(rsp, 0); /* y = empty */
12090+
TPM2_Packet_AppendU16(rsp, (UINT16)z2xSz);
12091+
TPM2_Packet_AppendBytes(rsp, z2xBuf, z2xSz);
12092+
TPM2_Packet_AppendU16(rsp, (UINT16)z2ySz);
12093+
TPM2_Packet_AppendBytes(rsp, z2yBuf, z2ySz);
1208912094
TPM2_Packet_PlaceU16(rsp, markPos);
1209012095

1209112096
FwRspParamsEnd(rsp, cmdTag, paramSzPos, paramStart);
1209212097
}
1209312098

1209412099
/* Cleanup */
12095-
TPM2_ForceZero(z1Buf, sizeof(z1Buf));
12096-
TPM2_ForceZero(z2Buf, sizeof(z2Buf));
12100+
TPM2_ForceZero(z1xBuf, sizeof(z1xBuf));
12101+
TPM2_ForceZero(z1yBuf, sizeof(z1yBuf));
12102+
TPM2_ForceZero(z2xBuf, sizeof(z2xBuf));
12103+
TPM2_ForceZero(z2yBuf, sizeof(z2yBuf));
1209712104
/* Zero ephemeral key — it was consumed and must not be reused */
1209812105
TPM2_ForceZero(ctx->ecEphemeralKey, sizeof(ctx->ecEphemeralKey));
1209912106
ctx->ecEphemeralKeySz = 0;

src/fwtpm/fwtpm_crypto.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,67 @@ int FwImportEccKey(const FWTPM_Object* obj, ecc_key* key)
21952195
}
21962196
return FwImportEccPubFromPublic(&obj->pub, key);
21972197
}
2198+
2199+
/* Compute ECDH shared point R = priv.k * peer.pub, returning both x and y
2200+
* coordinates. wc_ecc_shared_secret only returns x; ZGen_2Phase marshals a
2201+
* full TPM2B_ECC_POINT and TPM_ALG_ECMQV requires y as well.
2202+
* xBuf/yBuf must each hold at least curve byte length. */
2203+
int FwEccSharedPoint(ecc_key* priv, ecc_key* peer,
2204+
byte* xBuf, word32* xSz, byte* yBuf, word32* ySz)
2205+
{
2206+
int rc;
2207+
int curveIdx;
2208+
ecc_point* R = NULL;
2209+
mp_int prime, a;
2210+
const ecc_set_type* dp;
2211+
int primeInit = 0, aInit = 0;
2212+
2213+
if (priv == NULL || peer == NULL || xBuf == NULL || xSz == NULL ||
2214+
yBuf == NULL || ySz == NULL) {
2215+
return BAD_FUNC_ARG;
2216+
}
2217+
2218+
curveIdx = wc_ecc_get_curve_idx(priv->dp->id);
2219+
if (curveIdx < 0)
2220+
return ECC_BAD_ARG_E;
2221+
dp = wc_ecc_get_curve_params(curveIdx);
2222+
if (dp == NULL)
2223+
return ECC_BAD_ARG_E;
2224+
2225+
R = wc_ecc_new_point();
2226+
if (R == NULL)
2227+
return MEMORY_E;
2228+
2229+
rc = mp_init(&prime);
2230+
if (rc == 0) {
2231+
primeInit = 1;
2232+
rc = mp_init(&a);
2233+
}
2234+
if (rc == 0) {
2235+
aInit = 1;
2236+
rc = mp_read_radix(&prime, dp->prime, MP_RADIX_HEX);
2237+
}
2238+
if (rc == 0)
2239+
rc = mp_read_radix(&a, dp->Af, MP_RADIX_HEX);
2240+
if (rc == 0)
2241+
rc = wc_ecc_mulmod(ecc_get_k(priv), &peer->pubkey, R, &a, &prime, 1);
2242+
2243+
if (rc == 0) {
2244+
*xSz = (word32)mp_unsigned_bin_size(R->x);
2245+
rc = mp_to_unsigned_bin(R->x, xBuf);
2246+
}
2247+
if (rc == 0) {
2248+
*ySz = (word32)mp_unsigned_bin_size(R->y);
2249+
rc = mp_to_unsigned_bin(R->y, yBuf);
2250+
}
2251+
2252+
if (aInit)
2253+
mp_clear(&a);
2254+
if (primeInit)
2255+
mp_clear(&prime);
2256+
wc_ecc_del_point(R);
2257+
return rc;
2258+
}
21982259
#endif /* HAVE_ECC */
21992260

22002261
#ifndef NO_RSA

src/fwtpm/fwtpm_tis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static void TisHandleRegAccess(FWTPM_CTX* ctx, FWTPM_TIS_REGS* regs)
331331
if (len > 4) {
332332
len = 4;
333333
}
334-
XMEMSET(regs->reg_data, 0, len);
334+
XMEMSET(regs->reg_data, 0, sizeof(regs->reg_data));
335335
/* Pack value into reg_data (little-endian, matching TIS spec) */
336336
if (len >= 1) regs->reg_data[0] = (BYTE)(val);
337337
if (len >= 2) regs->reg_data[1] = (BYTE)(val >> 8);

src/tpm2_wrap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,10 +1705,10 @@ int wolfTPM2_SetAuth(WOLFTPM2_DEV* dev, int index,
17051705
XMEMCPY(session->auth.buffer, auth->buffer, session->auth.size);
17061706
}
17071707
if (name) {
1708-
if (name->size > sizeof(session->name.name)) {
1709-
return BUFFER_E;
1710-
}
17111708
session->name.size = name->size;
1709+
if (session->name.size > sizeof(session->name.name)) {
1710+
session->name.size = sizeof(session->name.name);
1711+
}
17121712
XMEMCPY(session->name.name, name->name, session->name.size);
17131713
}
17141714

tests/fwtpm_unit_tests.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ static void test_fwtpm_clock_sethal(void)
19931993
printf("Test fwTPM:\tClock HAL:\t\t\tPassed\n");
19941994
}
19951995

1996+
#ifndef FWTPM_NO_NV
19961997
/* Mock NV HAL backend: 64 KB byte buffer with per-call counters. */
19971998
#define MOCK_NV_SIZE (64 * 1024)
19981999
static byte gMockNvStore[MOCK_NV_SIZE];
@@ -2030,6 +2031,7 @@ static int mock_nv_erase(void* c, word32 off, word32 sz)
20302031
gMockNvErases++;
20312032
return TPM_RC_SUCCESS;
20322033
}
2034+
#endif /* !FWTPM_NO_NV */
20332035

20342036
/* Register a mock NV HAL before FWTPM_Init and verify that NV writes
20352037
* go to the mock backend rather than the default file. */

wolftpm/fwtpm/fwtpm_crypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ int FwGetRsaHashOid(UINT16 hashAlg);
222222
int FwImportEccKeyFromDer(const FWTPM_Object* obj, ecc_key* key);
223223
int FwImportEccPubFromPublic(const TPMT_PUBLIC* pub, ecc_key* key);
224224
int FwImportEccKey(const FWTPM_Object* obj, ecc_key* key);
225+
int FwEccSharedPoint(ecc_key* priv, ecc_key* peer,
226+
byte* xBuf, word32* xSz, byte* yBuf, word32* ySz);
225227
#endif /* HAVE_ECC */
226228

227229
/* --- Sign/Verify --- */

0 commit comments

Comments
 (0)