Decryptor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // Enclave2.cpp : Defines the exported functions for the DLL application
  32. #include "Decryptor.h"
  33. uint32_t Decryptor::create_mitigator_token_M(uint8_t* token)
  34. {
  35. uint32_t internal_return_status;
  36. uint32_t counter;
  37. uint8_t verifier_mr_enclave[32];
  38. // create short-term ECDH key pair
  39. internal_return_status = hybridEncryptionBoxClient.generate_keypair();
  40. if(internal_return_status == NULL)
  41. return 0xff;
  42. hybridEncryptionBoxClient.get_public_key(token);
  43. // create token: concatenate short-term keypair with verifiers mrenclave.
  44. LocalAttestationTrusted::get_verifier_mr_enclave(verifier_mr_enclave);
  45. for(counter=0;counter<32;counter++)
  46. *(token + counter + ECDH_PUBLIC_KEY_SIZE) = verifier_mr_enclave[counter];
  47. return 0;
  48. }
  49. uint32_t Decryptor::create_mitigator_header_H(uint8_t* signature_data, uint8_t* signature)
  50. {
  51. uint32_t internal_return_status;
  52. uint8_t local_signature[64];
  53. uint8_t local_signature_data[ECDH_PUBLIC_KEY_SIZE + 32];
  54. uint32_t counter;
  55. // Otherwise: DoS or possible bypass (fake verifier does LA but real verifier mrenclave is given out by decryptor) - signature with junk verifier mrenclave or whatever is in the memory.
  56. if(LocalAttestationTrusted::one_successful_la_done() < 1)
  57. return 0xde; // This needs to be called at any point after the first local attestation is done - else, a junk verifier mrenclave will be included in the signature
  58. internal_return_status = Decryptor::create_mitigator_token_M(local_signature_data);
  59. if(internal_return_status != 0x0)
  60. return internal_return_status;
  61. internal_return_status = signatureBox.sign(local_signature_data, ECDH_PUBLIC_KEY_SIZE + 32, local_signature);
  62. if(internal_return_status != 0x0)
  63. return internal_return_status;
  64. for(counter=0;counter<ECDH_PUBLIC_KEY_SIZE + 32;counter++)
  65. signature_data[counter] = local_signature_data[counter];
  66. for(counter=0;counter<64;counter++)
  67. signature[counter] = local_signature[counter];
  68. return 0;
  69. }
  70. // done. But there might be one more return statement for the case when get_keypair returns sth (it is non void).
  71. uint32_t Decryptor::create_long_term_signing_keypair(uint8_t* private_public_key_string)
  72. {
  73. uint32_t internal_return_status;
  74. internal_return_status = signatureBox.generate_keypair();
  75. if(internal_return_status != 0)
  76. return internal_return_status;
  77. signatureBox.get_keypair(private_public_key_string);
  78. return 0;
  79. }
  80. 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)
  81. {
  82. uint8_t* ciphertext;
  83. uint32_t ciphertext_length;
  84. uint8_t* tag;
  85. uint32_t internal_return_status;
  86. // and now I will derive a shared key from the plaintext_client_public_key
  87. internal_return_status = hybridEncryptionBoxClient.initialize_symmetric_key(plaintext_client_public_key_plus_encrypted_data_plus_tag);
  88. if(internal_return_status != 0)
  89. return internal_return_status;
  90. // and then I will decrypt the rest of the client data with that key.
  91. ciphertext = plaintext_client_public_key_plus_encrypted_data_plus_tag + ECDH_PUBLIC_KEY_SIZE;
  92. ciphertext_length = total_length - ECDH_PUBLIC_KEY_SIZE - 16;
  93. tag = ciphertext + ciphertext_length;
  94. internal_return_status = hybridEncryptionBoxClient.encrypt_decrypt(ciphertext, ciphertext_length, plaintext_client_data, &plaintext_client_data_length, tag);
  95. return internal_return_status;
  96. }
  97. // 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. internal_return_status = create_long_term_signing_keypair(private_public_key_string);
  110. temp_sealed_data = (uint8_t*) malloc(*temp_sealed_data_length);
  111. 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);
  112. free(private_public_key_string); // first_decryption_output data - don't need it anymore, whether the call succeeds or fails.
  113. if(sgx_libcall_status != SGX_SUCCESS)
  114. {
  115. free(temp_sealed_data);
  116. return sgx_libcall_status;
  117. }
  118. for(counter=0;counter<temp_sealed_data_length;counter++)
  119. *(sealed_data + counter)=*(temp_sealed_data + counter);
  120. free(temp_sealed_data);
  121. return 0;
  122. }
  123. // DONE.
  124. uint32_t Decryptor::create_and_encrypt_mitigator_header_H(uint8_t* ciphertext_token_H_plus_tag)
  125. {
  126. uint32_t counter;
  127. uint8_t sign_data_and_sign[ECDH_PUBLIC_KEY_SIZE + 32 + 64];
  128. uint8_t temp_ciphertext_token_H[ECDH_PUBLIC_KEY_SIZE + 32 + 64 + 10];
  129. uint8_t temp_tag[16];
  130. uint32_t temp_ciphertext_token_H_length;
  131. uint32_t internal_return_status;
  132. internal_return_status = create_mitigator_header_value(token_H, sign_data_and_sign + ECDH_PUBLIC_KEY_SIZE + 32);
  133. if(internal_return_status != 0)
  134. return internal_return_status;
  135. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(1, sign_data_and_sign, ECDH_PUBLIC_KEY_SIZE + 32 + 64, temp_ciphertext_token_H, &temp_ciphertext_token_H_length, temp_tag);
  136. if(internal_return_status != 0)
  137. return internal_return_status;
  138. if(temp_ciphertext_token_H_length != 160)
  139. return 0x45;
  140. for(counter=0; counter<160; counter++)
  141. {
  142. *(ciphertext_token_H_plus_tag + counter) = *(temp_ciphertext_token_H + counter);
  143. }
  144. for(counter=0; counter<16; counter++)
  145. {
  146. *(ciphertext_token_H_plus_tag + counter) = *(temp_tag + counter);
  147. }
  148. return 0;
  149. }
  150. // DONE.
  151. uint32_t Decryptor::unseal_and_restore_long_term_signing_key_pair(uint8_t* sealed_data, uint32_t* sgx_sealed_data_length)
  152. {
  153. uint32_t temp_plaintext_length;
  154. uint8_t* temp_plaintext;
  155. uint32_t counter;
  156. uint32_t ret_status;
  157. uint8_t* temp_sealed_data ;
  158. temp_sealed_data = (uint8_t*) malloc(*sgx_sealed_data_length);
  159. for(counter=0;counter<*sgx_sealed_data_length;counter++)
  160. *(temp_sealed_data+counter)=*(sealed_data+counter);
  161. temp_plaintext_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sealed_data);
  162. if(temp_plaintext_length == 0xffffffff)
  163. return 0xFFFFFFFF;
  164. temp_plaintext = (uint8_t*)malloc( temp_plaintext_length );
  165. ret_status = sgx_unseal_data((sgx_sealed_data_t*)temp_sealed_data, NULL, 0, temp_plaintext, &temp_plaintext_length);
  166. free(temp_sealed_data);
  167. if(ret_status != SGX_SUCCESS)
  168. {
  169. free(temp_plaintext);
  170. return ret_status;
  171. }
  172. signatureBox.set_keypair(temp_plaintext);
  173. free(temp_plaintext);
  174. return 0;
  175. }
  176. // DONE.
  177. uint32_t Decryptor::decrypt_verifiers_message_set_apache_mrsigner(uint8_t* ciphertext_plus_tag)
  178. {
  179. uint8_t temp_apache_mrsigner[32+10];
  180. uint32_t temp_apache_mrsigner_length;
  181. uint32_t internal_return_status;
  182. uint32_t counter;
  183. uint8_t* tag;
  184. tag = ciphertext_plus_tag + 32;
  185. internal_return_status = localAttestation.symmetricEncryptionBoxVerifier.encrypt_decrypt(0, ciphertext_plus_tag, 32, temp_apache_mrsigner, &temp_apache_mrsigner_length, tag);
  186. if(internal_return_status != 0)
  187. return internal_return_status;
  188. if(temp_apache_mrsigner_length != 32)
  189. return 0x33;
  190. for(counter=0; counter<32; counter++)
  191. {
  192. apache_mr_signer[counter] = *(temp_apache_mrsigner + counter);
  193. }
  194. return 0;
  195. }
  196. // DONE.
  197. uint32_t Decryptor::process_apache_message_generate_response(uint8_t* input_ciphertext, uint32_t input_ciphertext_plus_tag_length, uint8_t* output_ciphertext, uint32_t* output_ciphertext_plus_tag_length)
  198. {
  199. uint8_t* first_decryption_output, plaintext_client_data, temp_output_ciphertext;
  200. uint32_t first_decryption_output_length, plaintext_client_data_length, temp_output_ciphertext_plus_tag_length, internal_return_status;
  201. uint8_t temp_output_tag[16];
  202. // TODO: May be have temporary variables for input ciphertext as they can't be passed directly to functions?
  203. first_decryption_output = (uint8_t*) malloc(input_ciphertext_length + 10);
  204. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(0, input_ciphertext, input_ciphertext_length, first_decryption_output, &first_decryption_output_length, input_ciphertext + input_ciphertext_length - 16);
  205. if(internal_return_status != 0)
  206. {
  207. free(first_decryption_output);
  208. return internal_return_status;
  209. }
  210. plaintext_client_data = (uint8_t*) malloc(first_decryption_output_length); // you will need less than this coz public key size.
  211. internal_return_status = initialize_symmetric_key_decrypt_client_data(first_decryption_output, first_decryption_output_length, plaintext_client_data, &plaintext_client_data_length);
  212. free(first_decryption_output);
  213. if(internal_return_status != 0)
  214. return internal_return_status;
  215. temp_output_ciphertext = (uint8_t*) malloc(plaintext_client_data_length + 20);
  216. // then I will encrypt the resulting first_decryption_output to the apache enclave.
  217. internal_return_status = localAttestation.symmetricEncryptionBoxApache.encrypt_decrypt(1, plaintext_client_data, plaintext_client_data_length, temp_output_ciphertext, &temp_output_ciphertext_length, temp_output_tag);
  218. free(plaintext_client_data);
  219. if(internal_return_status != 0)
  220. {
  221. free(temp_output_ciphertext);
  222. return internal_return_status;
  223. }
  224. for(counter=0; counter<temp_output_ciphertext_length; counter++)
  225. output_ciphertext[counter] = temp_output_ciphertext[counter];
  226. *output_ciphertext_length = temp_output_ciphertext_length + 16;
  227. free(temp_output_ciphertext);
  228. return 0;
  229. }