esys-hmac.int.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. *******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include "tss2_esys.h"
  11. #include "esys_iutil.h"
  12. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This test is intended to test the ESYS command Esys_HMAC with password
  16. * authentication.
  17. *
  18. * We create a symmetric HMAC key signing key which will be used
  19. * for signing. This key will be used to create the HMAC for a test
  20. * buffer.
  21. *
  22. * Tested ESYS commands:
  23. * - Esys_CreatePrimary() (M)
  24. * - Esys_FlushContext() (M)
  25. * - Esys_HMAC() (O)
  26. *
  27. * @param[in,out] esys_context The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SUCCESS
  30. */
  31. int
  32. test_esys_hmac(ESYS_CONTEXT * esys_context)
  33. {
  34. TSS2_RC r;
  35. ESYS_TR primaryHandle = ESYS_TR_NONE;
  36. TPM2B_PUBLIC *outPublic = NULL;
  37. TPM2B_CREATION_DATA *creationData = NULL;
  38. TPM2B_DIGEST *creationHash = NULL;
  39. TPMT_TK_CREATION *creationTicket = NULL;
  40. TPM2B_DIGEST *outHMAC = NULL;
  41. TPM2B_AUTH authValuePrimary = {
  42. .size = 5,
  43. .buffer = {1, 2, 3, 4, 5}
  44. };
  45. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  46. .size = 0,
  47. .sensitive = {
  48. .userAuth = {
  49. .size = 0,
  50. .buffer = {0 },
  51. },
  52. .data = {
  53. .size = 0,
  54. .buffer = {0},
  55. },
  56. },
  57. };
  58. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  59. TPM2B_PUBLIC inPublic = { 0 };
  60. TPM2B_DATA outsideInfo = {
  61. .size = 0,
  62. .buffer = {},
  63. };
  64. TPML_PCR_SELECTION creationPCR = {
  65. .count = 0,
  66. };
  67. inPublic.publicArea.nameAlg = TPM2_ALG_SHA256;
  68. inPublic.publicArea.type = TPM2_ALG_KEYEDHASH;
  69. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SIGN_ENCRYPT;
  70. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  71. inPublic.publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
  72. inPublic.publicArea.parameters.keyedHashDetail.scheme.scheme = TPM2_ALG_HMAC;
  73. inPublic.publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg = TPM2_ALG_SHA256;
  74. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  75. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  76. &inPublic, &outsideInfo, &creationPCR,
  77. &primaryHandle, &outPublic, &creationData,
  78. &creationHash, &creationTicket);
  79. goto_if_error(r, "Error: CreatePrimary", error);
  80. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  81. goto_if_error(r, "Error: TR_SetAuth", error);
  82. TPM2B_MAX_BUFFER test_buffer = { .size = 20,
  83. .buffer={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
  84. 1, 2, 3, 4, 5, 6, 7, 8, 9}} ;
  85. r = Esys_HMAC(
  86. esys_context,
  87. primaryHandle,
  88. ESYS_TR_PASSWORD,
  89. ESYS_TR_NONE,
  90. ESYS_TR_NONE,
  91. &test_buffer,
  92. TPM2_ALG_SHA256,
  93. &outHMAC);
  94. goto_if_error(r, "Error: HMAC", error);
  95. r = Esys_FlushContext(esys_context, primaryHandle);
  96. goto_if_error(r, "Error: FlushContext", error);
  97. Esys_Free(outPublic);
  98. Esys_Free(creationData);
  99. Esys_Free(creationHash);
  100. Esys_Free(creationTicket);
  101. Esys_Free(outHMAC);
  102. return EXIT_SUCCESS;
  103. error:
  104. if (primaryHandle != ESYS_TR_NONE) {
  105. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  106. LOG_ERROR("Cleanup primaryHandle failed.");
  107. }
  108. }
  109. Esys_Free(outPublic);
  110. Esys_Free(creationData);
  111. Esys_Free(creationHash);
  112. Esys_Free(creationTicket);
  113. Esys_Free(outHMAC);
  114. return EXIT_FAILURE;
  115. }
  116. int
  117. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  118. return test_esys_hmac(esys_context);
  119. }