tSeal.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2011-2016 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. // tSeal.cpp - Trusted Sealing Routines
  32. #include "sgx_tseal.h"
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "sgx_trts.h"
  36. #include "sgx_report.h"
  37. #include "se_cdefs.h"
  38. #include "sgx_utils.h"
  39. #include "tSeal_internal.h"
  40. extern "C" sgx_status_t sgx_seal_data(const uint32_t additional_MACtext_length,
  41. const uint8_t *p_additional_MACtext, const uint32_t text2encrypt_length,
  42. const uint8_t *p_text2encrypt, const uint32_t sealed_data_size,
  43. sgx_sealed_data_t *p_sealed_data)
  44. {
  45. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  46. sgx_attributes_t attribute_mask;
  47. attribute_mask.flags = SGX_FLAGS_RESERVED | SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG;
  48. attribute_mask.xfrm = 0x0;
  49. err = sgx_seal_data_ex(SGX_KEYPOLICY_MRSIGNER, attribute_mask, TSEAL_DEFAULT_MISCMASK, additional_MACtext_length,
  50. p_additional_MACtext, text2encrypt_length, p_text2encrypt, sealed_data_size, p_sealed_data);
  51. return err;
  52. }
  53. extern "C" sgx_status_t sgx_seal_data_ex(const uint16_t key_policy,
  54. const sgx_attributes_t attribute_mask,
  55. const sgx_misc_select_t misc_mask,
  56. const uint32_t additional_MACtext_length,
  57. const uint8_t *p_additional_MACtext, const uint32_t text2encrypt_length,
  58. const uint8_t *p_text2encrypt, const uint32_t sealed_data_size,
  59. sgx_sealed_data_t *p_sealed_data)
  60. {
  61. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  62. sgx_report_t report;
  63. sgx_key_id_t keyID;
  64. sgx_key_request_t tmp_key_request;
  65. uint8_t payload_iv[SGX_SEAL_IV_SIZE];
  66. memset(&payload_iv, 0, sizeof(payload_iv));
  67. uint32_t sealedDataSize = sgx_calc_sealed_data_size(additional_MACtext_length,text2encrypt_length);
  68. // Check for overflow
  69. if (sealedDataSize == UINT32_MAX)
  70. {
  71. return SGX_ERROR_INVALID_PARAMETER;
  72. }
  73. //
  74. // Check parameters
  75. //
  76. // check key_request->key_policy reserved bits are not set and one of policy bits are set
  77. if ((key_policy & ~(SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER)) ||
  78. ((key_policy & (SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER)) == 0))
  79. {
  80. return SGX_ERROR_INVALID_PARAMETER;
  81. }
  82. if ( !(attribute_mask.flags & SGX_FLAGS_INITTED)
  83. || !(attribute_mask.flags & SGX_FLAGS_DEBUG) )
  84. {
  85. return SGX_ERROR_INVALID_PARAMETER;
  86. }
  87. if ((additional_MACtext_length > 0) && (p_additional_MACtext == NULL))
  88. {
  89. return SGX_ERROR_INVALID_PARAMETER;
  90. }
  91. if ((text2encrypt_length == 0) || (p_text2encrypt == NULL) || (!sgx_is_within_enclave(p_text2encrypt,text2encrypt_length)))
  92. {
  93. return SGX_ERROR_INVALID_PARAMETER;
  94. }
  95. // Ensure sealed data blob is within an enclave during the sealing process
  96. if ((p_sealed_data == NULL) || (!sgx_is_within_enclave(p_sealed_data,sealed_data_size)))
  97. {
  98. return SGX_ERROR_INVALID_PARAMETER;
  99. }
  100. // Ensure aad data does not cross enclave boundary
  101. if ((additional_MACtext_length > 0) &&
  102. (!(sgx_is_within_enclave(p_additional_MACtext,additional_MACtext_length) || sgx_is_outside_enclave(p_additional_MACtext, additional_MACtext_length))))
  103. {
  104. return SGX_ERROR_INVALID_PARAMETER;
  105. }
  106. if (sealedDataSize != sealed_data_size)
  107. {
  108. return SGX_ERROR_INVALID_PARAMETER;
  109. }
  110. memset(&report, 0, sizeof(sgx_report_t));
  111. memset(p_sealed_data, 0, sealedDataSize);
  112. memset(&keyID, 0, sizeof(sgx_key_id_t));
  113. memset(&tmp_key_request, 0, sizeof(sgx_key_request_t));
  114. // Get the report to obtain isv_svn and cpu_svn
  115. err = sgx_create_report(NULL, NULL, &report);
  116. if (err != SGX_SUCCESS)
  117. {
  118. goto clear_return;
  119. }
  120. // Get a random number to populate the key_id of the key_request
  121. err = sgx_read_rand(reinterpret_cast<uint8_t *>(&keyID), sizeof(sgx_key_id_t));
  122. if (err != SGX_SUCCESS)
  123. {
  124. goto clear_return;
  125. }
  126. memcpy(&(tmp_key_request.cpu_svn), &(report.body.cpu_svn), sizeof(sgx_cpu_svn_t));
  127. memcpy(&(tmp_key_request.isv_svn), &(report.body.isv_svn), sizeof(sgx_isv_svn_t));
  128. tmp_key_request.key_name = SGX_KEYSELECT_SEAL;
  129. tmp_key_request.key_policy = key_policy;
  130. tmp_key_request.attribute_mask.flags = attribute_mask.flags;
  131. tmp_key_request.attribute_mask.xfrm = attribute_mask.xfrm;
  132. memcpy(&(tmp_key_request.key_id), &keyID, sizeof(sgx_key_id_t));
  133. tmp_key_request.misc_mask = misc_mask;
  134. err = sgx_seal_data_iv(additional_MACtext_length, p_additional_MACtext,
  135. text2encrypt_length, p_text2encrypt, payload_iv, &tmp_key_request, p_sealed_data);
  136. if (err == SGX_SUCCESS)
  137. {
  138. // Copy data from the temporary key request buffer to the sealed data blob
  139. memcpy(&(p_sealed_data->key_request), &tmp_key_request, sizeof(sgx_key_request_t));
  140. }
  141. clear_return:
  142. // Clear temp state
  143. memset_s(&report, sizeof(sgx_report_t), 0, sizeof(sgx_report_t));
  144. memset_s(&keyID, sizeof(sgx_key_id_t), 0, sizeof(sgx_key_id_t));
  145. return err;
  146. }
  147. extern "C" sgx_status_t sgx_unseal_data(const sgx_sealed_data_t *p_sealed_data, uint8_t *p_additional_MACtext,
  148. uint32_t *p_additional_MACtext_length, uint8_t *p_decrypted_text, uint32_t *p_decrypted_text_length)
  149. {
  150. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  151. // Ensure the the sgx_sealed_data_t members are all inside enclave before using them.
  152. if ((p_sealed_data == NULL) || (!sgx_is_within_enclave(p_sealed_data,sizeof(sgx_sealed_data_t))))
  153. {
  154. return SGX_ERROR_INVALID_PARAMETER;
  155. }
  156. uint32_t encrypt_text_length = sgx_get_encrypt_txt_len(p_sealed_data);
  157. if(encrypt_text_length == UINT32_MAX)
  158. {
  159. return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted
  160. }
  161. uint32_t add_text_length = sgx_get_add_mac_txt_len(p_sealed_data);
  162. if(add_text_length == UINT32_MAX)
  163. {
  164. return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted
  165. }
  166. uint32_t sealedDataSize = sgx_calc_sealed_data_size(add_text_length,encrypt_text_length);
  167. if (sealedDataSize == UINT32_MAX)
  168. {
  169. return SGX_ERROR_MAC_MISMATCH; // Return error indicating the blob is corrupted
  170. }
  171. //
  172. // Check parameters
  173. //
  174. // Ensure sealed data blob is within an enclave during the sealing process
  175. if (!sgx_is_within_enclave(p_sealed_data,sealedDataSize))
  176. {
  177. return SGX_ERROR_INVALID_PARAMETER;
  178. }
  179. if ((add_text_length > 0) && ((p_additional_MACtext == NULL) || (p_additional_MACtext_length == NULL)))
  180. {
  181. return SGX_ERROR_INVALID_PARAMETER;
  182. }
  183. if ((encrypt_text_length < 1) || (p_decrypted_text == NULL) || (p_decrypted_text_length == NULL))
  184. {
  185. return SGX_ERROR_INVALID_PARAMETER;
  186. }
  187. if (!sgx_is_within_enclave(p_decrypted_text,encrypt_text_length))
  188. {
  189. return SGX_ERROR_INVALID_PARAMETER;
  190. }
  191. if (!sgx_is_within_enclave(p_decrypted_text_length,sizeof(p_decrypted_text_length)))
  192. {
  193. return SGX_ERROR_INVALID_PARAMETER;
  194. }
  195. // Ensure aad data does not cross enclave boundary
  196. if ((add_text_length > 0) &&
  197. (!(sgx_is_within_enclave(p_additional_MACtext,add_text_length) || sgx_is_outside_enclave(p_additional_MACtext, add_text_length))))
  198. {
  199. return SGX_ERROR_INVALID_PARAMETER;
  200. }
  201. if ((*p_decrypted_text_length) < encrypt_text_length)
  202. {
  203. return SGX_ERROR_INVALID_PARAMETER;
  204. }
  205. uint32_t additional_MACtext_length = (NULL != p_additional_MACtext_length) ? *p_additional_MACtext_length : 0;
  206. if (additional_MACtext_length < add_text_length) {
  207. return SGX_ERROR_INVALID_PARAMETER;
  208. }
  209. err = sgx_unseal_data_helper(p_sealed_data, p_additional_MACtext, add_text_length,
  210. p_decrypted_text, encrypt_text_length);
  211. if (err == SGX_SUCCESS)
  212. {
  213. *p_decrypted_text_length = encrypt_text_length;
  214. if(p_additional_MACtext_length != NULL)
  215. *p_additional_MACtext_length = add_text_length;
  216. }
  217. return err;
  218. }