esys-rsa-encrypt-decrypt.int.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 RSA encryption / decryption.
  16. * with password
  17. * authentication.
  18. * We create a RSA primary key (Esys_CreatePrimary) for every crypto action
  19. * This key will be used for encryption/decryption in with the schemes:
  20. * TPM2_ALG_NULL, TPM2_ALG_RSAES, and TPM2_ALG_OAEP
  21. *
  22. * Tested ESYS commands:
  23. * - Esys_CreatePrimary() (M)
  24. * - Esys_FlushContext() (M)
  25. * - Esys_RSA_Decrypt() (M)
  26. * - Esys_RSA_Encrypt() (M)
  27. *
  28. * @param[in,out] esys_context The ESYS_CONTEXT.
  29. * @retval EXIT_FAILURE
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_rsa_encrypt_decrypt(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR primaryHandle = ESYS_TR_NONE;
  37. TPM2B_PUBLIC *outPublic = NULL;
  38. TPM2B_CREATION_DATA *creationData = NULL;
  39. TPM2B_DIGEST *creationHash = NULL;
  40. TPMT_TK_CREATION *creationTicket = NULL;
  41. TPM2B_PUBLIC_KEY_RSA *cipher = NULL;
  42. TPM2B_PUBLIC_KEY_RSA *plain2 = NULL;
  43. TPM2B_DATA * null_data = NULL;
  44. TPM2B_AUTH authValuePrimary = {
  45. .size = 5,
  46. .buffer = {1, 2, 3, 4, 5}
  47. };
  48. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  49. .size = 0,
  50. .sensitive = {
  51. .userAuth = {
  52. .size = 0,
  53. .buffer = {0},
  54. },
  55. .data = {
  56. .size = 0,
  57. .buffer = {0},
  58. },
  59. },
  60. };
  61. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  62. TPM2B_PUBLIC inPublic = {
  63. .size = 0,
  64. .publicArea = {
  65. .type = TPM2_ALG_RSA,
  66. .nameAlg = TPM2_ALG_SHA256,
  67. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  68. TPMA_OBJECT_DECRYPT |
  69. TPMA_OBJECT_FIXEDTPM |
  70. TPMA_OBJECT_FIXEDPARENT |
  71. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  72. .authPolicy = {
  73. .size = 0,
  74. },
  75. .parameters.rsaDetail = {
  76. .symmetric = {
  77. .algorithm = TPM2_ALG_NULL},
  78. .scheme = { .scheme = TPM2_ALG_RSAES },
  79. .keyBits = 2048,
  80. .exponent = 0,
  81. },
  82. .unique.rsa = {
  83. .size = 0,
  84. .buffer = {},
  85. },
  86. },
  87. };
  88. LOG_INFO("\nRSA key will be created.");
  89. TPM2B_DATA outsideInfo = {
  90. .size = 0,
  91. .buffer = {},
  92. };
  93. TPML_PCR_SELECTION creationPCR = {
  94. .count = 0,
  95. };
  96. TPM2B_AUTH authValue = {
  97. .size = 0,
  98. .buffer = {}
  99. };
  100. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  101. goto_if_error(r, "Error: TR_SetAuth", error);
  102. RSRC_NODE_T *primaryHandle_node;
  103. for (int mode = 0; mode <= 2; mode++) {
  104. if (mode == 0) {
  105. inPublic.publicArea.parameters.rsaDetail.scheme.scheme =
  106. TPM2_ALG_NULL;
  107. } else if (mode == 1) {
  108. inPublic.publicArea.parameters.rsaDetail.scheme.scheme =
  109. TPM2_ALG_RSAES;
  110. } else if (mode == 2) {
  111. inPublic.publicArea.parameters.rsaDetail.scheme.scheme =
  112. TPM2_ALG_OAEP;
  113. inPublic.publicArea.parameters.rsaDetail.scheme.details.oaep.
  114. hashAlg = TPM2_ALG_SHA256;
  115. }
  116. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  117. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  118. &inPublic, &outsideInfo, &creationPCR,
  119. &primaryHandle, &outPublic, &creationData,
  120. &creationHash, &creationTicket);
  121. goto_if_error(r, "Error esys create primary", error);
  122. Esys_Free(outPublic);
  123. Esys_Free(creationData);
  124. Esys_Free(creationHash);
  125. Esys_Free(creationTicket);
  126. r = esys_GetResourceObject(esys_context, primaryHandle,
  127. &primaryHandle_node);
  128. goto_if_error(r, "Error Esys GetResourceObject", error);
  129. LOG_INFO("Created Primary with handle 0x%08x...",
  130. primaryHandle_node->rsrc.handle);
  131. r = Esys_TR_SetAuth(esys_context, primaryHandle,
  132. &authValuePrimary);
  133. goto_if_error(r, "Error: TR_SetAuth", error);
  134. size_t plain_size = 3;
  135. TPM2B_PUBLIC_KEY_RSA plain = {.size = plain_size,.buffer = {1, 2, 3}
  136. };
  137. TPMT_RSA_DECRYPT scheme;
  138. if (mode == 0) {
  139. scheme.scheme = TPM2_ALG_NULL;
  140. } else if (mode == 1) {
  141. scheme.scheme = TPM2_ALG_RSAES;
  142. } else if (mode == 2) {
  143. scheme.scheme = TPM2_ALG_OAEP;
  144. scheme.details.oaep.hashAlg = TPM2_ALG_SHA256;
  145. }
  146. r = Esys_RSA_Encrypt(esys_context, primaryHandle, ESYS_TR_NONE,
  147. ESYS_TR_NONE, ESYS_TR_NONE, &plain, &scheme,
  148. null_data, &cipher);
  149. goto_if_error(r, "Error esys rsa encrypt", error);
  150. Esys_Free(null_data);
  151. r = Esys_RSA_Decrypt(esys_context, primaryHandle,
  152. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  153. cipher, &scheme, null_data, &plain2);
  154. goto_if_error(r, "Error esys rsa decrypt", error);
  155. Esys_Free(null_data);
  156. Esys_Free(cipher);
  157. if (mode > 0 && memcmp(&plain.buffer[0], &plain2->buffer[0], plain_size)) {
  158. LOG_ERROR("plain texts are not equal for mode %i", mode);
  159. goto error;
  160. }
  161. r = Esys_FlushContext(esys_context, primaryHandle);
  162. goto_if_error(r, "Error: FlushContext", error);
  163. Esys_Free(plain2);
  164. }
  165. return EXIT_SUCCESS;
  166. error:
  167. if (primaryHandle != ESYS_TR_NONE) {
  168. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  169. LOG_ERROR("Cleanup primaryHandle failed.");
  170. }
  171. }
  172. Esys_Free(outPublic);
  173. Esys_Free(creationData);
  174. Esys_Free(creationHash);
  175. Esys_Free(creationTicket);
  176. Esys_Free(cipher);
  177. Esys_Free(plain2);
  178. Esys_Free(null_data);
  179. return EXIT_FAILURE;
  180. }
  181. int
  182. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  183. return test_esys_rsa_encrypt_decrypt(esys_context);
  184. }