[Assignment-7] seal_key_pair: removed dynamic memory allocations; fixed pointer usage

This commit is contained in:
Sascha Tommasone 2024-07-02 23:11:26 +02:00 committed by saschato
parent 83ad706ad7
commit 97dfb2b82e

View file

@ -69,40 +69,27 @@ int get_private_key_size() {
return SK_SIZE; return SK_SIZE;
} }
sgx_status_t seal_key_pair(sgx_ec256_private_t *private, sgx_ec256_public_t *public, uint8_t *sealed, uint32_t *sealed_size) { sgx_status_t seal_key_pair(sgx_ec256_private_t *private, sgx_ec256_public_t *public, uint8_t **sealed, uint32_t sealed_size) {
// handle missing keypair // invalid parameter handling
if((private == NULL) || (public == NULL)) if((private == NULL) || (public == NULL))
return SGX_ERROR_UNEXPECTED; return SGX_ERROR_INVALID_PARAMETER;
// allocate temporary buffers on stack // allocate temporary buffers on stack
uint8_t pk[2*SGX_ECP256_KEY_SIZE] = {0}; uint8_t pk[PK_SIZE] = {0};
uint8_t sk[SGX_ECP256_KEY_SIZE] = {0}; uint8_t sk[SK_SIZE] = {0};
// copy key pair into buffers // copy key pair into buffers
memcpy(pk, public->gx, SGX_ECP256_KEY_SIZE); memcpy(pk, public->gx, PK_SIZE);
memcpy(pk + SGX_ECP256_KEY_SIZE, public->gy, SGX_ECP256_KEY_SIZE); memcpy(sk, private->r, SK_SIZE);
memcpy(sk, private->r, SGX_ECP256_KEY_SIZE);
// calculate needed size // calculate needed size
*sealed_size = sgx_calc_sealed_data_size((uint32_t)(2*SGX_ECP256_KEY_SIZE), (uint32_t)SGX_ECP256_KEY_SIZE); uint32_t size = get_sealed_size();
if(*sealed_size == UINT32_MAX) if(size > sealed_size) {
return SGX_ERROR_UNEXPECTED; return SGX_ERROR_INVALID_PARAMETER;
// allocate buffer on heap
sealed = (uint8_t *)malloc(*sealed_size);
if(sealed == NULL) {
free(sealed);
return SGX_ERROR_OUT_OF_MEMORY;
} }
// seal keypair // seal keypair
sgx_status_t status = sgx_seal_data((uint32_t)2*SGX_ECP256_KEY_SIZE, (const uint8_t *)pk, (uint32_t)SGX_ECP256_KEY_SIZE, (uint8_t *)sk, *sealed_size, (sgx_sealed_data_t *) sealed); return sgx_seal_data(PK_SIZE, (const uint8_t *)pk, SK_SIZE, (const uint8_t *)sk, size, (sgx_sealed_data_t *) *sealed);
if(status != SGX_SUCCESS) {
free(sealed);
return SGX_ERROR_UNEXPECTED;
}
return SGX_SUCCESS;
} }
sgx_status_t unseal_key_pair(uint8_t *sealed, uint32_t *sealed_size, sgx_ec256_private_t *private, sgx_ec256_public_t *public) { sgx_status_t unseal_key_pair(uint8_t *sealed, uint32_t *sealed_size, sgx_ec256_private_t *private, sgx_ec256_public_t *public) {