Skip to content

Commit 8f68f76

Browse files
committed
peer review fixes
1 parent 22a1fc6 commit 8f68f76

13 files changed

Lines changed: 916 additions & 29 deletions

File tree

examples/keygen/create_primary.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,20 @@ static void usage(void)
9090
#ifdef WOLFTPM_MLDSA
9191
static int mldsaParamSet(const char* optVal, TPMI_MLDSA_PARAMETER_SET* ps)
9292
{
93-
int n = XATOI(optVal);
93+
int n;
94+
const char* p;
95+
96+
if (optVal[0] == '\0') { /* missing or empty suffix, use default */
97+
*ps = TPM_MLDSA_65;
98+
return TPM_RC_SUCCESS;
99+
}
100+
/* reject non-digit input (e.g. -mldsa=abc) before XATOI */
101+
for (p = optVal; *p != '\0'; p++) {
102+
if (*p < '0' || *p > '9')
103+
return TPM_RC_FAILURE;
104+
}
105+
n = XATOI(optVal);
94106
switch (n) {
95-
case 0: /* missing or empty suffix, use default */
96107
case 65: *ps = TPM_MLDSA_65; return TPM_RC_SUCCESS;
97108
case 44: *ps = TPM_MLDSA_44; return TPM_RC_SUCCESS;
98109
case 87: *ps = TPM_MLDSA_87; return TPM_RC_SUCCESS;
@@ -147,7 +158,7 @@ int TPM2_CreatePrimaryKey_Example(void* userCtx, int argc, char *argv[])
147158
argv[argc-1] + 7 : "";
148159
if (mldsaParamSet(optVal, &mldsaPs) != TPM_RC_SUCCESS) {
149160
usage();
150-
return 0;
161+
return -1;
151162
}
152163
alg = TPM_ALG_MLDSA;
153164
}

examples/keygen/ecdh

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#! /bin/bash
2+
3+
# examples/keygen/ecdh - temporary wrapper script for .libs/ecdh
4+
# Generated by libtool (GNU libtool) 2.4.7 Debian-2.4.7-7build1
5+
#
6+
# The examples/keygen/ecdh program cannot be directly executed until all the libtool
7+
# libraries that it depends on are installed.
8+
#
9+
# This wrapper script should never be moved out of the build directory.
10+
# If it is, it will not operate correctly.
11+
12+
# Sed substitution that helps us do robust quoting. It backslashifies
13+
# metacharacters that are still active within double-quoted strings.
14+
sed_quote_subst='s|\([`"$\\]\)|\\\1|g'
15+
16+
# Be Bourne compatible
17+
if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
18+
emulate sh
19+
NULLCMD=:
20+
# Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
21+
# is contrary to our usage. Disable this feature.
22+
alias -g '${1+"$@"}'='"$@"'
23+
setopt NO_GLOB_SUBST
24+
else
25+
case `(set -o) 2>/dev/null` in *posix*) set -o posix;; esac
26+
fi
27+
BIN_SH=xpg4; export BIN_SH # for Tru64
28+
DUALCASE=1; export DUALCASE # for MKS sh
29+
30+
# The HP-UX ksh and POSIX shell print the target directory to stdout
31+
# if CDPATH is set.
32+
(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
33+
34+
relink_command=""
35+
36+
# This environment variable determines our operation mode.
37+
if test "$libtool_install_magic" = "%%%MAGIC variable%%%"; then
38+
# install mode needs the following variables:
39+
generated_by_libtool_version='2.4.7'
40+
notinst_deplibs=' src/libwolftpm.la'
41+
else
42+
# When we are sourced in execute mode, $file and $ECHO are already set.
43+
if test "$libtool_execute_magic" != "%%%MAGIC variable%%%"; then
44+
file="$0"
45+
46+
# A function that is used when there is no print builtin or printf.
47+
func_fallback_echo ()
48+
{
49+
eval 'cat <<_LTECHO_EOF
50+
$1
51+
_LTECHO_EOF'
52+
}
53+
ECHO="printf %s\\n"
54+
fi
55+
56+
# Very basic option parsing. These options are (a) specific to
57+
# the libtool wrapper, (b) are identical between the wrapper
58+
# /script/ and the wrapper /executable/ that is used only on
59+
# windows platforms, and (c) all begin with the string --lt-
60+
# (application programs are unlikely to have options that match
61+
# this pattern).
62+
#
63+
# There are only two supported options: --lt-debug and
64+
# --lt-dump-script. There is, deliberately, no --lt-help.
65+
#
66+
# The first argument to this parsing function should be the
67+
# script's ./libtool value, followed by no.
68+
lt_option_debug=
69+
func_parse_lt_options ()
70+
{
71+
lt_script_arg0=$0
72+
shift
73+
for lt_opt
74+
do
75+
case "$lt_opt" in
76+
--lt-debug) lt_option_debug=1 ;;
77+
--lt-dump-script)
78+
lt_dump_D=`$ECHO "X$lt_script_arg0" | /usr/bin/sed -e 's/^X//' -e 's%/[^/]*$%%'`
79+
test "X$lt_dump_D" = "X$lt_script_arg0" && lt_dump_D=.
80+
lt_dump_F=`$ECHO "X$lt_script_arg0" | /usr/bin/sed -e 's/^X//' -e 's%^.*/%%'`
81+
cat "$lt_dump_D/$lt_dump_F"
82+
exit 0
83+
;;
84+
--lt-*)
85+
$ECHO "Unrecognized --lt- option: '$lt_opt'" 1>&2
86+
exit 1
87+
;;
88+
esac
89+
done
90+
91+
# Print the debug banner immediately:
92+
if test -n "$lt_option_debug"; then
93+
echo "ecdh:examples/keygen/ecdh:$LINENO: libtool wrapper (GNU libtool) 2.4.7 Debian-2.4.7-7build1" 1>&2
94+
fi
95+
}
96+
97+
# Used when --lt-debug. Prints its arguments to stdout
98+
# (redirection is the responsibility of the caller)
99+
func_lt_dump_args ()
100+
{
101+
lt_dump_args_N=1;
102+
for lt_arg
103+
do
104+
$ECHO "ecdh:examples/keygen/ecdh:$LINENO: newargv[$lt_dump_args_N]: $lt_arg"
105+
lt_dump_args_N=`expr $lt_dump_args_N + 1`
106+
done
107+
}
108+
109+
# Core function for launching the target application
110+
func_exec_program_core ()
111+
{
112+
113+
if test -n "$lt_option_debug"; then
114+
$ECHO "ecdh:examples/keygen/ecdh:$LINENO: newargv[0]: $progdir/$program" 1>&2
115+
func_lt_dump_args ${1+"$@"} 1>&2
116+
fi
117+
exec "$progdir/$program" ${1+"$@"}
118+
119+
$ECHO "$0: cannot exec $program $*" 1>&2
120+
exit 1
121+
}
122+
123+
# A function to encapsulate launching the target application
124+
# Strips options in the --lt-* namespace from $@ and
125+
# launches target application with the remaining arguments.
126+
func_exec_program ()
127+
{
128+
case " $* " in
129+
*\ --lt-*)
130+
for lt_wr_arg
131+
do
132+
case $lt_wr_arg in
133+
--lt-*) ;;
134+
*) set x "$@" "$lt_wr_arg"; shift;;
135+
esac
136+
shift
137+
done ;;
138+
esac
139+
func_exec_program_core ${1+"$@"}
140+
}
141+
142+
# Parse options
143+
func_parse_lt_options "$0" ${1+"$@"}
144+
145+
# Find the directory that this script lives in.
146+
thisdir=`$ECHO "$file" | /usr/bin/sed 's%/[^/]*$%%'`
147+
test "x$thisdir" = "x$file" && thisdir=.
148+
149+
# Follow symbolic links until we get to the real thisdir.
150+
file=`ls -ld "$file" | /usr/bin/sed -n 's/.*-> //p'`
151+
while test -n "$file"; do
152+
destdir=`$ECHO "$file" | /usr/bin/sed 's%/[^/]*$%%'`
153+
154+
# If there was a directory component, then change thisdir.
155+
if test "x$destdir" != "x$file"; then
156+
case "$destdir" in
157+
[\\/]* | [A-Za-z]:[\\/]*) thisdir="$destdir" ;;
158+
*) thisdir="$thisdir/$destdir" ;;
159+
esac
160+
fi
161+
162+
file=`$ECHO "$file" | /usr/bin/sed 's%^.*/%%'`
163+
file=`ls -ld "$thisdir/$file" | /usr/bin/sed -n 's/.*-> //p'`
164+
done
165+
166+
# Usually 'no', except on cygwin/mingw when embedded into
167+
# the cwrapper.
168+
WRAPPER_SCRIPT_BELONGS_IN_OBJDIR=no
169+
if test "$WRAPPER_SCRIPT_BELONGS_IN_OBJDIR" = "yes"; then
170+
# special case for '.'
171+
if test "$thisdir" = "."; then
172+
thisdir=`pwd`
173+
fi
174+
# remove .libs from thisdir
175+
case "$thisdir" in
176+
*[\\/].libs ) thisdir=`$ECHO "$thisdir" | /usr/bin/sed 's%[\\/][^\\/]*$%%'` ;;
177+
.libs ) thisdir=. ;;
178+
esac
179+
fi
180+
181+
# Try to get the absolute directory name.
182+
absdir=`cd "$thisdir" && pwd`
183+
test -n "$absdir" && thisdir="$absdir"
184+
185+
program='ecdh'
186+
progdir="$thisdir/.libs"
187+
188+
189+
if test -f "$progdir/$program"; then
190+
# Add our own library path to LD_LIBRARY_PATH
191+
LD_LIBRARY_PATH="/home/davidgarske/GitHub/wolftpm-alt/src/.libs:/usr/local/lib:$LD_LIBRARY_PATH"
192+
193+
# Some systems cannot cope with colon-terminated LD_LIBRARY_PATH
194+
# The second colon is a workaround for a bug in BeOS R4 sed
195+
LD_LIBRARY_PATH=`$ECHO "$LD_LIBRARY_PATH" | /usr/bin/sed 's/::*$//'`
196+
197+
export LD_LIBRARY_PATH
198+
199+
if test "$libtool_execute_magic" != "%%%MAGIC variable%%%"; then
200+
# Run the actual program with our arguments.
201+
func_exec_program ${1+"$@"}
202+
fi
203+
else
204+
# The program doesn't exist.
205+
$ECHO "$0: error: '$progdir/$program' does not exist" 1>&2
206+
$ECHO "This script is just a wrapper for $program." 1>&2
207+
$ECHO "See the libtool documentation for more information." 1>&2
208+
exit 1
209+
fi
210+
fi

examples/nvram/counter.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ int TPM2_NVRAM_Counter_Example(void* userCtx, int argc, char *argv[])
7474
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
7575
TPM_ALG_ID pqcParamEncAlg = TPM_ALG_NULL;
7676
int pqcParamSet = 0;
77+
int pqcRc = 0;
7778
WOLFTPM2_KEY pqcKey;
7879
#endif
7980
word32 nvIndex = TPM2_DEMO_NV_COUNTER_INDEX;
@@ -121,13 +122,15 @@ int TPM2_NVRAM_Counter_Example(void* userCtx, int argc, char *argv[])
121122
paramEncAlg = TPM_ALG_XOR;
122123
}
123124
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
124-
else if (parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
125-
&pqcParamSet) != 0) {
126-
/* PQC option; an unsupported parameter set (alg left NULL) is a
127-
* fatal typo, not a silent drop of parameter encryption. */
128-
if (pqcParamEncAlg == TPM_ALG_NULL) {
125+
else if ((pqcRc = parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
126+
&pqcParamSet)) != 0) {
127+
/* An unsupported parameter set (parser returns -1) is a fatal typo,
128+
* not a silent drop of parameter encryption. Branch on the parser
129+
* result directly: pqcParamEncAlg may already hold a valid alg from
130+
* an earlier PQC option (argv is parsed right-to-left). */
131+
if (pqcRc < 0) {
129132
usage();
130-
return 0;
133+
return -1;
131134
}
132135
}
133136
#endif

examples/nvram/store.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
8282
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
8383
TPM_ALG_ID pqcParamEncAlg = TPM_ALG_NULL;
8484
int pqcParamSet = 0;
85+
int pqcRc = 0;
8586
WOLFTPM2_KEY pqcKey;
8687
#endif
8788
int partialStore = 0;
@@ -135,13 +136,15 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
135136
partialStore = PUBLIC_PART_ONLY;
136137
}
137138
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
138-
else if (parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
139-
&pqcParamSet) != 0) {
140-
/* PQC option; an unsupported parameter set (alg left NULL) is a
141-
* fatal typo, not a silent drop of parameter encryption. */
142-
if (pqcParamEncAlg == TPM_ALG_NULL) {
139+
else if ((pqcRc = parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
140+
&pqcParamSet)) != 0) {
141+
/* An unsupported parameter set (parser returns -1) is a fatal typo,
142+
* not a silent drop of parameter encryption. Branch on the parser
143+
* result directly: pqcParamEncAlg may already hold a valid alg from
144+
* an earlier PQC option (argv is parsed right-to-left). */
145+
if (pqcRc < 0) {
143146
usage();
144-
return 0;
147+
return -1;
145148
}
146149
}
147150
#endif

examples/pcr/quote.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int TPM2_PCR_Quote_Test(void* userCtx, int argc, char *argv[])
9191
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
9292
TPM_ALG_ID pqcParamEncAlg = TPM_ALG_NULL;
9393
int pqcParamSet = 0;
94+
int pqcRc = 0;
9495
WOLFTPM2_KEY pqcKey;
9596
#endif
9697
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
@@ -140,13 +141,15 @@ int TPM2_PCR_Quote_Test(void* userCtx, int argc, char *argv[])
140141
paramEncAlg = TPM_ALG_XOR;
141142
}
142143
#if defined(WOLFTPM_MLKEM) || defined(WOLFTPM_MLDSA)
143-
else if (parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
144-
&pqcParamSet) != 0) {
145-
/* PQC option; an unsupported parameter set (alg left NULL) is a
146-
* fatal typo, not a silent drop of parameter encryption. */
147-
if (pqcParamEncAlg == TPM_ALG_NULL) {
144+
else if ((pqcRc = parsePqcParamEncArg(argv[argc-1], &pqcParamEncAlg,
145+
&pqcParamSet)) != 0) {
146+
/* An unsupported parameter set (parser returns -1) is a fatal typo,
147+
* not a silent drop of parameter encryption. Branch on the parser
148+
* result directly: pqcParamEncAlg may already hold a valid alg from
149+
* an earlier PQC option (argv is parsed right-to-left). */
150+
if (pqcRc < 0) {
148151
usage();
149-
return 0;
152+
return -1;
150153
}
151154
}
152155
#endif

examples/run_examples.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ if [ $ENABLE_V185 -eq 1 ]; then
371371
[ $RESULT -ne 0 ] && echo -e "create_primary mldsa=$PS failed! $RESULT" && exit 1
372372
done
373373

374+
echo -e "PQC usage-error checks (invalid parameter sets must be rejected)"
375+
# These return before touching the TPM; a zero exit means an invalid
376+
# parameter set was silently accepted as the default.
377+
./examples/keygen/create_primary -mldsa=0 -oh >> $TPMPWD/run.out 2>&1
378+
[ $? -eq 0 ] && echo -e "create_primary -mldsa=0 should fail!" && exit 1
379+
./examples/keygen/create_primary -mldsa=abc -oh >> $TPMPWD/run.out 2>&1
380+
[ $? -eq 0 ] && echo -e "create_primary -mldsa=abc should fail!" && exit 1
381+
# An invalid PQC option must stay fatal even after a valid one (argv is
382+
# parsed right-to-left), in either argument order.
383+
./examples/wrap/wrap_test -aes -mldsa=999 -mlkem=768 >> $TPMPWD/run.out 2>&1
384+
[ $? -eq 0 ] && echo -e "wrap_test -mldsa=999 -mlkem=768 should fail!" && exit 1
385+
./examples/wrap/wrap_test -aes -mlkem=768 -mldsa=999 >> $TPMPWD/run.out 2>&1
386+
[ $? -eq 0 ] && echo -e "wrap_test -mlkem=768 -mldsa=999 should fail!" && exit 1
387+
374388
echo -e "PQC parameter encryption (ML-KEM salt / ML-DSA bind)"
375389
# ML-KEM as the param-enc session salt, ML-DSA as the param-enc session
376390
# bind; exercise AES-CFB and XOR across child-create, attestation and NV.

0 commit comments

Comments
 (0)