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