sys-param-encrypt-decrypt.int.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <inttypes.h>
  14. #include "tss2_sys.h"
  15. #include "context-util.h"
  16. #include "sys-util.h"
  17. #include "session-util.h"
  18. #define LOGMODULE test
  19. #include "util/log.h"
  20. #include "test.h"
  21. #define TEST_DATA "test data to encrypt"
  22. #define TEST_DATA_LEN 21
  23. int
  24. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  25. {
  26. TSS2_RC rc, rc2;
  27. SESSION *session;
  28. TSS2_TCTI_CONTEXT *tcti_ctx;
  29. TPM2B_MAX_NV_BUFFER data_to_write, data_read;
  30. TPM2B_MAX_BUFFER encrypted_param, decrypted_param;
  31. TPMI_RH_NV_INDEX nv_index = TPM2_HR_NV_INDEX | 0x01;
  32. size_t decrypt_param_size, encrypt_param_size;
  33. const uint8_t *decrypt_param_ptr, *encrypt_param_ptr;
  34. TPM2B_AUTH nv_auth;
  35. TPM2B_NV_PUBLIC nv_public;
  36. TPM2B_DIGEST policy_auth;
  37. TPMA_NV nv_attribs;
  38. TPM2B_NONCE nonce_caller;
  39. TPMT_SYM_DEF symmetric;
  40. TSS2L_SYS_AUTH_COMMAND req_auth = {
  41. .auths = {{ .sessionHandle = TPM2_RH_PW }},
  42. .count = 1
  43. };
  44. TSS2L_SYS_AUTH_RESPONSE resp_auth = {
  45. .count = 0
  46. };
  47. nv_attribs = TPMA_NV_AUTHREAD |
  48. TPMA_NV_AUTHWRITE |
  49. TPMA_NV_ORDERLY;
  50. nonce_caller.size = 0;
  51. policy_auth.size = 0;
  52. nv_auth.size = 0;
  53. LOG_INFO("param-encrypt-decrypt test");
  54. rc = Tss2_Sys_GetTctiContext(sys_context, &tcti_ctx);
  55. if (rc) {
  56. LOG_ERROR("Tss2_Sys_GetTctiContext failed 0x%" PRIx32, rc);
  57. return rc;
  58. }
  59. nv_public.size = 0;
  60. nv_public.nvPublic.attributes = nv_attribs;
  61. CopySizedByteBuffer((TPM2B *)&nv_public.nvPublic.authPolicy, (TPM2B *)&policy_auth);
  62. nv_public.nvPublic.dataSize = TEST_DATA_LEN;
  63. nv_public.nvPublic.nvIndex = nv_index;
  64. nv_public.nvPublic.nameAlg = TPM2_ALG_SHA256;
  65. rc = Tss2_Sys_NV_DefineSpace(sys_context, TPM2_RH_OWNER, &req_auth,
  66. &nv_auth, &nv_public, &resp_auth);
  67. if (rc) {
  68. LOG_ERROR("Tss2_Sys_NV_DefineSpace failed 0x%" PRIx32, rc);
  69. return rc;
  70. }
  71. symmetric.algorithm = TPM2_ALG_AES;
  72. symmetric.keyBits.aes = 128;
  73. symmetric.mode.aes = TPM2_ALG_CFB;
  74. retry:
  75. rc = create_auth_session(&session, TPM2_RH_NULL, 0,
  76. TPM2_RH_NULL, 0, &nonce_caller, 0, TPM2_SE_POLICY,
  77. &symmetric, TPM2_ALG_SHA256, tcti_ctx);
  78. if (rc) {
  79. LOG_ERROR("create_auth_session failed 0x%" PRIx32, rc);
  80. goto clean;
  81. }
  82. memcpy(data_to_write.buffer, TEST_DATA, TEST_DATA_LEN);
  83. data_to_write.size = TEST_DATA_LEN;
  84. rc = Tss2_Sys_NV_Write_Prepare(sys_context, nv_index, nv_index,
  85. &data_to_write, 0);
  86. if (rc) {
  87. LOG_ERROR("Tss2_Sys_NV_Write_Prepare failed 0x%" PRIx32, rc);
  88. goto clean;
  89. }
  90. req_auth.count = 2;
  91. /* Set up auth session structure */
  92. req_auth.auths[0].sessionHandle = TPM2_RH_PW;
  93. req_auth.auths[0].nonce.size = 0;
  94. req_auth.auths[0].sessionAttributes = 0;
  95. req_auth.auths[0].hmac.size = nv_auth.size;
  96. memcpy(req_auth.auths[0].hmac.buffer, nv_auth.buffer,
  97. req_auth.auths[0].hmac.size);
  98. /* Set up encrypt/decrypt session structure */
  99. req_auth.auths[1].sessionHandle = session->sessionHandle;
  100. req_auth.auths[1].nonce.size = 0;
  101. req_auth.auths[1].sessionAttributes = TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_DECRYPT;
  102. req_auth.auths[1].hmac.size = 0;
  103. rc = Tss2_Sys_SetCmdAuths(sys_context, &req_auth);
  104. if (rc) {
  105. LOG_ERROR("Tss2_Sys_SetCmdAuths failed 0x%" PRIx32, rc);
  106. goto clean;
  107. }
  108. rc = Tss2_Sys_GetDecryptParam(sys_context, &decrypt_param_size, &decrypt_param_ptr);
  109. if (rc) {
  110. LOG_ERROR("Tss2_Sys_GetDecryptParam failed 0x%" PRIx32, rc);
  111. goto clean;
  112. }
  113. if (decrypt_param_size != TEST_DATA_LEN) {
  114. rc = 99;
  115. LOG_ERROR("Invalid decrypt_param_size %d", (int)decrypt_param_size);
  116. goto clean;
  117. }
  118. roll_nonces(session, &req_auth.auths[1].nonce);
  119. rc = encrypt_command_param(session, &encrypted_param,
  120. (TPM2B_MAX_BUFFER *)&data_to_write, &nv_auth);
  121. if (rc) {
  122. LOG_ERROR("encrypt_command_param failed 0x%" PRIx32, rc);
  123. goto clean;
  124. }
  125. rc = Tss2_Sys_SetDecryptParam(sys_context, encrypted_param.size,
  126. encrypted_param.buffer);
  127. if (rc) {
  128. LOG_ERROR("Tss2_Sys_SetDecryptParam failed 0x%" PRIx32, rc);
  129. goto clean;
  130. }
  131. rc = Tss2_Sys_Execute(sys_context);
  132. if (rc) {
  133. if ((rc & 0x0000ffff) == TPM2_RC_RETRY) {
  134. LOG_INFO("Tss2_Sys_Execute returned retry 0x%" PRIx32, rc);
  135. Tss2_Sys_FlushContext(sys_context, session->sessionHandle);
  136. end_auth_session(session);
  137. goto retry;
  138. }
  139. LOG_ERROR("Tss2_Sys_Execute failed 0x%" PRIx32, rc);
  140. goto clean;
  141. }
  142. rc = Tss2_Sys_GetRspAuths(sys_context, &resp_auth);
  143. if (rc) {
  144. LOG_ERROR("Tss2_Sys_GetRspAuths failed 0x%" PRIx32, rc);
  145. goto clean;
  146. }
  147. /* Roll the nonces for response */
  148. roll_nonces(session, &resp_auth.auths[1].nonce);
  149. /* Roll the nonces for next command */
  150. roll_nonces(session, &req_auth.auths[1].nonce);
  151. req_auth.count = 1;
  152. rc = Tss2_Sys_NV_Read(sys_context, nv_index, nv_index, &req_auth,
  153. TEST_DATA_LEN, 0, &data_read, &resp_auth);
  154. if (rc) {
  155. LOG_ERROR("Tss2_Sys_NV_Read failed 0x%" PRIx32, rc);
  156. goto clean;
  157. }
  158. roll_nonces(session, &resp_auth.auths[1].nonce);
  159. if (memcmp(data_read.buffer, data_to_write.buffer, data_read.size)) {
  160. LOG_ERROR("Read data not equal to written data");
  161. LOGBLOB_ERROR(data_to_write.buffer, data_to_write.size, "written");
  162. LOGBLOB_ERROR(data_read.buffer, data_read.size, "read");
  163. rc = 99;
  164. goto clean;
  165. }
  166. rc = Tss2_Sys_NV_Read_Prepare(sys_context, nv_index, nv_index, TEST_DATA_LEN, 0);
  167. if (rc) {
  168. LOG_ERROR("Tss2_Sys_NV_Read_Prepare failed 0x%" PRIx32, rc);
  169. goto clean;
  170. }
  171. roll_nonces(session, &req_auth.auths[1].nonce);
  172. req_auth.count = 2;
  173. req_auth.auths[1].sessionAttributes &= ~TPMA_SESSION_DECRYPT;
  174. req_auth.auths[1].sessionAttributes |= TPMA_SESSION_ENCRYPT;
  175. req_auth.auths[1].sessionAttributes |= TPMA_SESSION_CONTINUESESSION;
  176. rc = Tss2_Sys_SetCmdAuths(sys_context, &req_auth);
  177. if (rc) {
  178. LOG_ERROR("Tss2_Sys_SetCmdAuths failed 0x%" PRIx32, rc);
  179. goto clean;
  180. }
  181. rc = Tss2_Sys_Execute(sys_context);
  182. if (rc) {
  183. LOG_ERROR("Tss2_Sys_Execute failed 0x%" PRIx32, rc);
  184. goto clean;
  185. }
  186. rc = Tss2_Sys_GetEncryptParam(sys_context, &encrypt_param_size, &encrypt_param_ptr);
  187. if (rc) {
  188. LOG_ERROR("Tss2_Sys_GetEncryptParam failed 0x%" PRIx32, rc);
  189. goto clean;
  190. }
  191. rc = Tss2_Sys_GetRspAuths(sys_context, &resp_auth);
  192. if (rc) {
  193. LOG_ERROR("Tss2_Sys_GetRspAuths failed 0x%" PRIx32, rc);
  194. goto clean;
  195. }
  196. roll_nonces(session, &resp_auth.auths[1].nonce);
  197. encrypted_param.size = encrypt_param_size;
  198. memcpy(encrypted_param.buffer, encrypt_param_ptr, encrypt_param_size);
  199. rc = decrypt_response_param(session, &decrypted_param,
  200. &encrypted_param, &nv_auth);
  201. if (rc) {
  202. LOG_ERROR("decrypt_response_param failed 0x%" PRIx32, rc);
  203. goto clean;
  204. }
  205. roll_nonces(session, &resp_auth.auths[1].nonce);
  206. rc = Tss2_Sys_SetEncryptParam(sys_context, decrypted_param.size,
  207. decrypted_param.buffer);
  208. if (rc) {
  209. LOG_ERROR("Tss2_Sys_SetEncryptParam failed 0x%" PRIx32, rc);
  210. goto clean;
  211. }
  212. rc = Tss2_Sys_NV_Read_Complete(sys_context, &data_read);
  213. if (rc) {
  214. LOG_ERROR("Tss2_Sys_NV_Read_Complete failed 0x%" PRIx32, rc);
  215. goto clean;
  216. }
  217. LOGBLOB_DEBUG(data_read.buffer, (UINT32)data_read.size, "Decrypted read data = ");
  218. if (memcmp(data_read.buffer, data_to_write.buffer, data_read.size)) {
  219. LOG_ERROR("Read data not equal to written data");
  220. rc = 99;
  221. goto clean;
  222. }
  223. rc = Tss2_Sys_FlushContext(sys_context, session->sessionHandle);
  224. if (rc)
  225. LOG_ERROR("Tss2_Sys_FlushContext failed 0x%" PRIx32, rc);
  226. end_auth_session(session);
  227. clean:
  228. req_auth.count = 1;
  229. req_auth.auths[0].sessionHandle = TPM2_RH_PW;
  230. rc2 = Tss2_Sys_NV_UndefineSpace(sys_context, TPM2_RH_OWNER,
  231. nv_index, &req_auth, 0);
  232. if (rc2)
  233. LOG_ERROR("Tss2_Sys_NV_UndefineSpace failed 0x%" PRIx32, rc);
  234. if (rc == 0)
  235. LOG_INFO("param-encrypt-decrypt test PASSED");
  236. return rc;
  237. }