SgxProtobufLAInitiator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <sys/mman.h>
  7. #include "sgx_trts.h"
  8. #include "sgx_utils.h"
  9. #include "error_codes.h"
  10. #include "sgx_ecp_types.h"
  11. #include "sgx_thread.h"
  12. #include <map>
  13. #include "sgx_dh.h"
  14. #include "dh_session_protocol.h"
  15. #include "sgx_tcrypto.h"
  16. #include "datatypes.h"
  17. #include "SgxProtobufLAInitiator_Transforms.h"
  18. #define MAX_SESSION_COUNT 16
  19. #define SGX_CAST(type, item) ((type)(item))
  20. #include <string.h>
  21. #include "crypto.h"
  22. #include "stdio.h"
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. dh_session_t global_session_info;
  27. sgx_dh_session_t sgx_dh_session;
  28. uint8_t key[16];
  29. 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)
  30. {
  31. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  32. sgx_dh_msg2_t dh_msg2;
  33. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  34. uint32_t ret_status;
  35. if(decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
  36. return -1;
  37. //Intialize the session as a session initiator
  38. ret_status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
  39. if(ret_status != SGX_SUCCESS)
  40. return ret_status;
  41. //Process the message 1 obtained from desination enclave and generate message 2
  42. ret_status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
  43. if(SGX_SUCCESS != ret_status)
  44. return ret_status;
  45. encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
  46. return 0;
  47. }
  48. uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id) {
  49. uint32_t ret_status;
  50. sgx_dh_msg3_t dh_msg3;
  51. sgx_key_128bit_t dh_aek; // Session Key
  52. sgx_dh_session_enclave_identity_t responder_identity;
  53. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  54. if(decode_msg3_from_protobuf(protobuf_msg3, &dh_msg3)!=0)
  55. return -1;
  56. //Process Message 3 obtained from the destination enclave
  57. ret_status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
  58. if(SGX_SUCCESS != ret_status)
  59. return ret_status;
  60. //No verification checks here: no security guarantees obtained from any such checks
  61. memcpy(key, &dh_aek, sizeof(sgx_key_128bit_t));
  62. //memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  63. global_session_info.session_id = 1; // TODO: session_id;
  64. global_session_info.active.counter = 0;
  65. global_session_info.status = ACTIVE;
  66. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  67. return 0;
  68. }
  69. uint32_t aes_gcm_wrapper(int enc, uint8_t* plaintext, uint32_t plaintext_length, uint8_t* ciphertext, uint32_t* ciphertext_length)
  70. {
  71. uint32_t actual_plaintext_length=plaintext_length;
  72. uint8_t tag[16];uint32_t counter, return_status;
  73. uint8_t iv[12];
  74. if(enc == 0)
  75. {
  76. for(counter=0;counter<16;counter++)
  77. tag[counter]=plaintext[counter+plaintext_length-16];
  78. for(counter=0;counter<12;counter++)
  79. iv[counter]=plaintext[counter+plaintext_length-28];
  80. actual_plaintext_length-=28;
  81. }
  82. else
  83. {
  84. return_status=sgx_read_rand(iv, 12);
  85. if(return_status != 0)
  86. return return_status;
  87. }
  88. return_status = aes_gcm_128(enc, key, iv, plaintext, actual_plaintext_length, ciphertext, ciphertext_length, tag);
  89. if(enc == 1 && return_status == 0)
  90. {
  91. for(counter=0;counter<12;counter++)
  92. ciphertext[counter + *ciphertext_length] = iv[counter];
  93. for(counter=0;counter<16;counter++)
  94. ciphertext[counter + 12 + *ciphertext_length] = tag[counter];
  95. *ciphertext_length=*ciphertext_length + 28;
  96. }
  97. return return_status;
  98. }