enclave_creator_sign.cpp 9.4 KB

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