Skip to content

Commit 197754b

Browse files
committed
Minor cleanups.
1 parent 1769a3a commit 197754b

8 files changed

Lines changed: 28 additions & 22 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ tests/unit.test
5050
examples/keygen/keyload
5151
examples/keygen/keygen
5252
examples/keygen/keyimport
53+
examples/nvram/store
54+
examples/nvram/read
5355

5456
# Generated Cert Files
5557
certs/ca-*.pem

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ The `keyload` tool takes only one argument, the filename of the stored key. Beca
298298

299299
## Storing keys into the TPM's NVRAM
300300

301-
These examples demonstrates how to use the TPM as a secure vault for keys. There are two programs, one to store a TPM key into the TPM's NVRAM and another to extract the key from the TPM's NVRAM. Both examples can use parameter encryption to protect from MITM attacks. The Non-volatile memory location is protected with a password authorization that is passed in encrypted form, when "-aes" is given on the commmand line.
301+
These examples demonstrates how to use the TPM as a secure vault for keys. There are two programs, one to store a TPM key into the TPM's NVRAM and another to extract the key from the TPM's NVRAM. Both examples can use parameter encryption to protect from MITM attacks. The Non-volatile memory location is protected with a password authorization that is passed in encrypted form, when "-aes" is given on the command line.
302302

303303
Before running the examples, make sure there is a keyblob.bin generated using the keygen tool. The key can be of any type, RSA, ECC or symmetric. The example will store the private and public part. In case of a symmetric key the public part is meta data from the TPM. How to generate a key you can see above, in the description of the keygen example.
304304

@@ -341,7 +341,7 @@ Loaded key to 0x80000001
341341
342342
```
343343

344-
The "read" example will try to load the extracted key, if both the public and private part of the key were stored in NVRAM. The "-aes" swiches triggers the use of parameter encryption.
344+
The "read" example will try to load the extracted key, if both the public and private part of the key were stored in NVRAM. The "-aes" switches triggers the use of parameter encryption.
345345

346346
The examples can work with partial key material - private or public. This is achieved by using the "-priv" and "-pub" options.
347347

examples/nvram/read.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ int TPM2_NVRAM_Read_Example(void* userCtx, int argc, char *argv[])
160160
offset += readSize;
161161

162162
/* Necessary for storing the publicArea with the correct encoding */
163-
rc = TPM2_ParsePublic(&keyBlob.pub, pubAreaBuffer, sizeof(pubAreaBuffer), &pubAreaSize);
163+
rc = TPM2_ParsePublic(&keyBlob.pub, pubAreaBuffer,
164+
(word32)sizeof(pubAreaBuffer), &pubAreaSize);
164165
if (rc != TPM_RC_SUCCESS) {
165166
printf("Decoding of PublicArea failed. Unable to extract correctly.\n");
166167
goto exit;

examples/nvram/store.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
158158
offset += sizeof(keyBlob.pub.size);
159159

160160
/* Necessary for storing the publicArea with the correct byte encoding */
161-
rc = TPM2_AppendPublic(pubAreaBuffer, sizeof(pubAreaBuffer), &pubAreaSize, &keyBlob.pub);
161+
rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer),
162+
&pubAreaSize, &keyBlob.pub);
162163
/* Note:
163164
* Public Area is the only part of a TPM key that can be stored encoded
164165
* Private Area is stored as-is, because TPM2B_PRIVATE is byte buffer

examples/tpm_test_keys.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ int writeKeyBlob(const char* filename,
4848
fp = XFOPEN(filename, "wb");
4949
if (fp != XBADFILE) {
5050
/* Make publicArea in encoded format to eliminate empty fields, save space */
51-
rc = TPM2_AppendPublic(pubAreaBuffer, sizeof(pubAreaBuffer), &pubAreaSize, &key->pub);
51+
rc = TPM2_AppendPublic(pubAreaBuffer, (word32)sizeof(pubAreaBuffer),
52+
&pubAreaSize, &key->pub);
5253
if (rc != TPM_RC_SUCCESS) return rc;
5354
if (pubAreaSize != (key->pub.size + sizeof(key->pub.size))) {
5455
#ifdef DEBUG_WOLFTPM
@@ -98,20 +99,23 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
9899

99100
bytes_read = XFREAD(&key->pub.size, 1, sizeof(key->pub.size), fp);
100101
if (bytes_read != sizeof(key->pub.size)) {
101-
printf("Read %zu, expected size marker of %zu bytes\n", bytes_read, sizeof(key->pub.size));
102+
printf("Read %zu, expected size marker of %zu bytes\n",
103+
bytes_read, sizeof(key->pub.size));
102104
goto exit;
103105
}
104106
fileSz -= bytes_read;
105107

106108
bytes_read = XFREAD(pubAreaBuffer, 1, sizeof(UINT16) + key->pub.size, fp);
107109
if (bytes_read != sizeof(UINT16) + key->pub.size) {
108-
printf("Read %zu, expected public blob %lu bytes\n", bytes_read, sizeof(UINT16) + key->pub.size);
110+
printf("Read %zu, expected public blob %lu bytes\n",
111+
bytes_read, sizeof(UINT16) + key->pub.size);
109112
goto exit;
110113
}
111114
fileSz -= bytes_read; /* Reminder bytes for private key part */
112115

113116
/* Decode the byte stream into a publicArea structure ready for use */
114-
rc = TPM2_ParsePublic(&key->pub, pubAreaBuffer, sizeof(pubAreaBuffer), &pubAreaSize);
117+
rc = TPM2_ParsePublic(&key->pub, pubAreaBuffer,
118+
(word32)sizeof(pubAreaBuffer), &pubAreaSize);
115119
if (rc != TPM_RC_SUCCESS) return rc;
116120
#ifdef DEBUG_WOLFTPM
117121
TPM2_PrintPublicArea(&key->pub);
@@ -121,7 +125,8 @@ int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
121125
printf("Reading the private part of the key\n");
122126
bytes_read = XFREAD(&key->priv, 1, fileSz, fp);
123127
if (bytes_read != fileSz) {
124-
printf("Read %zu, expected private blob %zu bytes\n", bytes_read, fileSz);
128+
printf("Read %zu, expected private blob %zu bytes\n",
129+
bytes_read, fileSz);
125130
goto exit;
126131
}
127132
rc = 0; /* success */

src/tpm2.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,7 +5721,6 @@ int TPM2_HashNvPublic(TPMS_NV_PUBLIC* nvPublic, byte* buffer, UINT16* size)
57215721
if (rc == 0) {
57225722
rc = wc_HashUpdate(&hash, hashType, packet.buf, packet.pos);
57235723
}
5724-
57255724
if (rc == 0) {
57265725
rc = wc_HashFinal(&hash, hashType, &buffer[2]);
57275726
}
@@ -5747,11 +5746,11 @@ int TPM2_HashNvPublic(TPMS_NV_PUBLIC* nvPublic, byte* buffer, UINT16* size)
57475746
#endif
57485747
}
57495748

5750-
WOLFTPM_API int TPM2_AppendPublic(byte* buf, size_t size, int* sizeUsed, TPM2B_PUBLIC* pub)
5749+
int TPM2_AppendPublic(byte* buf, word32 size, int* sizeUsed, TPM2B_PUBLIC* pub)
57515750
{
57525751
TPM2_Packet packet;
57535752

5754-
if (buf == NULL || sizeUsed == NULL)
5753+
if (buf == NULL || pub == NULL || sizeUsed == NULL)
57555754
return BAD_FUNC_ARG;
57565755

57575756
if (size < sizeof(TPM2B_PUBLIC)) {
@@ -5770,11 +5769,11 @@ WOLFTPM_API int TPM2_AppendPublic(byte* buf, size_t size, int* sizeUsed, TPM2B_P
57705769
return TPM_RC_SUCCESS;
57715770
}
57725771

5773-
WOLFTPM_API int TPM2_ParsePublic(TPM2B_PUBLIC* pub, byte* buf, size_t size, int* sizeUsed)
5772+
int TPM2_ParsePublic(TPM2B_PUBLIC* pub, byte* buf, word32 size, int* sizeUsed)
57745773
{
57755774
TPM2_Packet packet;
57765775

5777-
if (buf == NULL || sizeUsed == NULL)
5776+
if (buf == NULL || pub == NULL || sizeUsed == NULL)
57785777
return BAD_FUNC_ARG;
57795778

57805779
if (size < sizeof(TPM2B_PUBLIC)) {

src/tpm2_wrap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ int wolfTPM2_NVWriteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
26642664
rc = wolfTPM2_NVReadPublic(dev, nv->handle.hndl, &nvPublic);
26652665
if (rc != TPM_RC_SUCCESS) {
26662666
#ifdef DEBUG_WOLFTPM
2667-
printf("Failed to read fresh NvPublic\n");
2667+
printf("Failed to read fresh NV Public\n");
26682668
#endif
26692669
return TPM_RC_FAILURE;
26702670
}
@@ -2677,7 +2677,7 @@ int wolfTPM2_NVWriteAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
26772677
}
26782678

26792679
/* Necessary, because NVWrite has two handles, second is NV Index */
2680-
rc = wolfTPM2_SetAuthHandleName(dev, 0, &nv->handle);
2680+
rc = wolfTPM2_SetAuthHandleName(dev, 0, &nv->handle);
26812681
rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle);
26822682
if (rc != TPM_RC_SUCCESS) {
26832683
printf("Storing NV Index Name failed\n");
@@ -2751,7 +2751,7 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
27512751
rc = wolfTPM2_NVReadPublic(dev, nv->handle.hndl, &nvPublic);
27522752
if (rc != TPM_RC_SUCCESS) {
27532753
#ifdef DEBUG_WOLFTPM
2754-
printf("Failed to read fresh NvPublic\n");
2754+
printf("Failed to read fresh NV Public\n");
27552755
#endif
27562756
return TPM_RC_FAILURE;
27572757
}
@@ -2764,15 +2764,14 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
27642764
}
27652765

27662766
/* Necessary, because NVWrite has two handles, second is NV Index */
2767-
rc = wolfTPM2_SetAuthHandleName(dev, 0, &nv->handle);
2767+
rc = wolfTPM2_SetAuthHandleName(dev, 0, &nv->handle);
27682768
rc |= wolfTPM2_SetAuthHandleName(dev, 1, &nv->handle);
27692769
if (rc != TPM_RC_SUCCESS) {
27702770
printf("Storing NV Index Name failed\n");
27712771
return TPM_RC_FAILURE;
27722772
}
27732773

27742774
dataSz = *pDataSz;
2775-
27762775
while (dataSz > 0) {
27772776
toread = dataSz;
27782777
if (toread > MAX_NV_BUFFER_SIZE)
@@ -2810,7 +2809,6 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
28102809
pos += toread;
28112810
dataSz -= toread;
28122811
}
2813-
28142812
*pDataSz = pos;
28152813

28162814
return rc;

wolftpm/tpm2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,8 +2801,8 @@ WOLFTPM_API int TPM2_GetWolfCurve(int curve_id);
28012801

28022802
WOLFTPM_API int TPM2_ParseAttest(const TPM2B_ATTEST* in, TPMS_ATTEST* out);
28032803
WOLFTPM_API int TPM2_HashNvPublic(TPMS_NV_PUBLIC* nvPublic, byte* buffer, UINT16* size);
2804-
WOLFTPM_API int TPM2_AppendPublic(byte* buf, size_t size, int* sizeUsed, TPM2B_PUBLIC* pub);
2805-
WOLFTPM_API int TPM2_ParsePublic(TPM2B_PUBLIC* pub, byte* buf, size_t size, int* sizeUsed);
2804+
WOLFTPM_API int TPM2_AppendPublic(byte* buf, word32 size, int* sizeUsed, TPM2B_PUBLIC* pub);
2805+
WOLFTPM_API int TPM2_ParsePublic(TPM2B_PUBLIC* pub, byte* buf, word32 size, int* sizeUsed);
28062806
WOLFTPM_LOCAL int TPM2_GetName(TPM2_CTX* ctx, UINT32 handleValue, int handleCnt, int idx, TPM2B_NAME* name);
28072807

28082808
#ifdef WOLFTPM2_USE_WOLF_RNG

0 commit comments

Comments
 (0)