Skip to content

Commit d0618ad

Browse files
Merge pull request #388 from dgarske/various_20241206
Various cleanups
2 parents 4540ed9 + 87edf3d commit d0618ad

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

examples/keygen/keygen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,14 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
221221
}
222222

223223
if (endorseKey) {
224-
/* endorsement is always RSA */
224+
/* endorsement key (EK) */
225225
rc = wolfTPM2_CreateEK(&dev, &endorse, srkAlg);
226226
endorse.handle.policyAuth = 1; /* EK requires Policy auth, not Password */
227227
pubFilename = ekPubFile;
228228
primary = &endorse;
229229
}
230230
else {
231+
/* storage root key (SRK) */
231232
rc = getPrimaryStoragekey(&dev, &storage, srkAlg);
232233
pubFilename = srkPubFile;
233234
primary = &storage;

examples/keygen/keyload.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
138138
printf("Loading %s key\n", TPM2_GetAlgName(alg));
139139

140140
if (endorseKey) {
141-
/* endorsement is always RSA */
141+
/* endorsement key (EK) */
142142
rc = wolfTPM2_CreateEK(&dev, &endorse, srkAlg);
143143
if (rc != 0) goto exit;
144144
endorse.handle.policyAuth = 1;
145145
primary = &endorse;
146146
}
147147
else {
148+
/* storage root key (SRK) */
148149
rc = getPrimaryStoragekey(&dev, &storage, srkAlg);
149150
if (rc != 0) goto exit;
150151
primary = &storage;

src/tpm2_param_enc.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ static int TPM2_ParamEnc_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* keyIn,
299299
/* Perform AES CFB Encryption */
300300
rc = wc_AesInit(&enc, NULL, INVALID_DEVID);
301301
if (rc == 0) {
302-
rc = wc_AesSetKey(&enc, symKey, symKeySz, &symKey[symKeySz], AES_ENCRYPTION);
302+
rc = wc_AesSetKey(&enc, symKey, symKeySz, &symKey[symKeySz],
303+
AES_ENCRYPTION);
303304
if (rc == 0) {
304305
rc = wc_AesCfbEncrypt(&enc, paramData, paramData, paramSz);
305306
}
@@ -315,7 +316,7 @@ static int TPM2_ParamDec_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* keyIn,
315316
UINT32 paramSz)
316317
{
317318
int rc = TPM_RC_FAILURE;
318-
BYTE symKey[32 + 16]; /* AES key 128-bit + IV (block size) */
319+
BYTE symKey[32 + 16]; /* AES key 128-bit + IV (block size) */
319320
int symKeySz = session->symmetric.keyBits.aes / 8;
320321
const int symKeyIvSz = 16;
321322
Aes dec;
@@ -344,7 +345,8 @@ static int TPM2_ParamDec_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* keyIn,
344345
/* Perform AES CFB Decryption */
345346
rc = wc_AesInit(&dec, NULL, INVALID_DEVID);
346347
if (rc == 0) {
347-
rc = wc_AesSetKey(&dec, symKey, symKeySz, &symKey[symKeySz], AES_ENCRYPTION);
348+
rc = wc_AesSetKey(&dec, symKey, symKeySz, &symKey[symKeySz],
349+
AES_ENCRYPTION);
348350
if (rc == 0) {
349351
rc = wc_AesCfbDecrypt(&dec, paramData, paramData, paramSz);
350352
}
@@ -392,21 +394,21 @@ int TPM2_CalcCpHash(TPMI_ALG_HASH authHash, TPM_CC cmdCode,
392394
if (rc == 0 && name1 && name1->size > 0) {
393395
#ifdef WOLFTPM_DEBUG_VERBOSE
394396
printf("Name 0: %d\n", name1->size);
395-
TPM2_PrintBin(name1->name, name1->size);
397+
TPM2_PrintBin(name1->name, name1->size);
396398
#endif
397399
rc = wc_HashUpdate(&hash_ctx, hashType, name1->name, name1->size);
398400
}
399401
if (rc == 0 && name2 && name2->size > 0) {
400402
#ifdef WOLFTPM_DEBUG_VERBOSE
401403
printf("Name 1: %d\n", name2->size);
402-
TPM2_PrintBin(name2->name, name2->size);
404+
TPM2_PrintBin(name2->name, name2->size);
403405
#endif
404406
rc = wc_HashUpdate(&hash_ctx, hashType, name2->name, name2->size);
405407
}
406408
if (rc == 0 && name3 && name3->size > 0) {
407409
#ifdef WOLFTPM_DEBUG_VERBOSE
408410
printf("Name 2: %d\n", name3->size);
409-
TPM2_PrintBin(name3->name, name3->size);
411+
TPM2_PrintBin(name3->name, name3->size);
410412
#endif
411413
rc = wc_HashUpdate(&hash_ctx, hashType, name3->name, name3->size);
412414
}

src/tpm2_wrap.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,11 +2127,11 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21272127
int integritySz = 0;
21282128
int ivSz = 0;
21292129
int sensSz = 0;
2130-
BYTE* sensitiveData = NULL;
2131-
TPM2B_SYM_KEY symKey;
21322130
TPM2B_IV ivField;
21332131
TPM2_Packet packet;
21342132
#ifdef WOLFTPM2_PRIVATE_IMPORT
2133+
BYTE* sensitiveData = NULL;
2134+
TPM2B_SYM_KEY symKey;
21352135
TPM2B_DIGEST hmacKey;
21362136
Aes enc;
21372137
Hmac hmac_ctx;
@@ -2144,12 +2144,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21442144
/* if using a parent then use it's integrity algorithm */
21452145
if (parentKey != NULL) {
21462146
nameAlg = parentKey->pub.publicArea.nameAlg;
2147-
symKey.size = parentKey->handle.symmetric.keyBits.sym;
21482147
}
2149-
else {
2150-
symKey.size = sym->keyBits.sym;
2151-
}
2152-
21532148
digestSz = TPM2_GetHashDigestSize(nameAlg);
21542149
if (digestSz == 0) {
21552150
#ifdef DEBUG_WOLFTPM
@@ -2188,9 +2183,23 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21882183
TPM2_Packet_AppendSensitive(&packet, sens);
21892184
sensSz = packet.pos;
21902185
priv->size = integritySz + ivSz + sensSz;
2186+
sensSz = ivSz + sensSz;
21912187

2188+
#ifdef WOLFTPM2_PRIVATE_IMPORT
21922189
sensitiveData = &priv->buffer[integritySz];
2193-
sensSz = ivSz + sensSz;
2190+
if (parentKey != NULL) {
2191+
symKey.size = parentKey->handle.symmetric.keyBits.sym;
2192+
}
2193+
else {
2194+
symKey.size = sym->keyBits.sym;
2195+
}
2196+
/* convert from bit to byte and round up */
2197+
symKey.size = (symKey.size + 7) / 8;
2198+
/* check for invalid value */
2199+
if (symKey.size > sizeof(symKey.buffer)) {
2200+
return BUFFER_E;
2201+
}
2202+
#endif
21942203

21952204
if (innerWrap) {
21962205
/* TODO: Inner wrap support */
@@ -2199,7 +2208,6 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21992208
if (outerWrap) {
22002209
#ifdef WOLFTPM2_PRIVATE_IMPORT
22012210
/* Generate symmetric key for encryption of inner values */
2202-
symKey.size = (symKey.size + 7) / 8; /* convert to byte and round up */
22032211
rc = TPM2_KDFa(nameAlg, symSeed, "STORAGE", (TPM2B_NONCE*)name,
22042212
NULL, symKey.buffer, symKey.size);
22052213
if (rc != symKey.size) {
@@ -2213,7 +2221,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
22132221
rc = wc_AesInit(&enc, NULL, INVALID_DEVID);
22142222
if (rc == 0) {
22152223
rc = wc_AesSetKey(&enc, symKey.buffer, symKey.size,
2216-
ivField.size == 0 ? NULL : ivField.buffer, AES_ENCRYPTION);
2224+
ivField.buffer, AES_ENCRYPTION);
22172225
if (rc == 0) {
22182226
/* use inline encryption for both IV and sensitive */
22192227
rc = wc_AesCfbEncrypt(&enc, sensitiveData, sensitiveData,
@@ -2270,9 +2278,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
22702278
digestSz = TPM2_Packet_SwapU16(digestSz);
22712279
XMEMCPY(&priv->buffer[0], &digestSz, sizeof(word16));
22722280
#else
2273-
(void)sensitiveData;
22742281
(void)name;
2275-
(void)symKey;
22762282
(void)sensSz;
22772283
rc = NOT_COMPILED_IN;
22782284
#endif
@@ -5258,7 +5264,7 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
52585264
return BUFFER_E;
52595265
}
52605266

5261-
hashAlg = (keySz == 32) ? TPM_ALG_SHA256 : TPM_ALG_SHA1;
5267+
hashAlg = WOLFTPM2_WRAP_DIGEST;
52625268
hashAlgDigSz = TPM2_GetHashDigestSize(hashAlg);
52635269

52645270
/* Setup load command */

0 commit comments

Comments
 (0)