Skip to content

Commit a32bb35

Browse files
committed
Fix H-35, M-74, M-75 for wolfTPM
1 parent 366e4d6 commit a32bb35

3 files changed

Lines changed: 37 additions & 22 deletions

File tree

src/tpm2_asn.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#endif
2525

2626
#include <wolftpm/tpm2_asn.h>
27+
#include <wolftpm/tpm2_wrap.h>
2728

2829
#ifndef WOLFTPM2_NO_ASN
2930

@@ -343,8 +344,8 @@ int TPM2_ASN_DecodeRsaPubKey(uint8_t* input, int inputSz,
343344
}
344345
}
345346
if (rc == 0) {
346-
XMEMCPY(&pub->publicArea.parameters.rsaDetail.exponent, &input[idx],
347-
exp_len);
347+
pub->publicArea.parameters.rsaDetail.exponent =
348+
wolfTPM2_RsaKey_Exponent(&input[idx], exp_len);
348349
}
349350
return rc;
350351
}

src/tpm2_wrap.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
#include <wolftpm/tpm2_wrap.h>
2727
#include <wolftpm/tpm2_param_enc.h>
2828

29+
/* Convert big-endian byte array to native word32 */
30+
word32 wolfTPM2_RsaKey_Exponent(const byte* e, word32 eSz)
31+
{
32+
word32 exponent = 0, i;
33+
for (i = 0; i < eSz && i < sizeof(word32); i++) {
34+
exponent = (exponent << 8) | e[i];
35+
}
36+
return exponent;
37+
}
38+
2939
#ifndef WOLFTPM2_NO_WRAPPER
3040

3141
/* For some struct to buffer conversions */
@@ -3529,15 +3539,6 @@ int wolfTPM2_RsaKey_TpmToPemPub(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
35293539
#endif /* !NO_ASN */
35303540

35313541
#ifndef NO_RSA
3532-
static word32 wolfTPM2_RsaKey_Exponent(byte* e, word32 eSz)
3533-
{
3534-
word32 exponent = 0, i;
3535-
for (i=0; i<eSz && i<sizeof(word32); i++) {
3536-
exponent |= ((word32)e[i]) << (i*8);
3537-
}
3538-
return exponent;
3539-
}
3540-
35413542
int wolfTPM2_RsaKey_TpmToWolf(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
35423543
RsaKey* wolfKey)
35433544
{
@@ -4144,22 +4145,22 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
41444145
/* Assemble R and S into signature (R then S) */
41454146
sigOutSz = curveSize * 2;
41464147
if (sigOutSz > *sigSz ||
4147-
curveSize > ecdsa->signatureR.size ||
4148-
curveSize > ecdsa->signatureS.size) {
4148+
ecdsa->signatureR.size > curveSize ||
4149+
ecdsa->signatureS.size > curveSize) {
41494150
#ifdef DEBUG_WOLFTPM
41504151
printf("TPM2_Sign: ECC result buffer too small %d -> %d\n",
41514152
sigOutSz, *sigSz);
41524153
#endif
41534154
return BUFFER_E;
41544155
}
4155-
XMEMCPY(sig, ecdsa->signatureR.buffer,
4156-
ecdsa->signatureR.size);
4157-
XMEMSET(sig + ecdsa->signatureR.size, 0,
4158-
curveSize - ecdsa->signatureR.size);
4159-
XMEMCPY(sig + curveSize, ecdsa->signatureS.buffer,
4160-
ecdsa->signatureS.size);
4161-
XMEMSET(sig + curveSize + ecdsa->signatureS.size, 0,
4162-
curveSize - ecdsa->signatureS.size);
4156+
/* Left-pad R */
4157+
XMEMSET(sig, 0, curveSize - ecdsa->signatureR.size);
4158+
XMEMCPY(sig + curveSize - ecdsa->signatureR.size,
4159+
ecdsa->signatureR.buffer, ecdsa->signatureR.size);
4160+
/* Left-pad S */
4161+
XMEMSET(sig + curveSize, 0, curveSize - ecdsa->signatureS.size);
4162+
XMEMCPY(sig + curveSize + (curveSize - ecdsa->signatureS.size),
4163+
ecdsa->signatureS.buffer, ecdsa->signatureS.size);
41634164
}
41644165
else if (key->pub.publicArea.type == TPM_ALG_RSA) {
41654166
/* RSA signature size and buffer (with padding depending on scheme) */
@@ -5857,7 +5858,7 @@ int wolfTPM2_EncryptDecryptBlock(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
58575858

58585859
/* update IV */
58595860
if (iv) {
5860-
if (ivSz < encDecOut.ivOut.size)
5861+
if (ivSz > encDecOut.ivOut.size)
58615862
ivSz = encDecOut.ivOut.size;
58625863
XMEMCPY(iv, encDecOut.ivOut.buffer, ivSz);
58635864
}

wolftpm/tpm2_wrap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,18 @@ WOLFTPM_API int wolfTPM2_SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE
12561256
TPMI_ALG_HASH nameAlg, TPM2B_NAME* name, const WOLFTPM2_KEY* parentKey,
12571257
TPMT_SYM_DEF_OBJECT* sym, TPM2B_DATA* symSeed);
12581258

1259+
/*!
1260+
\ingroup wolfTPM2_Wrappers
1261+
\brief Converts a big-endian byte array to a native word32 value.
1262+
Used for RSA exponent conversion from ASN.1/DER format.
1263+
1264+
\param e pointer to big-endian byte array
1265+
\param eSz size of the byte array (max 4 bytes)
1266+
1267+
\return word32 value in native byte order
1268+
*/
1269+
WOLFTPM_API word32 wolfTPM2_RsaKey_Exponent(const byte* e, word32 eSz);
1270+
12591271
#ifndef WOLFTPM2_NO_WOLFCRYPT
12601272
/*!
12611273
\ingroup wolfTPM2_Wrappers
@@ -1321,6 +1333,7 @@ WOLFTPM_API int wolfTPM2_ExportPublicKeyBuffer(WOLFTPM2_DEV* dev, WOLFTPM2_KEY*
13211333
int encodingType, byte* out, word32* outSz);
13221334

13231335
#ifndef NO_RSA
1336+
13241337
/*!
13251338
\ingroup wolfTPM2_Wrappers
13261339
\brief Helper function to import Der rsa key directly

0 commit comments

Comments
 (0)