SgxProtobufLAInitiator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "sgx_eid.h"
  2. #define __STDC_FORMAT_MACROS
  3. #include <inttypes.h>
  4. #include "ProtobufLAMessages.pb.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. uint8_t iv[12];
  25. 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)
  26. {
  27. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  28. sgx_dh_msg2_t dh_msg2;
  29. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  30. uint32_t ret_status;
  31. if(decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
  32. return -1;
  33. //Intialize the session as a session initiator
  34. ret_status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
  35. if(ret_status != SGX_SUCCESS)
  36. return ret_status;
  37. //Process the message 1 obtained from desination enclave and generate message 2
  38. ret_status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
  39. if(SGX_SUCCESS != ret_status)
  40. return ret_status;
  41. encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
  42. return 0;
  43. }
  44. uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id) {
  45. uint32_t ret_status;
  46. sgx_dh_msg3_t dh_msg3;
  47. sgx_key_128bit_t dh_aek; // Session Key
  48. sgx_dh_session_enclave_identity_t responder_identity;
  49. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  50. if(decode_msg3_from_protobuf(protobuf_msg3, &dh_msg3)!=0)
  51. return -1;
  52. //Process Message 3 obtained from the destination enclave
  53. ret_status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
  54. if(SGX_SUCCESS != ret_status)
  55. return ret_status;
  56. //No verification checks here: no security guarantees obtained from any such checks
  57. memset(iv, 0, 12);
  58. memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  59. global_session_info.session_id = 1; // TODO: session_id;
  60. global_session_info.active.counter = 0;
  61. global_session_info.status = ACTIVE;
  62. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  63. return 0;
  64. }
  65. /*
  66. uint32_t decrypt_wrapper(unsigned char* ciphertext, int ciphertext_len, unsigned char* op_plaintext, int* op_plaintext_len, unsigned char* ip_encrypted_tag)
  67. {
  68. uint32_t return_status;
  69. unsigned char key[16]; uint32_t count;
  70. for(count=0;count<16;count++)
  71. key[count]=global_session_info.active.AEK[count];
  72. printf("Doing decryption\n"); fflush(stdout);
  73. return_status=aes_cipher(0, key, iv, ciphertext, ciphertext_len, op_plaintext, op_plaintext_len, ip_encrypted_tag);
  74. return return_status;
  75. }
  76. */
  77. uint32_t aes_gcm_wrapper(int enc, unsigned char* ciphertext, int ciphertext_len, unsigned char* op_plaintext, int* op_plaintext_len, unsigned char* ip_tag)
  78. {
  79. uint32_t return_status;
  80. unsigned char key[16]; uint32_t count;
  81. for(count=0;count<16;count++)
  82. key[count]=global_session_info.active.AEK[count];
  83. printf("About to do enc/dec\n"); fflush(stdout);
  84. return_status=aes_gcm(enc, key, iv, ciphertext, ciphertext_len, op_plaintext, op_plaintext_len, ip_tag);
  85. return return_status;
  86. }