helper.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: helper.cpp
  33. * Description: Cpp file to some helper function to extract some enclave information
  34. *
  35. * Wrap functions to get PPID, PWK, PSID, PSVN, PSK and seal/unseal function
  36. */
  37. #include "helper.h"
  38. #include "string.h"
  39. #include "sgx_error.h"
  40. #include "sgx_utils.h"
  41. #include "cipher.h"
  42. #include "sgx_trts.h"
  43. #include "sgx_tcrypto.h"
  44. #include <stdlib.h>
  45. #include "byte_order.h"
  46. //Function to get provisioning key using the provided PSVN
  47. //If the psvn is NULL, both CPUSVN and ISVSVN is set to 0 (used for PPID generation only)
  48. //Input: psvn, the psvn used to generate provisioning key
  49. //Output: key, the provisioning key to return
  50. // return PVEC_SUCCESS on success
  51. static pve_status_t get_provision_key(sgx_key_128bit_t *key, const psvn_t *psvn)
  52. {
  53. sgx_status_t se_ret = SGX_SUCCESS;
  54. sgx_key_request_t wrap_key_req;
  55. //memset here will also set cpusvn isvsvn to 0 for the case when psvn==NULL
  56. memset(&wrap_key_req, 0, sizeof(sgx_key_request_t));
  57. if(psvn==NULL){
  58. //keeping isv_svn and cpu_svn all 0 according to spec (this is for calcuation of PPID)
  59. }else{
  60. memcpy(&wrap_key_req.cpu_svn, &psvn->cpu_svn, sizeof(wrap_key_req.cpu_svn));
  61. memcpy(&wrap_key_req.isv_svn, &psvn->isv_svn, sizeof(wrap_key_req.isv_svn));
  62. }
  63. wrap_key_req.key_name = SGX_KEYSELECT_PROVISION; //provisioning key
  64. wrap_key_req.attribute_mask.xfrm = 0;
  65. wrap_key_req.misc_mask = 0xFFFFFFFF;
  66. wrap_key_req.attribute_mask.flags = ~SGX_FLAGS_MODE64BIT; //set all bits except the SGX_FLAGS_MODE64BIT
  67. se_ret = sgx_get_key(&wrap_key_req, key);
  68. if(SGX_SUCCESS != se_ret)
  69. {
  70. return sgx_error_to_pve_error(se_ret);
  71. }
  72. return PVEC_SUCCESS;
  73. }
  74. pve_status_t get_ppid(ppid_t* ppid)
  75. {
  76. sgx_key_128bit_t key_tmp;
  77. sgx_status_t sgx_status = SGX_SUCCESS;
  78. memset(&key_tmp, 0, sizeof(key_tmp));
  79. //get Provisioning Key with both CPUSVN and ISVSVN set to 0
  80. pve_status_t status = get_provision_key(&key_tmp, NULL);
  81. if(status != PVEC_SUCCESS){
  82. (void)memset_s(&key_tmp,sizeof(key_tmp), 0, sizeof(key_tmp));
  83. return status;
  84. }
  85. uint8_t content[16];
  86. memset(&content, 0, sizeof(content));
  87. //generate the mac as PPID
  88. static_assert(sizeof(sgx_cmac_128bit_key_t) == sizeof(sgx_key_128bit_t), "size of sgx_cmac_128bit_key_t and sgx_key_128bit_t should be same");
  89. static_assert(sizeof(sgx_cmac_128bit_tag_t) == sizeof(ppid_t), "size of sgx_cmac_128bit_tag_t and ppit_t should be same");
  90. if((sgx_status=sgx_rijndael128_cmac_msg(reinterpret_cast<const sgx_cmac_128bit_key_t *>(&key_tmp),
  91. content, sizeof(content), reinterpret_cast<sgx_cmac_128bit_tag_t *>(ppid)))!=SGX_SUCCESS){
  92. status = sgx_error_to_pve_error(sgx_status);
  93. }else{
  94. status = PVEC_SUCCESS;
  95. }
  96. (void)memset_s(&key_tmp,sizeof(key_tmp), 0, sizeof(key_tmp));//clear provisioning key in stack
  97. return status;
  98. }
  99. #define PROV_WRAP_2 "PROV_WRAP_2"
  100. #define PROV_WRAP_2_LEN 11
  101. #define START_OFF_PROV_WRAP_2 1
  102. #define START_OFF_NONCE_2 14
  103. #define OFF_BYTE_ZERO 30
  104. #define OFF_BYTE_0X80 31
  105. //Get Provisioning Wrap2 Key with respect to the PSVN
  106. pve_status_t get_pwk2(
  107. const psvn_t* psvn,
  108. const uint8_t n2[NONCE_2_SIZE],
  109. sgx_key_128bit_t* wrap_key)
  110. {
  111. if( psvn == NULL)
  112. return PVEC_PARAMETER_ERROR;
  113. uint8_t content[32];
  114. sgx_status_t sgx_status = SGX_SUCCESS;
  115. sgx_key_128bit_t key_tmp;
  116. pve_status_t status = PVEC_SUCCESS;
  117. memset(&key_tmp, 0, sizeof(key_tmp));
  118. status = get_provision_key(&key_tmp, psvn); //Generate Provisioning Key with respect to the psvn
  119. if(status != PVEC_SUCCESS)
  120. goto ret_point;
  121. memset(&content, 0, sizeof(content));
  122. content[0] = 0x01;
  123. memcpy(&content[START_OFF_PROV_WRAP_2], PROV_WRAP_2, PROV_WRAP_2_LEN); // byte 1-11 : "PROV_WRAP_2" (ascii encoded)
  124. memcpy(&content[START_OFF_NONCE_2], n2, NONCE_2_SIZE);
  125. content[OFF_BYTE_ZERO] = 0x00; //fill zero in byte offset 30
  126. content[OFF_BYTE_0X80] = 0x80; //fill 0x80 in byte offset 31
  127. //get the cmac of provision key as PWK2
  128. static_assert(sizeof(sgx_cmac_128bit_key_t)==sizeof(key_tmp), "size of sgx_cmac_128bit_key_t should be same as sgx_key_128bit_t");
  129. static_assert(sizeof(sgx_cmac_128bit_tag_t)==sizeof(sgx_key_128bit_t),"size of sgx_cmac_128bit_tag_t should be same as sgx_key_128bit_t");
  130. if((sgx_status = sgx_rijndael128_cmac_msg(reinterpret_cast<const sgx_cmac_128bit_key_t *>(&key_tmp),
  131. reinterpret_cast<const uint8_t *>(content), sizeof(content),
  132. reinterpret_cast<sgx_cmac_128bit_tag_t *>(wrap_key)))!=SGX_SUCCESS){
  133. status = sgx_error_to_pve_error(sgx_status);
  134. }else{
  135. status = PVEC_SUCCESS;
  136. }
  137. ret_point:
  138. (void)memset_s(&key_tmp,sizeof(key_tmp), 0 ,sizeof(key_tmp)); //clear provisioninig key in stack
  139. return status;
  140. }
  141. //Function to generate Provisioning Sealing Key given the psvn
  142. //The key is used to seal the private parameter f before sending to backend server
  143. pve_status_t get_pve_psk(
  144. const psvn_t* psvn,
  145. sgx_key_128bit_t* seal_key)
  146. {
  147. sgx_status_t se_ret = SGX_SUCCESS;
  148. sgx_key_request_t seal_key_req;
  149. if(psvn == NULL)
  150. return PVEC_PARAMETER_ERROR;
  151. memset(&seal_key_req, 0, sizeof(sgx_key_request_t));
  152. memcpy(&seal_key_req.cpu_svn, &psvn->cpu_svn, SGX_CPUSVN_SIZE);
  153. memcpy(&seal_key_req.isv_svn, &psvn->isv_svn, sizeof(psvn->isv_svn));
  154. seal_key_req.key_name = SGX_KEYSELECT_PROVISION_SEAL; //provisioning sealling key
  155. seal_key_req.attribute_mask.xfrm = 0;
  156. seal_key_req.attribute_mask.flags = ~SGX_FLAGS_MODE64BIT;
  157. se_ret = sgx_get_key(&seal_key_req, seal_key);
  158. if(SGX_SUCCESS != se_ret)
  159. {
  160. return sgx_error_to_pve_error(se_ret);
  161. }
  162. return PVEC_SUCCESS;
  163. }
  164. //simple wrapper for memcpy but checking type of parameter
  165. void pve_memcpy_out(external_memory_byte_t *dst, const void *src, uint32_t size)
  166. {
  167. memcpy(dst, src, size);
  168. }
  169. void pve_memcpy_in(void *dst, const external_memory_byte_t *src, uint32_t size)
  170. {
  171. memcpy(dst, src, size);
  172. }
  173. pve_status_t se_read_rand_error_to_pve_error(sgx_status_t error)
  174. {
  175. if(error == SGX_SUCCESS)return PVEC_SUCCESS;
  176. else if(error == SGX_ERROR_INVALID_PARAMETER) return PVEC_UNEXPECTED_ERROR;
  177. else return PVEC_READ_RAND_ERROR; //read rand hardware error
  178. }
  179. pve_status_t epid_error_to_pve_error(EpidStatus epid_result)
  180. {
  181. if(kEpidNoErr == epid_result)
  182. return PVEC_SUCCESS;
  183. switch(epid_result){
  184. case kEpidMemAllocErr:
  185. case kEpidNoMemErr:
  186. return PVEC_MALLOC_ERROR;
  187. case kEpidSigInvalid:
  188. return PVEC_INVALID_EPID_KEY;
  189. default:
  190. return PVEC_EPID_ERROR;
  191. }
  192. }
  193. pve_status_t sgx_error_to_pve_error(sgx_status_t status)
  194. {
  195. switch(status){
  196. case SGX_SUCCESS:
  197. return PVEC_SUCCESS;
  198. case SGX_ERROR_OUT_OF_MEMORY:
  199. return PVEC_MALLOC_ERROR;
  200. case SGX_ERROR_INVALID_CPUSVN:
  201. case SGX_ERROR_INVALID_ISVSVN:
  202. return PVEC_INVALID_CPU_ISV_SVN;
  203. default:
  204. return PVEC_SE_ERROR;
  205. }
  206. }