sys-encrypt-decrypt-2.int.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <inttypes.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #define LOGMODULE test
  14. #include "util/log.h"
  15. #include "sys-util.h"
  16. #include "test-esys.h"
  17. #include "test.h"
  18. #define ENC_STR "test-data-test-data-test-data"
  19. /*
  20. * This test is intended to exercise the EncryptDecrypt2 command.
  21. */
  22. int
  23. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  24. {
  25. TSS2_RC rc;
  26. TPM2_HANDLE handle_parent, handle;
  27. TPM2B_MAX_BUFFER data_in = { 0 };
  28. TPM2B_MAX_BUFFER data_encrypted = TPM2B_MAX_BUFFER_INIT;
  29. TPM2B_MAX_BUFFER data_decrypted = TPM2B_MAX_BUFFER_INIT;
  30. data_in.size = strlen (ENC_STR);
  31. strcpy ((char*)data_in.buffer, ENC_STR);
  32. rc = create_primary_rsa_2048_aes_128_cfb (sys_context, &handle_parent);
  33. if (rc != TSS2_RC_SUCCESS) {
  34. LOG_ERROR("Failed to create primary RSA 2048 key: 0x%" PRIx32 "",
  35. rc);
  36. exit(1);
  37. }
  38. rc = create_aes_128_cfb (sys_context, handle_parent, &handle);
  39. if (rc != TSS2_RC_SUCCESS) {
  40. LOG_ERROR("Failed to create child AES 128 key: 0x%" PRIx32 "", rc);
  41. exit(1);
  42. }
  43. LOG_INFO("Encrypting data: \"%s\" with key handle: 0x%08" PRIx32,
  44. data_in.buffer, handle);
  45. rc = tpm_encrypt_2_cfb (sys_context, handle, &data_in, &data_encrypted);
  46. if (rc == TPM2_RC_COMMAND_CODE) {
  47. LOG_WARNING("Encrypt/Decrypt 2 not supported by TPM");
  48. rc = Tss2_Sys_FlushContext(sys_context, handle_parent);
  49. if (rc != TSS2_RC_SUCCESS) {
  50. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  51. return 99; /* fatal error */
  52. }
  53. rc = Tss2_Sys_FlushContext(sys_context, handle);
  54. if (rc != TSS2_RC_SUCCESS) {
  55. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  56. return 99; /* fatal error */
  57. }
  58. return EXIT_SKIP;
  59. }
  60. if (rc != TSS2_RC_SUCCESS) {
  61. LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
  62. exit(1);
  63. }
  64. rc = tpm_decrypt_2_cfb (sys_context, handle, &data_encrypted, &data_decrypted);
  65. if (rc != TSS2_RC_SUCCESS) {
  66. LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
  67. exit(1);
  68. }
  69. LOG_INFO("Decrypted data: \"%s\" with key handle: 0x%08" PRIx32,
  70. data_decrypted.buffer, handle);
  71. if (strcmp ((char*)data_in.buffer, (char*)data_decrypted.buffer)) {
  72. LOG_ERROR("Decrypt succeeded but decrypted data != to input data");
  73. exit(1);
  74. }
  75. rc = Tss2_Sys_FlushContext(sys_context, handle_parent);
  76. if (rc != TSS2_RC_SUCCESS) {
  77. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  78. return 99; /* fatal error */
  79. }
  80. rc = Tss2_Sys_FlushContext(sys_context, handle);
  81. if (rc != TSS2_RC_SUCCESS) {
  82. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  83. return 99; /* fatal error */
  84. }
  85. return 0;
  86. }