Decryptor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. // Enclave2.cpp : Defines the exported functions for the DLL application
  32. #include "sgx_eid.h"
  33. #include "sgx_tcrypto.h"
  34. #include "Decryptor_t.h"
  35. #include "EnclaveMessageExchange.h"
  36. #include "error_codes.h"
  37. #include "Utility_Decryptor.h"
  38. #include "sgx_thread.h"
  39. #include "sgx_dh.h"
  40. #include <map>
  41. #include "sgx_tcrypto.h"
  42. #include "LocalAttestationCode_t.h"
  43. #include "sgx_tseal.h"
  44. // openssl keygen function
  45. #include "tSgxSSL_api.h"
  46. #include <openssl/ec.h>
  47. #include <openssl/bn.h>
  48. #include <openssl/rsa.h>
  49. #include <openssl/evp.h>
  50. #include <openssl/err.h>
  51. #include <openssl/rand.h>
  52. //extern dh_session_t global_session_info;
  53. uint8_t apache_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  54. uint8_t client_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  55. uint8_t verifier_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  56. extern uint8_t apache_key[16];
  57. extern uint8_t verifier_key[16];
  58. //uint32_t client_iv=0;
  59. // internal-internal
  60. uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  61. void serialize_key_pair_to_string( sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string);
  62. void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  63. uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signature_data, __attribute__((unused)) uint8_t* signature, __attribute__((unused)) uint8_t* private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2);
  64. uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* tag, uint8_t* op_plaintext, uint32_t enc);
  65. void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length);
  66. uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key);
  67. uint32_t calculate_sealed_data_size( uint32_t input_size) ;
  68. uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length, __attribute__((unused)) uint8_t* sealed_data);
  69. uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, uint8_t* sealed_data, size_t* sgx_sealed_data_length);
  70. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag);
  71. uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag, uint8_t* signing_private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2);
  72. static void reverse_byte_array(uint8_t *array, size_t size);
  73. uint32_t one_la_done=0;
  74. static sgx_ec256_public_t short_term_pub_key;
  75. static sgx_ec256_private_t short_term_priv_key;
  76. //sgx_ec256_signature_t generated_signature; // TODO: remove
  77. sgx_measurement_t apache_mr_signer; // TODO: remove
  78. sgx_measurement_t verifier_mr_enclave; // TODO: remove
  79. static sgx_ec256_private_t signing_priv_key;
  80. extern "C" uint32_t verify_peer_enclave_trust(__attribute__((unused)) sgx_dh_session_enclave_identity_t* peer_enclave_identity)
  81. {
  82. uint32_t count;
  83. if(!peer_enclave_identity)
  84. {
  85. return INVALID_PARAMETER_ERROR;
  86. }
  87. if(one_la_done==0)
  88. {
  89. // return 0x55;
  90. //sgx_measurement_t local_mr_enclave;
  91. verifier_mr_enclave = peer_enclave_identity->mr_enclave;
  92. memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
  93. one_la_done=1;
  94. }
  95. else // apache enclave
  96. {
  97. sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
  98. // verifier's mrsigner
  99. // uint8_t expected_mr_signer[32] ={0xdf, 0xd7, 0x3b, 0x93, 0xea, 0x39, 0x02, 0x02, 0x3c, 0xd0, 0x52, 0x1a, 0xbd, 0x00, 0xaf, 0xb9, 0xa6, 0x54, 0x57, 0x3e, 0xe5, 0xef, 0x36, 0xf4, 0x8c, 0xc2, 0x4d, 0x92, 0x70, 0xae, 0xd4, 0x7c};
  100. int count;
  101. for(count=0; count<SGX_HASH_SIZE; count++)
  102. {
  103. if( actual_mr_signer.m[count] != apache_mr_signer.m[count] )
  104. return ENCLAVE_TRUST_ERROR;
  105. }
  106. }
  107. return SGX_SUCCESS;
  108. }
  109. // increments last 4 bytes (in big-endian order)
  110. uint32_t aes_gcm_increment_iv_internal_call(uint8_t* iv)
  111. {
  112. uint32_t counter;
  113. for(counter=11;counter>7;counter--)
  114. {
  115. if(iv[counter] == 0xff)
  116. {
  117. if(counter - 1 == 7)
  118. return 0xff;
  119. iv[counter-1] = 0x01;
  120. iv[counter] = 0x0;
  121. }
  122. else
  123. iv[counter] += 1;
  124. }
  125. return 0;
  126. }
  127. // TODO: change global_session_info to two different dh_sessions
  128. // This needs to be called after the first local attestation is successful - otherwise, the internal apache_mr_signer.m will not be set properly for the comparison of the mrsigner for the 2nd LA in verify_peer_enclave_trust.
  129. // (I.e. if it is not called then DoS
  130. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag)
  131. {
  132. uint32_t internal_ret_status= aes_gcm_internal_call(ciphertext, 32, verifier_key, verifier_iv , tag, (uint8_t*) &(apache_mr_signer.m), 0);
  133. return internal_ret_status;
  134. }
  135. // signature_data - 96 bytes, encrypted_signature assumed to be at least 64 bytes, tag - at least 16 bytes
  136. uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag, uint8_t* signing_private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2)
  137. {
  138. uint32_t count;
  139. uint8_t sign_data_and_sign[160];
  140. uint32_t ret_status=create_mitigator_header_value(sign_data_and_sign, sign_data_and_sign+96, signing_private_key, sig2);
  141. if(ret_status != SGX_SUCCESS)
  142. return 0xFFFFFFDD;
  143. // TODO: Remove - just for troubleshooting
  144. for(count=0; count<160; count++)
  145. *(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count];
  146. ret_status = aes_gcm_internal_call(sign_data_and_sign, 160, apache_key, apache_iv, tag, encrypted_sign_data_and_sign, 1);
  147. // ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign);
  148. aes_gcm_increment_iv_internal_call(apache_iv);
  149. return ret_status;
  150. }
  151. uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  152. {
  153. sgx_status_t se_ret; sgx_status_t se_ret2;
  154. //create ECC context
  155. sgx_ecc_state_handle_t ecc_state = NULL;
  156. se_ret = sgx_ecc256_open_context(&ecc_state);
  157. if(SGX_SUCCESS != se_ret)
  158. return se_ret;
  159. // generate private key and public key
  160. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  161. se_ret2 = sgx_ecc256_close_context(ecc_state);
  162. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  163. return 0xFFFFFFFF;
  164. return SGX_SUCCESS;
  165. }
  166. // todo: set to private
  167. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  168. void serialize_key_pair_to_string(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key, uint8_t* private_public_key_string)
  169. {
  170. if(private_public_key_string != NULL) // nowhere to serialize to
  171. {
  172. uint32_t counter;
  173. if(pub_key != NULL) // public key to serialize
  174. {
  175. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  176. *(private_public_key_string+counter)=pub_key->gx[counter];
  177. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  178. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  179. }
  180. if(signing_priv_key != NULL) // private key to serialize
  181. {
  182. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  183. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  184. }
  185. }
  186. }
  187. // todo: set to private
  188. void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* signing_priv_key)
  189. {
  190. if(private_public_key_string != NULL) // nowhere to deserialize from
  191. {
  192. uint32_t counter;
  193. if(signing_priv_key != NULL)
  194. {
  195. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  196. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  197. }
  198. if(pub_key != NULL)
  199. {
  200. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  201. pub_key->gx[counter]=*(private_public_key_string+counter);
  202. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  203. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  204. }
  205. }
  206. }
  207. uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length,
  208. __attribute__((unused)) uint8_t* sealed_data)
  209. {
  210. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  211. ret_status=create_ec_key_pair(pub_key, &private_key);
  212. if(ret_status!=SGX_SUCCESS)
  213. return ret_status;
  214. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  215. signing_priv_key.r[counter]=private_key.r[counter];
  216. // generating the entire string as there is no SGX function to generate the public key from the private one.
  217. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  218. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  219. // serializing keypair to string
  220. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  221. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  222. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  223. *(private_key_string+counter)=private_key.r[counter];
  224. // return *sealed_data_length;
  225. ret_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, *sealed_data_length, (sgx_sealed_data_t*) sealed_data2);
  226. for(counter=0;counter<*sealed_data_length;counter++)
  227. *(sealed_data+counter)=*(sealed_data2+counter);
  228. free(sealed_data2);
  229. free(private_key_string); //free(private_key);
  230. free(private_public_key_string);
  231. return ret_status; // SGX_SUCCESS;
  232. }
  233. uint32_t unseal_and_restore_sealed_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, uint8_t* sealed_data, size_t* sgx_sealed_data_length)
  234. {
  235. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  236. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  237. if(expected_plaintext_msg_length == 0xffffffff)
  238. return 0xFFFFFFFF;
  239. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  240. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  241. {
  242. *(sealed_data2+counter)=*(sealed_data+counter);
  243. }
  244. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  245. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  246. if(ret_status != SGX_SUCCESS)
  247. {
  248. free(temp_plaintext);free(sealed_data2);
  249. return ret_status;
  250. }
  251. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  252. free(temp_plaintext); free(sealed_data2);
  253. return SGX_SUCCESS;
  254. }
  255. uint32_t create_mitigator_header_value(__attribute__((unused)) uint8_t* signature_data, __attribute__((unused)) uint8_t* signature, __attribute__((unused)) uint8_t* private_key, __attribute__((unused)) sgx_ec256_signature_t* sig2)
  256. {
  257. // Otherwise: DoS or possible bypass (fake verifier does LA but real verifier mrenclave is given out by decryptor) - signature with junk verifier mrenclave or whatever is in the memory.
  258. if(one_la_done < 1)
  259. return 0xde; // This needs to be called at any point after the first local attestation is done - else, a junk verifier mrenclave will be included in the signature
  260. // TODO: Comment this
  261. // memset(&(verifier_mr_enclave.m), 0x55, 32);
  262. // create key pair
  263. uint32_t ret_status = create_ec_key_pair(&short_term_pub_key, &short_term_priv_key); uint32_t counter;
  264. uint32_t ret_status2;
  265. if(ret_status!=SGX_SUCCESS)
  266. return ret_status;
  267. // serialize public key, append mr_enclave
  268. // uint8_t* public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE); // for .edl file - size parameter for serialize is 96 and this fits coz we need to append the mr_enclave to the pub key
  269. serialize_key_pair_to_string(&short_term_pub_key, NULL, signature_data);
  270. reverse_byte_array(signature_data, 32);
  271. reverse_byte_array(signature_data+32, 32);
  272. for(counter=32*2; counter<32*3; counter++) // appending mr_enclave
  273. *(signature_data+counter)=0x55;//verifier_mr_enclave.m[counter]; // TODO: uncomment verifier_mrenclave
  274. // for(counter=0;counter<32*3;counter++) // TODO: REmove - for checking against js library only
  275. // *(signature_data+counter)=0x55;
  276. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  277. sgx_ec256_private_t long_term_priv_key;
  278. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  279. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  280. // sign public key with long-term private key
  281. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  282. for(counter=0;counter<32;counter++)
  283. *(private_key+counter)=short_term_priv_key.r[counter];
  284. // uint8_t signature_data2[96];
  285. // for(counter=0;counter<96;counter++)
  286. // signature_data2[counter]=0x55;
  287. //// opening context for signature
  288. ret_status = sgx_ecc256_open_context(&ecc_handle);
  289. if(ret_status != SGX_SUCCESS)
  290. return ret_status;
  291. ret_status = sgx_ecdsa_sign(signature_data, 96, &long_term_priv_key, &local_signature, ecc_handle);
  292. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  293. // free(public_key_string);
  294. if(ret_status == SGX_SUCCESS)
  295. { // this only works for Little-endian architectures - need to do byte-wise swapping of the bytes obtained on RHS
  296. uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
  297. uint32_t ecdsa_sig_count;
  298. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  299. signature[31-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
  300. current_sig_byte = (uint8_t*)(&(local_signature.y));
  301. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  302. signature[63-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
  303. for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
  304. sig2->x[ecdsa_sig_count]=local_signature.x[ecdsa_sig_count];
  305. for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
  306. sig2->y[ecdsa_sig_count]=local_signature.y[ecdsa_sig_count];
  307. }
  308. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  309. return 0xFFFFFFFF;
  310. return 0;
  311. }
  312. uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key)
  313. {
  314. sgx_ec256_public_t local_pub_key; uint32_t counter; uint32_t ret_status; uint32_t ret_status2;
  315. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  316. {
  317. local_pub_key.gx[counter] = pub_key->gx[counter];
  318. local_pub_key.gy[counter] = pub_key->gy[counter];
  319. }
  320. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  321. uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
  322. uint32_t ecdsa_sig_count; uint8_t verification_result;
  323. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  324. *(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count];
  325. current_sig_byte = (uint8_t*)(&(local_signature.y));
  326. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  327. *(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count+32];
  328. //// opening context for signature
  329. ret_status = sgx_ecc256_open_context(&ecc_handle);
  330. if(ret_status != SGX_SUCCESS)
  331. return ret_status;
  332. ret_status = sgx_ecdsa_verify(signature_data,3*SGX_ECP256_KEY_SIZE, &local_pub_key, &local_signature, &verification_result, ecc_handle);
  333. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  334. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  335. return 0xFFFFFFFF;
  336. if(verification_result != SGX_EC_VALID)
  337. return 0xee;
  338. return 0;
  339. }
  340. uint32_t derive_shared_secret_for_client(uint8_t* pub_key, uint8_t* shared_key)
  341. {
  342. return 0;
  343. }
  344. uint32_t calculate_sealed_data_size( uint32_t input_size)
  345. {
  346. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  347. return sgx_calc_sealed_data_size(0, input_size);
  348. }
  349. // ip_key will always be within the enclave.
  350. // enc = 1 for encryption and 0 for decryption, like openssl api
  351. uint32_t aes_gcm_internal_call(uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* tag, uint8_t* op_plaintext, uint32_t enc)
  352. {
  353. uint32_t counter;
  354. if(ip_ciphertext == NULL)
  355. return 0x33;
  356. if(tag == NULL)
  357. return 0x34;
  358. if(op_plaintext == NULL)
  359. return 0x36;
  360. if(ip_key == NULL)
  361. return 0x35;
  362. if(ip_iv == NULL)
  363. return 0x37;
  364. uint8_t* ip_ciphertext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
  365. memcpy_equivalent_copy(ip_ciphertext_in_enclave, ip_ciphertext, ip_ciphertext_len);
  366. uint8_t tag_in_enclave [16];
  367. if(!enc)
  368. memcpy_equivalent_copy(tag_in_enclave, tag, 16);
  369. uint8_t* op_plaintext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
  370. uint32_t internal_ret_status;
  371. if(enc)
  372. internal_ret_status = sgx_rijndael128GCM_encrypt((sgx_key_128bit_t*) ip_key, ip_ciphertext_in_enclave, ip_ciphertext_len, op_plaintext_in_enclave, ip_iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag_in_enclave);
  373. else
  374. internal_ret_status = sgx_rijndael128GCM_decrypt((sgx_key_128bit_t*) ip_key, ip_ciphertext_in_enclave, ip_ciphertext_len, op_plaintext_in_enclave, ip_iv, 0xc, NULL, 0, (sgx_aes_gcm_128bit_tag_t*)tag_in_enclave);
  375. if(internal_ret_status == 0)
  376. {
  377. memcpy_equivalent_copy(op_plaintext, op_plaintext_in_enclave, ip_ciphertext_len);
  378. if(enc)
  379. memcpy_equivalent_copy(tag, tag_in_enclave, 16);
  380. }
  381. free(ip_ciphertext_in_enclave); free(op_plaintext_in_enclave);
  382. return internal_ret_status;
  383. }
  384. void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length)
  385. {
  386. uint32_t counter;
  387. for(counter=0; counter<length; counter++)
  388. *(dest + counter) = *(src + counter);
  389. }
  390. uint32_t decrypt_client_data(__attribute__((unused)) unsigned char* ip_client_pub_key, uint32_t ciphertext_length, unsigned char* ip_user_data, unsigned char* op_client_data_to_apache)
  391. {
  392. int counter;
  393. for(counter=0;counter<ciphertext_length;counter++)
  394. op_client_data_to_apache[counter]=ip_user_data[counter];
  395. // return 0;
  396. sgx_ecc_state_handle_t ecc_handle;
  397. unsigned char local_ip_client_pub_key[64]; uint32_t ret_status; int valid_point;
  398. memcpy_equivalent_copy(local_ip_client_pub_key, ip_client_pub_key, 64);
  399. sgx_ec256_public_t client_pub_key;
  400. for(counter=0; counter<32; counter++)
  401. {
  402. client_pub_key.gx[counter] = *(local_ip_client_pub_key + counter);
  403. client_pub_key.gy[counter] = *(local_ip_client_pub_key + 32 + counter);
  404. }
  405. /* sgx_ec256_public_t temp_client_pub_key;
  406. sgx_ec256_private_t temp_client_priv_key;
  407. ret_status=create_ec_key_pair(&temp_client_pub_key, &temp_client_priv_key);
  408. if(ret_status == SGX_SUCCESS)
  409. return ret_status;
  410. */
  411. // return ret_status;
  412. ret_status = sgx_ecc256_open_context(&ecc_handle);
  413. if(ret_status != SGX_SUCCESS)
  414. return ret_status;
  415. memset(&client_pub_key, 0, sizeof(client_pub_key));
  416. // reverse_byte_array(client_pub_key.gx, sizeof(client_pub_key.gx));
  417. // reverse_byte_array(client_pub_key.gy, sizeof(client_pub_key.gy));
  418. ret_status = sgx_ecc256_check_point(&client_pub_key, ecc_handle, &valid_point);
  419. if(valid_point == 0 && ret_status == SGX_SUCCESS)
  420. {
  421. return 0x55;
  422. }
  423. sgx_ec256_private_t local_short_term_priv_key;
  424. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  425. local_short_term_priv_key.r[counter] = short_term_priv_key.r[counter];
  426. sgx_ec256_dh_shared_t dh_key;
  427. ret_status=sgx_ecc256_compute_shared_dhkey(&local_short_term_priv_key, &client_pub_key, &dh_key, ecc_handle);
  428. if(ret_status != 0)
  429. return ret_status;
  430. // memcpy_equivalent_copy(op_client_data_to_apache, dh_key.s, 32);
  431. ret_status = sgx_ecc256_close_context(ecc_handle);
  432. return ret_status;
  433. }
  434. static void reverse_byte_array(uint8_t *array, size_t size)
  435. {
  436. size_t i = 0;
  437. for(i = 0; i < size / 2; i++)
  438. {
  439. uint8_t temp = array[i];
  440. array[i] = array[size - i - 1];
  441. array[size - i - 1] = temp;
  442. }
  443. }
  444. uint32_t ec_key_gen(unsigned char* pub_key_x, unsigned char* pub_key_y, unsigned char* priv_key)
  445. {
  446. // unsigned char entropy_buf[ADD_ENTROPY_SIZE] = {0};
  447. // RAND_add(entropy_buf, sizeof(entropy_buf), ADD_ENTROPY_SIZE);
  448. // RAND_seed(entropy_buf, sizeof(entropy_buf));
  449. EC_KEY * ec_key = NULL;
  450. ec_key = EC_KEY_new_by_curve_name(OBJ_txt2nid("secp256r1"));
  451. if (ec_key == NULL) {
  452. // printf("EC_KEY_new_by_curve_name failure: %ld\n", ERR_get_error());
  453. return 0xff;
  454. }
  455. EC_GROUP* ec_group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  456. if (NULL == ec_group) {
  457. EC_KEY_free(ec_key);
  458. return 0x0f;
  459. }
  460. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  461. int ret = EC_KEY_generate_key(ec_key);
  462. if (!ret) {
  463. // printf("EC_KEY_generate_key failure\n");
  464. return 0x01;
  465. }
  466. ///////////////////////// Openssl code ////////////
  467. const EC_POINT *public_k = NULL;
  468. const BIGNUM *private_k = NULL;
  469. public_k = EC_KEY_get0_public_key(ec_key);
  470. if (NULL == public_k)
  471. {
  472. EC_KEY_free(ec_key);
  473. return 0x04;
  474. }
  475. private_k = EC_KEY_get0_private_key(ec_key);
  476. if (NULL == private_k)
  477. {
  478. EC_KEY_free(ec_key);
  479. return 0x05;
  480. }
  481. if(!BN_bn2bin(private_k, priv_key))
  482. {
  483. EC_KEY_free(ec_key);
  484. return 0x06;
  485. }
  486. BIGNUM *pub_k_x = NULL;
  487. BIGNUM *pub_k_y = NULL;
  488. pub_k_x = BN_new();
  489. pub_k_y = BN_new();
  490. if (NULL == pub_k_x || NULL == pub_k_y) {
  491. return SGX_ERROR_OUT_OF_MEMORY;
  492. }
  493. // extract two BNs representing the public key
  494. //
  495. if (!EC_POINT_get_affine_coordinates_GFp(ec_group, public_k, pub_k_x, pub_k_y, NULL))
  496. EC_KEY_free(ec_key);
  497. {
  498. BN_clear_free(pub_k_x); BN_clear_free(pub_k_y);
  499. return 0x07;
  500. }
  501. ret = BN_bn2bin(pub_k_x, pub_key_x);
  502. BN_clear_free(pub_k_x);
  503. if(ret == 0)
  504. {
  505. BN_clear_free(pub_k_y);
  506. return 0x08;
  507. }
  508. ret = BN_bn2bin(pub_k_y, pub_key_y);
  509. BN_clear_free(pub_k_y);
  510. if(ret == 0)
  511. {
  512. return 0x09;
  513. }
  514. return 0;
  515. }