tSeal_aad.cpp 8.5 KB

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