Skip to content

Commit 4b0b708

Browse files
committed
Added parameter encryption support to more examples. Fix to not set "encrypt" or "decrypt" if command doesn't allow it. Updated documentation.
1 parent 4c2e8d3 commit 4b0b708

38 files changed

Lines changed: 776 additions & 512 deletions

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ examples/tls/tls_client
3838
examples/pkcs7/pkcs7
3939
examples/timestamp/signed_timestamp
4040
examples/pcr/quote
41-
examples/pcr/quote_paramenc
4241
examples/pcr/extend
4342
examples/pcr/reset
4443
examples/timestamp/clock_set
@@ -51,8 +50,6 @@ tests/unit.test
5150
examples/keygen/keyload
5251
examples/keygen/keygen
5352
examples/keygen/keyimport
54-
examples/keygen/keygen_paramenc
55-
examples/keygen/keyload_paramenc
5653

5754
# Generated Cert Files
5855
certs/ca-*.pem

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Portable TPM 2.0 project designed for embedded use.
2424
* TLS Client
2525
* TLS Server
2626
* Benchmarking TPM algorithms and TLS
27+
* Parameter encryption support using AES-CFB or XOR. Supports salted unbound authenticated sessions.
2728

2829
Note: See [examples/README.md](examples/README.md) for details on using the examples.
2930

@@ -637,6 +638,10 @@ Connection: close
637638
## Todo
638639

639640
* Update to v1.59 of specification.
641+
* Add HMAC support for "authValue".
642+
* Add ECC encrypted salt.
643+
* Add bound auth session support.
644+
* Add multiple auth session (nonceTPMDecrypt and nonceTPMEncrypt) support.
640645

641646
## Support
642647

examples/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The examples create RSA and ECC keys in NV for testing using handles defined in
66

77
The PKCS #7 and TLS examples require generating CSR's and signing them using a test script. See CSR and Certificate Signing below.
88

9+
To enable parameter encryption use `-aes` for AES-CFB mode or `-xor` for XOR mode. Only some TPM commands / responses support parameter encryption. If the TPM2_ API has .flags `CMD_FLAG_ENC2` or `CMD_FLAG_DEC2` set then the command will use parameter encryption / decryption.
10+
911
## Native API Test
1012

1113
Demonstrates calling native TPM2_* API's.
@@ -110,8 +112,8 @@ To use symmetric AES/Hashing/HMAC with the TPM define `WOLFTPM_USE_SYMMETRIC`.
110112
Generation of the Client and Server Certificates requires running:
111113

112114

113-
1. `./examples/keygen/keygen rsa_test_blob.raw -RSA -T`
114-
2. `./examples/keygen/keygen ecc_test_blob.raw -ECC -T`
115+
1. `./examples/keygen/keygen rsa_test_blob.raw -rsa -t`
116+
2. `./examples/keygen/keygen ecc_test_blob.raw -ecc -t`
115117
3. `./examples/csr/csr`
116118
4. `./certs/certreq.sh`
117119
5. Copy the CA files from wolfTPM to wolfSSL certs directory.
@@ -134,9 +136,9 @@ or
134136
`./examples/server/server -b -p 11111 -g -A ./certs/tpm-ca-ecc-cert.pem -i -V`
135137

136138
Then run the wolfTPM TLS client example:
137-
`./examples/tls/tls_client RSA`
139+
`./examples/tls/tls_client -rsa`
138140
or
139-
`./examples/tls/tls_client ECC`
141+
`./examples/tls/tls_client -ecc`
140142

141143

142144
### TLS Server
@@ -146,9 +148,9 @@ This example shows using a TPM key and certificate for a TLS server.
146148
By default it listens on port 11111 and can be overridden at build-time using the `TLS_PORT` macro.
147149

148150
Run the wolfTPM TLS server example:
149-
`./examples/tls/tls_server RSA`
151+
`./examples/tls/tls_server -rsa`
150152
or
151-
`./examples/tls/tls_server ECC`
153+
`./examples/tls/tls_server -ecc`
152154

153155
Then run the wolfSSL example client this like:
154156
`./examples/client/client -h localhost -p 11111 -g -d`
@@ -194,7 +196,7 @@ Performance benchmarks.
194196
Examples for generating a TPM key blob and storing to disk, then loading from disk and loading into temporary TPM handle.
195197

196198
```
197-
$ ./examples/keygen/keygen keyblob.bin -RSA
199+
$ ./examples/keygen/keygen keyblob.bin -rsa
198200
TPM2.0 Key generation example
199201
Loading SRK: Storage 0x81000200 (282 bytes)
200202
Creating new RSA key...
@@ -208,7 +210,7 @@ Reading 840 bytes from keyblob.bin
208210
Loaded key to 0x80000001
209211
210212
211-
$ ./examples/keygen/keygen keyblob.bin -ECC
213+
$ ./examples/keygen/keygen keyblob.bin -ecc
212214
TPM2.0 Key generation example
213215
Loading SRK: Storage 0x81000200 (282 bytes)
214216
Creating new ECC key...
@@ -225,7 +227,7 @@ Loaded key to 0x80000001
225227
Example for importing a private key as TPM key blob and storing to disk, then loading from disk and loading into temporary TPM handle.
226228

227229
```
228-
$ ./examples/keygen/keyimport keyblob.bin -RSA
230+
$ ./examples/keygen/keyimport keyblob.bin -rsa
229231
TPM2.0 Key import example
230232
Loading SRK: Storage 0x81000200 (282 bytes)
231233
Imported key (pub 278, priv 222 bytes)
@@ -238,7 +240,7 @@ Reading 840 bytes from keyblob.bin
238240
Loaded key to 0x80000001
239241
240242
241-
$ ./examples/keygen/keyimport keyblob.bin -ECC
243+
$ ./examples/keygen/keyimport keyblob.bin -ecc
242244
TPM2.0 Key Import example
243245
Loading SRK: Storage 0x81000200 (282 bytes)
244246
Imported key (pub 86, priv 126 bytes)

examples/bench/bench.c

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,22 @@ static int bench_sym_aes(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* storageKey,
181181
return rc;
182182
}
183183

184+
static void usage(void)
185+
{
186+
printf("Expected usage:\n");
187+
printf("./examples/bench/bench [-aes/xor]\n");
188+
printf("* -aes/xor: Use Parameter Encryption\n");
189+
}
190+
184191
/******************************************************************************/
185192
/* --- BEGIN Bench Wrapper -- */
186193
/******************************************************************************/
194+
int TPM2_Wrapper_Bench(void* userCtx)
195+
{
196+
return TPM2_Wrapper_BenchArgs(userCtx, 0, NULL);
197+
}
187198

188-
int TPM2_Wrapper_Bench(void* userCtx, int argc, char *argv[])
199+
int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
189200
{
190201
int rc;
191202
WOLFTPM2_DEV dev;
@@ -199,11 +210,35 @@ int TPM2_Wrapper_Bench(void* userCtx, int argc, char *argv[])
199210
TPM2B_ECC_POINT pubPoint;
200211
double start;
201212
int count;
213+
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
214+
WOLFTPM2_SESSION tpmSession;
215+
216+
if (argc >= 2) {
217+
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
218+
XSTRNCMP(argv[1], "-h", 2) == 0 ||
219+
XSTRNCMP(argv[1], "--help", 6) == 0) {
220+
usage();
221+
return 0;
222+
}
223+
}
224+
while (argc > 1) {
225+
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
226+
paramEncAlg = TPM_ALG_CFB;
227+
}
228+
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
229+
paramEncAlg = TPM_ALG_XOR;
230+
}
231+
argc--;
232+
}
202233

203-
printf("TPM2 Benchmark using Wrapper API's\n");
234+
XMEMSET(&storageKey, 0, sizeof(storageKey));
235+
XMEMSET(&eccKey, 0, sizeof(eccKey));
236+
XMEMSET(&rsaKey, 0, sizeof(rsaKey));
237+
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
204238

205-
(void)argc;
206-
(void)argv;
239+
240+
printf("TPM2 Benchmark using Wrapper API's\n");
241+
printf("\tUse Parameter Encryption: %s\n", TPM2_GetAlgName(paramEncAlg));
207242

208243
/* Init the TPM2 device */
209244
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
@@ -213,6 +248,20 @@ int TPM2_Wrapper_Bench(void* userCtx, int argc, char *argv[])
213248
rc = getPrimaryStoragekey(&dev, &storageKey, TPM_ALG_RSA);
214249
if (rc != 0) goto exit;
215250

251+
if (paramEncAlg != TPM_ALG_NULL) {
252+
/* Start an authenticated session (salted / unbound) with parameter encryption */
253+
rc = wolfTPM2_StartSession(&dev, &tpmSession, &storageKey, NULL,
254+
TPM_SE_HMAC, paramEncAlg);
255+
if (rc != 0) goto exit;
256+
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n",
257+
(word32)tpmSession.handle.hndl);
258+
259+
/* set session for authorization of the storage key */
260+
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSession,
261+
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt | TPMA_SESSION_continueSession));
262+
if (rc != 0) goto exit;
263+
}
264+
216265
/* RNG Benchmark */
217266
bench_stats_start(&count, &start);
218267
do {
@@ -423,6 +472,7 @@ int TPM2_Wrapper_Bench(void* userCtx, int argc, char *argv[])
423472

424473
wolfTPM2_UnloadHandle(&dev, &rsaKey.handle);
425474
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
475+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
426476

427477
wolfTPM2_Cleanup(&dev);
428478

@@ -441,7 +491,7 @@ int main(int argc, char *argv[])
441491
int rc = -1;
442492

443493
#if !defined(WOLFTPM2_NO_WRAPPER) && !defined(NO_TPM_BENCH)
444-
rc = TPM2_Wrapper_Bench(NULL, argc, argv);
494+
rc = TPM2_Wrapper_BenchArgs(NULL, argc, argv);
445495
#else
446496
printf("Wrapper code not compiled in\n");
447497
#endif

examples/bench/bench.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
extern "C" {
2727
#endif
2828

29-
int TPM2_Wrapper_Bench(void* userCtx, int argc, char *argv[]);
29+
int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[]);
30+
int TPM2_Wrapper_Bench(void* userCtx);
3031

3132
#ifdef __cplusplus
3233
} /* extern "C" */

examples/csr/csr.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int key_type, void* wolfKey,
137137
return rc;
138138
}
139139

140-
int TPM2_CSR_Example(void* userCtx, int argc, char *argv[])
140+
int TPM2_CSR_Example(void* userCtx)
141+
{
142+
return TPM2_CSR_ExampleArgs(userCtx, 0, NULL);
143+
}
144+
int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
141145
{
142146
int rc;
143147
WOLFTPM2_DEV dev;
@@ -243,7 +247,7 @@ int main(int argc, char *argv[])
243247
#if !defined(WOLFTPM2_NO_WRAPPER) && !defined(WOLFTPM2_NO_WOLFCRYPT) && \
244248
defined(WOLFSSL_CERT_REQ) && \
245249
(defined(WOLF_CRYPTO_DEV) || defined(WOLF_CRYPTO_CB))
246-
rc = TPM2_CSR_Example(NULL, argc, argv);
250+
rc = TPM2_CSR_ExampleArgs(NULL, argc, argv);
247251
#else
248252
(void)argc;
249253
(void)argv;

examples/csr/csr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
extern "C" {
2727
#endif
2828

29-
int TPM2_CSR_Example(void* userCtx, int argc, char *argv[]);
29+
int TPM2_CSR_Example(void* userCtx);
30+
int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[]);
3031

3132
#ifdef __cplusplus
3233
} /* extern "C" */

examples/keygen/keygen.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
static void usage(void)
3838
{
3939
printf("Expected usage:\n");
40-
printf("keygen [keyblob.bin] [-ECC/-RSA] [-T] [-e]\n");
41-
printf("-T: Use default template (otherwise AIK)\n");
42-
printf("-e: Use Parameter Encryption\n");
40+
printf("./examples/keygen/keygen [keyblob.bin] [-ecc/-rsa] [-t] [-aes/xor]\n");
41+
printf("* -ecc: Use RSA or ECC for keys\n");
42+
printf("* -t: Use default template (otherwise AIK)\n");
43+
printf("* -aes/xor: Use Parameter Encryption\n");
4344
}
4445

4546
int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
@@ -50,7 +51,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
5051
WOLFTPM2_KEYBLOB newKey;
5152
TPMT_PUBLIC publicTemplate;
5253
TPMI_ALG_PUBLIC alg = TPM_ALG_RSA; /* TPM_ALG_ECC */
53-
int useParamEnc = 0;
54+
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
5455
WOLFTPM2_SESSION tpmSession;
5556
TPM2B_AUTH auth;
5657
int bAIK = 1;
@@ -71,14 +72,17 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
7172
outputFile = argv[1];
7273
}
7374
while (argc > 1) {
74-
if (XSTRNCMP(argv[argc-1], "-ECC", 4) == 0) {
75+
if (XSTRNCMP(argv[argc-1], "-ecc", 4) == 0) {
7576
alg = TPM_ALG_ECC;
7677
}
77-
if (XSTRNCMP(argv[argc-1], "-T", 2) == 0) {
78+
if (XSTRNCMP(argv[argc-1], "-t", 2) == 0) {
7879
bAIK = 0;
7980
}
80-
if (XSTRNCMP(argv[argc-1], "-e", 2) == 0) {
81-
useParamEnc = 1;
81+
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
82+
paramEncAlg = TPM_ALG_CFB;
83+
}
84+
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
85+
paramEncAlg = TPM_ALG_XOR;
8286
}
8387
argc--;
8488
}
@@ -92,7 +96,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
9296
printf("\tKey Blob: %s\n", outputFile);
9397
printf("\tAlgorithm: %s\n", TPM2_GetAlgName(alg));
9498
printf("\tTemplate: %s\n", bAIK ? "AIK" : "Default");
95-
printf("\tUse Parameter Encryption: %d\n", useParamEnc);
99+
printf("\tUse Parameter Encryption: %s\n", TPM2_GetAlgName(paramEncAlg));
96100

97101
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
98102
if (rc != TPM_RC_SUCCESS) {
@@ -104,10 +108,10 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
104108
rc = getPrimaryStoragekey(&dev, &storage, TPM_ALG_RSA);
105109
if (rc != 0) goto exit;
106110

107-
if (useParamEnc) {
108-
/* Start an authenticated session (salted / unbound with AES CFB parameter encryption) */
111+
if (paramEncAlg != TPM_ALG_NULL) {
112+
/* Start an authenticated session (salted / unbound) with parameter encryption */
109113
rc = wolfTPM2_StartSession(&dev, &tpmSession, &storage, NULL,
110-
TPM_SE_POLICY, TPM_ALG_CFB);
114+
TPM_SE_HMAC, paramEncAlg);
111115
if (rc != 0) goto exit;
112116
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n",
113117
(word32)tpmSession.handle.hndl);

examples/keygen/keygen.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[]);
3030
int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[]);
3131
int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[]);
32-
int TPM2_Keygen_ParamEnc_Example(void* userCtx, int argc, char *argv[]);
33-
int TPM2_Keyload_ParamEnc_Example(void* userCtx, int argc, char *argv[]);
3432

3533
#ifdef __cplusplus
3634
} /* extern "C" */

0 commit comments

Comments
 (0)