pcl_tSeal_internal.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /* Content from sdk/tseal/tSeal_internal.cpp */
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <sgx_tseal.h>
  35. #ifdef SE_SIM
  36. #include <deriv.h>
  37. #endif // #ifdef SE_SIM
  38. #include <pcl_common.h>
  39. #include <pcl_internal.h>
  40. #include <pcl_unseal_internal.h>
  41. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. // pcl_unseal_data_helper()
  43. // helper function for pcl_unseal_data, does the actual decryption and MAC compare
  44. // Parameters:
  45. // [IN] p_sealed_data - pointer to sealed key blob
  46. // [OUT] p_additional_MACtext - additional text to MAC
  47. // [IN] additional_MACtext_length - length of additional text
  48. // [OUT] p_decrypted_text - buffer to fill with decrypted data
  49. // [IN] decrypted_text_length - length of p_decrypted_text buffer
  50. // Return Value:
  51. // SGX_SUCCESS or error codes
  52. sgx_status_t pcl_unseal_data_helper(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext,
  53. uint32_t additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t decrypted_text_length)
  54. {
  55. // code based on tSeal_internal.cpp in tseal library
  56. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  57. sgx_key_128bit_t seal_key;
  58. pcl_memset(&seal_key, 0, sizeof(sgx_key_128bit_t));
  59. uint8_t payload_iv[SGX_SEAL_IV_SIZE];
  60. pcl_memset(&payload_iv, 0, SGX_SEAL_IV_SIZE);
  61. const uint8_t* p_aad = NULL;
  62. uint32_t res = 0;
  63. sgx_aes_gcm_128bit_tag_t mac;
  64. //uint8_t mac[16];
  65. if (decrypted_text_length > 0)
  66. pcl_memset(p_decrypted_text, 0, decrypted_text_length);
  67. if (additional_MACtext_length > 0)
  68. {
  69. // Verify GUID in sealed blob matches GUID in PCL table:
  70. p_aad = const_cast<uint8_t *>(&(p_sealed_data->aes_data.payload[decrypted_text_length]));
  71. if (pcl_consttime_memequal(p_aad, p_additional_MACtext, additional_MACtext_length) == 0)
  72. {
  73. return SGX_ERROR_PCL_GUID_MISMATCH;
  74. }
  75. pcl_memset(p_additional_MACtext, 0, additional_MACtext_length);
  76. }
  77. // Get the seal key
  78. err = pcl_sgx_get_key(&p_sealed_data->key_request, &seal_key);
  79. if (err != SGX_SUCCESS)
  80. {
  81. // Provide only error codes that the calling code could act on
  82. if ((err != SGX_ERROR_INVALID_CPUSVN) && (err != SGX_ERROR_INVALID_ISVSVN))
  83. err = SGX_ERROR_MAC_MISMATCH;
  84. goto out;
  85. }
  86. err = pcl_gcm_decrypt(
  87. (uint8_t*)p_decrypted_text,
  88. (uint8_t*)p_sealed_data->aes_data.payload,
  89. decrypted_text_length,
  90. (uint8_t*)p_aad,
  91. additional_MACtext_length,
  92. (uint8_t*)(&seal_key),
  93. (uint8_t*)(&payload_iv[0]),
  94. (uint8_t*)(p_sealed_data->aes_data.payload_tag));
  95. if (SGX_SUCCESS != err)
  96. {
  97. // Scrub p_decrypted_text
  98. pcl_volatile_memset((volatile void*)p_decrypted_text, 0, decrypted_text_length);
  99. goto out;
  100. }
  101. err = SGX_SUCCESS;
  102. out:
  103. // Scrub seal_key
  104. pcl_volatile_memset((volatile void*)(&seal_key), 0, sizeof(sgx_key_128bit_t));
  105. return err;
  106. }