pcl_sgx_get_key.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* Content from sdk/selib/sgx_get_key.cpp */
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <sgx_tseal.h>
  35. #ifdef SE_SIM
  36. #include <deriv.h>
  37. #endif // #ifdef SE_SIM
  38. #include <pcl_common.h>
  39. #include <pcl_internal.h>
  40. #include <pcl_unseal_internal.h>
  41. PCL_COMPILE_TIME_ASSERT(KEY_ALIGN_SIZE == SGX_AESGCM_KEY_SIZE) ;
  42. PCL_COMPILE_TIME_ASSERT(sizeof(sgx_key_128bit_t) == SGX_AESGCM_KEY_SIZE) ;
  43. PCL_COMPILE_TIME_ASSERT(KEY_REQUEST_ALIGN_SIZE == KEY_REQUEST_SIZE);
  44. PCL_COMPILE_TIME_ASSERT(sizeof(sgx_key_request_t) == KEY_REQUEST_SIZE);
  45. uint8_t ip1_buf[PCL_EGETKEY_BUFFER_SIZE];
  46. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. // pcl_sgx_get_key()
  48. // Intel(R) SGX PCL version of sgx_get_key.
  49. // It is requried because the Intel(R) SGX SDK is unavailable at
  50. // this point
  51. // Parameters:
  52. // [IN] key_request - pointer to key_request structure
  53. // [OUT] key - resulting key
  54. // Return Value:
  55. // SGX_SUCCESS or error codes
  56. sgx_status_t pcl_sgx_get_key(IN const sgx_key_request_t *key_request, OUT sgx_key_128bit_t *key)
  57. {
  58. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  59. void *buffer = NULL;
  60. size_t size = 0, buf_ptr =0;
  61. sgx_key_request_t *tmp_key_request = NULL;
  62. sgx_key_128bit_t *tmp_key = NULL;
  63. egetkey_status_t egetkey_status = EGETKEY_SUCCESS;
  64. int i = 0;
  65. // check parameters
  66. //
  67. // key_request must be within the enclave
  68. if(!key_request || !pcl_is_within_enclave(key_request, sizeof(*key_request)))
  69. {
  70. err = SGX_ERROR_INVALID_PARAMETER;
  71. goto CLEANUP;
  72. }
  73. if (key_request->reserved1 != 0)
  74. {
  75. err = SGX_ERROR_INVALID_PARAMETER;
  76. goto CLEANUP;
  77. }
  78. for (i=0; i<SGX_KEY_REQUEST_RESERVED2_BYTES; ++i)
  79. {
  80. if (key_request->reserved2[i] != 0)
  81. {
  82. err = SGX_ERROR_INVALID_PARAMETER;
  83. goto CLEANUP;
  84. }
  85. }
  86. // key must be within the enclave
  87. if(!key || !pcl_is_within_enclave(key, sizeof(*key)))
  88. {
  89. err = SGX_ERROR_INVALID_PARAMETER;
  90. goto CLEANUP;
  91. }
  92. // check key_request->key_policy reserved bits
  93. if(key_request->key_policy & ~(SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER))
  94. {
  95. err = SGX_ERROR_INVALID_PARAMETER;
  96. goto CLEANUP;
  97. }
  98. // allocate memory
  99. //
  100. // To minimize the effort of memory management, the two elements allocation
  101. // are combined in a single malloc. The calculation for the required size has
  102. // an assumption, that
  103. // the elements should be allocated in descending order of the alignment size.
  104. //
  105. // If the alignment requirements are changed, the allocation order needs to
  106. // change accordingly.
  107. //
  108. // Current allocation order is:
  109. // key_request -> key
  110. //
  111. // key_request: 512-byte aligned, 512-byte length
  112. // key: 16-byte aligned, 16-byte length
  113. size = ROUND_TO(sizeof(*key_request), KEY_REQUEST_ALIGN_SIZE) + ROUND_TO(sizeof(*key), KEY_ALIGN_SIZE);
  114. size += MAX(KEY_REQUEST_ALIGN_SIZE, KEY_ALIGN_SIZE) - 1;
  115. // There is no heap during Intel(R) SGX PCL execution.
  116. // Must use global memory
  117. buffer = &ip1_buf[0];
  118. pcl_memset(buffer, 0, size);
  119. buf_ptr = reinterpret_cast<size_t>(buffer);
  120. buf_ptr = ROUND_TO(buf_ptr, KEY_REQUEST_ALIGN_SIZE);
  121. tmp_key_request = reinterpret_cast<sgx_key_request_t *>(buf_ptr);
  122. buf_ptr += sizeof(*tmp_key_request);
  123. buf_ptr = ROUND_TO(buf_ptr, KEY_ALIGN_SIZE);
  124. tmp_key = reinterpret_cast<sgx_key_128bit_t *>(buf_ptr);
  125. // Copy data from user buffer to the aligned memory
  126. pcl_memcpy(tmp_key_request, (void*)key_request, sizeof(*tmp_key_request));
  127. // Do EGETKEY
  128. #ifndef SE_SIM
  129. egetkey_status = (egetkey_status_t) do_egetkey(tmp_key_request, tmp_key);
  130. #else
  131. egetkey_status = (egetkey_status_t)pcl_egetkey(tmp_key_request,(uint8_t *)tmp_key);
  132. #endif
  133. switch(egetkey_status)
  134. {
  135. case EGETKEY_SUCCESS:
  136. err = SGX_SUCCESS;
  137. break;
  138. case EGETKEY_INVALID_ATTRIBUTE:
  139. err = SGX_ERROR_INVALID_ATTRIBUTE;
  140. break;
  141. case EGETKEY_INVALID_CPUSVN:
  142. err = SGX_ERROR_INVALID_CPUSVN;
  143. break;
  144. case EGETKEY_INVALID_ISVSVN:
  145. err = SGX_ERROR_INVALID_ISVSVN;
  146. break;
  147. case EGETKEY_INVALID_KEYNAME:
  148. err = SGX_ERROR_INVALID_KEYNAME;
  149. break;
  150. default:
  151. err = SGX_ERROR_UNEXPECTED;
  152. break;
  153. }
  154. CLEANUP:
  155. /*
  156. * - If success, copy resulting key into key buffer.
  157. * - In case of failure, leave the key buffer untouched becuase
  158. * Intel(R) SGX PCL does not call sgx_read_rand.
  159. */
  160. if((SGX_SUCCESS == err) && (NULL != key))
  161. {
  162. // Copy data to the user buffer
  163. pcl_memcpy(key, tmp_key, sizeof(*key));
  164. }
  165. // cleanup
  166. if(buffer)
  167. {
  168. pcl_volatile_memset((volatile void*)buffer, 0, size);
  169. }
  170. return err;
  171. }