123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- #include <assert.h>
- #include "isv_enclave_t.h"
- #include "sgx_tkey_exchange.h"
- #include "sgx_tcrypto.h"
- #include "string.h"
- static const sgx_ec256_public_t g_sp_pub_key = {
- {
- 0x72, 0x12, 0x8a, 0x7a, 0x17, 0x52, 0x6e, 0xbf,
- 0x85, 0xd0, 0x3a, 0x62, 0x37, 0x30, 0xae, 0xad,
- 0x3e, 0x3d, 0xaa, 0xee, 0x9c, 0x60, 0x73, 0x1d,
- 0xb0, 0x5b, 0xe8, 0x62, 0x1c, 0x4b, 0xeb, 0x38
- },
- {
- 0xd4, 0x81, 0x40, 0xd9, 0x50, 0xe2, 0x57, 0x7b,
- 0x26, 0xee, 0xb7, 0x41, 0xe7, 0xc6, 0x14, 0xe2,
- 0x24, 0xb7, 0xbd, 0xc9, 0x03, 0xf2, 0x9a, 0x28,
- 0xa8, 0x3c, 0xc8, 0x10, 0x11, 0x14, 0x5e, 0x06
- }
- };
- uint8_t g_secret[8] = {0};
- #ifdef SUPPLIED_KEY_DERIVATION
- #pragma message ("Supplied key derivation function is used.")
- typedef struct _hash_buffer_t
- {
- uint8_t counter[4];
- sgx_ec256_dh_shared_t shared_secret;
- uint8_t algorithm_id[4];
- } hash_buffer_t;
- const char ID_U[] = "SGXRAENCLAVE";
- const char ID_V[] = "SGXRASERVER";
- bool derive_key(
- const sgx_ec256_dh_shared_t *p_shared_key,
- uint8_t key_id,
- sgx_ec_key_128bit_t *first_derived_key,
- sgx_ec_key_128bit_t *second_derived_key)
- {
- sgx_status_t sgx_ret = SGX_SUCCESS;
- hash_buffer_t hash_buffer;
- sgx_sha_state_handle_t sha_context;
- sgx_sha256_hash_t key_material;
- memset(&hash_buffer, 0, sizeof(hash_buffer_t));
-
- hash_buffer.counter[3] = key_id;
-
- for (size_t i = 0; i < sizeof(sgx_ec256_dh_shared_t); i++)
- {
- hash_buffer.shared_secret.s[i] = p_shared_key->s[sizeof(p_shared_key->s)-1 - i];
- }
- sgx_ret = sgx_sha256_init(&sha_context);
- if (sgx_ret != SGX_SUCCESS)
- {
- return false;
- }
- sgx_ret = sgx_sha256_update((uint8_t*)&hash_buffer, sizeof(hash_buffer_t), sha_context);
- if (sgx_ret != SGX_SUCCESS)
- {
- sgx_sha256_close(sha_context);
- return false;
- }
- sgx_ret = sgx_sha256_update((uint8_t*)&ID_U, sizeof(ID_U), sha_context);
- if (sgx_ret != SGX_SUCCESS)
- {
- sgx_sha256_close(sha_context);
- return false;
- }
- sgx_ret = sgx_sha256_update((uint8_t*)&ID_V, sizeof(ID_V), sha_context);
- if (sgx_ret != SGX_SUCCESS)
- {
- sgx_sha256_close(sha_context);
- return false;
- }
- sgx_ret = sgx_sha256_get_hash(sha_context, &key_material);
- if (sgx_ret != SGX_SUCCESS)
- {
- sgx_sha256_close(sha_context);
- return false;
- }
- sgx_ret = sgx_sha256_close(sha_context);
- assert(sizeof(sgx_ec_key_128bit_t)* 2 == sizeof(sgx_sha256_hash_t));
- memcpy(first_derived_key, &key_material, sizeof(sgx_ec_key_128bit_t));
- memcpy(second_derived_key, (uint8_t*)&key_material + sizeof(sgx_ec_key_128bit_t), sizeof(sgx_ec_key_128bit_t));
-
-
- memset(&key_material, 0, sizeof(sgx_sha256_hash_t));
- return true;
- }
- #define ISV_KDF_ID 2
- typedef enum _derive_key_type_t
- {
- DERIVE_KEY_SMK_SK = 0,
- DERIVE_KEY_MK_VK,
- } derive_key_type_t;
- sgx_status_t key_derivation(const sgx_ec256_dh_shared_t* shared_key,
- uint16_t kdf_id,
- sgx_ec_key_128bit_t* smk_key,
- sgx_ec_key_128bit_t* sk_key,
- sgx_ec_key_128bit_t* mk_key,
- sgx_ec_key_128bit_t* vk_key)
- {
- bool derive_ret = false;
- if (NULL == shared_key)
- {
- return SGX_ERROR_INVALID_PARAMETER;
- }
- if (ISV_KDF_ID != kdf_id)
- {
-
- return SGX_ERROR_KDF_MISMATCH;
- }
- derive_ret = derive_key(shared_key, DERIVE_KEY_SMK_SK,
- smk_key, sk_key);
- if (derive_ret != true)
- {
-
- return SGX_ERROR_UNEXPECTED;
- }
- derive_ret = derive_key(shared_key, DERIVE_KEY_MK_VK,
- mk_key, vk_key);
- if (derive_ret != true)
- {
-
- return SGX_ERROR_UNEXPECTED;
- }
- return SGX_SUCCESS;
- }
- #else
- #pragma message ("Default key derivation function is used.")
- #endif
- sgx_status_t enclave_init_ra(
- int b_pse,
- sgx_ra_context_t *p_context)
- {
-
- sgx_status_t ret;
- if(b_pse)
- {
- int busy_retry_times = 2;
- do{
- ret = sgx_create_pse_session();
- }while (ret == SGX_ERROR_BUSY && busy_retry_times--);
- if (ret != SGX_SUCCESS)
- return ret;
- }
- #ifdef SUPPLIED_KEY_DERIVATION
- ret = sgx_ra_init_ex(&g_sp_pub_key, b_pse, key_derivation, p_context);
- #else
- ret = sgx_ra_init(&g_sp_pub_key, b_pse, p_context);
- #endif
- if(b_pse)
- {
- sgx_close_pse_session();
- return ret;
- }
- return ret;
- }
- sgx_status_t SGXAPI enclave_ra_close(
- sgx_ra_context_t context)
- {
- sgx_status_t ret;
- ret = sgx_ra_close(context);
- return ret;
- }
- sgx_status_t verify_att_result_mac(sgx_ra_context_t context,
- uint8_t* p_message,
- size_t message_size,
- uint8_t* p_mac,
- size_t mac_size)
- {
- sgx_status_t ret;
- sgx_ec_key_128bit_t mk_key;
- if(mac_size != sizeof(sgx_mac_t))
- {
- ret = SGX_ERROR_INVALID_PARAMETER;
- return ret;
- }
- if(message_size > UINT32_MAX)
- {
- ret = SGX_ERROR_INVALID_PARAMETER;
- return ret;
- }
- do {
- uint8_t mac[SGX_CMAC_MAC_SIZE] = {0};
- ret = sgx_ra_get_keys(context, SGX_RA_KEY_MK, &mk_key);
- if(SGX_SUCCESS != ret)
- {
- break;
- }
- ret = sgx_rijndael128_cmac_msg(&mk_key,
- p_message,
- (uint32_t)message_size,
- &mac);
- if(SGX_SUCCESS != ret)
- {
- break;
- }
- if(0 == consttime_memequal(p_mac, mac, sizeof(mac)))
- {
- ret = SGX_ERROR_MAC_MISMATCH;
- break;
- }
- }
- while(0);
- return ret;
- }
- sgx_status_t put_secret_data(
- sgx_ra_context_t context,
- uint8_t *p_secret,
- uint32_t secret_size,
- uint8_t *p_gcm_mac)
- {
- sgx_status_t ret = SGX_SUCCESS;
- sgx_ec_key_128bit_t sk_key;
- do {
- if(secret_size != 8)
- {
- ret = SGX_ERROR_INVALID_PARAMETER;
- break;
- }
- ret = sgx_ra_get_keys(context, SGX_RA_KEY_SK, &sk_key);
- if(SGX_SUCCESS != ret)
- {
- break;
- }
- uint8_t aes_gcm_iv[12] = {0};
- ret = sgx_rijndael128GCM_decrypt(&sk_key,
- p_secret,
- secret_size,
- &g_secret[0],
- &aes_gcm_iv[0],
- 12,
- NULL,
- 0,
- (const sgx_aes_gcm_128bit_tag_t *)
- (p_gcm_mac));
- uint32_t i;
- bool secret_match = true;
- for(i=0;i<secret_size;i++)
- {
- if(g_secret[i] != i)
- {
- secret_match = false;
- }
- }
- if(!secret_match)
- {
- ret = SGX_ERROR_UNEXPECTED;
- }
-
-
-
-
- } while(0);
- return ret;
- }
|