Decryptor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // question 1: When should I call the create_mitigator_header_H function? It can be called as early as verify_peer_enclave_trust. Or as late as when encrypt_mitigator_header_H_to_apache is called. But then you need to make sure that successful_la_count is right for that.
  32. // question 2: nightmare about the process_message_generate_response function.
  33. #include "Decryptor.h"
  34. #include "sgx_tseal.h"
  35. #include "sgx_tcrypto.h"
  36. #include "sgx_dh.h"
  37. #include "datatypes.h"
  38. #include "error_codes.h"
  39. ECDSASignatureBox Decryptor::signatureBox;
  40. HybridEncryptionBox Decryptor::hybridEncryptionBoxClient;
  41. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxApache;
  42. SymmetricEncryptionBox Decryptor::symmetricEncryptionBoxVerifier;
  43. uint8_t Decryptor::verifier_mr_enclave[32] = {0};
  44. uint8_t Decryptor::apache_mr_signer[32] = {0};
  45. unsigned int successful_la_count;
  46. uint8_t Decryptor::plaintext_mitigator_header_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64] = {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)
  80. {
  81. uint32_t temp_ciphertext_token_H_plus_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_plus_tag_length);
  90. return 0;
  91. }
  92. // INTERNAL. done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  93. uint32_t Decryptor::create_long_term_signing_keypair(uint8_t* private_public_key_string)
  94. {
  95. uint32_t internal_return_status;
  96. internal_return_status = signatureBox.generate_keypair();
  97. if(internal_return_status != 0)
  98. return internal_return_status;
  99. signatureBox.get_keypair(private_public_key_string);
  100. return 0;
  101. }
  102. // INTERNAL.
  103. 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)
  104. {
  105. uint8_t* ciphertext;
  106. uint32_t ciphertext_length;
  107. uint32_t internal_return_status;
  108. // and now I will derive a shared key from the plaintext_client_public_key
  109. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  110. if(internal_return_status != 0)
  111. return internal_return_status;
  112. // and then I will decrypt the rest of the client data with that key.
  113. ciphertext = plaintext_client_public_key_plus_encrypted_data_plus_tag + ECDH_PUBLIC_KEY_SIZE;
  114. ciphertext_length = total_length - ECDH_PUBLIC_KEY_SIZE - 16;
  115. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(0, ciphertext, ciphertext_length, plaintext_client_data, plaintext_client_data_length);
  116. return internal_return_status;
  117. }
  118. void Decryptor::testing_long_term_verification_key(uint8_t* output)
  119. {
  120. uint8_t keypair[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  121. uint32_t counter;
  122. signatureBox.get_keypair(keypair);
  123. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE; counter++)
  124. output[counter]=keypair[ECDH_PRIVATE_KEY_SIZE+counter];
  125. }
  126. // EXTERNAL. DONE.
  127. uint32_t Decryptor::create_and_seal_long_term_signing_key_pair(uint32_t* sealed_data_length, uint8_t* sealed_data)
  128. {
  129. uint32_t sgx_libcall_status;
  130. uint32_t internal_return_status;
  131. uint32_t temp_sealed_data_length;
  132. uint8_t* temp_sealed_data;
  133. uint8_t private_public_key_string[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  134. uint32_t counter;
  135. temp_sealed_data_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  136. if(temp_sealed_data_length == 0xFFFFFFFF)
  137. return 0x01;
  138. temp_sealed_data = (uint8_t*) malloc(temp_sealed_data_length);
  139. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  140. if(internal_return_status != 0)
  141. {
  142. free(temp_sealed_data);
  143. return internal_return_status;
  144. }
  145. 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);
  146. if(sgx_libcall_status != SGX_SUCCESS)
  147. {
  148. free(temp_sealed_data);
  149. return sgx_libcall_status;
  150. }
  151. for(counter=0;counter<temp_sealed_data_length;counter++)
  152. *(sealed_data + counter)= *(temp_sealed_data + counter);
  153. *sealed_data_length = temp_sealed_data_length;
  154. free(temp_sealed_data);
  155. return 0;
  156. }
  157. // EXTERNAL. DONE.
  158. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  159. {
  160. uint32_t temp_plaintext_length;
  161. uint8_t* temp_plaintext;
  162. uint32_t counter;
  163. uint32_t ret_status;
  164. uint8_t* temp_sealed_data ;
  165. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  166. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  167. *(temp_sealed_data+counter)=*(sealed_data+counter);
  168. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  169. if(temp_plaintext_length == 0xffffffff)
  170. return 0xFFFFFFFF;
  171. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  172. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  173. free(temp_sealed_data);
  174. if(ret_status != SGX_SUCCESS)
  175. {
  176. free(temp_plaintext);
  177. return ret_status;
  178. }
  179. signatureBox.set_private_public_key(temp_plaintext, temp_plaintext + ECDH_PRIVATE_KEY_SIZE);
  180. free(temp_plaintext);
  181. return 0;
  182. }
  183. uint32_t Decryptor::process_verifiers_message(uint8_t* input_ciphertext_plus_tag)
  184. {
  185. uint8_t first_decryption_output[48];
  186. uint32_t first_decryption_output_length;
  187. uint32_t internal_return_status;
  188. uint32_t counter;
  189. 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.
  190. return 0x23;
  191. internal_return_status = symmetricEncryptionBoxVerifier.encrypt_decrypt(0, input_ciphertext_plus_tag, 32, first_decryption_output, &first_decryption_output_length);
  192. if(internal_return_status != 0)
  193. return internal_return_status;
  194. if(first_decryption_output_length != 32)
  195. return 0x33;
  196. for(counter=0; counter<32; counter++)
  197. apache_mr_signer[counter] = *(first_decryption_output + counter);
  198. return 0;
  199. }
  200. // EXTERNAL. DONE.
  201. 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)
  202. {
  203. uint8_t *first_decryption_output, *plaintext_client_data;
  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_decryption_output = (uint8_t*) malloc(input_ciphertext_plus_tag_length);
  208. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length - 16, first_decryption_output, &first_decryption_output_length);
  209. if(internal_return_status != 0)
  210. {
  211. free(first_decryption_output);
  212. return internal_return_status;
  213. }
  214. plaintext_client_data = (uint8_t*) malloc(first_decryption_output_length); // you will need less than this coz public key size.
  215. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  216. free(first_decryption_output);
  217. if(internal_return_status != 0)
  218. return internal_return_status;
  219. // then I will encrypt the resulting first_decryption_output to the apache enclave.
  220. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  221. free(plaintext_client_data);
  222. if(internal_return_status != 0)
  223. return internal_return_status;
  224. return 0;
  225. }
  226. // INTERNAL.
  227. uint32_t Decryptor::verify_peer_enclave_trust(uint8_t* given_mr_enclave, uint8_t* given_mr_signer, uint8_t* dhaek)
  228. {
  229. uint32_t count;
  230. uint32_t internal_return_status;
  231. if(successful_la_count == 0) // verifier enclave
  232. {
  233. for(count=0; count<SGX_HASH_SIZE; count++)
  234. verifier_mr_enclave[count] = given_mr_enclave[count];
  235. symmetricEncryptionBoxVerifier.set_symmetric_key(dhaek);
  236. }
  237. else // apache enclave
  238. {
  239. for(count=0; count<SGX_HASH_SIZE; count++)
  240. {
  241. if( given_mr_signer[count] != apache_mr_signer[count] )
  242. return ENCLAVE_TRUST_ERROR;
  243. }
  244. symmetricEncryptionBoxApache.set_symmetric_key(dhaek);
  245. }
  246. successful_la_count ++;
  247. return SGX_SUCCESS;
  248. }
  249. void Decryptor::calculate_sealed_keypair_size(uint32_t* output_length)
  250. {
  251. *output_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  252. }
  253. void Decryptor::testing_get_verifier_mrenclave_apache_mrsigner(uint8_t* output)
  254. {
  255. uint32_t counter;
  256. for(counter=0; counter<32;counter++)
  257. {
  258. output[counter]=verifier_mr_enclave[counter];
  259. output[counter+32]=apache_mr_signer[counter];
  260. }
  261. }
  262. void Decryptor::testing_get_short_term_public_key(uint8_t* output)
  263. {
  264. hybridEncryptionBoxClient.get_public_key(output);
  265. }