pcl_t_instructions.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/simulation/tinst/t_instructions.cpp */
  32. #include "t_instructions.h" /* for `g_global_data_sim' */
  33. #include "deriv.h"
  34. #include <sgx_tseal.h>
  35. #include "pcl_common.h"
  36. #include "pcl_internal.h"
  37. #include "pcl_unseal_internal.h"
  38. ////////////////////////////////////////////////////////////////////////
  39. // Simulation for EGETKEY
  40. ////////////////////////////////////////////////////////////////////////
  41. // The hard-coded OwnerEpoch.
  42. static const se_owner_epoch_t SIMU_OWNER_EPOCH_MSR = {
  43. 0x54, 0x48, 0x49, 0x53, 0x49, 0x53, 0x4f, 0x57,
  44. 0x4e, 0x45, 0x52, 0x45, 0x50, 0x4f, 0x43, 0x48,
  45. };
  46. static const sgx_cpu_svn_t PCL_DEFAULT_CPUSVN = {
  47. {
  48. 0x48, 0x20, 0xf3, 0x37, 0x6a, 0xe6, 0xb2, 0xf2,
  49. 0x03, 0x4d, 0x3b, 0x7a, 0x4b, 0x48, 0xa7, 0x78
  50. }
  51. };
  52. static const sgx_cpu_svn_t PCL_UPGRADED_CPUSVN = {
  53. {
  54. 0x53, 0x39, 0xae, 0x8c, 0x93, 0xae, 0x8f, 0x3c,
  55. 0xe4, 0x32, 0xdb, 0x92, 0x4d, 0x0f, 0x07, 0x33
  56. }
  57. };
  58. static const sgx_cpu_svn_t PCL_DOWNGRADED_CPUSVN = {
  59. {
  60. 0x64, 0xea, 0x4f, 0x3f, 0xa0, 0x03, 0x0c, 0x36,
  61. 0x38, 0x3c, 0x32, 0x2d, 0x4f, 0x3a, 0x8d, 0x4f
  62. }
  63. };
  64. egetkey_status_t pcl_check_cpu_svn(sgx_key_request_t* kr)
  65. {
  66. if(!pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_UPGRADED_CPUSVN, sizeof(PCL_UPGRADED_CPUSVN)) &&
  67. !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DEFAULT_CPUSVN, sizeof(PCL_DEFAULT_CPUSVN)) &&
  68. !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN))){
  69. return EGETKEY_INVALID_CPUSVN;
  70. }
  71. if ( (pcl_consttime_memequal(&g_global_data_sim.cpusvn_sim, (void*)&PCL_DEFAULT_CPUSVN, sizeof(PCL_DEFAULT_CPUSVN)) &&
  72. pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_UPGRADED_CPUSVN, sizeof(PCL_UPGRADED_CPUSVN))) ||
  73. (pcl_consttime_memequal(&g_global_data_sim.cpusvn_sim, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN)) &&
  74. !pcl_consttime_memequal(&kr->cpu_svn, (void*)&PCL_DOWNGRADED_CPUSVN, sizeof(PCL_DOWNGRADED_CPUSVN)))){
  75. return EGETKEY_INVALID_CPUSVN;
  76. }
  77. return EGETKEY_SUCCESS;
  78. }
  79. #define check_attr_flag(secs, flag) do { \
  80. if ((secs->attributes.flags & flag) == 0) { \
  81. return EGETKEY_INVALID_ATTRIBUTE; \
  82. } \
  83. } while(0)
  84. // The hardware EGETKEY instruction will set ZF on failure.
  85. //
  86. // In simulation mode, we can not guarentee that the ZF is always set
  87. // between _EGETKEY ending its life and tRTS testing ZF. Since there
  88. // are additional assembly code in between.
  89. //
  90. // In simulation mode, we check return code instead of ZF.
  91. // c.f. do_egetkey() in trts/linux/trts_pic.S
  92. # define GP() return ((egetkey_status_t)-1)
  93. #define GP_ON(cond) do { if (unlikely(cond)) GP(); } while (0)
  94. egetkey_status_t pcl_check_isv_svn(sgx_key_request_t* kr, secs_t* secs)
  95. {
  96. if (kr->isv_svn > secs->isv_svn) {
  97. return EGETKEY_INVALID_ISVSVN;
  98. }
  99. return EGETKEY_SUCCESS;
  100. }
  101. egetkey_status_t pcl_egetkey(sgx_key_request_t* kr, sgx_key_128bit_t okey)
  102. {
  103. // check alignment of KEYREQUEST
  104. GP_ON(((size_t)kr & (KEY_REQUEST_ALIGN_SIZE - 1)) != 0);
  105. // check to see if KEYREQEUST is inside the current enclave
  106. ////GP_ON(!sgx_is_within_enclave(kr, sizeof(sgx_key_request_t)));
  107. // check alignment of OUTPUTDATA
  108. GP_ON(((size_t)okey & (KEY_ALIGN_SIZE - 1)) != 0);
  109. // check to see if OUTPUTDATA is inside the current enclave
  110. ////GP_ON(!sgx_is_within_enclave(okey, sizeof(sgx_key_128bit_t)));
  111. // check reserved bits are not set
  112. GP_ON((kr->key_policy & ~(SGX_KEYPOLICY_MRENCLAVE | SGX_KEYPOLICY_MRSIGNER)) != 0);
  113. // check to see if reserved space in KEYREQUEST are valid
  114. const uint8_t* u8ptr = (uint8_t *)(&(kr->reserved1));
  115. for (unsigned i = 0; i < sizeof(kr->reserved1); ++i)
  116. GP_ON(u8ptr[i] != (uint8_t)0);
  117. u8ptr = (uint8_t *)(&(kr->reserved2));
  118. for (unsigned i = 0; i < sizeof(kr->reserved2); ++i)
  119. GP_ON(u8ptr[i] != (uint8_t)0);
  120. secs_t* cur_secs = g_global_data_sim.secs_ptr;
  121. sgx_attributes_t tmp_attr;
  122. derivation_data_t dd;
  123. pcl_memset(&dd, 0, sizeof(dd));
  124. dd.key_name = kr->key_name;
  125. // Determine which enclave attributes that must be included in the key.
  126. // Attributes that must always be included INIT & DEBUG.
  127. pcl_memset(&tmp_attr, 0, sizeof(tmp_attr));
  128. tmp_attr.flags = kr->attribute_mask.flags | SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG;
  129. tmp_attr.flags &= cur_secs->attributes.flags;
  130. tmp_attr.xfrm = kr->attribute_mask.xfrm & cur_secs->attributes.xfrm;
  131. // HW supports CPUSVN to be set as 0.
  132. // To be consistent with HW behaviour, we replace the cpusvn as DEFAULT_CPUSVN if the input cpusvn is 0.
  133. if(pcl_consttime_memequal(&kr->cpu_svn, &dd.ddpk.cpu_svn, sizeof(sgx_cpu_svn_t)))
  134. {
  135. pcl_memcpy(&kr->cpu_svn, (void*)&DEFAULT_CPUSVN, sizeof(sgx_cpu_svn_t));
  136. }
  137. // Must not use swich else relocations
  138. /* PCL UNUSED START *
  139. switch (kr->key_name) {
  140. case SGX_KEYSELECT_SEAL:
  141. /* PCL UNUSED END */
  142. egetkey_status_t esa = pcl_check_isv_svn(kr, cur_secs);
  143. if(EGETKEY_SUCCESS != esa)return esa;
  144. esa = pcl_check_cpu_svn(kr);
  145. if(EGETKEY_SUCCESS != esa)return esa;
  146. // assemble derivation data
  147. dd.size = sizeof(dd_seal_key_t);
  148. if (kr->key_policy & SGX_KEYPOLICY_MRENCLAVE) {
  149. pcl_memcpy(&dd.ddsk.mrenclave, &cur_secs->mr_enclave, sizeof(sgx_measurement_t));
  150. }
  151. if (kr->key_policy & SGX_KEYPOLICY_MRSIGNER) {
  152. pcl_memcpy(&dd.ddsk.mrsigner, (void*)&cur_secs->mr_signer, sizeof(sgx_measurement_t));
  153. }
  154. pcl_memcpy(&dd.ddsk.tmp_attr, &tmp_attr, sizeof(sgx_attributes_t));
  155. pcl_memcpy(&dd.ddsk.attribute_mask, &kr->attribute_mask, sizeof(sgx_attributes_t));
  156. pcl_memcpy(dd.ddsk.csr_owner_epoch, (void*)SIMU_OWNER_EPOCH_MSR, sizeof(se_owner_epoch_t));
  157. pcl_memcpy(&dd.ddsk.cpu_svn,&kr->cpu_svn,sizeof(sgx_cpu_svn_t));
  158. dd.ddsk.isv_svn = kr->isv_svn;
  159. dd.ddsk.isv_prod_id = cur_secs->isv_prod_id;
  160. pcl_memcpy(&dd.ddsk.key_id, &kr->key_id, sizeof(sgx_key_id_t));
  161. /* PCL UNUSED START *
  162. default:
  163. return EGETKEY_INVALID_KEYNAME;
  164. }
  165. /* PCL UNUSED END */
  166. pcl_derive_key(&dd, okey);
  167. return EGETKEY_SUCCESS;
  168. }