enclave_creator_sign.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2011-2017 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. /**
  32. * File:
  33. * enclave_creator_sign.cpp
  34. * Description:
  35. * Measure the necessary information of the enclave
  36. * to calculate the HASH value using SHA256 algorithm.
  37. */
  38. #include "enclave_creator.h"
  39. #include "sgx_eid.h"
  40. #include "enclave_creator_sign.h"
  41. #include "ipp_wrapper.h"
  42. #include "se_trace.h"
  43. #include "sgx_error.h"
  44. #include "util_st.h"
  45. #include "util.h"
  46. #include "se_page_attr.h"
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <assert.h>
  50. #define DATA_BLOCK_SIZE 64
  51. #define EID 0x44444444
  52. EnclaveCreatorST::EnclaveCreatorST()
  53. {
  54. m_hash_valid_flag = false;
  55. memset(m_enclave_hash, 0, SGX_HASH_SIZE);
  56. m_ctx = NULL;
  57. m_eid = EID;
  58. m_quota = 0;
  59. }
  60. EnclaveCreatorST::~EnclaveCreatorST()
  61. {
  62. SAFE_FREE_MM(m_ctx);
  63. }
  64. int EnclaveCreatorST::create_enclave(secs_t *secs, sgx_enclave_id_t *enclave_id, void **start_addr, bool ae)
  65. {
  66. if(!secs || !enclave_id || !start_addr)
  67. {
  68. se_trace(SE_TRACE_DEBUG, "ERROR: Bad pointer.\n");
  69. return SGX_ERROR_UNEXPECTED;
  70. }
  71. UNUSED(ae);
  72. memset(m_enclave_hash, 0, SGX_HASH_SIZE);
  73. int size_in_byte = 0;
  74. IppStatus error_code = ippsHashGetSize(&size_in_byte);
  75. if(error_code != ippStsNoErr)
  76. {
  77. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashGetSize() failed in the enclave measurement process.\n");
  78. return SGX_ERROR_UNEXPECTED;
  79. }
  80. m_ctx = (IppsHashState *)malloc(size_in_byte);
  81. if(m_ctx == NULL)
  82. {
  83. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  84. return SGX_ERROR_OUT_OF_MEMORY;
  85. }
  86. error_code = ippsHashInit(m_ctx, IPP_ALG_HASH_SHA256);
  87. if(error_code != ippStsNoErr)
  88. {
  89. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashInit() failed in the enclave measurement process.\n");
  90. return SGX_ERROR_UNEXPECTED;
  91. }
  92. uint8_t ecreat_val[SIZE_NAMED_VALUE] = "ECREATE";
  93. uint8_t data_block[DATA_BLOCK_SIZE];
  94. size_t offset = 0;
  95. memset(data_block, 0, DATA_BLOCK_SIZE);
  96. memcpy_s(data_block, sizeof(data_block), ecreat_val, SIZE_NAMED_VALUE);
  97. offset += SIZE_NAMED_VALUE;
  98. memcpy_s(&data_block[offset], sizeof(data_block)-offset, &secs->ssa_frame_size, sizeof(secs->ssa_frame_size));
  99. offset += sizeof(secs->ssa_frame_size);
  100. memcpy_s(&data_block[offset], sizeof(data_block)-offset, &secs->size, sizeof(secs->size));
  101. error_code = ippsHashUpdate((Ipp8u *)&data_block, DATA_BLOCK_SIZE, m_ctx);
  102. if(error_code != ippStsNoErr)
  103. {
  104. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashUpdate() failed in the enclave measurement(ECREATE) process.\n");
  105. return SGX_ERROR_UNEXPECTED;
  106. }
  107. *enclave_id = m_eid;
  108. *start_addr = secs->base;
  109. return SGX_SUCCESS;
  110. }
  111. int EnclaveCreatorST::add_enclave_page(sgx_enclave_id_t enclave_id, void *src, uint64_t offset, const sec_info_t &sinfo, uint32_t attr)
  112. {
  113. assert(m_ctx!=NULL);
  114. UNUSED(enclave_id);
  115. void* source = src;
  116. uint8_t color_page[SE_PAGE_SIZE];
  117. if(!source)
  118. {
  119. memset(color_page, 0, SE_PAGE_SIZE);
  120. source = reinterpret_cast<void*>(&color_page);
  121. }
  122. for(unsigned int i = 0; i< sizeof(sinfo.reserved)/sizeof(sinfo.reserved[0]); i++)
  123. {
  124. if(sinfo.reserved[i] != 0)
  125. return SGX_ERROR_UNEXPECTED;
  126. }
  127. /* sinfo.flags[64:16] should be 0 */
  128. if((sinfo.flags & (~SI_FLAGS_EXTERNAL)) != 0)
  129. {
  130. return SGX_ERROR_UNEXPECTED;
  131. }
  132. /* Check the page attributes: must be ADD only or ADD+EXTEND */
  133. if (!(attr & (ADD_PAGE_ONLY)) || (attr & (~(ADD_EXTEND_PAGE))))
  134. {
  135. return SGX_ERROR_UNEXPECTED;
  136. }
  137. uint64_t page_offset = (uint64_t)offset;
  138. uint8_t eadd_val[SIZE_NAMED_VALUE] = "EADD\0\0\0";
  139. uint8_t data_block[DATA_BLOCK_SIZE];
  140. size_t db_offset = 0;
  141. memset(data_block, 0, DATA_BLOCK_SIZE);
  142. memcpy_s(data_block, sizeof(data_block), eadd_val, SIZE_NAMED_VALUE);
  143. db_offset += SIZE_NAMED_VALUE;
  144. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &page_offset, sizeof(page_offset));
  145. db_offset += sizeof(page_offset);
  146. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &sinfo, sizeof(data_block)-db_offset);
  147. IppStatus error_code = ippsHashUpdate((Ipp8u *)&data_block, DATA_BLOCK_SIZE, m_ctx);
  148. if(error_code != ippStsNoErr)
  149. {
  150. se_trace(SE_TRACE_DEBUG, "ERROR::ippsHashUpdate() failed in the enclave measurement(EADD) process.\n");
  151. return SGX_ERROR_UNEXPECTED;
  152. }
  153. /* If the page need to eextend, do eextend. */
  154. if((attr & ADD_EXTEND_PAGE) == ADD_EXTEND_PAGE)
  155. {
  156. uint8_t *pdata = (uint8_t *)source;
  157. uint8_t eextend_val[SIZE_NAMED_VALUE] = "EEXTEND";
  158. #define EEXTEND_TIME 4
  159. for(int i = 0; i < SE_PAGE_SIZE; i += (DATA_BLOCK_SIZE * EEXTEND_TIME))
  160. {
  161. db_offset = 0;
  162. memset(data_block, 0, DATA_BLOCK_SIZE);
  163. memcpy_s(data_block, sizeof(data_block), eextend_val, SIZE_NAMED_VALUE);
  164. db_offset += SIZE_NAMED_VALUE;
  165. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &page_offset, sizeof(page_offset));
  166. error_code = ippsHashUpdate((Ipp8u *)&data_block, DATA_BLOCK_SIZE, m_ctx);
  167. if(error_code != ippStsNoErr)
  168. {
  169. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashUpdate() failed in the enclave measurement(EEXTEND) process.\n");
  170. return SGX_ERROR_UNEXPECTED;
  171. }
  172. for(int j = 0; j < EEXTEND_TIME; j++)
  173. {
  174. memcpy_s(data_block, sizeof(data_block), pdata, DATA_BLOCK_SIZE);
  175. error_code = ippsHashUpdate((Ipp8u *)&data_block, DATA_BLOCK_SIZE, m_ctx);
  176. if(error_code != ippStsNoErr)
  177. {
  178. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashUpdate() failed in the enclave measurement(EEXTEND) process.\n");
  179. return SGX_ERROR_UNEXPECTED;
  180. }
  181. pdata += DATA_BLOCK_SIZE;
  182. page_offset += DATA_BLOCK_SIZE;
  183. }
  184. }
  185. }
  186. m_quota += SE_PAGE_SIZE;
  187. return SGX_SUCCESS;
  188. }
  189. int EnclaveCreatorST::init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, SGXLaunchToken *lc, le_prd_css_file_t *prd_css_file)
  190. {
  191. assert(m_ctx != NULL);
  192. UNUSED(enclave_id), UNUSED(enclave_css), UNUSED(lc), UNUSED(prd_css_file);
  193. uint8_t temp_hash[SGX_HASH_SIZE];
  194. memset(temp_hash, 0, SGX_HASH_SIZE);
  195. /* Complete computation of the SHA256 digest and store the result into the hash. */
  196. IppStatus error_code = ippsHashFinal((Ipp8u *)temp_hash, m_ctx);
  197. if(error_code != ippStsNoErr)
  198. {
  199. se_trace(SE_TRACE_DEBUG, "ERROR:ippsHashFinal() failed in the enclave measurement process.\n");
  200. return SGX_ERROR_UNEXPECTED;
  201. }
  202. for (int i = 0; i< SGX_HASH_SIZE; i++)
  203. {
  204. m_enclave_hash[i] = temp_hash[i];
  205. }
  206. m_hash_valid_flag = true;
  207. return SGX_SUCCESS;
  208. }
  209. int EnclaveCreatorST::get_misc_attr(sgx_misc_attribute_t *sgx_misc_attr, metadata_t *metadata, SGXLaunchToken * const lc, uint32_t flag)
  210. {
  211. UNUSED(metadata), UNUSED(lc), UNUSED(flag);
  212. memset(sgx_misc_attr, 0, sizeof(sgx_misc_attribute_t));
  213. return SGX_SUCCESS;
  214. }
  215. int EnclaveCreatorST::destroy_enclave(sgx_enclave_id_t enclave_id, uint64_t enclave_size)
  216. {
  217. UNUSED(enclave_id);
  218. UNUSED(enclave_size);
  219. SAFE_FREE_MM(m_ctx);
  220. return SGX_SUCCESS;
  221. }
  222. bool EnclaveCreatorST::get_plat_cap(sgx_misc_attribute_t *se_attr)
  223. {
  224. UNUSED(se_attr);
  225. return false;
  226. }
  227. int EnclaveCreatorST::initialize(sgx_enclave_id_t enclave_id)
  228. {
  229. UNUSED(enclave_id);
  230. return SGX_SUCCESS;
  231. }
  232. bool EnclaveCreatorST::use_se_hw() const
  233. {
  234. return false;
  235. }
  236. int EnclaveCreatorST::get_enclave_info(uint8_t *hash, int size, uint64_t *quota)
  237. {
  238. if(hash == NULL || size != SGX_HASH_SIZE || m_hash_valid_flag == false)
  239. {
  240. se_trace(SE_TRACE_DEBUG, "ERROR: something went wrong in the function get_enclave_hash().\n");
  241. return SGX_ERROR_UNEXPECTED;
  242. }
  243. else
  244. {
  245. memcpy_s(hash, size, m_enclave_hash, SGX_HASH_SIZE);
  246. }
  247. *quota = m_quota;
  248. return SGX_SUCCESS;
  249. }
  250. static EnclaveCreatorST g_enclave_creator_st;
  251. EnclaveCreator* g_enclave_creator = &g_enclave_creator_st;