LA.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "sgx_eid.h"
  2. #define __STDC_FORMAT_MACROS
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include "sgx_trts.h"
  6. #include "sgx_utils.h"
  7. #include "error_codes.h"
  8. #include "sgx_ecp_types.h"
  9. #include "sgx_thread.h"
  10. #include "sgx_tcrypto.h"
  11. #include "datatypes.h"
  12. #include "Transforms.h"
  13. #define MAX_SESSION_COUNT 16
  14. #define SGX_CAST(type, item) ((type)(item))
  15. #include <string.h>
  16. #include "crypto.h"
  17. #include "LA.h"
  18. uint32_t LA::process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2)
  19. {
  20. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  21. sgx_dh_msg2_t dh_msg2;
  22. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  23. uint32_t ret_status;
  24. if(Transforms::decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
  25. return 0x1;
  26. //Intialize the session as a session initiator
  27. sgx_dh_session = (sgx_dh_session_t*) malloc(sizeof(sgx_dh_session_t));
  28. ret_status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, sgx_dh_session);
  29. if(ret_status != SGX_SUCCESS)
  30. return ret_status;
  31. //Process the message 1 obtained from desination enclave and generate message 2
  32. ret_status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, sgx_dh_session);
  33. if(ret_status != SGX_SUCCESS)
  34. return ret_status;
  35. Transforms::encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
  36. return 0;
  37. }
  38. uint32_t LA::process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3) {
  39. uint32_t ret_status;
  40. sgx_dh_msg3_t dh_msg3;
  41. sgx_key_128bit_t dh_aek; // Session Key
  42. sgx_dh_session_enclave_identity_t responder_identity;
  43. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  44. if(Transforms::decode_msg3_from_protobuf(protobuf_msg3, &dh_msg3)!=0)
  45. return -1;
  46. //Process Message 3 obtained from the destination enclave
  47. ret_status = sgx_dh_initiator_proc_msg3(&dh_msg3, sgx_dh_session, &dh_aek, &responder_identity);
  48. if(SGX_SUCCESS != ret_status)
  49. return ret_status;
  50. //memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  51. memcpy(key, &dh_aek, sizeof(sgx_key_128bit_t));
  52. /*global_session_info.session_id = 1; // TODO: session_id;
  53. global_session_info.active.counter = 0;
  54. global_session_info.status = ACTIVE;
  55. */
  56. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  57. return 0;
  58. }
  59. uint32_t LA::conduct_la(int decryptor_fd) {
  60. // declare msg1, msg2, msg3 protobuf objects
  61. protobuf_sgx_dh_msg1_t protobuf_msg1;
  62. protobuf_sgx_dh_msg2_t protobuf_msg2;
  63. protobuf_sgx_dh_msg3_t protobuf_msg3;
  64. uint32_t protobuf_sgx_ret;
  65. setbuf(stdout,NULL);
  66. protobufReaderWriter.set_fd(decryptor_fd);
  67. printf("Reading message 1\n"); fflush(stdout);
  68. if(protobufReaderWriter.read_msg(protobuf_msg1)!=0)
  69. return -1;
  70. printf("Generating message 2\n"); fflush(stdout);
  71. protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2);
  72. if(protobuf_sgx_ret != 0)
  73. {
  74. printf("Error in process_protobuf_dh_msg1_generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  75. }
  76. printf("Writing message 2\n"); fflush(stdout);
  77. if(protobufReaderWriter.write_msg(protobuf_msg2)!=0)
  78. return -1;
  79. printf("Reading message 3\n"); fflush(stdout);
  80. if(protobufReaderWriter.read_msg(protobuf_msg3)!=0)
  81. return -1;
  82. printf("Processing message 3\n"); fflush(stdout);
  83. protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3);
  84. if(protobuf_sgx_ret != 0)
  85. {
  86. printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  87. }
  88. return 0;
  89. }
  90. void LA::get_la_symmetric_key(uint8_t* op_key)
  91. {
  92. uint32_t counter;
  93. for(counter=0; counter<16; counter++)
  94. op_key[counter] = key[counter];
  95. }