[Assignment-7] seal_key_pair: removed dynamic memory allocations; fixed pointer usage
This commit is contained in:
parent
c38917a48d
commit
04c74e2dc2
1 changed files with 12 additions and 25 deletions
|
@ -69,40 +69,27 @@ int get_private_key_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) {
|
||||
// handle missing keypair
|
||||
sgx_status_t seal_key_pair(sgx_ec256_private_t *private, sgx_ec256_public_t *public, uint8_t **sealed, uint32_t sealed_size) {
|
||||
// invalid parameter handling
|
||||
if((private == NULL) || (public == NULL))
|
||||
return SGX_ERROR_UNEXPECTED;
|
||||
return SGX_ERROR_INVALID_PARAMETER;
|
||||
|
||||
// allocate temporary buffers on stack
|
||||
uint8_t pk[2*SGX_ECP256_KEY_SIZE] = {0};
|
||||
uint8_t sk[SGX_ECP256_KEY_SIZE] = {0};
|
||||
uint8_t pk[PK_SIZE] = {0};
|
||||
uint8_t sk[SK_SIZE] = {0};
|
||||
|
||||
// copy key pair into buffers
|
||||
memcpy(pk, public->gx, SGX_ECP256_KEY_SIZE);
|
||||
memcpy(pk + SGX_ECP256_KEY_SIZE, public->gy, SGX_ECP256_KEY_SIZE);
|
||||
memcpy(sk, private->r, SGX_ECP256_KEY_SIZE);
|
||||
memcpy(pk, public->gx, PK_SIZE);
|
||||
memcpy(sk, private->r, SK_SIZE);
|
||||
|
||||
// calculate needed size
|
||||
*sealed_size = sgx_calc_sealed_data_size((uint32_t)(2*SGX_ECP256_KEY_SIZE), (uint32_t)SGX_ECP256_KEY_SIZE);
|
||||
if(*sealed_size == UINT32_MAX)
|
||||
return SGX_ERROR_UNEXPECTED;
|
||||
|
||||
// allocate buffer on heap
|
||||
sealed = (uint8_t *)malloc(*sealed_size);
|
||||
if(sealed == NULL) {
|
||||
free(sealed);
|
||||
return SGX_ERROR_OUT_OF_MEMORY;
|
||||
uint32_t size = get_sealed_size();
|
||||
if(size > sealed_size) {
|
||||
return SGX_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
// 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);
|
||||
if(status != SGX_SUCCESS) {
|
||||
free(sealed);
|
||||
return SGX_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
return SGX_SUCCESS;
|
||||
return sgx_seal_data(PK_SIZE, (const uint8_t *)pk, SK_SIZE, (const uint8_t *)sk, size, (sgx_sealed_data_t *) *sealed);
|
||||
}
|
||||
|
||||
sgx_status_t unseal_key_pair(uint8_t *sealed, uint32_t *sealed_size, sgx_ec256_private_t *private, sgx_ec256_public_t *public) {
|
||||
|
|
Loading…
Reference in a new issue