comms.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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_dh_privkey;
  26. sgx_ec256_public_t handshake_dh_pubkey;
  27. // The server keeps this state between handshake messages 1 and 3
  28. uint8_t handshake_cli_srv_mac[16];
  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, bool encrypt=true);
  88. void message_data(uint8_t *data, uint32_t len, bool encrypt=true);
  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 default_in_msg_received(NodeCommState &nodest,
  113. uint8_t *data, uint32_t plaintext_len, uint32_t)
  114. {
  115. printf("Received message of %u bytes from node %lu:\n",
  116. plaintext_len, nodest.node_num);
  117. for (uint32_t i=0;i<plaintext_len;++i) {
  118. printf("%02x", data[i]);
  119. }
  120. printf("\n");
  121. delete[] data;
  122. }
  123. static void handshake_1_msg_received(NodeCommState &nodest,
  124. uint8_t *data, uint32_t plaintext_len, uint32_t);
  125. static void handshake_2_msg_received(NodeCommState &nodest,
  126. uint8_t *data, uint32_t plaintext_len, uint32_t);
  127. static void handshake_3_msg_received(NodeCommState &nodest,
  128. uint8_t *data, uint32_t plaintext_len, uint32_t);
  129. // Receive (at the server) the first handshake message
  130. static void handshake_1_msg_received(NodeCommState &nodest,
  131. uint8_t *data, uint32_t plaintext_len, uint32_t)
  132. {
  133. /*
  134. printf("Received handshake_1 message of %u bytes:\n", plaintext_len);
  135. for (uint32_t i=0;i<plaintext_len;++i) {
  136. printf("%02x", data[i]);
  137. }
  138. printf("\n");
  139. */
  140. if (plaintext_len != sizeof(sgx_ec256_public_t)) {
  141. printf("Received handshake_1 message of incorrect size %u\n",
  142. plaintext_len);
  143. return;
  144. }
  145. sgx_ecc_state_handle_t ecc_handle;
  146. sgx_ec256_public_t peer_dh_pubkey;
  147. memmove(&peer_dh_pubkey, data, sizeof(peer_dh_pubkey));
  148. delete[] data;
  149. sgx_ecc256_open_context(&ecc_handle);
  150. int valid;
  151. if (sgx_ecc256_check_point(&peer_dh_pubkey, ecc_handle, &valid)
  152. || !valid) {
  153. printf("Invalid public key received from node %hu\n",
  154. nodest.node_num);
  155. sgx_ecc256_close_context(ecc_handle);
  156. return;
  157. }
  158. printf("Valid public key received from node %hu\n", nodest.node_num);
  159. // Create our own DH key pair
  160. sgx_ec256_public_t our_dh_pubkey;
  161. sgx_ec256_private_t our_dh_privkey;
  162. sgx_ecc256_create_key_pair(&our_dh_privkey, &our_dh_pubkey, ecc_handle);
  163. // Construct the shared secret
  164. sgx_ec256_dh_shared_t sharedsecret;
  165. sgx_ecc256_compute_shared_dhkey(&our_dh_privkey, &peer_dh_pubkey,
  166. &sharedsecret, ecc_handle);
  167. memset(&our_dh_privkey, 0, sizeof(our_dh_privkey));
  168. // Compute H1(sharedsecret) and H2(sharedsecret)
  169. sgx_sha_state_handle_t sha_handle;
  170. sgx_sha256_hash_t h1, h2;
  171. sgx_sha256_init(&sha_handle);
  172. sgx_sha256_update((const uint8_t*)"\x01", 1, sha_handle);
  173. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  174. sha_handle);
  175. sgx_sha256_get_hash(sha_handle, &h1);
  176. sgx_sha256_close(sha_handle);
  177. sgx_sha256_init(&sha_handle);
  178. sgx_sha256_update((const uint8_t*)"\x02", 1, sha_handle);
  179. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  180. sha_handle);
  181. sgx_sha256_get_hash(sha_handle, &h2);
  182. sgx_sha256_close(sha_handle);
  183. // Compute the server-to-client MAC
  184. sgx_hmac_state_handle_t hmac_handle;
  185. uint8_t srv_cli_mac[16];
  186. sgx_hmac256_init(h1, 16, &hmac_handle);
  187. sgx_hmac256_update((uint8_t*)&our_dh_pubkey, sizeof(our_dh_pubkey),
  188. hmac_handle);
  189. sgx_hmac256_update((uint8_t*)&peer_dh_pubkey, sizeof(peer_dh_pubkey),
  190. hmac_handle);
  191. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  192. hmac_handle);
  193. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  194. hmac_handle);
  195. sgx_hmac256_final(srv_cli_mac, 16, hmac_handle);
  196. sgx_hmac256_close(hmac_handle);
  197. // Compute the client-to-server MAC
  198. uint8_t cli_srv_mac[16];
  199. sgx_hmac256_init(((uint8_t*)h1)+16, 16, &hmac_handle);
  200. sgx_hmac256_update((uint8_t*)&peer_dh_pubkey, sizeof(peer_dh_pubkey),
  201. hmac_handle);
  202. sgx_hmac256_update((uint8_t*)&our_dh_pubkey, sizeof(our_dh_pubkey),
  203. hmac_handle);
  204. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  205. hmac_handle);
  206. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  207. hmac_handle);
  208. sgx_hmac256_final(cli_srv_mac, 16, hmac_handle);
  209. sgx_hmac256_close(hmac_handle);
  210. // Sign the server-to-client MAC
  211. sgx_ec256_signature_t srv_cli_sig;
  212. sgx_ecdsa_sign(srv_cli_mac, 16, &g_privkey, &srv_cli_sig, ecc_handle);
  213. sgx_ecc256_close_context(ecc_handle);
  214. // Save the state we'll need to process handshake message 3
  215. memmove(&nodest.in_aes_key, h2, 16);
  216. memmove(&nodest.out_aes_key, ((uint8_t*)h2)+16, 16);
  217. memmove(&nodest.handshake_cli_srv_mac, cli_srv_mac, 16);
  218. // Get us ready to receive handshake message 3
  219. nodest.in_msg_get_buf = default_in_msg_get_buf;
  220. nodest.in_msg_received = handshake_3_msg_received;
  221. nodest.handshake_step = HANDSHAKE_S_SENT_2;
  222. // Send handshake message 2
  223. nodest.message_start(sizeof(our_dh_pubkey) + sizeof(srv_cli_sig),
  224. false);
  225. nodest.message_data((uint8_t*)&our_dh_pubkey, sizeof(our_dh_pubkey),
  226. false);
  227. nodest.message_data((uint8_t*)&srv_cli_sig, sizeof(srv_cli_sig),
  228. false);
  229. }
  230. // Receive (at the client) the secong handshake message
  231. static void handshake_2_msg_received(NodeCommState &nodest,
  232. uint8_t *data, uint32_t plaintext_len, uint32_t)
  233. {
  234. /*
  235. printf("Received handshake_2 message of %u bytes:\n", plaintext_len);
  236. for (uint32_t i=0;i<plaintext_len;++i) {
  237. printf("%02x", data[i]);
  238. }
  239. printf("\n");
  240. */
  241. if (plaintext_len != sizeof(sgx_ec256_public_t) +
  242. sizeof(sgx_ec256_signature_t)) {
  243. printf("Received handshake_2 message of incorrect size %u\n",
  244. plaintext_len);
  245. return;
  246. }
  247. sgx_ecc_state_handle_t ecc_handle;
  248. sgx_ec256_public_t peer_dh_pubkey;
  249. sgx_ec256_signature_t peer_sig;
  250. memmove(&peer_dh_pubkey, data, sizeof(peer_dh_pubkey));
  251. memmove(&peer_sig, data+sizeof(peer_dh_pubkey), sizeof(peer_sig));
  252. delete[] data;
  253. sgx_ecc256_open_context(&ecc_handle);
  254. int valid;
  255. if (sgx_ecc256_check_point(&peer_dh_pubkey, ecc_handle, &valid)
  256. || !valid) {
  257. printf("Invalid public key received from node %hu\n",
  258. nodest.node_num);
  259. sgx_ecc256_close_context(ecc_handle);
  260. return;
  261. }
  262. // Construct the shared secret
  263. sgx_ec256_dh_shared_t sharedsecret;
  264. sgx_ecc256_compute_shared_dhkey(&nodest.handshake_dh_privkey,
  265. &peer_dh_pubkey, &sharedsecret, ecc_handle);
  266. memset(&nodest.handshake_dh_privkey, 0,
  267. sizeof(nodest.handshake_dh_privkey));
  268. // Compute H1(sharedsecret) and H2(sharedsecret)
  269. sgx_sha_state_handle_t sha_handle;
  270. sgx_sha256_hash_t h1, h2;
  271. sgx_sha256_init(&sha_handle);
  272. sgx_sha256_update((const uint8_t*)"\x01", 1, sha_handle);
  273. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  274. sha_handle);
  275. sgx_sha256_get_hash(sha_handle, &h1);
  276. sgx_sha256_close(sha_handle);
  277. sgx_sha256_init(&sha_handle);
  278. sgx_sha256_update((const uint8_t*)"\x02", 1, sha_handle);
  279. sgx_sha256_update((uint8_t*)&sharedsecret, sizeof(sharedsecret),
  280. sha_handle);
  281. sgx_sha256_get_hash(sha_handle, &h2);
  282. sgx_sha256_close(sha_handle);
  283. // Compute the server-to-client MAC
  284. sgx_hmac_state_handle_t hmac_handle;
  285. uint8_t srv_cli_mac[16];
  286. sgx_hmac256_init(h1, 16, &hmac_handle);
  287. sgx_hmac256_update((uint8_t*)&peer_dh_pubkey, sizeof(peer_dh_pubkey),
  288. hmac_handle);
  289. sgx_hmac256_update((uint8_t*)&nodest.handshake_dh_pubkey,
  290. sizeof(nodest.handshake_dh_pubkey), hmac_handle);
  291. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  292. hmac_handle);
  293. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  294. hmac_handle);
  295. sgx_hmac256_final(srv_cli_mac, 16, hmac_handle);
  296. sgx_hmac256_close(hmac_handle);
  297. // Compute the client-to-server MAC
  298. uint8_t cli_srv_mac[16];
  299. sgx_hmac256_init(((uint8_t*)h1)+16, 16, &hmac_handle);
  300. sgx_hmac256_update((uint8_t*)&nodest.handshake_dh_pubkey,
  301. sizeof(nodest.handshake_dh_pubkey), hmac_handle);
  302. sgx_hmac256_update((uint8_t*)&peer_dh_pubkey, sizeof(peer_dh_pubkey),
  303. hmac_handle);
  304. sgx_hmac256_update((uint8_t*)&g_pubkey, sizeof(g_pubkey),
  305. hmac_handle);
  306. sgx_hmac256_update((uint8_t*)&nodest.pubkey, sizeof(nodest.pubkey),
  307. hmac_handle);
  308. sgx_hmac256_final(cli_srv_mac, 16, hmac_handle);
  309. sgx_hmac256_close(hmac_handle);
  310. // Verify the signature on the server-to-client MAC
  311. uint8_t result;
  312. if (sgx_ecdsa_verify(srv_cli_mac, 16, &nodest.pubkey, &peer_sig,
  313. &result, ecc_handle) || result != SGX_EC_VALID) {
  314. printf("Invalid signature received from node %hu\n",
  315. nodest.node_num);
  316. sgx_ecc256_close_context(ecc_handle);
  317. return;
  318. }
  319. printf("Valid signature received from node %hu\n", nodest.node_num);
  320. // Sign the client-to-server MAC
  321. sgx_ec256_signature_t cli_srv_sig;
  322. sgx_ecdsa_sign(cli_srv_mac, 16, &g_privkey, &cli_srv_sig, ecc_handle);
  323. sgx_ecc256_close_context(ecc_handle);
  324. // Our side of the handshake is complete
  325. memmove(&nodest.out_aes_key, h2, 16);
  326. memmove(&nodest.in_aes_key, ((uint8_t*)h2)+16, 16);
  327. memset(&nodest.out_aes_iv, 0, SGX_AESGCM_IV_SIZE);
  328. memset(&nodest.in_aes_iv, 0, SGX_AESGCM_IV_SIZE);
  329. nodest.handshake_step = HANDSHAKE_COMPLETE;
  330. nodest.in_msg_get_buf = default_in_msg_get_buf;
  331. nodest.in_msg_received = default_in_msg_received;
  332. // Send handshake message 3
  333. nodest.message_start(sizeof(cli_srv_sig), false);
  334. nodest.message_data((uint8_t*)&cli_srv_sig, sizeof(cli_srv_sig),
  335. false);
  336. // Send a test message
  337. nodest.message_start(12);
  338. unsigned char buf[13];
  339. memmove(buf, "Hello, world", 13);
  340. nodest.message_data(buf, 12);
  341. }
  342. static void handshake_3_msg_received(NodeCommState &nodest,
  343. uint8_t *data, uint32_t plaintext_len, uint32_t)
  344. {
  345. /*
  346. printf("Received handshake_3 message of %u bytes:\n", plaintext_len);
  347. for (uint32_t i=0;i<plaintext_len;++i) {
  348. printf("%02x", data[i]);
  349. }
  350. printf("\n");
  351. */
  352. if (plaintext_len != sizeof(sgx_ec256_signature_t)) {
  353. printf("Received handshake_3 message of incorrect size %u\n",
  354. plaintext_len);
  355. return;
  356. }
  357. sgx_ecc_state_handle_t ecc_handle;
  358. sgx_ec256_signature_t peer_sig;
  359. memmove(&peer_sig, data, sizeof(peer_sig));
  360. delete[] data;
  361. sgx_ecc256_open_context(&ecc_handle);
  362. // Verify the signature on the client-to-server MAC
  363. uint8_t result;
  364. if (sgx_ecdsa_verify(nodest.handshake_cli_srv_mac, 16,
  365. &nodest.pubkey, &peer_sig, &result, ecc_handle)
  366. || result != SGX_EC_VALID) {
  367. printf("Invalid signature received from node %hu\n",
  368. nodest.node_num);
  369. sgx_ecc256_close_context(ecc_handle);
  370. return;
  371. }
  372. printf("Valid signature received from node %hu\n", nodest.node_num);
  373. // Our side of the handshake is complete
  374. memset(&nodest.out_aes_iv, 0, SGX_AESGCM_IV_SIZE);
  375. memset(&nodest.in_aes_iv, 0, SGX_AESGCM_IV_SIZE);
  376. nodest.handshake_step = HANDSHAKE_COMPLETE;
  377. nodest.in_msg_get_buf = default_in_msg_get_buf;
  378. nodest.in_msg_received = default_in_msg_received;
  379. // Send a test message
  380. nodest.message_start(12);
  381. unsigned char buf[13];
  382. memmove(buf, "Hello, world", 13);
  383. nodest.message_data(buf, 12);
  384. }
  385. // Start a new outgoing message. Pass the number of _plaintext_ bytes
  386. // the message will be.
  387. void NodeCommState::message_start(uint32_t plaintext_len, bool encrypt)
  388. {
  389. uint32_t ciphertext_len = plaintext_len;
  390. // If the handshake is complete, add SGX_AESGCM_MAC_SIZE bytes for
  391. // every FRAME_SIZE-SGX_AESGCM_MAC_SIZE bytes of plaintext.
  392. if (encrypt) {
  393. uint32_t num_chunks = (plaintext_len +
  394. FRAME_SIZE - SGX_AESGCM_MAC_SIZE - 1) /
  395. (FRAME_SIZE - SGX_AESGCM_MAC_SIZE);
  396. ciphertext_len = plaintext_len +
  397. num_chunks * SGX_AESGCM_MAC_SIZE;
  398. }
  399. ocall_message(&frame, node_num, ciphertext_len);
  400. frame_offset = 0;
  401. msg_size = ciphertext_len;
  402. msg_frame_offset = 0;
  403. msg_plaintext_size = plaintext_len;
  404. msg_plaintext_processed = 0;
  405. if (plaintext_len < FRAME_SIZE - SGX_AESGCM_MAC_SIZE) {
  406. msg_plaintext_chunk_remain = plaintext_len;
  407. } else {
  408. msg_plaintext_chunk_remain = FRAME_SIZE - SGX_AESGCM_MAC_SIZE;
  409. }
  410. if (!frame) {
  411. printf("Received NULL back from ocall_message\n");
  412. }
  413. if (msg_plaintext_chunk_remain > 0) {
  414. if (encrypt) {
  415. *(size_t*)out_aes_iv += 1;
  416. sgx_aes_gcm128_enc_init(out_aes_key, out_aes_iv,
  417. SGX_AESGCM_IV_SIZE, NULL, 0, &out_aes_gcm_state);
  418. }
  419. }
  420. }
  421. // Process len bytes of plaintext data into the current message.
  422. void NodeCommState::message_data(uint8_t *data, uint32_t len, bool encrypt)
  423. {
  424. while (len > 0) {
  425. if (msg_plaintext_chunk_remain == 0) {
  426. printf("Attempt to queue too much message data\n");
  427. return;
  428. }
  429. uint32_t bytes_to_process = len;
  430. if (bytes_to_process > msg_plaintext_chunk_remain) {
  431. bytes_to_process = msg_plaintext_chunk_remain;
  432. }
  433. if (frame == NULL) {
  434. printf("frame is NULL when queueing message data\n");
  435. return;
  436. }
  437. if (encrypt) {
  438. // Encrypt the data
  439. sgx_aes_gcm128_enc_update(data, bytes_to_process,
  440. frame+frame_offset, out_aes_gcm_state);
  441. } else {
  442. // Just copy the plaintext data during the handshake
  443. memmove(frame+frame_offset, data, bytes_to_process);
  444. }
  445. frame_offset += bytes_to_process;
  446. msg_plaintext_processed += bytes_to_process;
  447. msg_plaintext_chunk_remain -= bytes_to_process;
  448. len -= bytes_to_process;
  449. data += bytes_to_process;
  450. if (msg_plaintext_chunk_remain == 0) {
  451. // Complete and send this chunk
  452. if (encrypt) {
  453. sgx_aes_gcm128_enc_get_mac(frame+frame_offset,
  454. out_aes_gcm_state);
  455. frame_offset += SGX_AESGCM_MAC_SIZE;
  456. }
  457. uint8_t *nextframe = NULL;
  458. ocall_chunk(&nextframe, node_num, frame, frame_offset);
  459. frame = nextframe;
  460. msg_frame_offset += frame_offset;
  461. frame_offset = 0;
  462. msg_plaintext_chunk_remain =
  463. msg_plaintext_size - msg_plaintext_processed;
  464. if (msg_plaintext_chunk_remain >
  465. FRAME_SIZE - SGX_AESGCM_MAC_SIZE) {
  466. msg_plaintext_chunk_remain =
  467. FRAME_SIZE - SGX_AESGCM_MAC_SIZE;
  468. }
  469. if (encrypt) {
  470. sgx_aes_gcm_close(out_aes_gcm_state);
  471. if (msg_plaintext_chunk_remain > 0) {
  472. *(size_t*)out_aes_iv += 1;
  473. sgx_aes_gcm128_enc_init(out_aes_key, out_aes_iv,
  474. SGX_AESGCM_IV_SIZE, NULL, 0, &out_aes_gcm_state);
  475. }
  476. }
  477. }
  478. }
  479. }
  480. // The communication states for all the nodes. There's an entry for
  481. // ourselves in here, but it is unused.
  482. static std::vector<NodeCommState> commstates;
  483. static nodenum_t tot_nodes, my_node_num;
  484. // Generate a new identity signature key. Output the public key and the
  485. // sealed private key. outsealedpriv must point to SEALEDPRIVKEY_SIZE =
  486. // sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 18 bytes of
  487. // memory.
  488. void ecall_identity_key_new(sgx_ec256_public_t *outpub,
  489. sgx_sealed_data_t *outsealedpriv)
  490. {
  491. sgx_ecc_state_handle_t ecc_handle;
  492. sgx_ecc256_open_context(&ecc_handle);
  493. sgx_ecc256_create_key_pair(&g_privkey, &g_pubkey, ecc_handle);
  494. memmove(outpub, &g_pubkey, sizeof(g_pubkey));
  495. sgx_ecc256_close_context(ecc_handle);
  496. sgx_seal_data(18, (const uint8_t*)"TEEMS Identity key",
  497. sizeof(g_privkey), (const uint8_t*)&g_privkey,
  498. SEALED_PRIVKEY_SIZE, outsealedpriv);
  499. }
  500. // Load an identity key from a sealed privkey. Output the resulting
  501. // public key. insealedpriv must point to sizeof(sgx_sealed_data_t) +
  502. // sizeof(sgx_ec256_private_t) bytes of memory. Returns true for
  503. // success, false for failure.
  504. bool ecall_identity_key_load(sgx_ec256_public_t *outpub,
  505. const sgx_sealed_data_t *insealedpriv)
  506. {
  507. sgx_ecc_state_handle_t ecc_handle;
  508. char aad[18];
  509. uint32_t aadsize = sizeof(aad);
  510. sgx_ec256_private_t privkey;
  511. uint32_t privkeysize = sizeof(privkey);
  512. sgx_status_t res = sgx_unseal_data(
  513. insealedpriv, (uint8_t*)aad, &aadsize,
  514. (uint8_t*)&privkey, &privkeysize);
  515. if (res || aadsize != sizeof(aad) || privkeysize != sizeof(privkey)
  516. || memcmp(aad, "TEEMS Identity key", sizeof(aad))) {
  517. return false;
  518. }
  519. sgx_ecc256_open_context(&ecc_handle);
  520. sgx_ec256_public_t pubkey;
  521. int valid;
  522. if (sgx_ecc256_calculate_pub_from_priv(&privkey, &pubkey) ||
  523. sgx_ecc256_check_point(&pubkey, ecc_handle, &valid) ||
  524. !valid) {
  525. sgx_ecc256_close_context(ecc_handle);
  526. return false;
  527. }
  528. sgx_ecc256_close_context(ecc_handle);
  529. memmove(&g_pubkey, &pubkey, sizeof(pubkey));
  530. memmove(&g_privkey, &privkey, sizeof(privkey));
  531. memmove(outpub, &pubkey, sizeof(pubkey));
  532. return true;
  533. }
  534. bool comms_init_nodestate(const EnclaveAPINodeConfig *apinodeconfigs,
  535. nodenum_t num_nodes, nodenum_t me)
  536. {
  537. sgx_ecc_state_handle_t ecc_handle;
  538. sgx_ecc256_open_context(&ecc_handle);
  539. commstates.clear();
  540. tot_nodes = 0;
  541. commstates.reserve(num_nodes);
  542. for (nodenum_t i=0; i<num_nodes; ++i) {
  543. // Check that the pubkey is valid
  544. int valid;
  545. if (sgx_ecc256_check_point(&apinodeconfigs[i].pubkey,
  546. ecc_handle, &valid) ||
  547. !valid) {
  548. printf("Pubkey for node %hu invalid\n", i);
  549. commstates.clear();
  550. sgx_ecc256_close_context(ecc_handle);
  551. return false;
  552. }
  553. commstates.emplace_back(&apinodeconfigs[i].pubkey, i);
  554. }
  555. sgx_ecc256_close_context(ecc_handle);
  556. my_node_num = me;
  557. // Check that no one other than us has our pubkey (deals with
  558. // reflection attacks)
  559. for (nodenum_t i=0; i<num_nodes; ++i) {
  560. if (i == my_node_num) continue;
  561. if (!memcmp(&commstates[i].pubkey,
  562. &commstates[my_node_num].pubkey,
  563. sizeof(commstates[i].pubkey))) {
  564. printf("Pubkey %hu matches our own; possible reflection attack?\n",
  565. i);
  566. commstates.clear();
  567. return false;
  568. }
  569. }
  570. tot_nodes = num_nodes;
  571. // There will be an enclave-to-enclave channel between us and each
  572. // other node's enclave. For the node numbers smaller than ours, we
  573. // will be the server for the handshake for that channel. Prepare
  574. // to receive the first handshake message from those nodes'
  575. // enclaves.
  576. for (nodenum_t i=0; i<my_node_num; ++i) {
  577. commstates[i].in_msg_get_buf = default_in_msg_get_buf;
  578. commstates[i].in_msg_received = handshake_1_msg_received;
  579. }
  580. return true;
  581. }
  582. bool ecall_message(nodenum_t node_num, uint32_t message_len)
  583. {
  584. if (node_num >= tot_nodes) {
  585. printf("Out-of-range node_num %hu received in ecall_message\n",
  586. node_num);
  587. return false;
  588. }
  589. NodeCommState &nodest = commstates[node_num];
  590. if (nodest.in_msg_size != nodest.in_msg_offset) {
  591. printf("Received ecall_message without completing previous message\n");
  592. return false;
  593. }
  594. if (!nodest.in_msg_get_buf) {
  595. printf("No message header handler registered\n");
  596. return false;
  597. }
  598. uint8_t *buf = nodest.in_msg_get_buf(nodest, message_len);
  599. if (!buf) {
  600. printf("Message header handler returned NULL\n");
  601. return false;
  602. }
  603. nodest.in_msg_size = message_len;
  604. nodest.in_msg_offset = 0;
  605. nodest.in_msg_plaintext_processed = 0;
  606. nodest.in_msg_buf = buf;
  607. return true;
  608. }
  609. bool ecall_chunk(nodenum_t node_num, const uint8_t *chunkdata,
  610. uint32_t chunklen)
  611. {
  612. if (node_num >= tot_nodes) {
  613. printf("Out-of-range node_num %hu received in ecall_chunk\n",
  614. node_num);
  615. return false;
  616. }
  617. NodeCommState &nodest = commstates[node_num];
  618. if (nodest.in_msg_size == nodest.in_msg_offset) {
  619. printf("Received ecall_chunk after completing message\n");
  620. return false;
  621. }
  622. if (!nodest.in_msg_buf) {
  623. printf("No incoming message buffer allocated\n");
  624. return false;
  625. }
  626. if (!nodest.in_msg_received) {
  627. printf("No message received handler registered\n");
  628. return false;
  629. }
  630. if (nodest.in_msg_offset + chunklen > nodest.in_msg_size) {
  631. printf("Chunk larger than remaining message size\n");
  632. return false;
  633. }
  634. if (nodest.handshake_step == HANDSHAKE_COMPLETE) {
  635. // Decrypt the incoming data
  636. *(size_t*)(nodest.in_aes_iv) += 1;
  637. if (sgx_rijndael128GCM_decrypt(&nodest.in_aes_key, chunkdata,
  638. chunklen - SGX_AESGCM_MAC_SIZE,
  639. nodest.in_msg_buf + nodest.in_msg_plaintext_processed,
  640. nodest.in_aes_iv, SGX_AESGCM_IV_SIZE, NULL, 0,
  641. (const sgx_aes_gcm_128bit_tag_t *)
  642. (chunkdata + chunklen - SGX_AESGCM_MAC_SIZE))) {
  643. printf("Decryption failed\n");
  644. return false;
  645. }
  646. nodest.in_msg_plaintext_processed +=
  647. chunklen - SGX_AESGCM_MAC_SIZE;
  648. } else {
  649. // Just copy the handshake data
  650. memmove(nodest.in_msg_buf + nodest.in_msg_plaintext_processed,
  651. chunkdata, chunklen);
  652. nodest.in_msg_plaintext_processed += chunklen;
  653. }
  654. nodest.in_msg_offset += chunklen;
  655. if (nodest.in_msg_offset == nodest.in_msg_size) {
  656. // This was the last chunk; handle the received message
  657. uint8_t* buf = nodest.in_msg_buf;
  658. uint32_t plaintext_processed = nodest.in_msg_plaintext_processed;
  659. uint32_t msg_size = nodest.in_msg_size;
  660. nodest.in_msg_buf = NULL;
  661. nodest.in_msg_size = 0;
  662. nodest.in_msg_offset = 0;
  663. nodest.in_msg_plaintext_processed = 0;
  664. nodest.in_msg_received(nodest, buf, plaintext_processed, msg_size);
  665. }
  666. return true;
  667. }
  668. // Start the handshake (as the client)
  669. void NodeCommState::handshake_start()
  670. {
  671. sgx_ecc_state_handle_t ecc_handle;
  672. sgx_ecc256_open_context(&ecc_handle);
  673. // Create a DH keypair
  674. sgx_ecc256_create_key_pair(&handshake_dh_privkey, &handshake_dh_pubkey,
  675. ecc_handle);
  676. sgx_ecc256_close_context(ecc_handle);
  677. // Get us ready to receive handshake message 2
  678. in_msg_get_buf = default_in_msg_get_buf;
  679. in_msg_received = handshake_2_msg_received;
  680. handshake_step = HANDSHAKE_C_SENT_1;
  681. // Send the public key as the first message
  682. message_start(sizeof(handshake_dh_pubkey), false);
  683. message_data((uint8_t*)&handshake_dh_pubkey,
  684. sizeof(handshake_dh_pubkey), false);
  685. }
  686. // Start all handshakes for which we are the client
  687. bool ecall_comms_start()
  688. {
  689. for (nodenum_t t = my_node_num+1; t<tot_nodes; ++t) {
  690. commstates[t].handshake_start();
  691. }
  692. return true;
  693. }