t_pairing_blob.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_trts.h"
  32. #include "sgx_tseal.h"
  33. #include <string.h>
  34. #include "t_pairing_blob.h"
  35. ae_error_t UnsealPairingBlob(const pairing_blob_t* pPairingBlob, pairing_data_t* pPairingData)
  36. {
  37. ae_error_t status = PSE_PAIRING_BLOB_UNSEALING_ERROR;
  38. if (!pPairingBlob || !pPairingData)
  39. {
  40. return status;
  41. }
  42. do
  43. {
  44. // Verify that the pairing blob definition hasn't changed
  45. if (pPairingBlob->plaintext.seal_blob_type != PSE_SEAL_PAIRING_BLOB ||
  46. pPairingBlob->plaintext.pairing_blob_version != PSE_PAIRING_BLOB_VERSION)
  47. break;
  48. if (!sgx_is_within_enclave(pPairingData, sizeof(pairing_data_t)))
  49. break;
  50. memset_s(pPairingData, sizeof(*pPairingData), 0, sizeof(*pPairingData));
  51. uint32_t encrypted_data_len = sizeof(se_secret_pairing_data_t);
  52. uint32_t additional_MACtext_len = sizeof(se_plaintext_pairing_data_t);
  53. if (sgx_get_encrypt_txt_len((const sgx_sealed_data_t*)&pPairingBlob->sealed_pairing_data) != encrypted_data_len)
  54. break;
  55. if (sgx_get_add_mac_txt_len((const sgx_sealed_data_t*)&pPairingBlob->sealed_pairing_data) != additional_MACtext_len)
  56. break;
  57. sgx_status_t seStatus = sgx_unseal_data(
  58. (const sgx_sealed_data_t*)&pPairingBlob->sealed_pairing_data,
  59. (uint8_t*)&pPairingData->plaintext, &additional_MACtext_len,
  60. (uint8_t*)&pPairingData->secret_data, &encrypted_data_len);
  61. if ((SGX_SUCCESS != seStatus) ||
  62. (sizeof(se_secret_pairing_data_t) != encrypted_data_len) ||
  63. sizeof(se_plaintext_pairing_data_t) != additional_MACtext_len)
  64. {
  65. break;
  66. }
  67. status = AE_SUCCESS;
  68. } while (false);
  69. return status;
  70. }
  71. ae_error_t SealPairingBlob(pairing_data_t* pPairingData, pairing_blob_t* pPairingBlob)
  72. {
  73. const uint32_t encrypted_data_len = sizeof(se_secret_pairing_data_t);
  74. const uint32_t additional_MACtext_len = sizeof(se_plaintext_pairing_data_t);
  75. ae_error_t status = PSE_PAIRING_BLOB_SEALING_ERROR;
  76. if (!pPairingData || !pPairingBlob)
  77. {
  78. return status;
  79. }
  80. do
  81. {
  82. if (!sgx_is_within_enclave(pPairingData, sizeof(pairing_data_t)))
  83. break;
  84. memset_s(pPairingBlob, sizeof(*pPairingBlob), 0, sizeof(*pPairingBlob));
  85. pPairingData->plaintext.seal_blob_type = PSE_SEAL_PAIRING_BLOB;
  86. pPairingData->plaintext.pairing_blob_version = PSE_PAIRING_BLOB_VERSION;
  87. UINT32 sealed_data_size = sgx_calc_sealed_data_size(additional_MACtext_len, encrypted_data_len);
  88. sgx_status_t seStatus = sgx_seal_data(
  89. additional_MACtext_len, (uint8_t*)&pPairingData->plaintext,
  90. encrypted_data_len, (uint8_t*)&pPairingData->secret_data,
  91. sealed_data_size, (sgx_sealed_data_t*)&pPairingBlob->sealed_pairing_data);
  92. if (SGX_SUCCESS != seStatus)
  93. break;
  94. status = AE_SUCCESS;
  95. } while (false);
  96. return status;
  97. }