comms.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <cstring>
  2. #include "sgx_tcrypto.h"
  3. #include "sgx_tseal.h"
  4. #include "Enclave_t.h"
  5. #include "utils.hpp"
  6. // Our public and private identity keys
  7. sgx_ec256_private_t g_privkey;
  8. sgx_ec256_public_t g_pubkey;
  9. // Generate a new identity signature key. Output the public key and the
  10. // sealed private key. outsealedpriv must point to
  11. // sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19 bytes of
  12. // memory.
  13. void ecall_identity_key_new(sgx_ec256_public_t *outpub,
  14. sgx_sealed_data_t *outsealedpriv)
  15. {
  16. sgx_ecc_state_handle_t ecc_handle;
  17. sgx_ecc256_open_context(&ecc_handle);
  18. sgx_ecc256_create_key_pair(&g_privkey, &g_pubkey, ecc_handle);
  19. memmove(outpub, &g_pubkey, sizeof(g_pubkey));
  20. sgx_ecc256_close_context(ecc_handle);
  21. sgx_seal_data(19, (const uint8_t*)"TEEMS Identity key",
  22. sizeof(g_privkey), (const uint8_t*)&g_privkey,
  23. sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19,
  24. outsealedpriv);
  25. }
  26. // Load an identity key from a sealed privkey. Output the resulting
  27. // public key. insealedpriv must point to sizeof(sgx_sealed_data_t) +
  28. // sizeof(sgx_ec256_private_t) bytes of memory. Returns true for
  29. // success, false for failure.
  30. bool ecall_identity_key_load(sgx_ec256_public_t *outpub,
  31. const sgx_sealed_data_t *insealedpriv)
  32. {
  33. sgx_ecc_state_handle_t ecc_handle;
  34. char aad[19];
  35. uint32_t aadsize = sizeof(aad);
  36. sgx_ec256_private_t privkey;
  37. uint32_t privkeysize = sizeof(privkey);
  38. sgx_status_t res = sgx_unseal_data(
  39. insealedpriv, (uint8_t*)aad, &aadsize,
  40. (uint8_t*)&privkey, &privkeysize);
  41. if (res || aadsize != sizeof(aad) || privkeysize != sizeof(privkey)
  42. || memcmp(aad, "TEEMS Identity key", sizeof(aad))) {
  43. return false;
  44. }
  45. sgx_ecc256_open_context(&ecc_handle);
  46. sgx_ec256_public_t pubkey;
  47. int valid;
  48. if (sgx_ecc256_calculate_pub_from_priv(&privkey, &pubkey) ||
  49. sgx_ecc256_check_point(&pubkey, ecc_handle, &valid) ||
  50. !valid) {
  51. sgx_ecc256_close_context(ecc_handle);
  52. return false;
  53. }
  54. sgx_ecc256_close_context(ecc_handle);
  55. memmove(&g_pubkey, &pubkey, sizeof(pubkey));
  56. memmove(&g_privkey, &privkey, sizeof(privkey));
  57. memmove(outpub, &pubkey, sizeof(pubkey));
  58. return true;
  59. }