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 all commits
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
8 changes: 4 additions & 4 deletions examples/attestation/activate_credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
printf("Using default values\n");
}
else if (argc == 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
if (argv[1][0] != '-') {
input = argv[1];
}
if (XSTRNCMP(argv[1], "-eh", 3) == 0) {
if (XSTRCMP(argv[1], "-eh") == 0) {
printf("Use Endorsement Key\n");
endorseKey = 1;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/attestation/make_credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
printf("Using public key from SRK to create the challenge\n");
}
else if (argc == 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
if (XSTRNCMP(argv[1], "-eh", 3) == 0) {
if (XSTRCMP(argv[1], "-eh") == 0) {
printf("Using keys under the Endorsement Hierarchy\n");
endorseKey = 1;
}
Expand Down
10 changes: 5 additions & 5 deletions examples/bench/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,18 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
WOLFTPM2_SESSION tpmSession;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
paramEncAlg = TPM_ALG_CFB;
}
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
argc--;
Expand Down
8 changes: 4 additions & 4 deletions examples/csr/csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ int TPM2_CSR_ExampleArgs(void* userCtx, int argc, char *argv[])
printf("TPM2 CSR Example\n");

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-cert", 5) == 0) {
if (XSTRCMP(argv[argc-1], "-cert") == 0) {
makeSelfSignedCert = 1;
}
argc--;
Expand Down
6 changes: 3 additions & 3 deletions examples/gpio/gpio_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
#endif

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/gpio/gpio_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ int TPM2_GPIO_Read_Example(void* userCtx, int argc, char *argv[])
BYTE pinState;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions examples/gpio/gpio_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[])
BYTE pinState = 0x01;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
Expand All @@ -72,10 +72,10 @@ int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[])
}
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-high", 5) == 0) {
if (XSTRCMP(argv[argc-1], "-high") == 0) {
pinState = 0x01;
}
if (XSTRNCMP(argv[argc-1], "-low", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-low") == 0) {
pinState = 0x00;
}
argc--;
Expand Down
26 changes: 13 additions & 13 deletions examples/keygen/create_primary.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,42 @@ int TPM2_CreatePrimaryKey_Example(void* userCtx, int argc, char *argv[])
word32 persistHandle = 0;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-rsa", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-rsa") == 0) {
alg = TPM_ALG_RSA;
}
if (XSTRNCMP(argv[argc-1], "-ecc", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-ecc") == 0) {
alg = TPM_ALG_ECC;
}
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
paramEncAlg = TPM_ALG_CFB;
}
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
if (XSTRNCMP(argv[argc-1], "-eh", 3) == 0) {
if (XSTRCMP(argv[argc-1], "-eh") == 0) {
hierarchy = TPM_RH_ENDORSEMENT;
}
if (XSTRNCMP(argv[argc-1], "-ph", 3) == 0) {
if (XSTRCMP(argv[argc-1], "-ph") == 0) {
hierarchy = TPM_RH_PLATFORM;
}
if (XSTRNCMP(argv[argc-1], "-oh", 3) == 0) {
if (XSTRCMP(argv[argc-1], "-oh") == 0) {
hierarchy = TPM_RH_OWNER;
}
if (XSTRNCMP(argv[argc-1], "-unique=", 8) == 0) {
if (XSTRCMP(argv[argc-1], "-unique=") == 0) {
uniqueStr = argv[argc-1] + 8;
}
if (XSTRNCMP(argv[argc-1], "-auth=", 6) == 0) {
if (XSTRCMP(argv[argc-1], "-auth=") == 0) {
authStr = argv[argc-1] + 6;
}
if (XSTRNCMP(argv[argc-1], "-store=", 7) == 0) {
if (XSTRCMP(argv[argc-1], "-store=") == 0) {
persistHandle = (word32)strtol(argv[argc-1] + 7, NULL, 0);
if (persistHandle < 0x81000000 && persistHandle > 0x810FFFF) {
printf("Invalid storage handle %s\n", argv[argc-1] + 7);
Expand Down
47 changes: 29 additions & 18 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 @@ -83,20 +84,20 @@ static int symChoice(const char* arg, TPM_ALG_ID* algSym, int* keyBits,
if (len != SYM_EXTRA_OPTS_LEN) {
return TPM_RC_FAILURE;
}
if (XSTRNCMP(&arg[SYM_EXTRA_OPTS_POS+1], "aes", 3)) {
if (XSTRCMP(&arg[SYM_EXTRA_OPTS_POS+1], "aes")) {
return TPM_RC_FAILURE;
}

/* Copy string for user information later */
XMEMCPY(symMode, &arg[SYM_EXTRA_OPTS_POS+1], 6);

if (XSTRNCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "cfb", 3) == 0) {
if (XSTRCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "cfb") == 0) {
*algSym = TPM_ALG_CFB;
}
else if (XSTRNCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "ctr", 3) == 0) {
else if (XSTRCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "ctr") == 0) {
*algSym = TPM_ALG_CTR;
}
else if (XSTRNCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "cbc", 3) == 0) {
else if (XSTRCMP(&arg[SYM_EXTRA_OPTS_AES_MODE_POS], "cbc") == 0) {
*algSym = TPM_ALG_CBC;
}
else {
Expand Down Expand Up @@ -145,23 +146,23 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
char symMode[] = "aesctr";

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
if (argv[1][0] != '-')
outputFile = argv[1];
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-rsa", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-rsa") == 0) {
alg = TPM_ALG_RSA;
}
if (XSTRNCMP(argv[argc-1], "-ecc", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-ecc") == 0) {
alg = TPM_ALG_ECC;
}
if (XSTRNCMP(argv[argc-1], "-sym", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-sym") == 0) {
len = XSTRLEN(argv[argc-1]);
if (len >= SYM_EXTRA_OPTS_LEN) {
/* Did the user provide specific options? */
Expand All @@ -178,22 +179,26 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
alg = TPM_ALG_SYMCIPHER;
bAIK = 0;
}
if (XSTRNCMP(argv[argc-1], "-t", 2) == 0) {
if (XSTRCMP(argv[argc-1], "-keyedhash") == 0) {
alg = TPM_ALG_KEYEDHASH;
bAIK = 0;
}
if (XSTRNCMP(argv[argc-1], "-eh", 3) == 0) {
if (XSTRCMP(argv[argc-1], "-t") == 0) {
bAIK = 0;
}
if (XSTRCMP(argv[argc-1], "-eh") == 0) {
endorseKey = 1;
}
if (XSTRNCMP(argv[argc-1], "-pem", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-pem") == 0) {
pemFiles = 1;
}
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
paramEncAlg = TPM_ALG_CFB;
}
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
if (XSTRNCMP(argv[argc-1], "-unique=", 8) == 0) {
if (XSTRCMP(argv[argc-1], "-unique=") == 0) {
uniqueStr = argv[argc-1] + 8;
}

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,
TPM_ALG_SHA256, YES, NO);
}
else {
rc = BAD_FUNC_ARG;
}
Expand Down
12 changes: 6 additions & 6 deletions examples/keygen/keyimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
const char* outputFile = "keyblob.bin";

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}
Expand All @@ -68,13 +68,13 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
outputFile = argv[1];
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-ecc", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-ecc") == 0) {
alg = TPM_ALG_ECC;
}
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
paramEncAlg = TPM_ALG_CFB;
}
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
argc--;
Expand Down
14 changes: 7 additions & 7 deletions examples/keygen/keyload.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,28 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
int endorseKey = 0;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
XSTRNCMP(argv[1], "-h", 2) == 0 ||
XSTRNCMP(argv[1], "--help", 6) == 0) {
if (XSTRCMP(argv[1], "-?") == 0 ||
XSTRCMP(argv[1], "-h") == 0 ||
XSTRCMP(argv[1], "--help") == 0) {
usage();
return 0;
}

if (argv[1][0] != '-') {
inputFile = argv[1];
}
if (XSTRNCMP(argv[1], "-eh", 3) == 0) {
if (XSTRCMP(argv[1], "-eh") == 0) {
endorseKey = 1;
}
}
while (argc > 1) {
if (XSTRNCMP(argv[argc-1], "-aes", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-aes") == 0) {
paramEncAlg = TPM_ALG_CFB;
}
if (XSTRNCMP(argv[argc-1], "-xor", 4) == 0) {
if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
if (XSTRNCMP(argv[argc-1], "-persistent", 11) == 0) {
if (XSTRCMP(argv[argc-1], "-persistent") == 0) {
persistent = 1;
}
argc--;
Expand Down
Loading