Decryptor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 == NULL)
  55. return 0xff;
  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_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::encrypt_mitigator_header_H_to_apache(uint8_t* ciphertext_token_H_plus_tag)
  80. {
  81. uint32_t counter;
  82. uint8_t sign_data_and_sign[ECDH_PUBLIC_KEY_SIZE + 32 + 64 + 10];
  83. uint8_t temp_tag[16];
  84. uint32_t temp_ciphertext_token_H_length;
  85. uint32_t internal_return_status;
  86. if(successful_la_count != 2)
  87. return 0x33;
  88. internal_return_status = create_mitigator_header_H(plaintext_mitigator_header_H);
  89. if(internal_return_status != 0)
  90. return internal_return_status;
  91. 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_length);
  92. if(internal_return_status != 0)
  93. return internal_return_status;
  94. if(temp_ciphertext_token_H_length != 160)
  95. return 0x45;
  96. return 0;
  97. }
  98. // INTERNAL. done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  99. uint32_t Decryptor::create_long_term_signing_keypair(uint8_t* private_public_key_string)
  100. {
  101. uint32_t internal_return_status;
  102. internal_return_status = signatureBox.generate_keypair();
  103. if(internal_return_status != 0)
  104. return internal_return_status;
  105. signatureBox.get_keypair(private_public_key_string);
  106. return 0;
  107. }
  108. // INTERNAL.
  109. 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)
  110. {
  111. uint8_t* ciphertext;
  112. uint32_t ciphertext_length;
  113. uint32_t internal_return_status;
  114. // and now I will derive a shared key from the plaintext_client_public_key
  115. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  116. if(internal_return_status != 0)
  117. return internal_return_status;
  118. // and then I will decrypt the rest of the client data with that key.
  119. ciphertext = plaintext_client_public_key_plus_encrypted_data_plus_tag + ECDH_PUBLIC_KEY_SIZE;
  120. ciphertext_length = total_length - ECDH_PUBLIC_KEY_SIZE - 16;
  121. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(0, ciphertext, ciphertext_length, plaintext_client_data, plaintext_client_data_length);
  122. return internal_return_status;
  123. }
  124. // EXTERNAL. DONE.
  125. uint32_t Decryptor::create_and_seal_long_term_signing_key_pair(uint32_t* sealed_data_length, uint8_t* sealed_data)
  126. {
  127. uint32_t sgx_libcall_status;
  128. uint32_t internal_return_status;
  129. uint32_t temp_sealed_data_length;
  130. uint8_t* temp_sealed_data;
  131. uint8_t private_public_key_string[ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE];
  132. uint32_t counter;
  133. temp_sealed_data_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  134. if(temp_sealed_data_length == 0xFFFFFFFF)
  135. return 0x01;
  136. temp_sealed_data = (uint8_t*) malloc(temp_sealed_data_length);
  137. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  138. if(internal_return_status != 0)
  139. {
  140. free(temp_sealed_data);
  141. return internal_return_status;
  142. }
  143. 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);
  144. if(sgx_libcall_status != SGX_SUCCESS)
  145. {
  146. free(temp_sealed_data);
  147. return sgx_libcall_status;
  148. }
  149. for(counter=0;counter<temp_sealed_data_length;counter++)
  150. *(sealed_data + counter)= *(temp_sealed_data + counter);
  151. *sealed_data_length = temp_sealed_data_length;
  152. free(temp_sealed_data);
  153. return 0;
  154. }
  155. // EXTERNAL. DONE.
  156. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  157. {
  158. uint32_t temp_plaintext_length;
  159. uint8_t* temp_plaintext;
  160. uint32_t counter;
  161. uint32_t ret_status;
  162. uint8_t* temp_sealed_data ;
  163. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  164. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  165. *(temp_sealed_data+counter)=*(sealed_data+counter);
  166. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  167. if(temp_plaintext_length == 0xffffffff)
  168. return 0xFFFFFFFF;
  169. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  170. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  171. free(temp_sealed_data);
  172. if(ret_status != SGX_SUCCESS)
  173. {
  174. free(temp_plaintext);
  175. return ret_status;
  176. }
  177. signatureBox.set_private_public_key(temp_plaintext, temp_plaintext + ECDH_PRIVATE_KEY_SIZE);
  178. free(temp_plaintext);
  179. return 0;
  180. }
  181. uint32_t Decryptor::process_verifiers_message(uint8_t* input_ciphertext, uint32_t input_ciphertext_plus_tag_length)
  182. {
  183. uint8_t *first_decryption_output, *plaintext_client_data, *temp_output_ciphertext;
  184. uint32_t first_decryption_output_length, plaintext_client_data_length;
  185. uint32_t temp_output_ciphertext_length, internal_return_status;
  186. uint8_t temp_output_tag[16]; uint32_t counter;
  187. 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.
  188. return 0x23;
  189. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  190. first_decryption_output = (uint8_t*) malloc(input_ciphertext_plus_tag_length);
  191. internal_return_status = symmetricEncryptionBoxVerifier.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length - 16, first_decryption_output, &first_decryption_output_length);
  192. if(internal_return_status != 0)
  193. {
  194. free(first_decryption_output);
  195. return internal_return_status;
  196. }
  197. if(first_decryption_output_length != 32)
  198. {
  199. free(first_decryption_output);
  200. return 0x33;
  201. }
  202. for(counter=0; counter<32; counter++)
  203. {
  204. apache_mr_signer[counter] = *(first_decryption_output + counter);
  205. }
  206. free(first_decryption_output);
  207. return 0;
  208. }
  209. // EXTERNAL. DONE.
  210. 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)
  211. {
  212. uint8_t *first_decryption_output, *plaintext_client_data;
  213. uint32_t first_decryption_output_length, plaintext_client_data_length;
  214. uint32_t internal_return_status;
  215. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  216. first_decryption_output = (uint8_t*) malloc(input_ciphertext_plus_tag_length);
  217. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_plus_tag_length - 16, first_decryption_output, &first_decryption_output_length);
  218. if(internal_return_status != 0)
  219. {
  220. free(first_decryption_output);
  221. return internal_return_status;
  222. }
  223. plaintext_client_data = (uint8_t*) malloc(first_decryption_output_length); // you will need less than this coz public key size.
  224. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  225. free(first_decryption_output);
  226. if(internal_return_status != 0)
  227. return internal_return_status;
  228. // then I will encrypt the resulting first_decryption_output to the apache enclave.
  229. internal_return_status = symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  230. free(plaintext_client_data);
  231. if(internal_return_status != 0)
  232. return internal_return_status;
  233. return 0;
  234. }
  235. // INTERNAL.
  236. uint32_t Decryptor::verify_peer_enclave_trust(uint8_t* given_mr_enclave, uint8_t* given_mr_signer, uint8_t* dhaek)
  237. {
  238. uint32_t count;
  239. uint32_t internal_return_status;
  240. if(successful_la_count == 0) // verifier enclave
  241. {
  242. for(count=0; count<SGX_HASH_SIZE; count++)
  243. verifier_mr_enclave[count] = given_mr_enclave[count];
  244. symmetricEncryptionBoxVerifier.set_symmetric_key(dhaek);
  245. }
  246. else // apache enclave
  247. {
  248. for(count=0; count<SGX_HASH_SIZE; count++)
  249. {
  250. if( given_mr_signer[count] != apache_mr_signer[count] )
  251. return ENCLAVE_TRUST_ERROR;
  252. }
  253. symmetricEncryptionBoxApache.set_symmetric_key(dhaek);
  254. }
  255. successful_la_count ++;
  256. return SGX_SUCCESS;
  257. }
  258. void Decryptor::calculate_sealed_keypair_size(uint32_t* output_length)
  259. {
  260. *output_length = sgx_calc_sealed_data_size(0, ECDH_PUBLIC_KEY_SIZE + ECDH_PRIVATE_KEY_SIZE);
  261. }