SgxProtobufLAInitiator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "sgx_eid.h"
  2. #define __STDC_FORMAT_MACROS
  3. #include <inttypes.h>
  4. #include "ProtobufLAMessages.h"
  5. #include <stdio.h>
  6. #include "sgx_trts.h"
  7. #include "sgx_utils.h"
  8. #include "error_codes.h"
  9. #include "sgx_ecp_types.h"
  10. #include "sgx_thread.h"
  11. #include <map>
  12. #include "sgx_dh.h"
  13. #include "dh_session_protocol.h"
  14. #include "sgx_tcrypto.h"
  15. #include "datatypes.h"
  16. #include "SgxProtobufLAInitiator_Transforms.h"
  17. #define MAX_SESSION_COUNT 16
  18. #define SGX_CAST(type, item) ((type)(item))
  19. #include <string.h>
  20. #include "crypto.h"
  21. #include "stdio.h"
  22. dh_session_t global_session_info;
  23. sgx_dh_session_t sgx_dh_session;
  24. // sgx_key_128bit_t dh_aek; // Session Key
  25. uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer);
  26. uint32_t process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2, uint32_t* session_id)
  27. {
  28. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  29. sgx_dh_msg2_t dh_msg2;
  30. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  31. uint32_t ret_status;
  32. if(decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
  33. return -1;
  34. //Intialize the session as a session initiator
  35. ret_status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
  36. if(ret_status != SGX_SUCCESS)
  37. return ret_status;
  38. //Process the message 1 obtained from desination enclave and generate message 2
  39. ret_status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
  40. if(SGX_SUCCESS != ret_status)
  41. return ret_status;
  42. encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
  43. return 0;
  44. }
  45. uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id) {
  46. uint32_t ret_status;
  47. sgx_dh_msg3_t dh_msg3;
  48. sgx_key_128bit_t dh_aek; // Session Key
  49. sgx_dh_session_enclave_identity_t responder_identity;
  50. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  51. if(decode_msg3_from_protobuf(protobuf_msg3, &dh_msg3)!=0)
  52. return -1;
  53. //Process Message 3 obtained from the destination enclave
  54. ret_status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
  55. if(SGX_SUCCESS != ret_status)
  56. return ret_status;
  57. // Verify the identity of the destination enclave
  58. ret_status = verify_peer_enclave_trust(&responder_identity, NULL, NULL);
  59. if(ret_status != 0)
  60. return ret_status;
  61. memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  62. global_session_info.session_id = 1; // TODO: session_id;
  63. global_session_info.active.counter = 0;
  64. global_session_info.status = ACTIVE;
  65. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  66. return 0;
  67. }
  68. uint32_t generate_encrypted_rsa_keypair_hash(uint8_t* op_ciphertext, uint8_t* op_encrypted_tag)
  69. {
  70. uint8_t hash[32]; uint32_t return_status;
  71. unsigned char key[16]; uint32_t count;
  72. for(count=0;count<16;count++)
  73. key[count]=global_session_info.active.AEK[count];
  74. return_status=generate_rsa_keypair_hash(hash);
  75. if(return_status!=0)
  76. return return_status;
  77. uint8_t ciphertext[32];// uint8_t expected_plaintext[48];
  78. uint8_t encryption_tag[16];
  79. int ciphertext_len=32;// int plaintext_len=32;
  80. uint8_t iv[12];
  81. // memset(ciphertext, 0, 32); //memset(expected_plaintext, 0, 48);
  82. memset(iv, 0, 12); // memset(expected_plaintext, 0, 32);
  83. // memset(encryption_tag, 0, 16);
  84. // memset(encrypted_hash_and_tag, 0, 48);
  85. return_status=aes_cipher(1, key, iv, hash, 32, op_ciphertext, &ciphertext_len, op_encrypted_tag);
  86. /* if(return_status != 0)
  87. {
  88. // memcpy(encrypted_hash_and_tag, ciphertext, 32); // For some reason, memcpy does not work
  89. // memcpy(encrypted_hash_and_tag+32, encryption_tag, 16);
  90. for(count=0;count<32; count++)
  91. *(op_ciphertext+count) = ciphertext[count];
  92. for(count=0;count<16;count++)
  93. *(op_encrypted_tag+count) = encryption_tag[count];
  94. }
  95. // printf("ciphertext len: %d\n", ciphertext_len); fflush(stdout);
  96. // printf("Encryption return status: 0x%x", return_status); fflush(stdout);
  97. // return_status=aes_cipher(0, key, iv, ciphertext, ciphertext_len, expected_plaintext, &plaintext_len, encryption_tag);
  98. for(count=0;count<32;count++)
  99. {
  100. printf("0x%02x ", hash[count]);
  101. }
  102. printf("\n");
  103. fflush(stdout);
  104. */ for(count=0;count<32;count++)
  105. {
  106. printf("0x%02x ", op_ciphertext[count]);
  107. }
  108. printf("\n");
  109. fflush(stdout);
  110. for(count=0;count<16;count++)
  111. {
  112. printf("0x%02x ", op_encrypted_tag[count]);
  113. }
  114. printf("\n"); fflush(stdout);
  115. /* for(count=0;count<32;count++)
  116. {
  117. printf("0x%02x ", op_ciphertext[count]);
  118. }
  119. printf("\n"); fflush(stdout);
  120. */
  121. return return_status;
  122. }
  123. // TODO: Private function
  124. uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer)
  125. {
  126. int count=0;
  127. if(!peer_enclave_identity)
  128. return INVALID_PARAMETER_ERROR;
  129. sgx_measurement_t actual_mr_enclave = peer_enclave_identity->mr_enclave;
  130. sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
  131. if(expected_mr_enclave != NULL)
  132. {
  133. for(count=0; count<SGX_HASH_SIZE; count++)
  134. {
  135. if( actual_mr_enclave.m[count] != expected_mr_enclave[count] )
  136. return ENCLAVE_TRUST_ERROR;
  137. }
  138. }
  139. if(expected_mr_signer !=NULL)
  140. {
  141. for(count=0; count<SGX_HASH_SIZE; count++)
  142. {
  143. if( actual_mr_signer.m[count] != expected_mr_signer[count] )
  144. return ENCLAVE_TRUST_ERROR; // TODO: Different error here.
  145. }
  146. }
  147. return SUCCESS;
  148. }