Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key generation example for Keyed Hash #245

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ Reading 198 bytes from keyblob.bin
Reading the private part of the key
Loaded key to 0x80000001

$ ./examples/keygen/keygen -keyedhash
TPM2.0 Key generation example
Key Blob: keyblob.bin
Algorithm: KEYEDHASH
Template: Default
Use Parameter Encryption: NULL
Loading SRK: Storage 0x81000200 (282 bytes)
Keyed Hash template
Creating new KEYEDHASH key...
TPM2_Create key: pub 48, priv 158
Public Area (size 48):
Type: KEYEDHASH (0x8), name: SHA256 (0xB), objAttr: 0x40460, authPolicy sz: 0
Keyed Hash: scheme: HMAC (0x5), scheme hash: SHA256 (0xB), unique size 32
TPM2_Load Key Handle 0x80000001
New key created and loaded (pub 48, priv 158 bytes)
Wrote 212 bytes to keyblob.bin
```

When filename is not supplied, a default filename "keyblob.bin" is used, therefore `keyload` and `keygen` can be used without additional parameters for quick TPM 2.0 key generation demonstration.
Expand Down
15 changes: 13 additions & 2 deletions examples/keygen/keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static void usage(void)
printf("* -ecc: Use ECC for asymmetric key generation \n");
printf("* -sym: Use Symmetric Cipher for key generation\n");
printf("\tDefault Symmetric Cipher is AES CTR with 256 bits\n");
printf("* -keyedhash: Use Keyed Hash for key generation\n");
printf("* -t: Use default template (otherwise AIK)\n");
printf("* -aes/xor: Use Parameter Encryption\n");
printf("* -unique=[value]\n");
Expand Down Expand Up @@ -178,6 +179,10 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
alg = TPM_ALG_SYMCIPHER;
bAIK = 0;
}
if (XSTRNCMP(argv[argc-1], "-keyedhash", 10) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XSTRNCMP unnecessary. argv guaranteed to be null-terminated strings. Use XSTRCMP.

alg = TPM_ALG_KEYEDHASH;
bAIK = 0;
}
if (XSTRNCMP(argv[argc-1], "-t", 2) == 0) {
bAIK = 0;
}
Expand Down Expand Up @@ -268,8 +273,9 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
printf("ECC AIK template\n");
rc = wolfTPM2_GetKeyTemplate_ECC_AIK(&publicTemplate);
}
else if (alg == TPM_ALG_SYMCIPHER) {
printf("AIK are expected to be RSA or ECC, not symmetric keys.\n");
else if (alg == TPM_ALG_SYMCIPHER || alg == TPM_ALG_KEYEDHASH) {
printf("AIK are expected to be RSA or ECC only, "
"not symmetric or keyedhash keys.\n");
rc = BAD_FUNC_ARG;
}
else {
Expand Down Expand Up @@ -300,6 +306,11 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_GetKeyTemplate_Symmetric(&publicTemplate, keyBits,
algSym, YES, YES);
}
else if (alg == TPM_ALG_KEYEDHASH) {
printf("Keyed Hash template\n");
rc = wolfTPM2_GetKeyTemplate_KeyedHash(&publicTemplate,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is indented one level too far.

TPM_ALG_SHA256, YES, NO);
}
else {
rc = BAD_FUNC_ARG;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tpm2_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4356,7 +4356,7 @@ int wolfTPM2_GetKeyTemplate_KeyedHash(TPMT_PUBLIC* publicTemplate,
publicTemplate->type = TPM_ALG_KEYEDHASH;
publicTemplate->nameAlg = WOLFTPM2_WRAP_DIGEST;
publicTemplate->objectAttributes = (
TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
TPMA_OBJECT_noDA |
(isSign ? TPMA_OBJECT_sign : 0) |
(isDecrypt ? TPMA_OBJECT_decrypt : 0));
Expand Down