Decryptor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "Decryptor.h"
  32. #include "sgx_tseal.h"
  33. #include "sgx_tcrypto.h"
  34. #include "sgx_dh.h"
  35. #include "datatypes.h"
  36. #include "error_codes.h"
  37. ECDSASignatureBox Decryptor::signatureBox;
  38. HybridEncryptionBox Decryptor::hybridEncryptionBoxClient;
  39. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxApache;
  40. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxVerifier;
  41. uint8_t Decryptor::verifier_mr_enclave[32] = {0};
  42. uint8_t Decryptor::apache_mr_signer[32] = {0};
  43. unsigned int successful_la_count;
  44. uint8_t Decryptor::plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {0};
  45. uint8_t Decryptor::first_decryption_output[1092] = {0};
  46. uint8_t Decryptor::plaintext_client_data[1000] = {0};
  47. // INTERNAL
  48. uint32_t Decryptor::create_mitigator_token_M(uint8_t* token)
  49. {
  50. uint32_t internal_return_status;
  51. uint32_t counter;
  52. // create short-term ECDH key pair
  53. internal_return_status = hybridEncryptionBoxClient.generate_keypair();
  54. if(internal_return_status != 0)
  55. return internal_return_status;
  56. hybridEncryptionBoxClient.get_public_key(token);
  57. // create token: concatenate short-term keypair with verifiers mrenclave.
  58. for(counter=0;counter<32;counter++)
  59. *(token + counter + ECDH_PUBLIC_KEY_SIZE) = verifier_mr_enclave[counter];
  60. return 0;
  61. }
  62. // INTERNAL
  63. uint32_t Decryptor::create_mitigator_header_H(uint8_t* signature_data_and_signature)
  64. {
  65. uint32_t internal_return_status;
  66. uint8_t local_signature_data_and_signature[ECDH_PUBLIC_KEY_SIZE + 32 + 64];
  67. uint32_t counter;
  68. internal_return_status = Decryptor::create_mitigator_token_M(local_signature_data_and_signature);
  69. if(internal_return_status != 0x0)
  70. return internal_return_status;
  71. 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);
  72. if(internal_return_status != 0x0)
  73. return internal_return_status;
  74. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE + 32 + 64;counter++)
  75. signature_data_and_signature[counter] = local_signature_data_and_signature[counter];
  76. return 0;
  77. }
  78. // EXTERNAL ECALL.
  79. uint32_t Decryptor::create_and_encrypt_mitigator_header_H(uint8_t* ciphertext_token_H_plus_tag, uint32_t* op_length)
  80. {
  81. uint32_t temp_ciphertext_token_H_iv_tag_length;
  82. uint32_t internal_return_status;
  83. uint8_t plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {0x42};
  84. if(successful_la_count != 2)
  85. return 0x33;
  86. internal_return_status = create_mitigator_header_H(plaintext_mitigator_header_H);
  87. if(internal_return_status != 0)
  88. return internal_return_status;
  89. 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);
  90. *op_length = temp_ciphertext_token_H_iv_tag_length;
  91. return internal_return_status;
  92. }
  93. // INTERNAL. done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  94. uint32_t Decryptor::create_long_term_signing_keypair(uint8_t* private_public_key_string)
  95. {
  96. uint32_t internal_return_status;
  97. internal_return_status = signatureBox.generate_keypair();
  98. if(internal_return_status != 0)
  99. return internal_return_status;
  100. signatureBox.get_keypair(private_public_key_string);
  101. return 0;
  102. }
  103. // INTERNAL.
  104. 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)
  105. {
  106. uint8_t* ciphertext_plus_tag;
  107. uint32_t ciphertext_plus_tag_length;
  108. uint32_t internal_return_status;
  109. // I will derive a shared key from the plaintext_client_public_key
  110. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  111. if(internal_return_status != 0)
  112. return internal_return_status;
  113. // and then I will decrypt the rest of the client data with that key.
  114. ciphertext_plus_tag = plaintext_client_public_key_plus_encrypted_data_plus_tag + ECDH_PUBLIC_KEY_SIZE;
  115. ciphertext_plus_tag_length = total_length - ECDH_PUBLIC_KEY_SIZE;
  116. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(0, ciphertext_plus_tag, ciphertext_plus_tag_length, plaintext_client_data, plaintext_client_data_length);
  117. return internal_return_status;
  118. }
  119. void Decryptor::testing_long_term_verification_key(uint8_t* output)
  120. {
  121. uint8_t keypair[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  122. uint32_t counter;
  123. signatureBox.get_keypair(keypair);
  124. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE; counter++)
  125. output[counter]=keypair[ECDH_PRIVATE_KEY_SIZE+counter];
  126. }
  127. // EXTERNAL. DONE.
  128. uint32_t Decryptor::create_and_seal_long_term_signing_key_pair(uint32_t* sealed_data_length, uint8_t* sealed_data)
  129. {
  130. uint32_t sgx_libcall_status;
  131. uint32_t internal_return_status;
  132. uint32_t temp_sealed_data_length;
  133. uint8_t* temp_sealed_data;
  134. uint8_t private_public_key_string[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  135. uint32_t counter;
  136. temp_sealed_data_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  137. if(temp_sealed_data_length == 0xFFFFFFFF)
  138. return 0x01;
  139. temp_sealed_data = (uint8_t*) malloc(temp_sealed_data_length);
  140. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  141. if(internal_return_status != 0)
  142. {
  143. free(temp_sealed_data);
  144. return internal_return_status;
  145. }
  146. 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);
  147. if(sgx_libcall_status != SGX_SUCCESS)
  148. {
  149. free(temp_sealed_data);
  150. return sgx_libcall_status;
  151. }
  152. for(counter=0;counter<temp_sealed_data_length;counter++)
  153. *(sealed_data + counter)= *(temp_sealed_data + counter);
  154. *sealed_data_length = temp_sealed_data_length;
  155. free(temp_sealed_data);
  156. return 0;
  157. }
  158. // EXTERNAL. DONE.
  159. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  160. {
  161. uint32_t temp_plaintext_length;
  162. uint8_t* temp_plaintext;
  163. uint32_t counter;
  164. uint32_t ret_status;
  165. uint8_t* temp_sealed_data ;
  166. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  167. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  168. *(temp_sealed_data+counter)=*(sealed_data+counter);
  169. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  170. if(temp_plaintext_length == 0xffffffff)
  171. return 0xFFFFFFFF;
  172. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  173. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  174. free(temp_sealed_data);
  175. if(ret_status != SGX_SUCCESS)
  176. {
  177. free(temp_plaintext);
  178. return ret_status;
  179. }
  180. signatureBox.set_private_public_key(temp_plaintext, temp_plaintext + ECDH_PRIVATE_KEY_SIZE);
  181. free(temp_plaintext);
  182. return 0;
  183. }
  184. uint32_t Decryptor::process_verifiers_message(uint8_t* input_ciphertext_plus_tag, uint32_t length)
  185. {
  186. uint8_t first_decryption_output[150];
  187. uint32_t first_decryption_output_length;
  188. uint32_t internal_return_status;
  189. uint32_t counter;
  190. 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.
  191. return 0x23;
  192. internal_return_status = symmetricEncryptionBoxVerifier.encrypt_decrypt(0, input_ciphertext_plus_tag, length, first_decryption_output, &first_decryption_output_length);
  193. if(internal_return_status != 0)
  194. return internal_return_status;
  195. if(first_decryption_output_length != 32)
  196. return 0x33;
  197. for(counter=0; counter<32; counter++)
  198. apache_mr_signer[counter] = *(first_decryption_output + counter);
  199. return 0;
  200. }
  201. // EXTERNAL. DONE.
  202. 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)
  203. {
  204. uint32_t first_decryption_output_length, plaintext_client_data_length;
  205. uint32_t internal_return_status;
  206. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  207. // first, I decrypt the message from the target enclave, to get the client's public key and ciphertext data (and tag and IV)
  208. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length, first_decryption_output, &first_decryption_output_length);
  209. if(internal_return_status != 0)
  210. return internal_return_status;
  211. // then I obtain the plaintext client data, using the client's public key and own key to ultimately decrypt the client's ciphertext data
  212. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  213. if(internal_return_status != 0)
  214. return internal_return_status;
  215. // then I will encrypt the plaintext data to the target enclave.
  216. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  217. return internal_return_status;
  218. }
  219. // INTERNAL.
  220. uint32_t Decryptor::verify_peer_enclave_trust(uint8_t* given_mr_enclave, uint8_t* given_mr_signer, uint8_t* dhaek)
  221. {
  222. uint32_t count;
  223. uint32_t internal_return_status;
  224. if(successful_la_count == 0) // verifier enclave
  225. {
  226. for(count=0; count<SGX_HASH_SIZE; count++)
  227. verifier_mr_enclave[count] = given_mr_enclave[count];
  228. symmetricEncryptionBoxVerifier.set_symmetric_key(dhaek);
  229. }
  230. else // apache enclave
  231. {
  232. for(count=0; count<SGX_HASH_SIZE; count++)
  233. {
  234. if( given_mr_signer[count] != apache_mr_signer[count] )
  235. return ENCLAVE_TRUST_ERROR;
  236. }
  237. symmetricEncryptionBoxApache.set_symmetric_key(dhaek);
  238. }
  239. successful_la_count ++;
  240. return SGX_SUCCESS;
  241. }
  242. void Decryptor::calculate_sealed_keypair_size(uint32_t* output_length)
  243. {
  244. *output_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  245. }
  246. void Decryptor::testing_get_verifier_mrenclave_apache_mrsigner(uint8_t* output)
  247. {
  248. uint32_t counter;
  249. for(counter=0; counter<32;counter++)
  250. {
  251. output[counter]=verifier_mr_enclave[counter];
  252. output[counter+32]=apache_mr_signer[counter];
  253. }
  254. }
  255. void Decryptor::testing_get_short_term_public_key(uint8_t* output)
  256. {
  257. hybridEncryptionBoxClient.get_public_key(output);
  258. }
  259. void Decryptor::testing_get_apache_iv(uint8_t* op)
  260. {
  261. // symmetricEncryptionBoxApache.get_iv(op);
  262. }