Decryptor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 "sgx_thread.h"
  38. #include "sgx_dh.h"
  39. #include <map>
  40. #include "sgx_tcrypto.h"
  41. #include "LocalAttestationCode_t.h"
  42. #include "sgx_tseal.h"
  43. #include "Openssl_crypto.h"
  44. //extern dh_session_t global_session_info;
  45. uint8_t apache_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  46. uint8_t client_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  47. uint8_t verifier_iv[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
  48. extern uint8_t apache_key[16];
  49. extern uint8_t verifier_key[16];
  50. //uint32_t client_iv=0;
  51. // internal-internal
  52. uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  53. 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);
  54. void deserialize_string_to_key_pair(uint8_t* private_public_key_string, sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key);
  55. 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);
  56. uint32_t aes_gcm_192_call(uint32_t enc, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* op_plaintext, uint32_t* op_plaintext_length, uint8_t* tag);
  57. //uint32_t aes_gcm_internal_call(uint32_t enc, uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* op_plaintext, uint8_t* tag);
  58. void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length);
  59. uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key);
  60. uint32_t calculate_sealed_data_size( uint32_t input_size) ;
  61. 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);
  62. 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);
  63. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag);
  64. 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);
  65. //static void reverse_byte_array(uint8_t *array, size_t size);
  66. uint32_t one_la_done=0;
  67. static sgx_ec256_public_t short_term_pub_key;
  68. static sgx_ec256_private_t short_term_priv_key;
  69. unsigned char short_term_private_key_arr[32];
  70. unsigned char short_term_public_key_arr[64];
  71. //sgx_ec256_signature_t generated_signature; // TODO: remove
  72. sgx_measurement_t apache_mr_signer; // TODO: remove
  73. sgx_measurement_t verifier_mr_enclave; // TODO: remove
  74. static sgx_ec256_private_t signing_priv_key;
  75. extern "C" uint32_t verify_peer_enclave_trust(__attribute__((unused)) sgx_dh_session_enclave_identity_t* peer_enclave_identity)
  76. {
  77. uint32_t count;
  78. if(!peer_enclave_identity)
  79. {
  80. return INVALID_PARAMETER_ERROR;
  81. }
  82. if(one_la_done==0)
  83. {
  84. // return 0x55;
  85. //sgx_measurement_t local_mr_enclave;
  86. verifier_mr_enclave = peer_enclave_identity->mr_enclave;
  87. memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
  88. one_la_done=1;
  89. }
  90. else // apache enclave
  91. {
  92. sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
  93. // verifier's mrsigner
  94. // 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};
  95. int count;
  96. for(count=0; count<SGX_HASH_SIZE; count++)
  97. {
  98. if( actual_mr_signer.m[count] != apache_mr_signer.m[count] )
  99. return ENCLAVE_TRUST_ERROR;
  100. }
  101. }
  102. return SGX_SUCCESS;
  103. }
  104. // increments last 4 bytes (in big-endian order)
  105. uint32_t aes_gcm_increment_iv_internal_call(uint8_t* iv)
  106. {
  107. uint32_t counter;
  108. for(counter=11;counter>7;counter--)
  109. {
  110. if(iv[counter] == 0xff)
  111. {
  112. if(counter - 1 == 7)
  113. return 0xff;
  114. iv[counter-1] = 0x01;
  115. iv[counter] = 0x0;
  116. }
  117. else
  118. iv[counter] += 1;
  119. }
  120. return 0;
  121. }
  122. // TODO: change global_session_info to two different dh_sessions
  123. // 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.
  124. // (I.e. if it is not called then DoS
  125. uint32_t decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext, uint8_t* tag)
  126. {
  127. uint32_t length;
  128. uint32_t internal_ret_status= aes_gcm_192_call(0, verifier_key, verifier_iv, ciphertext, 32, (uint8_t*) &(apache_mr_signer.m), &length, tag);
  129. return internal_ret_status;
  130. }
  131. // signature_data - 96 bytes, encrypted_signature assumed to be at least 64 bytes, tag - at least 16 bytes
  132. 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)
  133. {
  134. uint32_t count; uint32_t length;
  135. uint8_t sign_data_and_sign[160];
  136. uint32_t ret_status=create_mitigator_header_value(sign_data_and_sign, sign_data_and_sign+96, signing_private_key, sig2);
  137. if(ret_status != SGX_SUCCESS)
  138. return 0xFFFFFFDD;
  139. // TODO: Remove - just for troubleshooting
  140. for(count=0; count<160; count++)
  141. *(plaintext_sign_data_and_sign+count)=sign_data_and_sign[count];
  142. ret_status = aes_gcm_192_call(1, apache_key, apache_iv, sign_data_and_sign, 160, encrypted_sign_data_and_sign, &length, tag);
  143. // ret_status = encrypt_internal(sign_data_and_sign, 160, tag, encrypted_sign_data_and_sign);
  144. aes_gcm_increment_iv_internal_call(apache_iv);
  145. return ret_status;
  146. }
  147. uint32_t create_ec_key_pair(sgx_ec256_public_t* pub_key, sgx_ec256_private_t* priv_key)
  148. {
  149. sgx_status_t se_ret; sgx_status_t se_ret2;
  150. //create ECC context
  151. sgx_ecc_state_handle_t ecc_state = NULL;
  152. se_ret = sgx_ecc256_open_context(&ecc_state);
  153. if(SGX_SUCCESS != se_ret)
  154. return se_ret;
  155. // generate private key and public key
  156. se_ret = sgx_ecc256_create_key_pair(priv_key, pub_key, ecc_state);
  157. se_ret2 = sgx_ecc256_close_context(ecc_state);
  158. if(SGX_SUCCESS != se_ret || se_ret2!= SGX_SUCCESS) // something weird has happened - couldn't shut it down.
  159. return 0xFFFFFFFF;
  160. return SGX_SUCCESS;
  161. }
  162. // todo: set to private
  163. // todo: assumes that the length of the keystring is at least 3*SGX_ECP256_KEY_SIZE
  164. 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)
  165. {
  166. if(private_public_key_string != NULL) // nowhere to serialize to
  167. {
  168. uint32_t counter;
  169. if(pub_key != NULL) // public key to serialize
  170. {
  171. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  172. *(private_public_key_string+counter)=pub_key->gx[counter];
  173. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  174. *(private_public_key_string+counter)=pub_key->gy[counter-SGX_ECP256_KEY_SIZE];
  175. }
  176. if(signing_priv_key != NULL) // private key to serialize
  177. {
  178. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  179. *(private_public_key_string+counter)=signing_priv_key->r[counter - 2*SGX_ECP256_KEY_SIZE];
  180. }
  181. }
  182. }
  183. // todo: set to private
  184. 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)
  185. {
  186. if(private_public_key_string != NULL) // nowhere to deserialize from
  187. {
  188. uint32_t counter;
  189. if(signing_priv_key != NULL)
  190. {
  191. for(counter=2*SGX_ECP256_KEY_SIZE;counter<3*SGX_ECP256_KEY_SIZE; counter++)
  192. signing_priv_key->r[counter-2*SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  193. }
  194. if(pub_key != NULL)
  195. {
  196. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  197. pub_key->gx[counter]=*(private_public_key_string+counter);
  198. for(counter=SGX_ECP256_KEY_SIZE;counter<2*SGX_ECP256_KEY_SIZE; counter++)
  199. pub_key->gy[counter-SGX_ECP256_KEY_SIZE]=*(private_public_key_string+counter);
  200. }
  201. }
  202. }
  203. uint32_t create_and_seal_ecdsa_signing_key_pair(__attribute__((unused)) sgx_ec256_public_t* pub_key, __attribute__((unused)) uint32_t* sealed_data_length,
  204. __attribute__((unused)) uint8_t* sealed_data)
  205. {
  206. uint32_t ret_status; sgx_ec256_private_t private_key; uint32_t counter;
  207. ret_status=create_ec_key_pair(pub_key, &private_key);
  208. if(ret_status!=SGX_SUCCESS)
  209. return ret_status;
  210. for(counter=0;counter<SGX_ECP256_KEY_SIZE; counter++)
  211. signing_priv_key.r[counter]=private_key.r[counter];
  212. // generating the entire string as there is no SGX function to generate the public key from the private one.
  213. uint8_t* private_public_key_string = (uint8_t*) malloc(3*SGX_ECP256_KEY_SIZE);
  214. uint8_t* sealed_data2 = (uint8_t*) malloc(*sealed_data_length);
  215. // serializing keypair to string
  216. serialize_key_pair_to_string(pub_key, &private_key, private_public_key_string);
  217. uint8_t* private_key_string = (uint8_t*) malloc(SGX_ECP256_KEY_SIZE);
  218. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  219. *(private_key_string+counter)=private_key.r[counter];
  220. // return *sealed_data_length;
  221. 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);
  222. for(counter=0;counter<*sealed_data_length;counter++)
  223. *(sealed_data+counter)=*(sealed_data2+counter);
  224. free(sealed_data2);
  225. free(private_key_string); //free(private_key);
  226. free(private_public_key_string);
  227. return ret_status; // SGX_SUCCESS;
  228. }
  229. 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)
  230. {
  231. uint32_t expected_plaintext_msg_length; uint8_t* temp_plaintext; uint32_t counter; uint32_t ret_status;
  232. expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  233. if(expected_plaintext_msg_length == 0xffffffff)
  234. return 0xFFFFFFFF;
  235. uint8_t* sealed_data2 = (uint8_t*) malloc(*sgx_sealed_data_length);
  236. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  237. {
  238. *(sealed_data2+counter)=*(sealed_data+counter);
  239. }
  240. temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
  241. ret_status = sgx_unseal_data((sgx_sealed_data_t*)sealed_data2, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
  242. if(ret_status != SGX_SUCCESS)
  243. {
  244. free(temp_plaintext);free(sealed_data2);
  245. return ret_status;
  246. }
  247. deserialize_string_to_key_pair(temp_plaintext, pub_key, &signing_priv_key);
  248. free(temp_plaintext); free(sealed_data2);
  249. return SGX_SUCCESS;
  250. }
  251. 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)
  252. {
  253. // 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.
  254. if(one_la_done < 1)
  255. 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
  256. // create key pair
  257. uint32_t ret_status = ecdh_key_gen(short_term_public_key_arr, short_term_public_key_arr + 32, short_term_private_key_arr); //create_ec_key_pair(&short_term_pub_key, &short_term_priv_key);
  258. uint32_t counter;
  259. uint32_t ret_status2;
  260. if(ret_status!=0)
  261. return ret_status;
  262. for(counter=0;counter<32;counter++)
  263. {
  264. *(signature_data + counter) = short_term_public_key_arr[counter]; // public key -> x component
  265. *(signature_data + counter + 32) = short_term_public_key_arr[counter + 32]; // public key -> y component
  266. *(signature_data + counter + 64) = 0x55; // verifier mr_enclave // TODO: fix this.
  267. }
  268. // retrieve long-term private key from global variable - apparently, need to create a local copy or it crashes
  269. sgx_ec256_private_t long_term_priv_key;
  270. for(counter=0; counter<SGX_ECP256_KEY_SIZE; counter++)
  271. long_term_priv_key.r[counter] = signing_priv_key.r[counter];
  272. // sign public key with long-term private key
  273. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  274. // TODO: For testing/checking purposes only.
  275. for(counter=0;counter<32;counter++)
  276. *(private_key+counter)=short_term_private_key_arr[counter]; //short_term_priv_key.r[counter];
  277. //// opening context for signature
  278. ret_status = sgx_ecc256_open_context(&ecc_handle);
  279. if(ret_status != SGX_SUCCESS)
  280. return ret_status;
  281. ret_status = sgx_ecdsa_sign(signature_data, 96, &long_term_priv_key, &local_signature, ecc_handle);
  282. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  283. // free(public_key_string);
  284. if(ret_status == SGX_SUCCESS)
  285. { // this only works for Little-endian architectures - need to do byte-wise swapping of the bytes obtained on RHS
  286. uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
  287. uint32_t ecdsa_sig_count;
  288. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  289. signature[31-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
  290. current_sig_byte = (uint8_t*)(&(local_signature.y));
  291. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  292. signature[63-ecdsa_sig_count]=*(current_sig_byte+ecdsa_sig_count);
  293. for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
  294. sig2->x[ecdsa_sig_count]=local_signature.x[ecdsa_sig_count];
  295. for(ecdsa_sig_count=0;ecdsa_sig_count<8;ecdsa_sig_count++)
  296. sig2->y[ecdsa_sig_count]=local_signature.y[ecdsa_sig_count];
  297. }
  298. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  299. return 0xFFFFFFFF;
  300. return 0;
  301. }
  302. uint32_t verify_mitigator_header_value(uint8_t* signature_data, uint8_t* signature, sgx_ec256_public_t* pub_key)
  303. {
  304. sgx_ec256_public_t local_pub_key; uint32_t counter; uint32_t ret_status; uint32_t ret_status2;
  305. for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
  306. {
  307. local_pub_key.gx[counter] = pub_key->gx[counter];
  308. local_pub_key.gy[counter] = pub_key->gy[counter];
  309. }
  310. sgx_ec256_signature_t local_signature; sgx_ecc_state_handle_t ecc_handle;
  311. uint8_t *current_sig_byte = (uint8_t*)(&(local_signature.x));
  312. uint32_t ecdsa_sig_count; uint8_t verification_result;
  313. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  314. *(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count];
  315. current_sig_byte = (uint8_t*)(&(local_signature.y));
  316. for(ecdsa_sig_count=0;ecdsa_sig_count<32;ecdsa_sig_count++)
  317. *(current_sig_byte+ecdsa_sig_count)=signature[ecdsa_sig_count+32];
  318. //// opening context for signature
  319. ret_status = sgx_ecc256_open_context(&ecc_handle);
  320. if(ret_status != SGX_SUCCESS)
  321. return ret_status;
  322. ret_status = sgx_ecdsa_verify(signature_data,3*SGX_ECP256_KEY_SIZE, &local_pub_key, &local_signature, &verification_result, ecc_handle);
  323. ret_status2 = sgx_ecc256_close_context(ecc_handle);
  324. if(ret_status != SGX_SUCCESS || ret_status2 != SGX_SUCCESS)
  325. return 0xFFFFFFFF;
  326. if(verification_result != SGX_EC_VALID)
  327. return 0xee;
  328. return 0;
  329. }
  330. uint32_t derive_shared_secret_for_client(uint8_t* pub_key, uint8_t* shared_key)
  331. {
  332. return 0;
  333. }
  334. uint32_t calculate_sealed_data_size( uint32_t input_size)
  335. {
  336. // *op_size=sgx_calc_sealed_data_size(0, input_size);
  337. return sgx_calc_sealed_data_size(0, input_size);
  338. }
  339. // ip_key will always be within the enclave.
  340. // enc = 1 for encryption and 0 for decryption, like openssl api
  341. uint32_t aes_gcm_192_call(uint32_t enc, uint8_t* ip_key, uint8_t* ip_iv, uint8_t* ip_ciphertext, uint32_t ip_ciphertext_len, uint8_t* op_plaintext, uint32_t* op_plaintext_length, uint8_t* tag)
  342. {
  343. uint32_t counter;
  344. if(ip_ciphertext == NULL)
  345. return 0x33;
  346. if(tag == NULL)
  347. return 0x34;
  348. if(op_plaintext == NULL)
  349. return 0x36;
  350. if(ip_key == NULL)
  351. return 0x35;
  352. if(ip_iv == NULL)
  353. return 0x37;
  354. uint8_t* ip_ciphertext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
  355. memcpy_equivalent_copy(ip_ciphertext_in_enclave, ip_ciphertext, ip_ciphertext_len);
  356. uint8_t tag_in_enclave [16];
  357. if(!enc)
  358. memcpy_equivalent_copy(tag_in_enclave, tag, 16);
  359. uint8_t* op_plaintext_in_enclave = (uint8_t*) malloc(ip_ciphertext_len);
  360. uint32_t internal_ret_status;
  361. if(enc)
  362. 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);
  363. else
  364. 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);
  365. if(internal_ret_status == 0)
  366. {
  367. memcpy_equivalent_copy(op_plaintext, op_plaintext_in_enclave, ip_ciphertext_len);
  368. if(enc)
  369. memcpy_equivalent_copy(tag, tag_in_enclave, 16);
  370. *op_plaintext_length = ip_ciphertext_len;
  371. }
  372. free(ip_ciphertext_in_enclave); free(op_plaintext_in_enclave);
  373. return internal_ret_status;
  374. }
  375. void memcpy_equivalent_copy(uint8_t* dest, uint8_t* src, uint32_t length)
  376. {
  377. uint32_t counter;
  378. for(counter=0; counter<length; counter++)
  379. *(dest + counter) = *(src + counter);
  380. }
  381. // Output buffer should be of length at least equal to the second argument.
  382. uint32_t decrypt_client_data(unsigned char* ip_encrypted_client_pub_key_and_data, uint32_t ip_encrypted_client_pub_key_and_data_length, unsigned char* op_client_data, uint8_t* clen)
  383. {
  384. unsigned int counter; unsigned long check_ret; uint32_t ret;
  385. unsigned char derived_key[32];
  386. unsigned char* plaintext_client_public_key;
  387. unsigned char* plaintext_client_data;
  388. unsigned char* client_data_encrypted_to_decryptor;
  389. uint32_t client_data_encrypted_to_decryptor_length;
  390. unsigned char* tag_for_client_data_encrypted_to_decryptor;
  391. unsigned char* client_data_encrypted_to_apache;
  392. uint32_t client_data_encrypted_to_apache_length;
  393. unsigned char tag_for_client_data_encrypted_to_apache[16];
  394. unsigned char* temp_array;
  395. uint32_t temp_array_valid_length;
  396. unsigned char client_iv[12]={0,0,0,0, 0,0,0,0, 0,0,0,0};
  397. for(counter=0;counter<ip_encrypted_client_pub_key_and_data_length;counter++)
  398. op_client_data[counter]=ip_encrypted_client_pub_key_and_data[counter];
  399. *clen = (uint8_t) ip_encrypted_client_pub_key_and_data_length;
  400. temp_array = (unsigned char*) malloc(ip_encrypted_client_pub_key_and_data_length);
  401. // TODO: Remove aes_gcm_internal_call function - upgrade to using openssl's aesgcm 192 bit enc
  402. // TODO: Change the returned 2nd length o/p to be incoming length - tag length for decryption and incoming length + 16 for encryption
  403. ret = aes_gcm_192_call(0, apache_key, apache_iv, ip_encrypted_client_pub_key_and_data, ip_encrypted_client_pub_key_and_data_length, temp_array, &temp_array_valid_length, ip_encrypted_client_pub_key_and_data + ip_encrypted_client_pub_key_and_data_length - 16);
  404. if(ret != 0)
  405. {
  406. free(temp_array);
  407. return ret;
  408. }
  409. // Temp_array = {X component of public key (32 bytes), Y component of public key (32 bytes), client data encrypted to decryptor (x), tag for client data encrypted to decryptor (16 bytes)
  410. // therefore, length x = temp_array_valid_length - 64 (for public key) - 16 (for own tag)
  411. plaintext_client_public_key = temp_array;
  412. client_data_encrypted_to_decryptor = temp_array + 64;
  413. client_data_encrypted_to_decryptor_length = temp_array_valid_length - 64 - 16;
  414. tag_for_client_data_encrypted_to_decryptor = temp_array + 64 + client_data_encrypted_to_decryptor_length;
  415. check_ret = compute_ecdh_shared_key(plaintext_client_public_key, plaintext_client_public_key + 32, short_term_private_key_arr, derived_key);
  416. if(check_ret != 0)
  417. {
  418. plaintext_client_public_key = NULL;
  419. client_data_encrypted_to_decryptor = NULL;
  420. tag_for_client_data_encrypted_to_decryptor = NULL;
  421. free(temp_array);
  422. return check_ret;
  423. }
  424. check_ret = aes_gcm(0, derived_key, client_iv, client_data_encrypted_to_decryptor, client_data_encrypted_to_decryptor_length, temp_array, &temp_array_valid_length, tag_for_client_data_encrypted_to_decryptor);
  425. if(check_ret != 0)
  426. {
  427. plaintext_client_public_key = NULL;
  428. client_data_encrypted_to_decryptor = NULL;
  429. tag_for_client_data_encrypted_to_decryptor = NULL;
  430. free(temp_array);
  431. return check_ret;
  432. }
  433. client_data_encrypted_to_apache = (uint8_t*) malloc(temp_array_valid_length);
  434. ret = aes_gcm_192_call(1, apache_key, apache_iv, temp_array, temp_array_valid_length, client_data_encrypted_to_apache, &client_data_encrypted_to_apache_length, tag_for_client_data_encrypted_to_apache);
  435. if(ret == 0)
  436. {
  437. for(counter=0; counter<client_data_encrypted_to_apache_length; counter++)
  438. op_client_data[counter] = client_data_encrypted_to_apache[counter];
  439. for(counter=0; counter<16; counter++)
  440. op_client_data[counter] = tag_for_client_data_encrypted_to_apache[counter];
  441. *clen = (uint8_t) client_data_encrypted_to_apache_length + 16; // Need to give in the total wire length
  442. }
  443. plaintext_client_public_key = NULL;
  444. client_data_encrypted_to_decryptor = NULL;
  445. tag_for_client_data_encrypted_to_decryptor = NULL;
  446. free(temp_array);
  447. free(client_data_encrypted_to_apache);
  448. return ret;
  449. }