comms.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include <vector>
  2. #include <functional>
  3. #include <cstring>
  4. #include "sgx_tcrypto.h"
  5. #include "sgx_tseal.h"
  6. #include "Enclave_t.h"
  7. #include "utils.hpp"
  8. #include "config.hpp"
  9. // Our public and private identity keys
  10. static sgx_ec256_private_t g_privkey;
  11. static sgx_ec256_public_t g_pubkey;
  12. // What step of the handshake are we on?
  13. enum HandshakeStep {
  14. HANDSHAKE_NONE,
  15. HANDSHAKE_C_SENT_1,
  16. HANDSHAKE_S_SENT_2,
  17. HANDSHAKE_COMPLETE
  18. };
  19. // Communication state for a node
  20. struct NodeCommState {
  21. sgx_ec256_public_t pubkey;
  22. nodenum_t node_num;
  23. HandshakeStep handshake_step;
  24. // Our DH keypair during the handshake
  25. sgx_ec256_private_t handshake_privkey;
  26. sgx_ec256_public_t handshake_pubkey;
  27. // The peer's DH public key during the handshake
  28. sgx_ec256_public_t handshake_peer_pubkey;
  29. // The outgoing and incoming AES keys after the handshake
  30. sgx_aes_gcm_128bit_key_t out_aes_key, in_aes_key;
  31. // The outgoing and incoming IV counters
  32. uint8_t out_aes_iv[SGX_AESGCM_IV_SIZE];
  33. uint8_t in_aes_iv[SGX_AESGCM_IV_SIZE];
  34. // The GCM state for incrementally building each outgoing chunk
  35. sgx_aes_state_handle_t out_aes_gcm_state;
  36. // The current outgoing frame and the current offset into it
  37. uint8_t *frame;
  38. uint32_t frame_offset;
  39. // The current outgoing message ciphertext size and the offset into
  40. // it of the start of the current frame
  41. uint32_t msg_size;
  42. uint32_t msg_frame_offset;
  43. // The current outgoing message plaintext size, how many plaintext
  44. // bytes we've already processed with message_data, and how many
  45. // plaintext bytes remain for the current chunk
  46. uint32_t msg_plaintext_size;
  47. uint32_t msg_plaintext_processed;
  48. uint32_t msg_plaintext_chunk_remain;
  49. // The current incoming message ciphertext size and the offset into
  50. // it of all previous chunks of this message
  51. uint32_t in_msg_size;
  52. uint32_t in_msg_offset;
  53. // The current incoming message number of plaintext bytes processed
  54. uint32_t in_msg_plaintext_processed;
  55. // The internal buffer where we're storing the (decrypted) message
  56. uint8_t *in_msg_buf;
  57. // The function to call when a new incoming message header arrives.
  58. // This function should return a pointer to enough memory to hold
  59. // the (decrypted) chunks of the message. Remember that the length
  60. // passed here is the total size of the _encrypted_ chunks. This
  61. // function should not itself modify the in_msg_size, in_msg_offset,
  62. // or in_msg_buf members. This function will usually allocate an
  63. // appropriate amount of memory and return the pointer to it, but
  64. // may do other things, like return a pointer to the middle of a
  65. // previously allocated region of memory.
  66. std::function<uint8_t*(NodeCommState&,uint32_t)> in_msg_get_buf;
  67. // The function to call after the last chunk of a message has been
  68. // received. If in_msg_get_buf allocated memory, this function
  69. // should deallocate it. in_msg_size, in_msg_offset,
  70. // in_msg_plaintext_processed, and in_msg_buf will already have been
  71. // reset when this function is called. The uint32_t that is passed
  72. // are the total size of the _decrypted_ data and the original total
  73. // size of the _encrypted_ chunks that was passed to in_msg_get_buf.
  74. std::function<void(NodeCommState&,uint8_t*,uint32_t,uint32_t)>
  75. in_msg_received;
  76. NodeCommState(const sgx_ec256_public_t* conf_pubkey, nodenum_t i) :
  77. node_num(i), handshake_step(HANDSHAKE_NONE),
  78. out_aes_gcm_state(NULL), frame(NULL),
  79. frame_offset(0), msg_size(0), msg_frame_offset(0),
  80. msg_plaintext_size(0), msg_plaintext_processed(0),
  81. msg_plaintext_chunk_remain(0),
  82. in_msg_size(0), in_msg_offset(0),
  83. in_msg_plaintext_processed(0), in_msg_buf(NULL),
  84. in_msg_get_buf(NULL), in_msg_received(NULL) {
  85. memmove(&pubkey, conf_pubkey, sizeof(pubkey));
  86. }
  87. void message_start(uint32_t plaintext_len);
  88. void message_data(uint8_t *data, uint32_t len);
  89. // Start the handshake (as the client)
  90. void handshake_start();
  91. };
  92. // A typical default in_msg_get_buf handler. It computes the maximum
  93. // possible size of the decrypted data, allocates that much memory, and
  94. // returns a pointer to it.
  95. static uint8_t* default_in_msg_get_buf(NodeCommState &commst,
  96. uint32_t tot_enc_chunk_size)
  97. {
  98. uint32_t max_plaintext_bytes = tot_enc_chunk_size;
  99. // If the handshake is complete, chunks will be encrypted and have a
  100. // MAC tag attached which will not correspond to plaintext bytes, so
  101. // we can trim them.
  102. if (commst.handshake_step == HANDSHAKE_COMPLETE) {
  103. // The minimum number of chunks needed to transmit this message
  104. uint32_t min_num_chunks =
  105. (tot_enc_chunk_size + (FRAME_SIZE-1)) / FRAME_SIZE;
  106. // The maximum number of plaintext bytes this message could contain
  107. max_plaintext_bytes = tot_enc_chunk_size -
  108. SGX_AESGCM_MAC_SIZE * min_num_chunks;
  109. }
  110. return new uint8_t[max_plaintext_bytes];
  111. }
  112. static void handshake_1_msg_received(NodeCommState &nodest,
  113. uint8_t *data, uint32_t plaintext_len, uint32_t);
  114. static void handshake_2_msg_received(NodeCommState &nodest,
  115. uint8_t *data, uint32_t plaintext_len, uint32_t);
  116. static void handshake_3_msg_received(NodeCommState &nodest,
  117. uint8_t *data, uint32_t plaintext_len, uint32_t);
  118. // Receive (at the server) the first handshake message
  119. static void handshake_1_msg_received(NodeCommState &nodest,
  120. uint8_t *data, uint32_t plaintext_len, uint32_t)
  121. {
  122. /*
  123. printf("Received handshake_1 message of %u bytes:\n", plaintext_len);
  124. for (uint32_t i=0;i<plaintext_len;++i) {
  125. printf("%02x", data[i]);
  126. }
  127. printf("\n");
  128. */
  129. if (plaintext_len != sizeof(sgx_ec256_public_t)) {
  130. printf("Received handshake_1 message of incorrect size %u\n",
  131. plaintext_len);
  132. return;
  133. }
  134. sgx_ecc_state_handle_t ecc_handle;
  135. sgx_ec256_public_t peer_pubkey;
  136. memmove(&peer_pubkey, data, sizeof(peer_pubkey));
  137. delete[] data;
  138. sgx_ecc256_open_context(&ecc_handle);
  139. int valid;
  140. if (sgx_ecc256_check_point(&peer_pubkey, ecc_handle, &valid) || !valid) {
  141. printf("Invalid public key received from node %hu\n",
  142. nodest.node_num);
  143. sgx_ecc256_close_context(ecc_handle);
  144. return;
  145. }
  146. printf("Valid public key received from node %hu\n", nodest.node_num);
  147. // Create our own DH key pair
  148. sgx_ec256_public_t our_pubkey;
  149. sgx_ec256_private_t our_privkey;
  150. sgx_ecc256_create_key_pair(&our_privkey, &our_pubkey, ecc_handle);
  151. // Construct the shared secret
  152. sgx_ec256_dh_shared_t sharedsecret;
  153. sgx_ecc256_compute_shared_dhkey(&our_privkey, &peer_pubkey,
  154. &sharedsecret, ecc_handle);
  155. memset(&our_privkey, 0, sizeof(our_privkey));
  156. // Compute H1(sharedsecret) and H2(sharedsecret)
  157. sgx_sha_state_handle_t sha_handle;
  158. sgx_sha256_hash_t h1, h2;
  159. sgx_sha256_init(&sha_handle);
  160. sgx_sha256_update((const uint8_t*)"\x01", 1, sha_handle);
  161. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  162. sha_handle);
  163. sgx_sha256_get_hash(sha_handle, &h1);
  164. sgx_sha256_close(sha_handle);
  165. sgx_sha256_init(&sha_handle);
  166. sgx_sha256_update((const uint8_t*)"\x02", 1, sha_handle);
  167. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  168. sha_handle);
  169. sgx_sha256_get_hash(sha_handle, &h2);
  170. sgx_sha256_close(sha_handle);
  171. // Compute the server-to-client MAC
  172. sgx_hmac_state_handle_t hmac_handle;
  173. uint8_t srv_cli_mac[16];
  174. sgx_hmac256_init(h1, 16, &hmac_handle);
  175. sgx_hmac256_update((uint8_t*)&our_pubkey, sizeof(our_pubkey),
  176. hmac_handle);
  177. sgx_hmac256_update((uint8_t*)&peer_pubkey, sizeof(peer_pubkey),
  178. hmac_handle);
  179. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  180. hmac_handle);
  181. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  182. hmac_handle);
  183. sgx_hmac256_final(srv_cli_mac, 16, hmac_handle);
  184. sgx_hmac256_close(hmac_handle);
  185. // Compute the client-to-server MAC
  186. uint8_t cli_srv_mac[16];
  187. sgx_hmac256_init(((uint8_t*)h1)+16, 16, &hmac_handle);
  188. sgx_hmac256_update((uint8_t*)&peer_pubkey, sizeof(peer_pubkey),
  189. hmac_handle);
  190. sgx_hmac256_update((uint8_t*)&our_pubkey, sizeof(our_pubkey),
  191. hmac_handle);
  192. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  193. hmac_handle);
  194. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  195. hmac_handle);
  196. sgx_hmac256_final(cli_srv_mac, 16, hmac_handle);
  197. sgx_hmac256_close(hmac_handle);
  198. // Sign the server-to-client MAC
  199. sgx_ec256_signature_t srv_cli_sig;
  200. sgx_ecdsa_sign(srv_cli_mac, 16, &g_privkey, &srv_cli_sig, ecc_handle);
  201. sgx_ecc256_close_context(ecc_handle);
  202. // Get us ready to receive handshake message 3
  203. nodest.in_msg_get_buf = default_in_msg_get_buf;
  204. nodest.in_msg_received = handshake_3_msg_received;
  205. nodest.handshake_step = HANDSHAKE_S_SENT_2;
  206. // Send handshake message 2
  207. nodest.message_start(sizeof(our_pubkey) + sizeof(srv_cli_sig));
  208. nodest.message_data((uint8_t*)&our_pubkey, sizeof(our_pubkey));
  209. nodest.message_data((uint8_t*)&srv_cli_sig, sizeof(srv_cli_sig));
  210. }
  211. static void handshake_2_msg_received(NodeCommState &nodest,
  212. uint8_t *data, uint32_t plaintext_len, uint32_t)
  213. {
  214. printf("Received handshake_2 message of %u bytes:\n", plaintext_len);
  215. for (uint32_t i=0;i<plaintext_len;++i) {
  216. printf("%02x", data[i]);
  217. }
  218. printf("\n");
  219. }
  220. static void handshake_3_msg_received(NodeCommState &nodest,
  221. uint8_t *data, uint32_t plaintext_len, uint32_t)
  222. {
  223. }
  224. // Start a new outgoing message. Pass the number of _plaintext_ bytes
  225. // the message will be.
  226. void NodeCommState::message_start(uint32_t plaintext_len)
  227. {
  228. uint32_t ciphertext_len = plaintext_len;
  229. // If the handshake is complete, add SGX_AESGCM_MAC_SIZE bytes for
  230. // every FRAME_SIZE-SGX_AESGCM_MAC_SIZE bytes of plaintext.
  231. if (handshake_step == HANDSHAKE_COMPLETE) {
  232. uint32_t num_chunks = (plaintext_len +
  233. FRAME_SIZE - SGX_AESGCM_MAC_SIZE - 1) /
  234. (FRAME_SIZE - SGX_AESGCM_MAC_SIZE);
  235. ciphertext_len = plaintext_len +
  236. num_chunks * SGX_AESGCM_MAC_SIZE;
  237. }
  238. ocall_message(&frame, node_num, ciphertext_len);
  239. frame_offset = 0;
  240. msg_size = ciphertext_len;
  241. msg_frame_offset = 0;
  242. msg_plaintext_size = plaintext_len;
  243. msg_plaintext_processed = 0;
  244. if (plaintext_len < FRAME_SIZE - SGX_AESGCM_MAC_SIZE) {
  245. msg_plaintext_chunk_remain = plaintext_len;
  246. } else {
  247. msg_plaintext_chunk_remain = FRAME_SIZE - SGX_AESGCM_MAC_SIZE;
  248. }
  249. if (!frame) {
  250. printf("Received NULL back from ocall_message\n");
  251. }
  252. if (msg_plaintext_chunk_remain > 0) {
  253. *(size_t*)out_aes_iv += 1;
  254. sgx_aes_gcm128_enc_init(out_aes_key, out_aes_iv, SGX_AESGCM_IV_SIZE,
  255. NULL, 0, &out_aes_gcm_state);
  256. }
  257. }
  258. // Process len bytes of plaintext data into the current message.
  259. void NodeCommState::message_data(uint8_t *data, uint32_t len)
  260. {
  261. while (len > 0) {
  262. if (msg_plaintext_chunk_remain == 0) {
  263. printf("Attempt to queue too much message data\n");
  264. return;
  265. }
  266. uint32_t bytes_to_process = len;
  267. if (bytes_to_process > msg_plaintext_chunk_remain) {
  268. bytes_to_process = msg_plaintext_chunk_remain;
  269. }
  270. if (frame == NULL) {
  271. printf("frame is NULL when queueing message data\n");
  272. return;
  273. }
  274. if (handshake_step == HANDSHAKE_COMPLETE) {
  275. // Encrypt the data
  276. sgx_aes_gcm128_enc_update(data, bytes_to_process,
  277. frame+frame_offset, out_aes_gcm_state);
  278. } else {
  279. // Just copy the plaintext data during the handshake
  280. memmove(frame+frame_offset, data, bytes_to_process);
  281. }
  282. frame_offset += bytes_to_process;
  283. msg_plaintext_processed += bytes_to_process;
  284. msg_plaintext_chunk_remain -= bytes_to_process;
  285. len -= bytes_to_process;
  286. data += bytes_to_process;
  287. if (msg_plaintext_chunk_remain == 0) {
  288. // Complete and send this chunk
  289. if (handshake_step == HANDSHAKE_COMPLETE) {
  290. sgx_aes_gcm128_enc_get_mac(frame+frame_offset,
  291. out_aes_gcm_state);
  292. frame_offset += SGX_AESGCM_MAC_SIZE;
  293. }
  294. uint8_t *nextframe = NULL;
  295. ocall_chunk(&nextframe, node_num, frame, frame_offset);
  296. frame = nextframe;
  297. msg_frame_offset += frame_offset;
  298. frame_offset = 0;
  299. msg_plaintext_chunk_remain =
  300. msg_plaintext_size - msg_plaintext_processed;
  301. if (msg_plaintext_chunk_remain >
  302. FRAME_SIZE - SGX_AESGCM_MAC_SIZE) {
  303. msg_plaintext_chunk_remain =
  304. FRAME_SIZE - SGX_AESGCM_MAC_SIZE;
  305. }
  306. if (handshake_step == HANDSHAKE_COMPLETE) {
  307. sgx_aes_gcm_close(out_aes_gcm_state);
  308. if (msg_plaintext_chunk_remain > 0) {
  309. *(size_t*)out_aes_iv += 1;
  310. sgx_aes_gcm128_enc_init(out_aes_key, out_aes_iv,
  311. SGX_AESGCM_IV_SIZE, NULL, 0, &out_aes_gcm_state);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. // The communication states for all the nodes. There's an entry for
  318. // ourselves in here, but it is unused.
  319. static std::vector<NodeCommState> commstates;
  320. static nodenum_t tot_nodes, my_node_num;
  321. // Generate a new identity signature key. Output the public key and the
  322. // sealed private key. outsealedpriv must point to SEALEDPRIVKEY_SIZE =
  323. // sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 18 bytes of
  324. // memory.
  325. void ecall_identity_key_new(sgx_ec256_public_t *outpub,
  326. sgx_sealed_data_t *outsealedpriv)
  327. {
  328. sgx_ecc_state_handle_t ecc_handle;
  329. sgx_ecc256_open_context(&ecc_handle);
  330. sgx_ecc256_create_key_pair(&g_privkey, &g_pubkey, ecc_handle);
  331. memmove(outpub, &g_pubkey, sizeof(g_pubkey));
  332. sgx_ecc256_close_context(ecc_handle);
  333. sgx_seal_data(18, (const uint8_t*)"TEEMS Identity key",
  334. sizeof(g_privkey), (const uint8_t*)&g_privkey,
  335. SEALED_PRIVKEY_SIZE, outsealedpriv);
  336. }
  337. // Load an identity key from a sealed privkey. Output the resulting
  338. // public key. insealedpriv must point to sizeof(sgx_sealed_data_t) +
  339. // sizeof(sgx_ec256_private_t) bytes of memory. Returns true for
  340. // success, false for failure.
  341. bool ecall_identity_key_load(sgx_ec256_public_t *outpub,
  342. const sgx_sealed_data_t *insealedpriv)
  343. {
  344. sgx_ecc_state_handle_t ecc_handle;
  345. char aad[18];
  346. uint32_t aadsize = sizeof(aad);
  347. sgx_ec256_private_t privkey;
  348. uint32_t privkeysize = sizeof(privkey);
  349. sgx_status_t res = sgx_unseal_data(
  350. insealedpriv, (uint8_t*)aad, &aadsize,
  351. (uint8_t*)&privkey, &privkeysize);
  352. if (res || aadsize != sizeof(aad) || privkeysize != sizeof(privkey)
  353. || memcmp(aad, "TEEMS Identity key", sizeof(aad))) {
  354. return false;
  355. }
  356. sgx_ecc256_open_context(&ecc_handle);
  357. sgx_ec256_public_t pubkey;
  358. int valid;
  359. if (sgx_ecc256_calculate_pub_from_priv(&privkey, &pubkey) ||
  360. sgx_ecc256_check_point(&pubkey, ecc_handle, &valid) ||
  361. !valid) {
  362. sgx_ecc256_close_context(ecc_handle);
  363. return false;
  364. }
  365. sgx_ecc256_close_context(ecc_handle);
  366. memmove(&g_pubkey, &pubkey, sizeof(pubkey));
  367. memmove(&g_privkey, &privkey, sizeof(privkey));
  368. memmove(outpub, &pubkey, sizeof(pubkey));
  369. return true;
  370. }
  371. bool comms_init_nodestate(const EnclaveAPINodeConfig *apinodeconfigs,
  372. nodenum_t num_nodes, nodenum_t me)
  373. {
  374. sgx_ecc_state_handle_t ecc_handle;
  375. sgx_ecc256_open_context(&ecc_handle);
  376. commstates.clear();
  377. tot_nodes = 0;
  378. commstates.reserve(num_nodes);
  379. for (nodenum_t i=0; i<num_nodes; ++i) {
  380. // Check that the pubkey is valid
  381. int valid;
  382. if (sgx_ecc256_check_point(&apinodeconfigs[i].pubkey,
  383. ecc_handle, &valid) ||
  384. !valid) {
  385. printf("Pubkey for node %hu invalid\n", i);
  386. commstates.clear();
  387. sgx_ecc256_close_context(ecc_handle);
  388. return false;
  389. }
  390. commstates.emplace_back(&apinodeconfigs[i].pubkey, i);
  391. }
  392. sgx_ecc256_close_context(ecc_handle);
  393. my_node_num = me;
  394. // Check that no one other than us has our pubkey (deals with
  395. // reflection attacks)
  396. for (nodenum_t i=0; i<num_nodes; ++i) {
  397. if (i == my_node_num) continue;
  398. if (!memcmp(&commstates[i].pubkey,
  399. &commstates[my_node_num].pubkey,
  400. sizeof(commstates[i].pubkey))) {
  401. printf("Pubkey %hu matches our own; possible reflection attack?\n",
  402. i);
  403. commstates.clear();
  404. return false;
  405. }
  406. }
  407. tot_nodes = num_nodes;
  408. // There will be an enclave-to-enclave channel between us and each
  409. // other node's enclave. For the node numbers smaller than ours, we
  410. // will be the server for the handshake for that channel. Prepare
  411. // to receive the first handshake message from those nodes'
  412. // enclaves.
  413. for (nodenum_t i=0; i<my_node_num; ++i) {
  414. commstates[i].in_msg_get_buf = default_in_msg_get_buf;
  415. commstates[i].in_msg_received = handshake_1_msg_received;
  416. }
  417. return true;
  418. }
  419. bool ecall_message(nodenum_t node_num, uint32_t message_len)
  420. {
  421. if (node_num >= tot_nodes) {
  422. printf("Out-of-range node_num %hu received in ecall_message\n",
  423. node_num);
  424. return false;
  425. }
  426. NodeCommState &nodest = commstates[node_num];
  427. if (nodest.in_msg_size != nodest.in_msg_offset) {
  428. printf("Received ecall_message without completing previous message\n");
  429. return false;
  430. }
  431. if (!nodest.in_msg_get_buf) {
  432. printf("No message header handler registered\n");
  433. return false;
  434. }
  435. uint8_t *buf = nodest.in_msg_get_buf(nodest, message_len);
  436. if (!buf) {
  437. printf("Message header handler returned NULL\n");
  438. return false;
  439. }
  440. nodest.in_msg_size = message_len;
  441. nodest.in_msg_offset = 0;
  442. nodest.in_msg_plaintext_processed = 0;
  443. nodest.in_msg_buf = buf;
  444. return true;
  445. }
  446. bool ecall_chunk(nodenum_t node_num, const uint8_t *chunkdata,
  447. uint32_t chunklen)
  448. {
  449. if (node_num >= tot_nodes) {
  450. printf("Out-of-range node_num %hu received in ecall_chunk\n",
  451. node_num);
  452. return false;
  453. }
  454. NodeCommState &nodest = commstates[node_num];
  455. if (nodest.in_msg_size == nodest.in_msg_offset) {
  456. printf("Received ecall_chunk after completing message\n");
  457. return false;
  458. }
  459. if (!nodest.in_msg_buf) {
  460. printf("No incoming message buffer allocated\n");
  461. return false;
  462. }
  463. if (!nodest.in_msg_received) {
  464. printf("No message received handler registered\n");
  465. return false;
  466. }
  467. if (nodest.in_msg_offset + chunklen > nodest.in_msg_size) {
  468. printf("Chunk larger than remaining message size\n");
  469. return false;
  470. }
  471. if (nodest.handshake_step == HANDSHAKE_COMPLETE) {
  472. // Decrypt the incoming data
  473. *(size_t*)(nodest.in_aes_iv) += 1;
  474. if (sgx_rijndael128GCM_decrypt(&nodest.in_aes_key, chunkdata,
  475. chunklen - SGX_AESGCM_MAC_SIZE,
  476. nodest.in_msg_buf + nodest.in_msg_plaintext_processed,
  477. nodest.in_aes_iv, SGX_AESGCM_IV_SIZE, NULL, 0,
  478. (const sgx_aes_gcm_128bit_tag_t *)
  479. (chunkdata + chunklen - SGX_AESGCM_MAC_SIZE))) {
  480. printf("Decryption failed\n");
  481. return false;
  482. }
  483. nodest.in_msg_plaintext_processed +=
  484. chunklen - SGX_AESGCM_MAC_SIZE;
  485. } else {
  486. // Just copy the handshake data
  487. memmove(nodest.in_msg_buf + nodest.in_msg_plaintext_processed,
  488. chunkdata, chunklen);
  489. nodest.in_msg_plaintext_processed += chunklen;
  490. }
  491. nodest.in_msg_offset += chunklen;
  492. if (nodest.in_msg_offset == nodest.in_msg_size) {
  493. // This was the last chunk; handle the received message
  494. uint8_t* buf = nodest.in_msg_buf;
  495. uint32_t plaintext_processed = nodest.in_msg_plaintext_processed;
  496. uint32_t msg_size = nodest.in_msg_size;
  497. nodest.in_msg_buf = NULL;
  498. nodest.in_msg_size = 0;
  499. nodest.in_msg_offset = 0;
  500. nodest.in_msg_plaintext_processed = 0;
  501. nodest.in_msg_received(nodest, buf, plaintext_processed, msg_size);
  502. }
  503. return true;
  504. }
  505. // Start the handshake (as the client)
  506. void NodeCommState::handshake_start()
  507. {
  508. sgx_ecc_state_handle_t ecc_handle;
  509. sgx_ecc256_open_context(&ecc_handle);
  510. // Create a DH keypair
  511. sgx_ecc256_create_key_pair(&handshake_privkey, &handshake_pubkey,
  512. ecc_handle);
  513. sgx_ecc256_close_context(ecc_handle);
  514. // Get us ready to receive handshake message 2
  515. in_msg_get_buf = default_in_msg_get_buf;
  516. in_msg_received = handshake_2_msg_received;
  517. handshake_step = HANDSHAKE_C_SENT_1;
  518. // Send the public key as the first message
  519. message_start(sizeof(handshake_pubkey));
  520. message_data((uint8_t*)&handshake_pubkey, sizeof(handshake_pubkey));
  521. }
  522. // Start all handshakes for which we are the client
  523. bool ecall_comms_start()
  524. {
  525. for (nodenum_t t = my_node_num+1; t<tot_nodes; ++t) {
  526. commstates[t].handshake_start();
  527. }
  528. return true;
  529. }