provision_msg3.cpp 25 KB

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