Skip to content

Commit c7e9326

Browse files
author
elms
authored
Merge pull request #129 from tomoveu/add-keygen-example
2 parents 5a77c4c + 523a9f9 commit c7e9326

50 files changed

Lines changed: 2773 additions & 1697 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
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

README.md

Lines changed: 6 additions & 3 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

@@ -111,7 +112,7 @@ Mfg NTC (0), Vendor NPCT75x"!!4rls, Fw 7.2 (131072), FIPS 140-2 1, CC-EAL4 0
111112
git clone https://github.com/wolfSSL/wolfssl.git
112113
cd wolfssl
113114
./autogen.sh
114-
./configure --enable-certgen --enable-certreq --enable-certext --enable-pkcs7 --enable-cryptocb
115+
./configure --enable-certgen --enable-certreq --enable-certext --enable-pkcs7 --enable-cryptocb --enable-aescfb
115116
make
116117
sudo make install
117118
sudo ldconfig
@@ -636,9 +637,11 @@ Connection: close
636637

637638
## Todo
638639

639-
* Add support for SensitiveToPrivate inner and outer.
640-
* Add runtime support for detecting module type ST33, SLB9670 or ATTPM20.
641640
* 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.
642645

643646
## Support
644647

examples/README.md

Lines changed: 13 additions & 11 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`
@@ -179,7 +181,7 @@ This way the user can keep track of relative and current time using the TPM cloc
179181

180182
Note: If the new time value makes a change bigger than the TPM clock update interval, then the TPM will first update its volatile register for time and then the non-volatile register for time. This may cause a narrow delay before the commands returns execution to the user. Depending on the TPM manufacturer, the delay can vary from us to few ms.
181183

182-
Note: This example can take an optional argument, the time value in miliseconds used for incrementing the TPM clock. Default value is 50000ms (50 seconds).
184+
Note: This example can take an optional argument, the time value in milliseconds used for incrementing the TPM clock. Default value is 50000ms (50 seconds).
183185

184186
`./examples/timestamp/clock_set`
185187

@@ -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: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static int bench_sym_aes(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* storageKey,
157157

158158
XMEMSET(&aesKey, 0, sizeof(aesKey));
159159
rc = wolfTPM2_GetKeyTemplate_Symmetric(&publicTemplate, keyBits, algo,
160-
NO, YES);
160+
YES, YES);
161161
if (rc != 0) goto exit;
162162
rc = wolfTPM2_CreateAndLoadKey(dev, &aesKey, &storageKey->handle,
163163
&publicTemplate, (byte*)gUsageAuth, sizeof(gUsageAuth)-1);
@@ -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
/******************************************************************************/
187-
188194
int TPM2_Wrapper_Bench(void* userCtx)
195+
{
196+
return TPM2_Wrapper_BenchArgs(userCtx, 0, NULL);
197+
}
198+
199+
int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
189200
{
190201
int rc;
191202
WOLFTPM2_DEV dev;
@@ -199,20 +210,58 @@ int TPM2_Wrapper_Bench(void* userCtx)
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

205239

240+
printf("TPM2 Benchmark using Wrapper API's\n");
241+
printf("\tUse Parameter Encryption: %s\n", TPM2_GetAlgName(paramEncAlg));
242+
206243
/* Init the TPM2 device */
207244
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
208245
if (rc != 0) return rc;
209246

210247
/* See if primary storage key already exists */
211-
rc = getPrimaryStoragekey(&dev,
212-
&storageKey,
213-
&publicTemplate);
248+
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)
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

@@ -436,12 +486,12 @@ int TPM2_Wrapper_Bench(void* userCtx)
436486
#endif /* !WOLFTPM2_NO_WRAPPER && !NO_TPM_BENCH */
437487

438488
#ifndef NO_MAIN_DRIVER
439-
int main(void)
489+
int main(int argc, char *argv[])
440490
{
441491
int rc = -1;
442492

443493
#if !defined(WOLFTPM2_NO_WRAPPER) && !defined(NO_TPM_BENCH)
444-
rc = TPM2_Wrapper_Bench(NULL);
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
extern "C" {
2727
#endif
2828

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

3132
#ifdef __cplusplus

examples/csr/csr.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int key_type, void* wolfKey,
138138
}
139139

140140
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;
@@ -150,12 +154,14 @@ int TPM2_CSR_Example(void* userCtx)
150154
WOLFTPM2_KEY eccKey;
151155
ecc_key wolfEccKey;
152156
#endif
153-
TPMT_PUBLIC publicTemplate;
154157
TpmCryptoDevCtx tpmCtx;
155158
int tpmDevId;
156159

157160
printf("TPM2 CSR Example\n");
158161

162+
(void)argc;
163+
(void)argv;
164+
159165
/* Init the TPM2 device */
160166
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
161167
if (rc != 0) return rc;
@@ -174,21 +180,16 @@ int TPM2_CSR_Example(void* userCtx)
174180
if (rc != 0) goto exit;
175181

176182
/* See if primary storage key already exists */
177-
rc = getPrimaryStoragekey(&dev,
178-
&storageKey,
179-
&publicTemplate);
183+
rc = getPrimaryStoragekey(&dev, &storageKey, TPM_ALG_RSA);
180184
if (rc != 0) goto exit;
181185

182-
storageKey.handle.auth.size = sizeof(gStorageKeyAuth)-1;
183-
XMEMCPY(storageKey.handle.auth.buffer, gStorageKeyAuth,
184-
storageKey.handle.auth.size);
185-
186186
#ifndef NO_RSA
187187
rc = getRSAkey(&dev,
188188
&storageKey,
189189
&rsaKey,
190190
&wolfRsaKey,
191-
tpmDevId);
191+
tpmDevId,
192+
(byte*)gKeyAuth, sizeof(gKeyAuth)-1);
192193
if (rc != 0) goto exit;
193194

194195
rc = TPM2_CSR_Generate(&dev, RSA_TYPE, &wolfRsaKey, gClientCertRsaFile);
@@ -201,7 +202,8 @@ int TPM2_CSR_Example(void* userCtx)
201202
&storageKey,
202203
&eccKey,
203204
&wolfEccKey,
204-
tpmDevId);
205+
tpmDevId,
206+
(byte*)gKeyAuth, sizeof(gKeyAuth)-1);
205207
if (rc != 0) goto exit;
206208

207209
rc = TPM2_CSR_Generate(&dev, ECC_TYPE, &wolfEccKey, gClientCertEccFile);
@@ -238,15 +240,18 @@ int TPM2_CSR_Example(void* userCtx)
238240
#endif /* !WOLFTPM2_NO_WRAPPER && WOLFSSL_CERT_REQ && WOLF_CRYPTO_DEV */
239241

240242
#ifndef NO_MAIN_DRIVER
241-
int main(void)
243+
int main(int argc, char *argv[])
242244
{
243245
int rc = -1;
244246

245247
#if !defined(WOLFTPM2_NO_WRAPPER) && !defined(WOLFTPM2_NO_WOLFCRYPT) && \
246248
defined(WOLFSSL_CERT_REQ) && \
247249
(defined(WOLF_CRYPTO_DEV) || defined(WOLF_CRYPTO_CB))
248-
rc = TPM2_CSR_Example(NULL);
250+
rc = TPM2_CSR_ExampleArgs(NULL, argc, argv);
249251
#else
252+
(void)argc;
253+
(void)argv;
254+
250255
printf("Wrapper/CertReq/CryptoDev code not compiled in\n");
251256
printf("Build wolfssl with ./configure --enable-certgen --enable-certreq --enable-certext --enable-cryptocb\n");
252257
#endif

examples/csr/csr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#endif
2828

2929
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/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ noinst_HEADERS += examples/keygen/keygen.h
66

77
bin_PROGRAMS += examples/keygen/keyload
88
examples_keygen_keyload_SOURCES = examples/keygen/keyload.c \
9-
examples/tpm_test_keys.c \
9+
examples/tpm_test_keys.c \
1010
examples/tpm_io.c
1111
examples_keygen_keyload_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1212
examples_keygen_keyload_DEPENDENCIES = src/libwolftpm.la
@@ -20,6 +20,7 @@ examples_keygen_keygen_DEPENDENCIES = src/libwolftpm.la
2020

2121
bin_PROGRAMS += examples/keygen/keyimport
2222
examples_keygen_keyimport_SOURCES = examples/keygen/keyimport.c \
23+
examples/tpm_test_keys.c \
2324
examples/tpm_io.c
2425
examples_keygen_keyimport_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
2526
examples_keygen_keyimport_DEPENDENCIES = src/libwolftpm.la

0 commit comments

Comments
 (0)