tSeal_aad.cpp 8.5 KB

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