Skip to content

Commit 3e8b711

Browse files
committed
Code size reduction. Adds compile-time options to exclude groups of TPM2 commands. Combined some duplicate code.
1 parent 1c61ff6 commit 3e8b711

File tree

21 files changed

+358
-230
lines changed

21 files changed

+358
-230
lines changed

.github/workflows/make-test-swtpm.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ jobs:
116116
wolftpm_config: --disable-provisioning
117117
needs_swtpm: false
118118

119+
# No NV
120+
- name: no-nv
121+
wolftpm_config: --disable-nv
122+
needs_swtpm: false
123+
124+
# No PCR policy
125+
- name: no-pcr-policy
126+
wolftpm_config: --disable-pcr-policy
127+
needs_swtpm: false
128+
129+
# No attestation
130+
- name: no-attestation
131+
wolftpm_config: --disable-attestation
132+
needs_swtpm: false
133+
119134
# Symmetric encryption
120135
- name: symmetric
121136
wolftpm_cflags: "-DWOLFTPM_USE_SYMMETRIC"

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,36 @@ if(WOLFTPM_PROVISIONING)
119119
"-DWOLFTPM_PROVISIONING")
120120
endif()
121121

122+
# NV Storage
123+
set(WOLFTPM_NV "yes" CACHE STRING
124+
"Enable NV storage commands (default: enabled)")
125+
set_property(CACHE WOLFTPM_NV
126+
PROPERTY STRINGS "yes;no")
127+
if(NOT WOLFTPM_NV)
128+
list(APPEND WOLFTPM_DEFINITIONS
129+
"-DWOLFTPM_NO_NV")
130+
endif()
131+
132+
# PCR and Policy
133+
set(WOLFTPM_PCR_POLICY "yes" CACHE STRING
134+
"Enable extended PCR and Policy commands (default: enabled)")
135+
set_property(CACHE WOLFTPM_PCR_POLICY
136+
PROPERTY STRINGS "yes;no")
137+
if(NOT WOLFTPM_PCR_POLICY)
138+
list(APPEND WOLFTPM_DEFINITIONS
139+
"-DWOLFTPM_NO_PCR_POLICY")
140+
endif()
141+
142+
# Attestation commands (Quote, Certify, GetTime, etc.)
143+
set(WOLFTPM_ATTESTATION "yes" CACHE STRING
144+
"Enable attestation commands Quote/Certify/GetTime (default: enabled)")
145+
set_property(CACHE WOLFTPM_ATTESTATION
146+
PROPERTY STRINGS "yes;no")
147+
if(NOT WOLFTPM_ATTESTATION)
148+
list(APPEND WOLFTPM_DEFINITIONS
149+
"-DWOLFTPM_NO_ATTESTATION")
150+
endif()
151+
122152
# Enable Debugging
123153
set(WOLFTPM_DEBUG "no" CACHE STRING
124154
"Enables option for debug (default: disabled)")

configure.ac

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,39 @@ then
462462
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_PROVISIONING"
463463
fi
464464

465+
# NV Storage support
466+
AC_ARG_ENABLE([nv],
467+
[AS_HELP_STRING([--enable-nv],[Enable NV storage commands (default: enabled)])],
468+
[ ENABLED_NV=$enableval ],
469+
[ ENABLED_NV=yes ]
470+
)
471+
if test "x$ENABLED_NV" = "xno"
472+
then
473+
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_NO_NV"
474+
fi
475+
476+
# PCR and Policy support
477+
AC_ARG_ENABLE([pcr-policy],
478+
[AS_HELP_STRING([--enable-pcr-policy],[Enable extended PCR and Policy commands (default: enabled)])],
479+
[ ENABLED_PCR_POLICY=$enableval ],
480+
[ ENABLED_PCR_POLICY=yes ]
481+
)
482+
if test "x$ENABLED_PCR_POLICY" = "xno"
483+
then
484+
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_NO_PCR_POLICY"
485+
fi
486+
487+
# Attestation commands (Quote, Certify, GetTime, etc.)
488+
AC_ARG_ENABLE([attestation],
489+
[AS_HELP_STRING([--enable-attestation],[Enable attestation commands Quote/Certify/GetTime (default: enabled)])],
490+
[ ENABLED_ATTESTATION=$enableval ],
491+
[ ENABLED_ATTESTATION=yes ]
492+
)
493+
if test "x$ENABLED_ATTESTATION" = "xno"
494+
then
495+
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_NO_ATTESTATION"
496+
fi
497+
465498

466499
# HARDEN FLAGS
467500
AX_HARDEN_CC_COMPILER_FLAGS
@@ -492,6 +525,9 @@ AM_CONDITIONAL([BUILD_CHECKWAITSTATE], [test "x$ENABLED_CHECKWAITSTATE" = "xyes"
492525
AM_CONDITIONAL([BUILD_AUTODETECT], [test "x$ENABLED_AUTODETECT" = "xyes"])
493526
AM_CONDITIONAL([BUILD_FIRMWARE], [test "x$ENABLED_FIRMWARE" = "xyes"])
494527
AM_CONDITIONAL([BUILD_HAL], [test "x$ENABLED_EXAMPLE_HAL" = "xyes" || test "x$ENABLED_MMIO" = "xyes"])
528+
AM_CONDITIONAL([BUILD_NV], [test "x$ENABLED_NV" = "xyes"])
529+
AM_CONDITIONAL([BUILD_PCR_POLICY], [test "x$ENABLED_PCR_POLICY" = "xyes"])
530+
AM_CONDITIONAL([BUILD_ATTESTATION], [test "x$ENABLED_ATTESTATION" = "xyes"])
495531

496532

497533
CREATE_HEX_VERSION

examples/attestation/include.am

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22
# All paths should be given relative to the root
33

44
if BUILD_EXAMPLES
5-
noinst_PROGRAMS += examples/attestation/make_credential \
6-
examples/attestation/activate_credential \
7-
examples/attestation/certify
8-
95
noinst_HEADERS += examples/attestation/attestation.h
106

7+
noinst_PROGRAMS += examples/attestation/make_credential
118
examples_attestation_make_credential_SOURCES = examples/attestation/make_credential.c \
129
examples/tpm_test_keys.c
1310
examples_attestation_make_credential_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1411
examples_attestation_make_credential_DEPENDENCIES = src/libwolftpm.la
1512

13+
if BUILD_PCR_POLICY
14+
noinst_PROGRAMS += examples/attestation/activate_credential
1615
examples_attestation_activate_credential_SOURCES = examples/attestation/activate_credential.c \
1716
examples/tpm_test_keys.c
1817
examples_attestation_activate_credential_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1918
examples_attestation_activate_credential_DEPENDENCIES = src/libwolftpm.la
2019

20+
if BUILD_ATTESTATION
21+
noinst_PROGRAMS += examples/attestation/certify
2122
examples_attestation_certify_SOURCES = examples/attestation/certify.c \
2223
examples/tpm_test_keys.c
2324
examples_attestation_certify_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
2425
examples_attestation_certify_DEPENDENCIES = src/libwolftpm.la
25-
endif
26+
endif BUILD_ATTESTATION
27+
endif BUILD_PCR_POLICY
28+
endif BUILD_EXAMPLES
2629
example_attestationdir = $(exampledir)/attestation
2730
dist_example_attestation_DATA = \
2831
examples/attestation/make_credential.c \

examples/boot/include.am

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ EXTRA_DIST += examples/boot/README.md
66
if BUILD_EXAMPLES
77
noinst_HEADERS += examples/boot/boot.h
88

9+
if BUILD_NV
910
noinst_PROGRAMS += examples/boot/secure_rot
1011
examples_boot_secure_rot_SOURCES = examples/boot/secure_rot.c \
1112
examples/tpm_test_keys.c
1213
examples_boot_secure_rot_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1314
examples_boot_secure_rot_DEPENDENCIES = src/libwolftpm.la
15+
endif BUILD_NV
1416

17+
if BUILD_PCR_POLICY
1518
noinst_PROGRAMS += examples/boot/secret_seal
1619
examples_boot_secret_seal_SOURCES = examples/boot/secret_seal.c \
1720
examples/tpm_test_keys.c
@@ -23,7 +26,8 @@ examples_boot_secret_unseal_SOURCES = examples/boot/secret_unseal.c \
2326
examples/tpm_test_keys.c
2427
examples_boot_secret_unseal_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
2528
examples_boot_secret_unseal_DEPENDENCIES = src/libwolftpm.la
26-
endif
29+
endif BUILD_PCR_POLICY
30+
endif BUILD_EXAMPLES
2731

2832
example_bootdir = $(exampledir)/boot
2933
dist_example_boot_DATA = examples/boot/secure_rot.c \

examples/endorsement/include.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if BUILD_EXAMPLES
77
examples/endorsement/trusted_certs.h \
88
examples/endorsement/trusted_certs_der.h
99

10+
if BUILD_NV
1011
noinst_PROGRAMS += examples/endorsement/get_ek_certs
1112
examples_endorsement_get_ek_certs_SOURCES = examples/endorsement/get_ek_certs.c
1213
examples_endorsement_get_ek_certs_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
@@ -16,7 +17,8 @@ if BUILD_EXAMPLES
1617
examples_endorsement_verify_ek_cert_SOURCES = examples/endorsement/verify_ek_cert.c
1718
examples_endorsement_verify_ek_cert_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1819
examples_endorsement_verify_ek_cert_DEPENDENCIES = src/libwolftpm.la
19-
endif
20+
endif BUILD_NV
21+
endif BUILD_EXAMPLES
2022

2123
EXTRA_DIST+=examples/endorsement/README.md
2224
example_endorsementdir = $(exampledir)/endorsement

examples/keygen/create_primary.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,16 @@ int TPM2_CreatePrimaryKey_Example(void* userCtx, int argc, char *argv[])
274274
#endif
275275

276276
if (persistHandle > 0) {
277-
#ifndef WOLFTPM_WINAPI
277+
#if !defined(WOLFTPM_WINAPI) && !defined(WOLFTPM_NO_NV)
278278
/* Move storage key into persistent NV */
279279
printf("Storing Primary key to handle 0x%08x\n", persistHandle);
280280
rc = wolfTPM2_NVStoreKey(&dev, hierarchy, primary,
281281
persistHandle);
282282
if (rc != TPM_RC_SUCCESS) goto exit;
283283
#else
284-
printf("Windows TBS does not allow persisting handles to "
285-
"Non-Volatile (NV) Memory\n");
284+
printf("Persisting handles to Non-Volatile (NV) Memory not "
285+
"available\n");
286+
(void)rc;
286287
#endif
287288
}
288289

examples/keygen/keygen.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
287287
if (rc != 0) goto exit;
288288
}
289289

290+
#ifndef WOLFTPM_NO_PCR_POLICY
290291
if (endorseKey) {
291292
/* Endorsement Key requires authorization with Policy */
292293
rc = wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
@@ -298,6 +299,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
298299
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession, 0);
299300
if (rc != 0) goto exit;
300301
}
302+
#endif /* !WOLFTPM_NO_PCR_POLICY */
301303

302304
/* Create new key */
303305
if (bAIK) {
@@ -387,6 +389,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
387389
printf("wolfTPM2_CreateKey failed\n");
388390
goto exit;
389391
}
392+
#ifndef WOLFTPM_NO_PCR_POLICY
390393
if (endorseKey) {
391394
/* Endorsement policy session is closed after use, so start another */
392395
rc = wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
@@ -395,6 +398,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
395398
}
396399
if (rc != 0) goto exit;
397400
}
401+
#endif /* !WOLFTPM_NO_PCR_POLICY */
398402
rc = wolfTPM2_LoadKey(&dev, &newKeyBlob, &primary->handle);
399403
if (rc != TPM_RC_SUCCESS) {
400404
printf("wolfTPM2_LoadKey failed\n");

examples/keygen/keyload.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
6565
WOLFTPM2_KEY storage; /* SRK */
6666
WOLFTPM2_KEY *primary = NULL;
6767
WOLFTPM2_KEYBLOB newKey;
68+
#ifndef WOLFTPM_NO_NV
6869
WOLFTPM2_KEY persistKey;
70+
int persistent = 0;
71+
#endif
6972
TPM_ALG_ID alg;
7073
TPMI_ALG_PUBLIC srkAlg = TPM_ALG_ECC; /* prefer ECC, but allow RSA */
7174
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
7275
WOLFTPM2_SESSION tpmSession;
7376
const char* inputFile = "keyblob.bin";
74-
int persistent = 0;
7577
int endorseKey = 0;
7678

7779

@@ -97,9 +99,11 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
9799
else if (XSTRCMP(argv[argc-1], "-xor") == 0) {
98100
paramEncAlg = TPM_ALG_XOR;
99101
}
102+
#ifndef WOLFTPM_NO_NV
100103
else if (XSTRCMP(argv[argc-1], "-persistent") == 0) {
101104
persistent = 1;
102105
}
106+
#endif
103107
else {
104108
printf("Warning: Unrecognized option: %s\n", argv[argc-1]);
105109
}
@@ -109,7 +113,9 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
109113
XMEMSET(&endorse, 0, sizeof(endorse));
110114
XMEMSET(&storage, 0, sizeof(storage));
111115
XMEMSET(&newKey, 0, sizeof(newKey));
116+
#ifndef WOLFTPM_NO_NV
112117
XMEMSET(&persistKey, 0, sizeof(persistKey));
118+
#endif
113119
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
114120

115121
printf("TPM2.0 Key load example\n");
@@ -151,6 +157,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
151157
primary = &storage;
152158
}
153159

160+
#ifndef WOLFTPM_NO_PCR_POLICY
154161
if (endorseKey) {
155162
/* Fresh policy session for EK auth */
156163
rc = wolfTPM2_CreateAuthSession_EkPolicy(&dev, &tpmSession);
@@ -159,7 +166,9 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
159166
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSession, 0);
160167
if (rc != 0) goto exit;
161168
}
162-
else if (paramEncAlg != TPM_ALG_NULL) {
169+
else
170+
#endif
171+
if (paramEncAlg != TPM_ALG_NULL) {
163172
WOLFTPM2_KEY* bindKey = &storage;
164173
#ifndef HAVE_ECC
165174
if (srkAlg == TPM_ALG_ECC)
@@ -198,6 +207,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
198207
printf("Loaded key to 0x%x\n",
199208
(word32)newKey.handle.hndl);
200209

210+
#ifndef WOLFTPM_NO_NV
201211
/* Make the TPM key persistent, so it remains loaded after example exit */
202212
if (persistent) {
203213
/* Prepare key in the format expected by the wolfTPM wrapper */
@@ -213,6 +223,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
213223
}
214224
printf("Key was made persistent at 0x%X\n", persistKey.handle.hndl);
215225
}
226+
#endif
216227

217228
exit:
218229

@@ -222,8 +233,11 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
222233

223234
/* Close key handles */
224235
wolfTPM2_UnloadHandle(&dev, &primary->handle);
236+
#ifndef WOLFTPM_NO_NV
225237
/* newKey.handle is already flushed by wolfTPM2_NVStoreKey */
226-
if (!persistent) {
238+
if (!persistent)
239+
#endif
240+
{
227241
wolfTPM2_UnloadHandle(&dev, &newKey.handle);
228242
}
229243
/* EK policy is destroyed after use, flush parameter encryption session */

0 commit comments

Comments
 (0)