Skip to content

Commit de85c26

Browse files
committed
Peer review fixes
1 parent 81e5fe7 commit de85c26

5 files changed

Lines changed: 61 additions & 35 deletions

File tree

examples/keygen/keygen.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,27 +297,10 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
297297
XSTRLEN("-paramkey=")) == 0) {
298298
/* Choose a PQC key for the param-enc session (separate from the
299299
* child key -mlkem/-mldsa selection above). Value is e.g.
300-
* "mlkem", "mlkem=768", "mldsa" or "mldsa=65". */
300+
* "mlkem", "mlkem=768", "mldsa" or "mldsa=65". Reuses the shared
301+
* value-form parser so validation stays consistent. */
301302
const char* pkVal = argv[argc-1] + XSTRLEN("-paramkey=");
302-
#ifdef WOLFTPM_MLKEM
303-
if (XSTRNCMP(pkVal, "mlkem", 5) == 0) {
304-
int n = (pkVal[5] == '=') ? XATOI(&pkVal[6]) : 768;
305-
pqcParamEncAlg = TPM_ALG_MLKEM;
306-
pqcParamSet = (n == 512) ? TPM_MLKEM_512 :
307-
(n == 1024) ? TPM_MLKEM_1024 : TPM_MLKEM_768;
308-
}
309-
else
310-
#endif
311-
#ifdef WOLFTPM_MLDSA
312-
if (XSTRNCMP(pkVal, "mldsa", 5) == 0) {
313-
int n = (pkVal[5] == '=') ? XATOI(&pkVal[6]) : 65;
314-
pqcParamEncAlg = TPM_ALG_MLDSA;
315-
pqcParamSet = (n == 44) ? TPM_MLDSA_44 :
316-
(n == 87) ? TPM_MLDSA_87 : TPM_MLDSA_65;
317-
}
318-
else
319-
#endif
320-
{
303+
if (!parsePqcParamSet(pkVal, &pqcParamEncAlg, &pqcParamSet)) {
321304
printf("Invalid -paramkey value: %s\n", pkVal);
322305
usage();
323306
return 0;

examples/tpm_test_keys.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,32 +503,58 @@ int getPrimaryParamEncKey(WOLFTPM2_DEV* pDev, WOLFTPM2_SESSION* session,
503503
return rc;
504504
}
505505

506-
int parsePqcParamEncArg(const char* arg, TPM_ALG_ID* alg, int* paramSet)
506+
int parsePqcParamSet(const char* val, TPM_ALG_ID* alg, int* paramSet)
507507
{
508-
if (arg == NULL || alg == NULL || paramSet == NULL)
508+
int n;
509+
510+
if (val == NULL || alg == NULL || paramSet == NULL)
509511
return 0;
510512
#ifdef WOLFTPM_MLKEM
511-
if (XSTRCMP(arg, "-mlkem") == 0 ||
512-
XSTRNCMP(arg, "-mlkem=", 7) == 0) {
513-
int n = (arg[6] == '=') ? XATOI(&arg[7]) : 768;
513+
if (XSTRCMP(val, "mlkem") == 0 ||
514+
XSTRNCMP(val, "mlkem=", 6) == 0) {
515+
n = (val[5] == '=') ? XATOI(&val[6]) : 768;
514516
*alg = TPM_ALG_MLKEM;
515-
*paramSet = (n == 512) ? TPM_MLKEM_512 :
516-
(n == 1024) ? TPM_MLKEM_1024 : TPM_MLKEM_768;
517+
if (n == 512)
518+
*paramSet = TPM_MLKEM_512;
519+
else if (n == 768)
520+
*paramSet = TPM_MLKEM_768;
521+
else if (n == 1024)
522+
*paramSet = TPM_MLKEM_1024;
523+
else {
524+
printf("Invalid ML-KEM parameter set: %d (use 512, 768, or 1024)\n",
525+
n);
526+
return 0;
527+
}
517528
return 1;
518529
}
519530
#endif
520531
#ifdef WOLFTPM_MLDSA
521-
if (XSTRCMP(arg, "-mldsa") == 0 ||
522-
XSTRNCMP(arg, "-mldsa=", 7) == 0) {
523-
int n = (arg[6] == '=') ? XATOI(&arg[7]) : 65;
532+
if (XSTRCMP(val, "mldsa") == 0 ||
533+
XSTRNCMP(val, "mldsa=", 6) == 0) {
534+
n = (val[5] == '=') ? XATOI(&val[6]) : 65;
524535
*alg = TPM_ALG_MLDSA;
525-
*paramSet = (n == 44) ? TPM_MLDSA_44 :
526-
(n == 87) ? TPM_MLDSA_87 : TPM_MLDSA_65;
536+
if (n == 44)
537+
*paramSet = TPM_MLDSA_44;
538+
else if (n == 65)
539+
*paramSet = TPM_MLDSA_65;
540+
else if (n == 87)
541+
*paramSet = TPM_MLDSA_87;
542+
else {
543+
printf("Invalid ML-DSA parameter set: %d (use 44, 65, or 87)\n", n);
544+
return 0;
545+
}
527546
return 1;
528547
}
529548
#endif
530549
return 0;
531550
}
551+
552+
int parsePqcParamEncArg(const char* arg, TPM_ALG_ID* alg, int* paramSet)
553+
{
554+
if (arg == NULL || arg[0] != '-')
555+
return 0;
556+
return parsePqcParamSet(arg + 1, alg, paramSet);
557+
}
532558
#endif /* WOLFTPM_MLDSA || WOLFTPM_MLKEM */
533559

534560
int getRSAkey(WOLFTPM2_DEV* pDev, WOLFTPM2_KEY* pStorageKey, WOLFTPM2_KEY* key,

examples/tpm_test_keys.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,18 @@ WOLFTPM_LOCAL int getPrimaryParamEncKey(WOLFTPM2_DEV* pDev,
6666
int paramSet,
6767
int paramEncAlg);
6868

69+
/* Parse a PQC parameter-set value (no leading dash): "mlkem[=512|768|1024]"
70+
* or "mldsa[=44|65|87]". On match sets *alg (TPM_ALG_MLKEM / TPM_ALG_MLDSA)
71+
* and *paramSet and returns 1. Returns 0 if the value is not recognized, or
72+
* if a parameter set is given but is not a supported value (a message is
73+
* printed in that case). */
74+
WOLFTPM_LOCAL int parsePqcParamSet(const char* val, TPM_ALG_ID* alg,
75+
int* paramSet);
76+
6977
/* Parse a PQC parameter-encryption key option: "-mlkem[=512|768|1024]"
7078
* (salt) or "-mldsa[=44|65|87]" (bind). On match sets *alg (TPM_ALG_MLKEM /
71-
* TPM_ALG_MLDSA) and *paramSet and returns 1; returns 0 if not recognized. */
79+
* TPM_ALG_MLDSA) and *paramSet and returns 1; returns 0 if not recognized or
80+
* if an unsupported parameter set is given. Wraps parsePqcParamSet(). */
7281
WOLFTPM_LOCAL int parsePqcParamEncArg(const char* arg, TPM_ALG_ID* alg,
7382
int* paramSet);
7483
#endif

src/tpm2_param_enc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ static TPM2B_AUTH* TPM2_ParamEncBindKey(TPM2_AUTH_SESSION* session)
208208
* the session key). In every other case - a secondary encrypt-only
209209
* session (name.size 0), or a session authorizing a different entity
210210
* (whose authValue, if any, is already in session->auth, including the
211-
* empty-auth case) - the bind authValue must not be added. */
211+
* empty-auth case) - the bind authValue must not be added.
212+
* The size test uses <= (not ==) deliberately: in the realistic case the
213+
* lone session key is exactly one digest, but <= is kept as a defensive
214+
* lower bound so a bound-own-entity session is still admitted even if the
215+
* session key is shorter than a full digest. A session authorizing a
216+
* different entity always has auth.size strictly greater than the digest
217+
* (sessionKey || entityAuth) and so remains excluded either way. */
212218
if (session->bind != NULL && digestSz > 0 &&
213219
session->name.size > 0 &&
214220
session->name.size == session->bindName.size &&

wolftpm/tpm2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,9 @@ typedef struct TPM2_AUTH_SESSION {
19921992
* authorization session is authorizing its own bind entity, so the bind
19931993
* authValue is folded into the parameter-encryption key only in that case
19941994
* (TPM 2.0 Part 1: the param-enc key is the session key concatenated with
1995-
* the authValue of the entity the session authorizes). */
1995+
* the authValue of the entity the session authorizes).
1996+
* Placed at the end of the struct so the offsets of existing members are
1997+
* unchanged; the size growth is covered by the 4.0.0 major version. */
19961998
TPM2B_NAME bindName;
19971999
} TPM2_AUTH_SESSION;
19982000

0 commit comments

Comments
 (0)