22#include < stdio.h>
33
44#include < assert.h>
5+ #include < map>
6+ #include < string>
57#include " sgx_tkey_exchange.h"
68#include " sgx_tcrypto.h"
79#include " sgx_tseal.h"
810#include " string.h"
911#include " EUtils.h"
1012#include " Enclave_t.h"
1113
14+ using namespace std ;
15+
1216// This is the public EC key of the SP. The corresponding private EC key is
1317// used by the SP to sign data used in the remote attestation SIGMA protocol
1418// to sign channel binding data in MSG2. A successful verification of the
@@ -36,7 +40,17 @@ static const sgx_ec256_public_t g_sp_pub_key = {
3640
3741};
3842
43+ // Store user account id to user phone num mapping
44+ map<vector<uint8_t >, string> accid_phone_map;
45+ // Store context id to user account id mapping
46+ map<sgx_ra_context_t , vector<uint8_t >> contextid_accid_map;
3947
48+ /* *
49+ * @description: Initialize remote attestation context
50+ * @param b_pse -> Indicate whether create pse session
51+ * @param p_context -> RA session
52+ * @return: Initialize status
53+ * */
4054sgx_status_t ecall_init_ra (int b_pse, sgx_ra_context_t *p_context)
4155{
4256 // isv enclave call to trusted key exchange library.
@@ -61,32 +75,84 @@ sgx_status_t ecall_init_ra(int b_pse, sgx_ra_context_t *p_context)
6175 return ret;
6276}
6377
78+ /* *
79+ * @description: Close remote attestation
80+ * @param context -> Indicate to be closed context
81+ * @return: Close status
82+ * */
6483sgx_status_t SGXAPI ecall_ra_close (sgx_ra_context_t context)
6584{
6685 sgx_status_t ret;
6786 ret = sgx_ra_close (context);
87+ contextid_accid_map.erase (context);
6888 return ret;
6989}
7090
71- sgx_status_t ecall_verify_secret (sgx_ra_context_t context,
91+ /* *
92+ * @description: Store user account id
93+ * @param context -> Corresponding context
94+ * @param p_Gb -> Pointer to user account id
95+ * @param Gb_size -> User account id size
96+ * */
97+ void ecall_store_account_id (sgx_ra_context_t context, uint8_t * p_Gb, uint32_t Gb_size)
98+ {
99+ vector<uint8_t > Gb_v (p_Gb, p_Gb + Gb_size);
100+ contextid_accid_map[context] = Gb_v;
101+ }
102+
103+ /* *
104+ * @description: Decrypt user passed data
105+ * @param context -> Corresponding context
106+ * @param p_src -> Encrypted data
107+ * @param src_len -> Encrypted data size
108+ * @param p_in_mac -> Encrypted data mac
109+ * @return: Decrypted status
110+ * */
111+ sgx_status_t ecall_decrypt_secret (sgx_ra_context_t context,
72112 const uint8_t *p_src, uint32_t src_len,
73- uint8_t *p_dst, const sgx_aes_gcm_128bit_tag_t *p_in_mac)
113+ const sgx_aes_gcm_128bit_tag_t *p_in_mac)
74114{
115+ if (contextid_accid_map.find (context) == contextid_accid_map.end ())
116+ {
117+ return SGX_ERROR_UNEXPECTED;
118+ }
119+
75120 sgx_status_t sgx_status = SGX_SUCCESS;
76121 sgx_ra_key_128_t ra_key;
122+ string phone_num;
77123
78124 sgx_status = sgx_ra_get_keys (context, SGX_RA_KEY_SK, &ra_key);
79125 if (SGX_SUCCESS != sgx_status)
80126 {
81127 return sgx_status;
82128 }
83129
84- cfeprintf (" ra key:%s\n " , hexstring (&ra_key, sizeof (ra_key)));
130+ feprintf (" ra key:%s\n " , hexstring (&ra_key, sizeof (ra_key)));
85131
86132 uint8_t *p_iv = (uint8_t *)malloc (SGX_AESGCM_IV_SIZE);
87133 memset (p_iv, 0 , SGX_AESGCM_IV_SIZE);
134+ uint8_t *p_dst = (uint8_t *)malloc (src_len);
135+ memset (p_dst, 0 , src_len);
88136 sgx_status = sgx_rijndael128GCM_decrypt (&ra_key, p_src,
89137 src_len, p_dst, p_iv, SGX_AESGCM_IV_SIZE, NULL , 0 , p_in_mac);
90138
139+ if (SGX_SUCCESS != sgx_status)
140+ {
141+ sgx_status = SGX_ERROR_UNEXPECTED;
142+ goto cleanup;
143+ }
144+
145+ phone_num = string (hexstring (p_dst, src_len));
146+ phone_num = phone_num.substr (0 , 11 );
147+ accid_phone_map[contextid_accid_map[context]] = phone_num;
148+
149+ feprintf (" Phone number:%s\n " , accid_phone_map[contextid_accid_map[context]]);
150+
151+
152+ cleanup:
153+
154+ free (p_iv);
155+ free (p_dst);
156+
91157 return sgx_status;
92158}
0 commit comments