Skip to content

Commit 5a093ac

Browse files
authored
Merge pull request #494 from aidangarske/fix-nations-ns350-failures
Fix NS350 RSA-4096 Failures
2 parents 72bd518 + ebb98f1 commit 5a093ac

6 files changed

Lines changed: 36 additions & 11 deletions

File tree

examples/csr/csr.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ static const char* gClientCertEccFile = ECC_CERT_PEM;
6060
#endif
6161

6262
#ifndef MAX_PEM_SIZE
63-
#define MAX_PEM_SIZE MAX_CONTEXT_SIZE
63+
/* Must hold the full PEM-encoded CSR/cert (cert body + RSA signature +
64+
* ASN.1 + base64 overhead). MAX_CONTEXT_SIZE (2 KB) fits RSA-2048 but
65+
* overflows at RSA-4096 where the signature alone is 512 B. */
66+
#if MAX_RSA_KEY_BITS >= 4096
67+
#define MAX_PEM_SIZE 4096
68+
#else
69+
#define MAX_PEM_SIZE MAX_CONTEXT_SIZE
70+
#endif
6471
#endif
6572

6673
/******************************************************************************/

examples/keygen/keygen.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
132132
WOLFTPM2_KEYBLOB primaryBlob; /* Primary key as WOLFTPM2_KEYBLOB */
133133
TPMT_PUBLIC publicTemplate;
134134
TPMI_ALG_PUBLIC alg = TPM_ALG_RSA; /* default, see usage() for options */
135-
TPMI_ALG_PUBLIC srkAlg = TPM_ALG_ECC; /* prefer ECC, but allow RSA */
135+
TPMI_ALG_PUBLIC srkAlg = TPM_ALG_RSA; /* default matches seal.c / keyload.c */
136136
TPM_ALG_ID algSym = TPM_ALG_CTR; /* default Symmetric Cipher, see usage */
137137
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
138138
WOLFTPM2_SESSION tpmSession;
@@ -222,8 +222,10 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
222222
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
223223
XMEMSET(&auth, 0, sizeof(auth));
224224

225-
if (alg == TPM_ALG_RSA)
226-
srkAlg = TPM_ALG_RSA;
225+
/* Only use the ECC SRK for ECC child keys; RSA, SYMCIPHER, KEYEDHASH
226+
* all stay on the RSA SRK so that keyload/seal can round-trip them. */
227+
if (alg == TPM_ALG_ECC)
228+
srkAlg = TPM_ALG_ECC;
227229
if (alg == TPM_ALG_SYMCIPHER) {
228230
rc = symChoice(symMode, &algSym, &keyBits);
229231
if (rc != TPM_RC_SUCCESS) {

examples/keygen/keyload.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
6767
WOLFTPM2_KEYBLOB newKey;
6868
WOLFTPM2_KEY persistKey;
6969
TPM_ALG_ID alg;
70-
TPMI_ALG_PUBLIC srkAlg = TPM_ALG_ECC; /* prefer ECC, but allow RSA */
70+
TPMI_ALG_PUBLIC srkAlg = TPM_ALG_RSA; /* default matches seal.c */
7171
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
7272
WOLFTPM2_SESSION tpmSession;
7373
const char* inputFile = "keyblob.bin";
@@ -133,8 +133,11 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
133133
#endif
134134

135135
alg = newKey.pub.publicArea.type;
136-
if (alg == TPM_ALG_RSA)
137-
srkAlg = TPM_ALG_RSA;
136+
/* Only switch to the ECC SRK when the stored key itself is ECC; other
137+
* child types (RSA, KEYEDHASH from seal, SYMCIPHER) stay on the RSA SRK
138+
* so the parent algorithm matches how those keys were created. */
139+
if (alg == TPM_ALG_ECC)
140+
srkAlg = TPM_ALG_ECC;
138141
printf("Loading %s key\n", TPM2_GetAlgName(alg));
139142

140143
if (endorseKey) {

examples/nvram/read.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,12 @@ int TPM2_NVRAM_Read_Example(void* userCtx, int argc, char *argv[])
267267
nvIndex);
268268

269269
if (!nvExtend && !partialRead) {
270-
/* get SRK */
271-
rc = getPrimaryStoragekey(&dev, &storage, TPM_ALG_RSA);
270+
/* Select the SRK algorithm based on the stored key's type so an
271+
* ECC child isn't loaded under an RSA parent (or vice versa). */
272+
TPMI_ALG_PUBLIC srkAlg =
273+
(keyBlob.pub.publicArea.type == TPM_ALG_ECC)
274+
? TPM_ALG_ECC : TPM_ALG_RSA;
275+
rc = getPrimaryStoragekey(&dev, &storage, srkAlg);
272276
if (rc != 0) goto exit;
273277

274278
printf("Trying to load the key extracted from NVRAM\n");

examples/pkcs7/pkcs7.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@
5757
#endif
5858

5959
#ifndef MAX_PKCS7_SIZE
60-
#define MAX_PKCS7_SIZE MAX_CONTEXT_SIZE
60+
/* Must hold the full SignedData blob (cert + signature + ASN.1 overhead).
61+
* MAX_CONTEXT_SIZE (2 KB) is enough for RSA-2048 but overflows at
62+
* RSA-4096 where the signature alone is 512 B. */
63+
#if MAX_RSA_KEY_BITS >= 4096
64+
#define MAX_PKCS7_SIZE 4096
65+
#else
66+
#define MAX_PKCS7_SIZE MAX_CONTEXT_SIZE
67+
#endif
6168
#endif
6269

6370
/******************************************************************************/

examples/wrap/wrap_test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
338338
if (rc != 0) goto exit;
339339

340340
/* Perform RSA encrypt / decrypt (no pad) */
341-
message.size = 256; /* test message 0x11,0x11,etc */
341+
/* With TPM_ALG_NULL padding, the TPM returns a full modulus-sized
342+
* plaintext on decrypt, so the message must also be modulus-sized. */
343+
message.size = rsaKey.pub.publicArea.parameters.rsaDetail.keyBits / 8;
342344
XMEMSET(message.buffer, 0x11, message.size);
343345
cipher.size = sizeof(cipher.buffer); /* encrypted data */
344346
rc = wolfTPM2_RsaEncrypt(&dev, &rsaKey, TPM_ALG_NULL,

0 commit comments

Comments
 (0)