sys-encrypt-decrypt.int.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. We start by
  21. * creating a primary key, then a 128 bit AES key in CFB mode under it. We
  22. * then encrypt a well known string with this key, and then decrypt that same
  23. * string. The test is successful if the original string and the decrypted
  24. * string are the same.
  25. */
  26. int
  27. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  28. {
  29. TSS2_RC rc;
  30. TPM2_HANDLE handle_parent, handle;
  31. TPM2B_MAX_BUFFER data_in = { 0 };
  32. TPM2B_MAX_BUFFER data_encrypt = TPM2B_MAX_BUFFER_INIT;
  33. TPM2B_MAX_BUFFER data_decrypt = TPM2B_MAX_BUFFER_INIT;
  34. data_in.size = strlen (ENC_STR);
  35. strcpy ((char*)data_in.buffer, ENC_STR);
  36. rc = create_primary_rsa_2048_aes_128_cfb (sys_context, &handle_parent);
  37. if (rc != TSS2_RC_SUCCESS) {
  38. LOG_ERROR("Failed to create primary RSA 2048 key: 0x%" PRIx32 "",
  39. rc);
  40. exit(1);
  41. }
  42. rc = create_aes_128_cfb (sys_context, handle_parent, &handle);
  43. if (rc != TSS2_RC_SUCCESS) {
  44. LOG_ERROR("Failed to create child AES 128 key: 0x%" PRIx32 "", rc);
  45. exit(1);
  46. }
  47. LOG_INFO("Encrypting data: \"%s\" with key handle: 0x%08" PRIx32,
  48. data_in.buffer, handle);
  49. rc = tpm_encrypt_cfb (sys_context, handle, &data_in, &data_encrypt);
  50. if (rc == TPM2_RC_COMMAND_CODE) {
  51. LOG_WARNING("Encrypt/Decrypt 2 not supported by TPM");
  52. rc = Tss2_Sys_FlushContext(sys_context, handle_parent);
  53. if (rc != TSS2_RC_SUCCESS) {
  54. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  55. return 99; /* fatal error */
  56. }
  57. rc = Tss2_Sys_FlushContext(sys_context, handle);
  58. if (rc != TSS2_RC_SUCCESS) {
  59. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  60. return 99; /* fatal error */
  61. }
  62. return EXIT_SKIP;
  63. }
  64. if (rc != TSS2_RC_SUCCESS) {
  65. LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
  66. exit(1);
  67. }
  68. rc = tpm_decrypt_cfb (sys_context, handle, &data_encrypt, &data_decrypt);
  69. if (rc != TSS2_RC_SUCCESS) {
  70. LOG_ERROR("Failed to encrypt buffer: 0x%" PRIx32 "", rc);
  71. exit(1);
  72. }
  73. LOG_INFO("Decrypted data: \"%s\" with key handle: 0x%08" PRIx32,
  74. data_decrypt.buffer, handle);
  75. if (strcmp ((char*)data_in.buffer, (char*)data_decrypt.buffer)) {
  76. LOG_ERROR("Decrypt succeeded but decrypted data != to input data");
  77. exit(1);
  78. }
  79. rc = Tss2_Sys_FlushContext(sys_context, handle_parent);
  80. if (rc != TSS2_RC_SUCCESS) {
  81. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  82. return 99; /* fatal error */
  83. }
  84. rc = Tss2_Sys_FlushContext(sys_context, handle);
  85. if (rc != TSS2_RC_SUCCESS) {
  86. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  87. return 99; /* fatal error */
  88. }
  89. return 0;
  90. }