SgxProtobufLAInitiator.cpp 3.6 KB

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