provision_msg3.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #include "msg3_parm.h"
  32. #include "se_sig_rl.h"
  33. #include "cipher.h"
  34. #include "helper.h"
  35. #include "sgx_trts.h"
  36. #include "pve_qe_common.h"
  37. #include "pve_hardcoded_tlv_data.h"
  38. #include "sgx_utils.h"
  39. #include "byte_order.h"
  40. #include "ipp_wrapper.h"
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include "pek_pub_key.h"
  44. #include "util.h"
  45. /**
  46. * File: provision_msg3.cpp
  47. * Description: Provide the implementation of code to generate ProvMsg3
  48. *
  49. * Core Code for Provision Enclave
  50. * Piece-meal processing used to process SigRl of ProvMsg2 and generate EpidSignature of ProvMsg3
  51. */
  52. //initialize the epid signature header according to sigrl_header
  53. static pve_status_t gen_epid_signature_header(const SigRl *sigrl_header,
  54. EPIDMember *epid_member,
  55. const uint8_t *nonce_challenge,
  56. EpidSignature *epid_header)
  57. {
  58. if(NULL!=sigrl_header){
  59. memcpy(&epid_header->n2, &sigrl_header->n2, sizeof(sigrl_header->n2));//copy size into header in BigEndian
  60. memcpy(&epid_header->rl_ver, &sigrl_header->version, sizeof(sigrl_header->version)); //Copy rl_ver in BigEndian
  61. }else{
  62. memset(&epid_header->n2, 0, sizeof(epid_header->n2)); //set n2 and rl_ver to 0 if no sigrl provided
  63. memset(&epid_header->rl_ver, 0, sizeof(epid_header->rl_ver));
  64. }
  65. //challenge nonce value is used as sign message
  66. uint32_t msg_len = CHALLENGE_NONCE_SIZE;
  67. EpidStatus epid_ret = EpidSignBasic(epid_member,
  68. const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(nonce_challenge)),
  69. msg_len, NULL, 0, &epid_header->sigma0);//generate EpidSignature Header inside EPC memory
  70. if(kEpidNoErr != epid_ret){
  71. return epid_error_to_pve_error(epid_ret);
  72. }
  73. return PVEC_SUCCESS;
  74. }
  75. static uint32_t pve_htonl(uint32_t x)
  76. {
  77. uint32_t l0=x&0xFF;
  78. uint32_t l1=(x>>8)&0xFF;
  79. uint32_t l2=(x>>16)&0xFF;
  80. uint32_t l3=(x>>24)&0xFF;
  81. return l3|(l2<<8)|(l1<<16)|(l0<<24);
  82. }
  83. //This function will first generate EPIDSig Header according to sigrl_header
  84. //After that, piece meal algorithm is used to
  85. // decode SigRl Entry in msg2 and update hash value
  86. // generate EPIDSigEntry in msg3 and encrypt it
  87. // The memory of msg2 for SigRl and msg3 for EPIDSigEntry are all outside enclave
  88. // So that we need first copy each SigRl Entry into EPC memory, generate EPIDSigEntry inside EPC memory
  89. // and copy it out after it is generated
  90. // The function assumes the size of SigRl has been verfied and it is not checked again here.
  91. // Finally it checks whether the hash value is valid according to ECDHA Sign in the end of SigRl to verify data is not modified
  92. // A TLV Header for the EpidSignature should have been prepared in EPC memory signature_tlv_header
  93. //It is assumed that the parm->sigrl_count>0 when the function is called and the size of sigrl has been checked
  94. //EpidSignature TLV format: TLVHeader:EpidSignatureHeader:NrProof1:NrProof2:...:NrProofn
  95. static pve_status_t gen_msg3_signature(const proc_prov_msg2_blob_input_t *msg2_blob_input,
  96. prov_msg3_parm_t *parm,
  97. external_memory_byte_t *emp_signature,//pointer to external memory to write the EPID Signature
  98. uint32_t& signature_size)
  99. {
  100. pve_status_t ret = PVEC_SUCCESS;
  101. uint32_t cur_size = static_cast<uint32_t>(EPID_SIGNATURE_TLV_HEADER_SIZE+sizeof(EpidSignature)-sizeof(NrProof));
  102. //emp_proof_entry is pointer to external memory to each entry of the epid signature body in external memory
  103. external_memory_byte_t *emp_proof_entry = emp_signature + cur_size;
  104. //emp_sigrl_entry is pointer to external memory to each entry of the sigrl_body in external memory
  105. const external_memory_byte_t *emp_sigrl_entry = parm->emp_sigrl_sig_entries;
  106. uint32_t i,entry_count = parm->sigrl_count;
  107. bool revoked = false;
  108. uint8_t sigrl_sign[2*ECDSA_SIGN_SIZE];//temp buffer in EPC to hold ECDSA signature
  109. //declare a buffer to hold encrypted data of TLV Header and EpidSignature Header
  110. uint8_t signature_header_to_encrypt[EPID_SIGNATURE_TLV_HEADER_SIZE + sizeof(EpidSignature)-sizeof(NrProof)];
  111. SigRlEntry temp1;
  112. NrProof temp3;
  113. uint32_t tlv_payload_size = 0;
  114. const SigRl *sigrl_header = NULL;
  115. sgx_status_t sgx_status = SGX_SUCCESS;
  116. memset(sigrl_sign, 0, sizeof(sigrl_sign));
  117. memset(&temp1, 0, sizeof(temp1));
  118. memset(&temp3, 0, sizeof(temp3));
  119. memset(signature_header_to_encrypt, 0, sizeof(signature_header_to_encrypt));
  120. if(entry_count>0){
  121. sigrl_header = &parm->sigrl_header.sig_rl;//use the sigrl_header only when sigrl is available
  122. if(signature_size < cur_size){//size of output buffer at least to hold currently generated data
  123. ret = PVEC_INSUFFICIENT_MEMORY_ERROR;
  124. goto ret_point;
  125. }
  126. if((signature_size-cur_size)/entry_count<sizeof(NrProof)){//safe way to check buffer overflow of output buffer to avoid integer overflow
  127. ret = PVEC_INSUFFICIENT_MEMORY_ERROR;
  128. goto ret_point;
  129. }
  130. tlv_payload_size = static_cast<uint32_t>(sizeof(EpidSignature)-sizeof(NrProof) + entry_count * sizeof(NrProof));
  131. }else{
  132. tlv_payload_size = static_cast<uint32_t>(sizeof(EpidSignature)-sizeof(NrProof)); //payload size for 0 entry, only basic signature with n2 and rl_ver to be 0
  133. if(signature_size < cur_size){//size of output buffer at least to hold currently generated data
  134. ret = PVEC_INSUFFICIENT_MEMORY_ERROR;
  135. goto ret_point;
  136. }
  137. }
  138. memcpy(signature_header_to_encrypt, EPID_SIGNATURE_TLV_HEADER, EPID_SIGNATURE_TLV_HEADER_SIZE); //copy in the hard coded EPID Signature TLV Header
  139. tlv_payload_size = pve_htonl(tlv_payload_size);
  140. //overwritten the bigendian size in TLV Header. It is assumed that the size in TLV Header is always 4 bytes//Long format
  141. memcpy(signature_header_to_encrypt+EPID_SIGNATURE_TLV_SIZE_OFFSET, &tlv_payload_size, sizeof(tlv_payload_size));
  142. ret = gen_epid_signature_header(sigrl_header, parm->epid_member, msg2_blob_input->challenge_nonce, &parm->signature_header);//Now generate EpidSignatureHeader
  143. if( PVEC_SUCCESS != ret )
  144. goto ret_point;
  145. //Now encrypt the TLV Header and signature header including basic signature while the parm->signature_header is kept since piece-meal processing will use it
  146. memcpy(signature_header_to_encrypt+EPID_SIGNATURE_TLV_HEADER_SIZE, &parm->signature_header, cur_size-EPID_SIGNATURE_TLV_HEADER_SIZE);
  147. ret =pve_aes_gcm_encrypt_inplace_update(parm->p_msg3_state, signature_header_to_encrypt, cur_size);
  148. if( PVEC_SUCCESS != ret )
  149. goto ret_point;
  150. pve_memcpy_out(emp_signature, signature_header_to_encrypt, cur_size);//copy out tlv header, basic signature and other epid signature header info if required
  151. if(NULL==parm->emp_sigrl_sig_entries){//finish if no sigrl avaiable
  152. signature_size = cur_size;
  153. goto ret_point;
  154. }
  155. //copy the ECDSA Signature of the SigRl in ProvMsg2 into EPC memory in advance to defense in depth
  156. pve_memcpy_in(sigrl_sign, emp_sigrl_entry + entry_count *sizeof(SigRlEntry), 2*ECDSA_SIGN_SIZE);
  157. //piece-meal processing
  158. //The pointer calculation will never overflow as soon as size of sigrl and epid signature have been checked in advance
  159. //TO BE CLARIFY:We assume that the ecdsa signature follows entry array of SigRl directly
  160. // If later we change the format of sigrl to include extra data which should be ecdsa signed too,
  161. // we need do the modification here: change the sigrl_sign and do more sha update
  162. signature_size = static_cast<uint32_t>(cur_size+entry_count *sizeof(NrProof));//recalculate output buffer
  163. //Start piece meal processing for each entry
  164. for(i=0;i<entry_count; i++){
  165. pve_memcpy_in(&temp1, emp_sigrl_entry, sizeof(temp1));//copy the data into trusted memory
  166. //update hash for the SigRl Entry
  167. sgx_status = sgx_sha256_update(reinterpret_cast<uint8_t *>(&temp1), sizeof(SigRlEntry), parm->sha_state);
  168. if(sgx_status != SGX_SUCCESS){
  169. ret = sgx_error_to_pve_error(sgx_status);
  170. goto ret_point;
  171. }
  172. //generate NrProof for the SigRl Entry in trusted memory
  173. EpidStatus epid_ret = EpidNrProve(parm->epid_member,
  174. const_cast<uint8_t *>(msg2_blob_input->challenge_nonce),//msg to sign
  175. CHALLENGE_NONCE_SIZE,
  176. &parm->signature_header.sigma0, //B and K in BasicSignature
  177. &temp1, //B and K in sigrl entry
  178. &temp3); //output one NrProof
  179. if(kEpidNoErr != epid_ret){
  180. if(kEpidSigRevokedInSigRl == epid_ret){
  181. revoked = true;//if revoked, we could not return revoked status immediately until integrity checking passed
  182. }else{
  183. ret = epid_error_to_pve_error(epid_ret);
  184. goto ret_point;
  185. }
  186. }
  187. //encrypt the NrProof in EPC
  188. ret = pve_aes_gcm_encrypt_inplace_update(parm->p_msg3_state, reinterpret_cast<uint8_t *>(&temp3), sizeof(temp3));
  189. if(ret != PVEC_SUCCESS){
  190. goto ret_point;
  191. }
  192. pve_memcpy_out(emp_proof_entry, &temp3, sizeof(temp3));//copy encrypted NrProof out of enclave
  193. emp_sigrl_entry += sizeof(SigRlEntry);//pointer to next SigRlEntry in external memory
  194. emp_proof_entry += sizeof(NrProof);//pointer to next NrProof in external memory
  195. }
  196. se_ae_ecdsa_hash_t out;
  197. //generate SHA256 hash value of the whole SigRl
  198. if((sgx_status=sgx_sha256_get_hash(parm->sha_state,
  199. reinterpret_cast<sgx_sha256_hash_t *>(&out))) !=
  200. SGX_SUCCESS){
  201. ret = sgx_error_to_pve_error(sgx_status);
  202. goto ret_point;
  203. }
  204. //Verify the signature is signed by EPIDSK
  205. ret = verify_epid_ecdsa_signature(sigrl_sign, parm->local_xegb, &out);
  206. if(ret == PVEC_MSG_ERROR){
  207. ret = PVEC_SIGRL_INTEGRITY_CHECK_ERROR;//If sigrl signature checking failed, someone must has modified the message
  208. }
  209. ret_point:
  210. //clear unsealed NrProof to defense in depth for potential attack to match attacker created sigrl entry with key
  211. //While we need not clear BasicSignature
  212. (void)memset_s(&temp3, sizeof(temp3), 0, sizeof(temp3));
  213. if(ret == PVEC_SUCCESS &&revoked){
  214. ret = PVEC_REVOKED_ERROR;
  215. }
  216. return ret;
  217. }
  218. //The function will try to do some preparation for piece meal encryption of field1 in ProvMsg3
  219. // It prepares the encryption state in msg3
  220. //@parm: structure to provide some input data to generate ProvMsg3 and also some states for piece meal processing
  221. //@return PVEC_SUCCESS on success and error code if failed
  222. static pve_status_t proc_msg3_state_init(prov_msg3_parm_t *parm, const sgx_key_128bit_t *pwk2)
  223. {
  224. pve_status_t ret = PVEC_SUCCESS;
  225. sgx_status_t se_ret = SGX_SUCCESS;
  226. if((se_ret=sgx_read_rand(parm->iv, IV_SIZE))!=SGX_SUCCESS){//randomly generate the IV
  227. ret = se_read_rand_error_to_pve_error(se_ret);
  228. goto ret_point;
  229. }
  230. se_static_assert(SK_SIZE==sizeof(sgx_cmac_128bit_tag_t)); /*size of sgx_cmac_128bit_tag_t should same as value of SK_SIZE*/
  231. //initialize state for piece-meal encryption of field of ProvMsg3
  232. ret = pve_aes_gcm_encrypt_init((const uint8_t *)pwk2, parm->iv, IV_SIZE,//pwk2 as the key
  233. NULL, 0,//no AAD used for the encryption of EpidSignature
  234. &parm->p_msg3_state, &parm->msg3_state_size);
  235. ret_point:
  236. return ret;
  237. }
  238. //Function to generate Field1_0 of ProvMsg3
  239. //@msg2_blob_input, input decoded ProvMsg2 info
  240. //@join_proof, output the join proof and the escrow data which is encrypted f of Private Key
  241. //@return PVEC_SUCCESS on success and error code on failure
  242. //The function assume all required inputs have been prepared in msg2_blob_input
  243. static pve_status_t gen_msg3_join_proof_escrow_data(const proc_prov_msg2_blob_input_t *msg2_blob_input,
  244. join_proof_with_escrow_t& join_proof)
  245. {
  246. pve_status_t ret = PVEC_SUCCESS;
  247. BitSupplier epid_prng = (BitSupplier) epid_random_func;
  248. FpElemStr temp_f;
  249. //first generate private key f randomly before sealing it by PSK
  250. FpElemStr *f = &temp_f;
  251. sgx_status_t sgx_status = SGX_SUCCESS;
  252. JoinRequest *join_r = &join_proof.jr;
  253. EpidStatus epid_ret = kEpidNoErr;
  254. psvn_t psvn;
  255. memset(&temp_f, 0, sizeof(temp_f));
  256. //randomly generate the private EPID key f, host to network transformation not required since server will not decode it
  257. if(PVEC_SUCCESS != (ret=gen_epid_priv_f(f))){
  258. goto ret_point;
  259. }
  260. //generate JoinP using f before encryption by calling EPID library
  261. memset(join_r, 0, sizeof(JoinRequest));//first clear to 0
  262. //generate JoinP to fill it in field1_0_0 by EPID library
  263. epid_ret = EpidRequestJoin(
  264. &msg2_blob_input->group_cert.key, //EPID Group Cert from ProvMsgs2 used
  265. reinterpret_cast<const IssuerNonce *>(msg2_blob_input->challenge_nonce),
  266. f, epid_prng,
  267. NULL, kSha256, join_r);
  268. if(kEpidNoErr != epid_ret){
  269. ret = epid_error_to_pve_error(epid_ret);
  270. goto ret_point;
  271. }
  272. //get PSK
  273. sgx_key_128bit_t psk;
  274. memcpy(&psvn.cpu_svn, &msg2_blob_input->equiv_pi.cpu_svn, sizeof(psvn.cpu_svn));
  275. memcpy(&psvn.isv_svn, &msg2_blob_input->equiv_pi.pve_svn, sizeof(psvn.isv_svn));
  276. ret = get_pve_psk(&psvn, &psk);
  277. if(PVEC_SUCCESS != ret){
  278. goto ret_point;
  279. }
  280. join_proof.escrow.version = 0;//version 0 used for escrow data
  281. //now we could seal f by PSK
  282. ret = se_read_rand_error_to_pve_error(sgx_read_rand(join_proof.escrow.iv, IV_SIZE));
  283. if(PVEC_SUCCESS != ret){
  284. goto ret_point;
  285. }
  286. se_static_assert(sizeof(psk)==sizeof(sgx_aes_gcm_128bit_key_t)); /*sizeof sgx_aes_gcm_128bit_key_t tshould be same as size of psk*/
  287. se_static_assert(sizeof(sgx_aes_gcm_128bit_tag_t)==sizeof(join_proof.escrow.mac)); /*sizeof sgx_aes_gcm_128bit_tag_t should be same as MAC_SIZE*/
  288. sgx_status = sgx_rijndael128GCM_encrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(&psk),
  289. reinterpret_cast<uint8_t *>(f), sizeof(*f), reinterpret_cast<uint8_t *>(&join_proof.escrow.f),
  290. join_proof.escrow.iv, IV_SIZE, NULL, 0,
  291. reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(join_proof.escrow.mac));
  292. if(SGX_SUCCESS != sgx_status){
  293. ret = sgx_error_to_pve_error(sgx_status);
  294. }
  295. ret_point:
  296. (void)memset_s(&psk, sizeof(psk), 0, sizeof(psk));//clear the key
  297. (void)memset_s(&temp_f, sizeof(temp_f), 0, sizeof(temp_f));//clear temp f in stack
  298. if(PVEC_SUCCESS != ret){
  299. (void)memset_s(&join_proof, sizeof(join_proof), 0, sizeof(join_proof));
  300. }
  301. return ret;
  302. }
  303. //Function to create data for ProvMsg3 generation
  304. // The sigrl of ProvMsg2 will processed in this function in piece-meal method
  305. //@msg2_blob_input: structure to hold decoded data of ProvMsg2
  306. //@performance_rekey_used[in]: 1 if performance rekey used or 0 if not
  307. //@msg3_parm: structure to hold most information to generate ProvMsg3
  308. //@msg3_output: structure to hold output data to create ProvMsg3
  309. //@emp_epid_sig: output buffer to external memory for variable length EpidSignature
  310. //@epid_sig_buffer_size: size in bytes of buffer emp_epid_sig
  311. //@return PVEC_SUCCESS on success and error code if failed
  312. pve_status_t gen_prov_msg3_data(const proc_prov_msg2_blob_input_t *msg2_blob_input,
  313. prov_msg3_parm_t& msg3_parm,
  314. uint8_t performance_rekey_used,
  315. gen_prov_msg3_output_t *msg3_output,
  316. external_memory_byte_t *emp_epid_sig,
  317. uint32_t epid_sig_buffer_size)
  318. {
  319. pve_status_t ret = PVEC_SUCCESS;
  320. sgx_status_t sgx_status = SGX_SUCCESS;
  321. uint8_t temp_buf[JOIN_PROOF_TLV_TOTAL_SIZE];
  322. uint8_t *data_to_encrypt = NULL;
  323. uint8_t size_to_encrypt = 0;
  324. uint8_t pwk2_tlv_buffer[PWK2_TLV_TOTAL_SIZE];
  325. sgx_key_128bit_t *pwk2=reinterpret_cast<sgx_key_128bit_t *>(pwk2_tlv_buffer+PWK2_TLV_HEADER_SIZE);
  326. uint8_t report_data_payload[MAC_SIZE + HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE + NONCE_2_SIZE + PEK_MOD_SIZE];
  327. uint8_t* pdata = &report_data_payload[0];
  328. sgx_report_data_t report_data = { 0 };
  329. uint8_t aad[sizeof(GroupId)+sizeof(device_id_t)+CHALLENGE_NONCE_SIZE];
  330. IppsRSAPublicKeyState *pub_key = NULL;
  331. uint8_t *pub_key_buffer = NULL;
  332. IppStatus ipp_status;
  333. int pub_key_size;
  334. Ipp8u seeds[PVE_RSA_SEED_SIZE]={0};
  335. const signed_pek_t& pek = msg2_blob_input->pek;
  336. uint32_t le_e;
  337. int i;
  338. uint8_t le_n[sizeof(pek.n)];
  339. static_assert(sizeof(pek.n)==384, "pek.n should be 384 bytes");
  340. device_id_t *device_id_in_aad= (device_id_t *)(aad+sizeof(GroupId));
  341. join_proof_with_escrow_t* join_proof_with_escrow=reinterpret_cast<join_proof_with_escrow_t *>(temp_buf+JOIN_PROOF_TLV_HEADER_SIZE);
  342. se_static_assert(sizeof(join_proof_with_escrow_t)+JOIN_PROOF_TLV_HEADER_SIZE==JOIN_PROOF_TLV_TOTAL_SIZE); /*unmatched hardcoded size*/
  343. se_static_assert(sizeof(sgx_key_128bit_t)==PWK2_TLV_TOTAL_SIZE-PWK2_TLV_HEADER_SIZE); /*unmatched PWK2 size*/
  344. memset(temp_buf, 0 ,sizeof(temp_buf));
  345. memset(aad, 0, sizeof(aad));
  346. memset(pwk2, 0, sizeof(sgx_key_128bit_t));
  347. memcpy(pwk2_tlv_buffer, PWK2_TLV_HEADER, PWK2_TLV_HEADER_SIZE);
  348. msg3_output->is_join_proof_generated=false;
  349. msg3_output->is_epid_sig_generated=false;
  350. if ((msg2_blob_input->pce_target_info.attributes.flags & SGX_FLAGS_PROVISION_KEY) != SGX_FLAGS_PROVISION_KEY ||
  351. (msg2_blob_input->pce_target_info.attributes.flags & SGX_FLAGS_DEBUG) != 0){
  352. //PCE must have access to provisioning key
  353. //Can't be debug PCE
  354. ret = PVEC_PARAMETER_ERROR;
  355. goto ret_point;
  356. }
  357. if(!performance_rekey_used){
  358. //the temp_buf used for join_proof_with_escrow tlv
  359. memcpy(temp_buf, JOIN_PROOF_TLV_HEADER, JOIN_PROOF_TLV_HEADER_SIZE);//first copy in tlv header
  360. ret = gen_msg3_join_proof_escrow_data(msg2_blob_input, *join_proof_with_escrow);//generate the tlv payload
  361. if( PVEC_SUCCESS != ret )
  362. goto ret_point;
  363. msg3_output->is_join_proof_generated = true;
  364. data_to_encrypt = temp_buf;
  365. size_to_encrypt = JOIN_PROOF_TLV_TOTAL_SIZE;
  366. }
  367. //now encrypt field1
  368. ret = se_read_rand_error_to_pve_error(sgx_read_rand(msg3_output->field1_iv, IV_SIZE));//randomly generate IV
  369. if( PVEC_SUCCESS != ret)
  370. goto ret_point;
  371. memcpy(aad, &msg2_blob_input->group_cert.key.gid,sizeof(GroupId));//start to prepare AAD
  372. memcpy(&device_id_in_aad->fmsp, &msg2_blob_input->equiv_pi.fmsp, sizeof(fmsp_t));
  373. memcpy(&device_id_in_aad->psvn.cpu_svn, &msg2_blob_input->equiv_pi.cpu_svn, sizeof(sgx_cpu_svn_t));
  374. memcpy(&device_id_in_aad->psvn.isv_svn, &msg2_blob_input->equiv_pi.pve_svn, sizeof(sgx_isv_svn_t));
  375. memset(&device_id_in_aad->ppid, 0, sizeof(device_id_in_aad->ppid));
  376. ret = pve_rng_generate(NONCE_2_SIZE*8, msg3_output->n2);
  377. if(PVEC_SUCCESS !=ret){
  378. goto ret_point;
  379. }
  380. ret = get_pwk2(&device_id_in_aad->psvn, msg3_output->n2, pwk2);
  381. if( PVEC_SUCCESS != ret )
  382. goto ret_point;
  383. memcpy(aad+sizeof(GroupId)+sizeof(device_id_t), msg2_blob_input->challenge_nonce, CHALLENGE_NONCE_SIZE);
  384. se_static_assert(sizeof(sgx_aes_gcm_128bit_key_t)==SK_SIZE); /*sizeof sgx_aes_gcm_128bit_key_t should be same as TCB size*/
  385. se_static_assert(sizeof(sgx_aes_gcm_128bit_tag_t)==MAC_SIZE); /*sizeof sgx_aes_gcm_128bit_tag_t should be same as MAC_SIZE*/
  386. sgx_status = sgx_rijndael128GCM_encrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(pwk2),
  387. data_to_encrypt, size_to_encrypt, msg3_output->field1_data,
  388. msg3_output->field1_iv, IV_SIZE, aad, static_cast<uint32_t>(sizeof(GroupId)+sizeof(device_id_t)+CHALLENGE_NONCE_SIZE),
  389. reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(msg3_output->field1_mac));//encrypt field1
  390. if(SGX_SUCCESS != sgx_status){
  391. ret = sgx_error_to_pve_error(sgx_status);
  392. goto ret_point;
  393. }
  394. if( msg2_blob_input->is_previous_pi_provided ){
  395. //preparing the encryption state of ProvMsg3 and encrypt inplace of msg3_inside enclave (field1_0 and field1_1)
  396. //The function will randomly set the iv value too
  397. ret = proc_msg3_state_init(&msg3_parm, pwk2);
  398. if( PVEC_SUCCESS!=ret )
  399. goto ret_point;
  400. //Now start piece-meal generation of EPIDsign
  401. ret = gen_msg3_signature(msg2_blob_input, &msg3_parm, emp_epid_sig, epid_sig_buffer_size);
  402. if( PVEC_SUCCESS!=ret )
  403. goto ret_point;
  404. msg3_output->is_epid_sig_generated = true;
  405. msg3_output->epid_sig_output_size = epid_sig_buffer_size;
  406. memcpy(msg3_output->epid_sig_iv, msg3_parm.iv, IV_SIZE);
  407. //generate MAC in EPC
  408. ret = pve_aes_gcm_get_mac(msg3_parm.p_msg3_state, msg3_output->epid_sig_mac);
  409. if (PVEC_SUCCESS != ret)
  410. goto ret_point;
  411. }
  412. le_e = lv_ntohl(pek.e);
  413. se_static_assert(sizeof(pek.n)==sizeof(le_n)); /*unmatched size of pek.n*/
  414. //endian swap
  415. for(i=0;i<(int)(sizeof(pek.n)/sizeof(pek.n[0]));i++){
  416. le_n[i]=pek.n[sizeof(pek.n)/sizeof(pek.n[0])-i-1];
  417. }
  418. ipp_status = create_rsa_pub_key(sizeof(pek.n), sizeof(pek.e),
  419. reinterpret_cast<const Ipp32u *>(le_n), &le_e, &pub_key);
  420. if(ippStsNoErr != ipp_status){
  421. ret = ipp_error_to_pve_error(ipp_status);
  422. goto ret_point;
  423. }
  424. ipp_status = ippsRSA_GetBufferSizePublicKey(&pub_key_size, pub_key);
  425. if(ippStsNoErr != ipp_status){
  426. ret = ipp_error_to_pve_error(ipp_status);
  427. goto ret_point;
  428. }
  429. if(SGX_SUCCESS != (sgx_status =sgx_read_rand(seeds, PVE_RSA_SEED_SIZE))){
  430. ret = se_read_rand_error_to_pve_error(sgx_status);
  431. goto ret_point;
  432. }
  433. pub_key_buffer = (uint8_t *)malloc(pub_key_size);
  434. if(NULL ==pub_key_buffer){
  435. ret = PVEC_INSUFFICIENT_MEMORY_ERROR;
  436. goto ret_point;
  437. }
  438. ipp_status = ippsRSAEncrypt_OAEP(reinterpret_cast<const Ipp8u *>(pwk2_tlv_buffer), PWK2_TLV_TOTAL_SIZE, NULL, 0, seeds,
  439. msg3_output->encrypted_pwk2, pub_key, IPP_ALG_HASH_SHA256, pub_key_buffer);
  440. if(ippStsNoErr != ipp_status){
  441. ret = ipp_error_to_pve_error(ipp_status);
  442. goto ret_point;
  443. }
  444. // X = (NT)MAC_PWK2(... (NT)E_PWK2((T)(JoinP, f)) ...) | (NT)E_PWK2((T)(JoinP, f)) | (NT)PWK2N | (NT)E_PEK((T)PWK2)
  445. // REPORT.ReportData == SHA256[X]
  446. memcpy(pdata, msg3_output->field1_mac, MAC_SIZE);
  447. pdata += MAC_SIZE;
  448. if (!performance_rekey_used){
  449. memcpy(pdata, msg3_output->field1_data, HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE);
  450. pdata += HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE;
  451. }
  452. memcpy(pdata, msg3_output->n2, NONCE_2_SIZE);
  453. pdata += NONCE_2_SIZE;
  454. memcpy(pdata, msg3_output->encrypted_pwk2, PEK_MOD_SIZE);
  455. pdata += PEK_MOD_SIZE;
  456. se_static_assert(sizeof(report_data) >= sizeof(sgx_sha256_hash_t)); /*report data is no large enough*/
  457. sgx_status = sgx_sha256_msg(report_data_payload, (uint32_t)(pdata - &report_data_payload[0]), reinterpret_cast<sgx_sha256_hash_t *>(&report_data));
  458. if (SGX_SUCCESS != sgx_status){
  459. ret = sgx_error_to_pve_error(sgx_status);
  460. goto ret_point;
  461. }
  462. sgx_status = sgx_create_report(&msg2_blob_input->pce_target_info, &report_data, &msg3_output->pwk2_report);
  463. if (SGX_SUCCESS != sgx_status){
  464. ret = sgx_error_to_pve_error(sgx_status);
  465. goto ret_point;
  466. }
  467. ret_point:
  468. (void)memset_s(aad, sizeof(aad), 0, sizeof(aad));
  469. (void)memset_s(temp_buf, sizeof(temp_buf), 0, sizeof(temp_buf));
  470. (void)memset_s(pwk2_tlv_buffer, sizeof(pwk2_tlv_buffer),0,sizeof(pwk2_tlv_buffer));
  471. if(pub_key){
  472. secure_free_rsa_pub_key(sizeof(pek.n), sizeof(pek.e), pub_key);
  473. }
  474. if(pub_key_buffer){
  475. free(pub_key_buffer);
  476. }
  477. return ret;
  478. }