Decryptor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "Decryptor.h"
  2. #include "sgx_tseal.h"
  3. #include "sgx_tcrypto.h"
  4. #include "sgx_dh.h"
  5. #include "datatypes.h"
  6. #include "error_codes.h"
  7. ECDSASignatureBox Decryptor::signatureBox;
  8. HybridEncryptionBox Decryptor::hybridEncryptionBoxClient;
  9. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxApache;
  10. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxVerifier;
  11. uint8_t Decryptor::verifier_mr_enclave[32] = {0};
  12. uint8_t Decryptor::apache_mr_signer[32] = {0};
  13. unsigned int successful_la_count;
  14. uint8_t Decryptor::plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {0};
  15. uint8_t Decryptor::first_decryption_output[1092] = {0};
  16. uint8_t Decryptor::plaintext_client_data[1000] = {0};
  17. // INTERNAL
  18. uint32_t Decryptor::create_mitigator_token_M(uint8_t* token)
  19. {
  20. uint32_t internal_return_status;
  21. uint32_t counter;
  22. // create short-term ECDH key pair
  23. internal_return_status = hybridEncryptionBoxClient.generate_keypair();
  24. if(internal_return_status != 0)
  25. return internal_return_status;
  26. hybridEncryptionBoxClient.get_public_key(token);
  27. // create token: concatenate short-term keypair with verifiers mrenclave.
  28. for(counter=0;counter<32;counter++)
  29. *(token + counter + ECDH_PUBLIC_KEY_SIZE) = verifier_mr_enclave[counter];
  30. return 0;
  31. }
  32. // INTERNAL
  33. uint32_t Decryptor::create_mitigator_header_H(uint8_t* signature_data_and_signature)
  34. {
  35. uint32_t internal_return_status;
  36. uint8_t local_signature_data_and_signature[ECDH_PUBLIC_KEY_SIZE + 32 + 64];
  37. uint32_t counter;
  38. internal_return_status = Decryptor::create_mitigator_token_M(local_signature_data_and_signature);
  39. if(internal_return_status != 0x0)
  40. return internal_return_status;
  41. 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);
  42. if(internal_return_status != 0x0)
  43. return internal_return_status;
  44. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE + 32 + 64;counter++)
  45. signature_data_and_signature[counter] = local_signature_data_and_signature[counter];
  46. return 0;
  47. }
  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. // TODO: Edit to process a list of fields.
  74. uint32_t Decryptor::initialize_symmetric_key_decrypt_client_data(uint8_t* plaintext_client_public_key_plus_encrypted_data_plus_tag, uint32_t total_length, uint8_t* plaintext_client_data, uint32_t* plaintext_client_data_length)
  75. {
  76. uint8_t* ciphertext_plus_tag;
  77. uint32_t ciphertext_plus_tag_length;
  78. uint32_t internal_return_status;
  79. // I will derive a shared key from the plaintext_client_public_key
  80. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  81. if(internal_return_status != 0)
  82. return internal_return_status;
  83. // and then I will decrypt the rest of the client data with that key.
  84. ciphertext_plus_tag = plaintext_client_public_key_plus_encrypted_data_plus_tag + ECDH_PUBLIC_KEY_SIZE;
  85. ciphertext_plus_tag_length = total_length - ECDH_PUBLIC_KEY_SIZE;
  86. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(0, ciphertext_plus_tag, ciphertext_plus_tag_length, plaintext_client_data, plaintext_client_data_length);
  87. return internal_return_status;
  88. }
  89. void Decryptor::testing_long_term_verification_key(uint8_t* output)
  90. {
  91. uint8_t keypair[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  92. uint32_t counter;
  93. signatureBox.get_keypair(keypair);
  94. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE; counter++)
  95. output[counter]=keypair[ECDH_PRIVATE_KEY_SIZE+counter];
  96. }
  97. // EXTERNAL. DONE.
  98. uint32_t Decryptor::create_and_seal_long_term_signing_key_pair(uint32_t* sealed_data_length, uint8_t* sealed_data)
  99. {
  100. uint32_t sgx_libcall_status;
  101. uint32_t internal_return_status;
  102. uint32_t temp_sealed_data_length;
  103. uint8_t* temp_sealed_data;
  104. uint8_t private_public_key_string[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  105. uint32_t counter;
  106. temp_sealed_data_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  107. if(temp_sealed_data_length == 0xFFFFFFFF)
  108. return 0x01;
  109. temp_sealed_data = (uint8_t*) malloc(temp_sealed_data_length);
  110. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  111. if(internal_return_status != 0)
  112. {
  113. free(temp_sealed_data);
  114. return internal_return_status;
  115. }
  116. 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);
  117. if(sgx_libcall_status != SGX_SUCCESS)
  118. {
  119. free(temp_sealed_data);
  120. return sgx_libcall_status;
  121. }
  122. for(counter=0;counter<temp_sealed_data_length;counter++)
  123. *(sealed_data + counter)= *(temp_sealed_data + counter);
  124. *sealed_data_length = temp_sealed_data_length;
  125. free(temp_sealed_data);
  126. return 0;
  127. }
  128. // EXTERNAL. DONE.
  129. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  130. {
  131. uint32_t temp_plaintext_length;
  132. uint8_t* temp_plaintext;
  133. uint32_t counter;
  134. uint32_t ret_status;
  135. uint8_t* temp_sealed_data ;
  136. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  137. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  138. *(temp_sealed_data+counter)=*(sealed_data+counter);
  139. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  140. if(temp_plaintext_length == 0xffffffff)
  141. return 0xFFFFFFFF;
  142. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  143. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  144. free(temp_sealed_data);
  145. if(ret_status != SGX_SUCCESS)
  146. {
  147. free(temp_plaintext);
  148. return ret_status;
  149. }
  150. signatureBox.set_private_public_key(temp_plaintext, temp_plaintext + ECDH_PRIVATE_KEY_SIZE);
  151. free(temp_plaintext);
  152. return 0;
  153. }
  154. uint32_t Decryptor::process_verifiers_message(uint8_t* input_ciphertext_plus_tag, uint32_t length)
  155. {
  156. uint8_t first_decryption_output[150];
  157. uint32_t first_decryption_output_length;
  158. uint32_t internal_return_status;
  159. uint32_t counter;
  160. 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.
  161. return 0x23;
  162. internal_return_status = symmetricEncryptionBoxVerifier.encrypt_decrypt(0, input_ciphertext_plus_tag, length, first_decryption_output, &first_decryption_output_length);
  163. if(internal_return_status != 0)
  164. return internal_return_status;
  165. if(first_decryption_output_length != 32)
  166. return 0x33;
  167. for(counter=0; counter<32; counter++)
  168. apache_mr_signer[counter] = *(first_decryption_output + counter);
  169. return 0;
  170. }
  171. uint32_t Decryptor::encrypt_decrypt_fields_list(SymmetricEncryptionBox &symmetricEncryptionBox , uint32_t encrypt_decrypt,
  172. uint8_t *input_fields_list, uint32_t *input_field_sizes,
  173. uint32_t no_of_fields,
  174. uint8_t *output_fields_list,
  175. uint32_t *output_field_sizes, uint32_t* total_output_length) {
  176. // Enc-dec loops over no_of_fields.
  177. uint32_t field_counter, input_byte_accumulator=0, byte_counter, field_size, output_byte_accumulator=0;
  178. uint8_t* input_field, *output_field;
  179. uint32_t input_field_length, output_field_length, internal_return_status, counter;
  180. output_field = output_fields_list;
  181. for(field_counter=0; field_counter< no_of_fields; field_counter++)
  182. {
  183. input_field_length = input_field_sizes[field_counter];
  184. input_field = input_fields_list + input_byte_accumulator;
  185. output_field = output_fields_list + output_byte_accumulator;
  186. internal_return_status = symmetricEncryptionBox.encrypt_decrypt(encrypt_decrypt, input_field, input_field_length, output_field, &output_field_length);
  187. if(internal_return_status != 0)
  188. return internal_return_status;
  189. output_field_sizes[field_counter] = output_field_length;
  190. input_byte_accumulator += input_field_length;
  191. output_byte_accumulator += output_field_length;
  192. }
  193. *total_output_length = output_byte_accumulator;
  194. return 0;
  195. }
  196. uint32_t Decryptor::decrypt_client_data(uint8_t *input_fields_list, uint32_t *input_field_sizes,
  197. uint32_t no_of_fields, uint32_t total_input_size,
  198. uint8_t *output_fields_list,
  199. uint32_t *output_field_sizes) {
  200. uint8_t *intermediate_fields_list, *plaintext_fields_list, *temp_output_list;
  201. uint32_t *intermediate_fields_sizes, *plaintext_fields_sizes, *temp_output_sizes;
  202. uint32_t internal_return_status, total_intermediate_fields_length;
  203. uint32_t total_plaintext_fields_length, total_output_fields_length, counter;
  204. // List of symmetric decryption outputs - will get rid of 12 bytes of IV and 16 bytes of tag.
  205. intermediate_fields_list = (uint8_t*) malloc(total_input_size ); // - (28 * no_of_fields)
  206. intermediate_fields_sizes = (uint32_t*) malloc(no_of_fields);
  207. // Remove outermost layer of decryption, namely the one with the target enclave.
  208. internal_return_status = encrypt_decrypt_fields_list(symmetricEncryptionBoxApache, 0, input_fields_list,
  209. input_field_sizes, no_of_fields,
  210. intermediate_fields_list, intermediate_fields_sizes, &total_intermediate_fields_length);
  211. if(internal_return_status != 0)
  212. return internal_return_status;
  213. // Establish a symmetric encryption key with the client, using the client's public key.
  214. // TODO: Change this function to take in the key length.
  215. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(intermediate_fields_list);
  216. if(internal_return_status != 0)
  217. return internal_return_status;
  218. // The public key, which was the first element of the output list above, has been processed.
  219. // Do not attempt to decrypt it one more time.
  220. intermediate_fields_list += intermediate_fields_sizes[0];
  221. intermediate_fields_sizes += 1;
  222. no_of_fields -= 1;
  223. total_intermediate_fields_length -= intermediate_fields_sizes[0];
  224. // Initializing lists for the plaintext data.
  225. plaintext_fields_list = (uint8_t*) malloc(total_intermediate_fields_length);
  226. plaintext_fields_sizes = (uint32_t*) malloc(no_of_fields);
  227. // Obtain plaintext data by decrypting with the above newly-established key with the client.
  228. internal_return_status = encrypt_decrypt_fields_list(hybridEncryptionBoxClient, 0,
  229. intermediate_fields_list, intermediate_fields_sizes, no_of_fields,
  230. plaintext_fields_list, plaintext_fields_sizes, &total_plaintext_fields_length);
  231. if(internal_return_status != 0)
  232. return internal_return_status;
  233. temp_output_list = (uint8_t*) malloc(total_input_size);
  234. temp_output_sizes = (uint32_t*) malloc(no_of_fields);
  235. // Finally, encrypt the plaintext back to the target enclave.
  236. internal_return_status = encrypt_decrypt_fields_list(symmetricEncryptionBoxApache, 1, plaintext_fields_list, plaintext_fields_sizes,
  237. no_of_fields, temp_output_list, temp_output_sizes, &total_output_fields_length);
  238. if(internal_return_status != 0)
  239. return internal_return_status;
  240. for(counter=0; counter<total_output_fields_length; counter++)
  241. output_fields_list[counter] = temp_output_list[counter];
  242. for(counter=0;counter<no_of_fields; counter++)
  243. output_field_sizes[counter] = temp_output_sizes[counter];
  244. return 0;
  245. }
  246. 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)
  247. {
  248. uint32_t first_decryption_output_length, plaintext_client_data_length;
  249. uint32_t internal_return_status;
  250. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  251. // first, I decrypt the message from the target enclave, to get the client's public key and ciphertext data (and tag and IV)
  252. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length, first_decryption_output, &first_decryption_output_length);
  253. if(internal_return_status != 0)
  254. return internal_return_status;
  255. // then I obtain the plaintext client data, using the client's public key and own key to ultimately decrypt the client's ciphertext data
  256. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  257. if(internal_return_status != 0)
  258. return internal_return_status;
  259. // then I will encrypt the plaintext data to the target enclave.
  260. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  261. return internal_return_status;
  262. }
  263. // INTERNAL.
  264. uint32_t Decryptor::verify_peer_enclave_trust(uint8_t* given_mr_enclave, uint8_t* given_mr_signer, uint8_t* dhaek)
  265. {
  266. uint32_t count;
  267. uint32_t internal_return_status;
  268. if(successful_la_count == 0) // verifier enclave
  269. {
  270. for(count=0; count<SGX_HASH_SIZE; count++)
  271. verifier_mr_enclave[count] = given_mr_enclave[count];
  272. symmetricEncryptionBoxVerifier.set_symmetric_key(dhaek);
  273. }
  274. else // apache enclave
  275. {
  276. /*
  277. for(count=0; count<SGX_HASH_SIZE; count++)
  278. {
  279. if( given_mr_signer[count] != apache_mr_signer[count] )
  280. return ENCLAVE_TRUST_ERROR;
  281. }
  282. */
  283. symmetricEncryptionBoxApache.set_symmetric_key(dhaek);
  284. }
  285. successful_la_count ++;
  286. return SGX_SUCCESS;
  287. }
  288. void Decryptor::calculate_sealed_keypair_size(uint32_t* output_length)
  289. {
  290. *output_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  291. }
  292. void Decryptor::testing_get_verifier_mrenclave_apache_mrsigner(uint8_t* output)
  293. {
  294. uint32_t counter;
  295. for(counter=0; counter<32;counter++)
  296. {
  297. output[counter]=verifier_mr_enclave[counter];
  298. output[counter+32]=apache_mr_signer[counter];
  299. }
  300. }
  301. void Decryptor::testing_get_short_term_public_key(uint8_t* output)
  302. {
  303. hybridEncryptionBoxClient.get_public_key(output);
  304. }
  305. void Decryptor::testing_get_apache_iv(uint8_t* op)
  306. {
  307. // symmetricEncryptionBoxApache.get_iv(op);
  308. }