Decryptor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "../TrustedInclude/Decryptor.h"
  2. #include "sgx_tseal.h"
  3. #include "sgx_tcrypto.h"
  4. #include "sgx_dh.h"
  5. #include "../TrustedInclude/error_codes.h"
  6. ECDSASignatureBox Decryptor::signatureBox;
  7. HybridEncryptionBox Decryptor::hybridEncryptionBoxClient;
  8. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxApache;
  9. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxVerifier;
  10. uint8_t Decryptor::verifier_mr_enclave[32] = {0};
  11. uint8_t Decryptor::apache_mr_signer[32] = {0};
  12. unsigned int successful_la_count;
  13. uint8_t Decryptor::plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {0};
  14. uint8_t Decryptor::first_decryption_output[1092] = {0};
  15. uint8_t Decryptor::plaintext_client_data[1000] = {0};
  16. // INTERNAL
  17. uint32_t Decryptor::create_mitigator_token_M(uint8_t* token)
  18. {
  19. uint32_t internal_return_status;
  20. uint32_t counter;
  21. // create short-term ECDH key pair
  22. internal_return_status = hybridEncryptionBoxClient.generate_keypair();
  23. if(internal_return_status != 0)
  24. return internal_return_status;
  25. hybridEncryptionBoxClient.get_public_key(token);
  26. // create token: concatenate short-term keypair with verifiers mrenclave.
  27. for(counter=0;counter<32;counter++)
  28. *(token + counter + ECDH_PUBLIC_KEY_SIZE) = verifier_mr_enclave[counter];
  29. return 0;
  30. }
  31. // INTERNAL
  32. uint32_t Decryptor::create_mitigator_header_H(uint8_t* signature_data_and_signature)
  33. {
  34. uint32_t internal_return_status;
  35. uint8_t local_signature_data_and_signature[ECDH_PUBLIC_KEY_SIZE + 32 + 64];
  36. uint32_t counter;
  37. internal_return_status = Decryptor::create_mitigator_token_M(local_signature_data_and_signature);
  38. if(internal_return_status != 0x0)
  39. return internal_return_status;
  40. internal_return_status = signatureBox.sign(local_signature_data_and_signature, ECDH_PUBLIC_KEY_SIZE + 32, local_signature_data_and_signature + ECDH_PUBLIC_KEY_SIZE + 32);
  41. if(internal_return_status != 0x0)
  42. return internal_return_status;
  43. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE + 32 + 64;counter++)
  44. signature_data_and_signature[counter] = local_signature_data_and_signature[counter];
  45. return 0;
  46. }
  47. // EXTERNAL ECALL.
  48. uint32_t Decryptor::create_and_encrypt_mitigator_header_H(uint8_t* ciphertext_token_H_plus_tag, uint32_t* op_length)
  49. {
  50. uint32_t temp_ciphertext_token_H_iv_tag_length;
  51. uint32_t internal_return_status;
  52. uint8_t plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {0x42};
  53. if(successful_la_count != 2)
  54. return 0x33;
  55. internal_return_status = create_mitigator_header_H(plaintext_mitigator_header_H);
  56. if(internal_return_status != 0)
  57. return internal_return_status;
  58. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_mitigator_header_H, ECDH_PUBLIC_KEY_SIZE + 32 + 64, ciphertext_token_H_plus_tag, &temp_ciphertext_token_H_iv_tag_length);
  59. *op_length = temp_ciphertext_token_H_iv_tag_length;
  60. return internal_return_status;
  61. }
  62. // INTERNAL. done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  63. uint32_t Decryptor::create_long_term_signing_keypair(uint8_t* private_public_key_string)
  64. {
  65. uint32_t internal_return_status;
  66. internal_return_status = signatureBox.generate_keypair();
  67. if(internal_return_status != 0)
  68. return internal_return_status;
  69. signatureBox.get_keypair(private_public_key_string);
  70. return 0;
  71. }
  72. // INTERNAL.
  73. uint32_t Decryptor::initialize_symmetric_key_decrypt_client_data(
  74. uint8_t* client_public_key_plus_ciphertext_fields, uint32_t* client_public_key_plus_ciphertext_fields_lengths,
  75. uint32_t number_of_ciphertext_fields,
  76. uint8_t* plaintext_fields, uint32_t* plaintext_field_lengths)
  77. {
  78. uint8_t *ciphertext_field_ptr, *plaintext_field_ptr;
  79. uint32_t plaintext_field_length, ciphertext_field_length, internal_return_status, counter;
  80. // I will derive a shared key from the plaintext_client_public_key
  81. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(client_public_key_plus_ciphertext_fields);
  82. if(internal_return_status != 0)
  83. return internal_return_status;
  84. // and then I will decrypt the rest of the client data with that key.
  85. ciphertext_field_ptr = client_public_key_plus_ciphertext_fields + client_public_key_plus_ciphertext_fields_lengths[0]; // tag = 16 bytes, iv = 12 bytes.
  86. plaintext_field_ptr = plaintext_fields;
  87. for(counter=0; counter<number_of_ciphertext_fields; counter++)
  88. {
  89. ciphertext_field_length = client_public_key_plus_ciphertext_fields_lengths[counter+1];
  90. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(0, ciphertext_field_ptr,
  91. ciphertext_field_length, plaintext_field_ptr, &plaintext_field_length);
  92. if(internal_return_status != 0)
  93. return internal_return_status;
  94. plaintext_field_ptr += plaintext_field_length;
  95. ciphertext_field_ptr += ciphertext_field_length;
  96. plaintext_field_lengths[counter] = plaintext_field_length;
  97. }
  98. return internal_return_status;
  99. }
  100. void Decryptor::testing_long_term_verification_key(uint8_t* output)
  101. {
  102. uint8_t keypair[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  103. uint32_t counter;
  104. signatureBox.get_keypair(keypair);
  105. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE; counter++)
  106. output[counter]=keypair[ECDH_PRIVATE_KEY_SIZE+counter];
  107. }
  108. // EXTERNAL. DONE.
  109. uint32_t Decryptor::create_and_seal_long_term_signing_key_pair(size_t* sealed_data_length, uint8_t* sealed_data)
  110. {
  111. uint32_t sgx_libcall_status;
  112. uint32_t internal_return_status;
  113. uint32_t temp_sealed_data_length;
  114. uint8_t* temp_sealed_data;
  115. uint8_t private_public_key_string[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  116. uint32_t counter;
  117. temp_sealed_data_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  118. if(temp_sealed_data_length == 0xFFFFFFFF)
  119. return 0x01;
  120. temp_sealed_data = (uint8_t*) malloc(temp_sealed_data_length);
  121. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  122. if(internal_return_status != 0)
  123. {
  124. free(temp_sealed_data);
  125. return internal_return_status;
  126. }
  127. sgx_libcall_status = sgx_seal_data(0, NULL, 3*SGX_ECP256_KEY_SIZE, private_public_key_string, temp_sealed_data_length, (sgx_sealed_data_t*) temp_sealed_data);
  128. if(sgx_libcall_status != SGX_SUCCESS)
  129. {
  130. free(temp_sealed_data);
  131. return sgx_libcall_status;
  132. }
  133. for(counter=0;counter<temp_sealed_data_length;counter++)
  134. *(sealed_data + counter)= *(temp_sealed_data + counter);
  135. *sealed_data_length = temp_sealed_data_length;
  136. free(temp_sealed_data);
  137. return 0;
  138. }
  139. // EXTERNAL. DONE.
  140. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, size_t* sgx_sealed_data_length)
  141. {
  142. uint32_t temp_plaintext_length;
  143. uint8_t* temp_plaintext;
  144. uint32_t counter;
  145. uint32_t ret_status;
  146. uint8_t* temp_sealed_data ;
  147. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  148. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  149. *(temp_sealed_data+counter)=*(sealed_data+counter);
  150. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  151. if(temp_plaintext_length == 0xffffffff)
  152. return 0xFFFFFFFF;
  153. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  154. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  155. free(temp_sealed_data);
  156. if(ret_status != SGX_SUCCESS)
  157. {
  158. free(temp_plaintext);
  159. return ret_status;
  160. }
  161. signatureBox.set_private_public_key(temp_plaintext, temp_plaintext + ECDH_PRIVATE_KEY_SIZE);
  162. free(temp_plaintext);
  163. return 0;
  164. }
  165. uint32_t Decryptor::process_verifiers_message(uint8_t* input_ciphertext_plus_tag, uint32_t length)
  166. {
  167. uint8_t first_decryption_output[150];
  168. uint32_t first_decryption_output_length;
  169. uint32_t internal_return_status;
  170. uint32_t counter;
  171. if(successful_la_count != 1) // else, the untrusted application can call this on the first message by apache and cause the verifier to set its mrsigner.
  172. return 0x23;
  173. internal_return_status = symmetricEncryptionBoxVerifier.encrypt_decrypt(0, input_ciphertext_plus_tag, length, first_decryption_output, &first_decryption_output_length);
  174. if(internal_return_status != 0)
  175. return internal_return_status;
  176. if(first_decryption_output_length != 32)
  177. return 0x33;
  178. for(counter=0; counter<32; counter++)
  179. apache_mr_signer[counter] = *(first_decryption_output + counter);
  180. return 0;
  181. }
  182. // INTERNAL.
  183. uint32_t Decryptor::encrypt_decrypt_to_apache(uint32_t encrypt_decrypt,
  184. uint8_t* double_ciphertext,
  185. uint32_t* double_ciphertext_fields_lengths,
  186. uint32_t number_of_double_ciphertext_fields,
  187. uint8_t* ciphertext,
  188. uint32_t* ciphertext_length,
  189. uint32_t* ciphertext_fields_lengths)
  190. {
  191. uint32_t ciphertext_field_length, field_counter, temp_total_ciphertext_length, internal_return_status;
  192. unsigned char *ciphertext_arr_ptr, *double_ciphertext_arr_ptr;
  193. double_ciphertext_arr_ptr = double_ciphertext;
  194. ciphertext_arr_ptr = ciphertext;
  195. // decrypt each set of bytes of length input_sizes_array[counter] of the input_ciphertext
  196. for(field_counter=0; field_counter<number_of_double_ciphertext_fields; field_counter++)
  197. {
  198. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(encrypt_decrypt, double_ciphertext_arr_ptr,
  199. double_ciphertext_fields_lengths[field_counter],
  200. ciphertext_arr_ptr, &ciphertext_field_length);
  201. if(internal_return_status != 0)
  202. return internal_return_status;
  203. double_ciphertext_arr_ptr += double_ciphertext_fields_lengths[field_counter];
  204. ciphertext_arr_ptr += ciphertext_field_length;
  205. ciphertext_fields_lengths[field_counter] = ciphertext_field_length;
  206. temp_total_ciphertext_length += ciphertext_field_length;
  207. }
  208. *ciphertext_length = temp_total_ciphertext_length;
  209. return 0;
  210. }
  211. uint32_t Decryptor::process_apache_message_generate_response(
  212. uint8_t* double_ciphertext,
  213. uint32_t double_ciphertext_length,
  214. uint32_t* double_ciphertext_fields_lengths,
  215. uint32_t number_of_double_ciphertext_fields,
  216. uint8_t* output_ciphertext,
  217. uint32_t* output_sizes_array)
  218. {
  219. unsigned char *incoming_ciphertext, *plaintext;
  220. uint32_t *incoming_ciphertext_fields_lengths, *plaintext_fields_lengths;
  221. uint32_t internal_return_status, output_ciphertext_length, ciphertext_length;
  222. // UPPER BOUND - technically: double_ciphertext_length - (number_of_double_ciphertext_fields * 28)
  223. incoming_ciphertext = (unsigned char*) malloc(double_ciphertext_length);
  224. incoming_ciphertext_fields_lengths = (uint32_t*) malloc(number_of_double_ciphertext_fields);
  225. internal_return_status = encrypt_decrypt_to_apache(0,
  226. double_ciphertext,
  227. double_ciphertext_fields_lengths,
  228. number_of_double_ciphertext_fields,
  229. incoming_ciphertext,
  230. &ciphertext_length,
  231. incoming_ciphertext_fields_lengths);
  232. if(internal_return_status != 0)
  233. {
  234. free(incoming_ciphertext);
  235. free(incoming_ciphertext_fields_lengths);
  236. return internal_return_status;
  237. }
  238. plaintext = (unsigned char*) malloc(ciphertext_length);
  239. plaintext_fields_lengths = (uint32_t*) malloc(number_of_double_ciphertext_fields); // -1 technically.
  240. internal_return_status=initialize_symmetric_key_decrypt_client_data(
  241. incoming_ciphertext, incoming_ciphertext_fields_lengths,
  242. number_of_double_ciphertext_fields - 1,
  243. plaintext, plaintext_fields_lengths);
  244. free(incoming_ciphertext);
  245. free(incoming_ciphertext_fields_lengths);
  246. if(internal_return_status != 0)
  247. {
  248. free(plaintext);
  249. free(plaintext_fields_lengths);
  250. return internal_return_status;
  251. }
  252. internal_return_status = encrypt_decrypt_to_apache(1,
  253. plaintext,
  254. plaintext_fields_lengths,
  255. number_of_double_ciphertext_fields-1,
  256. output_ciphertext,
  257. &output_ciphertext_length,
  258. output_sizes_array);
  259. free(plaintext);
  260. free(plaintext_fields_lengths);
  261. return internal_return_status;
  262. }
  263. /*
  264. // EXTERNAL. DONE.
  265. uint32_t Decryptor::process_apache_message_generate_response(uint8_t* input_ciphertext, uint32_t input_ciphertext_plus_tag_length, uint8_t* output_ciphertext_plus_tag, uint32_t* output_ciphertext_plus_tag_length)
  266. {
  267. uint32_t first_decryption_output_length, plaintext_client_data_length;
  268. uint32_t internal_return_status;
  269. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  270. // first, I decrypt the message from the target enclave, to get the client's public key and ciphertext data (and tag and IV)
  271. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length,
  272. first_decryption_output, &first_decryption_output_length);
  273. if(internal_return_status != 0)
  274. return internal_return_status;
  275. // then I obtain the plaintext client data, using the client's public key and own key to ultimately decrypt the client's ciphertext data
  276. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  277. if(internal_return_status != 0)
  278. return internal_return_status;
  279. // then I will encrypt the plaintext data to the target enclave.
  280. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  281. return internal_return_status;
  282. }
  283. */
  284. // INTERNAL.
  285. uint32_t Decryptor::verify_peer_enclave_trust(uint8_t* given_mr_enclave, uint8_t* given_mr_signer, uint8_t* dhaek)
  286. {
  287. uint32_t count;
  288. uint32_t internal_return_status;
  289. if(successful_la_count == 0) // verifier enclave
  290. {
  291. for(count=0; count<SGX_HASH_SIZE; count++)
  292. verifier_mr_enclave[count] = given_mr_enclave[count];
  293. symmetricEncryptionBoxVerifier.set_symmetric_key(dhaek);
  294. }
  295. else // apache enclave
  296. {
  297. for(count=0; count<SGX_HASH_SIZE; count++)
  298. {
  299. if( given_mr_signer[count] != apache_mr_signer[count] )
  300. return ENCLAVE_TRUST_ERROR;
  301. }
  302. symmetricEncryptionBoxApache.set_symmetric_key(dhaek);
  303. }
  304. successful_la_count ++;
  305. return SGX_SUCCESS;
  306. }
  307. void Decryptor::calculate_sealed_keypair_size(size_t* output_length)
  308. {
  309. *output_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  310. }
  311. void Decryptor::testing_get_verifier_mrenclave_apache_mrsigner(uint8_t* output)
  312. {
  313. uint32_t counter;
  314. for(counter=0; counter<32;counter++)
  315. {
  316. output[counter]=verifier_mr_enclave[counter];
  317. output[counter+32]=apache_mr_signer[counter];
  318. }
  319. }
  320. void Decryptor::testing_get_short_term_public_key(uint8_t* output)
  321. {
  322. hybridEncryptionBoxClient.get_public_key(output);
  323. }
  324. void Decryptor::testing_get_apache_iv(uint8_t* op)
  325. {
  326. // symmetricEncryptionBoxApache.get_iv(op);
  327. }
  328. uint32_t Decryptor::session_request(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id)
  329. {
  330. return LA::session_request(dh_msg1, session_id);
  331. }
  332. uint32_t Decryptor::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t *session_id)
  333. {
  334. sgx_key_128bit_t dh_aek;
  335. sgx_dh_session_enclave_identity_t initiator_identity;
  336. uint32_t la_ret_status = LA::exchange_report(dh_msg2, dh_msg3, session_id, &dh_aek, &initiator_identity);
  337. if(la_ret_status == 0)
  338. {
  339. return Decryptor::verify_peer_enclave_trust(initiator_identity.mr_enclave.m,
  340. initiator_identity.mr_signer.m,
  341. dh_aek);
  342. }
  343. else
  344. return la_ret_status;
  345. }