1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <cstring>
- #include "sgx_tcrypto.h"
- #include "sgx_tseal.h"
- #include "Enclave_t.h"
- #include "utils.hpp"
- // Our public and private identity keys
- sgx_ec256_private_t g_privkey;
- sgx_ec256_public_t g_pubkey;
- // Generate a new identity signature key. Output the public key and the
- // sealed private key. outsealedpriv must point to
- // sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19 bytes of
- // memory.
- void ecall_identity_key_new(sgx_ec256_public_t *outpub,
- sgx_sealed_data_t *outsealedpriv)
- {
- sgx_ecc_state_handle_t ecc_handle;
- sgx_ecc256_open_context(&ecc_handle);
- sgx_ecc256_create_key_pair(&g_privkey, &g_pubkey, ecc_handle);
- memmove(outpub, &g_pubkey, sizeof(g_pubkey));
- sgx_ecc256_close_context(ecc_handle);
- sgx_seal_data(19, (const uint8_t*)"TEEMS Identity key",
- sizeof(g_privkey), (const uint8_t*)&g_privkey,
- sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19,
- outsealedpriv);
- }
- // Load an identity key from a sealed privkey. Output the resulting
- // public key. insealedpriv must point to sizeof(sgx_sealed_data_t) +
- // sizeof(sgx_ec256_private_t) bytes of memory. Returns true for
- // success, false for failure.
- bool ecall_identity_key_load(sgx_ec256_public_t *outpub,
- const sgx_sealed_data_t *insealedpriv)
- {
- sgx_ecc_state_handle_t ecc_handle;
- char aad[19];
- uint32_t aadsize = sizeof(aad);
- sgx_ec256_private_t privkey;
- uint32_t privkeysize = sizeof(privkey);
- sgx_status_t res = sgx_unseal_data(
- insealedpriv, (uint8_t*)aad, &aadsize,
- (uint8_t*)&privkey, &privkeysize);
- if (res || aadsize != sizeof(aad) || privkeysize != sizeof(privkey)
- || memcmp(aad, "TEEMS Identity key", sizeof(aad))) {
- return false;
- }
- sgx_ecc256_open_context(&ecc_handle);
- sgx_ec256_public_t pubkey;
- int valid;
- if (sgx_ecc256_calculate_pub_from_priv(&privkey, &pubkey) ||
- sgx_ecc256_check_point(&pubkey, ecc_handle, &valid) ||
- !valid) {
- sgx_ecc256_close_context(ecc_handle);
- return false;
- }
- sgx_ecc256_close_context(ecc_handle);
- memmove(&g_pubkey, &pubkey, sizeof(pubkey));
- memmove(&g_privkey, &privkey, sizeof(privkey));
- memmove(outpub, &pubkey, sizeof(pubkey));
- return true;
- }
|