I am developing a simple C API to be called from Java which connects to an Intel SGX Enclave for security Primitives. I was able to create an RSA-Key Pair (I Think) successfully. However, now after generating a 128 bit AES key using sgx_read_rand(), I am not able to encrypt it using the Public Key I have generated.
Enclave Code:
sgx_status_t get_symmetric_key(sgx_rsa3072_public_key_t* other_public_key, uint8_t *aes_key){
//Allocate memory for the AES key to be used.
//Populate the key.
//Only works if the aes_key pointer is allocated outside of the enclave.
sgx_status_t ret = sgx_read_rand((unsigned char *)aes_key,SGX_AESGCM_KEY_SIZE);
//Populate the actual Public key.
void *other_rsa;
size_t encryption_size;
ret = sgx_create_rsa_pub1_key(RSA_KEY_SIZE,RSA_EXPONENT_SIZE,(const unsigned char*) other_public_key->mod
, (const unsigned char *) other_public_key -> exp, &other_rsa);
ret = sgx_rsa_pub_encrypt_sha256(other_rsa,NULL,&encryption_size,(const unsigned char *) aes_key,SGX_AESGCM_KEY_SIZE);
uint8_t *out_data = (uint8_t*) malloc(encryption_size);
ret = sgx_rsa_pub_encrypt_sha256(other_rsa,out_data,&encryption_size, (const unsigned char*) aes_key,SGX_AESGCM_KEY_SIZE);
if(ret != SGX_SUCCESS)
return ret;
ocall_return_symmetric(out_data,encryption_size);
memset(aes_key,0,SGX_AESGCM_KEY_SIZE); //Does not Happen.
return SGX_SUCCESS;
}
Function declaration in the .edl file:
public sgx_status_t get_symmetric_key([in]sgx_rsa3072_public_key_t* other_public_key,[in]uint8_t *aes_key);
What I have discovered is that the aes_key pointer needs to be allocated outside of the enclave in order for it to be populated with random bytes (Otherwise it will be a 16-byte pointer with zeros only.)
After using the pub_encrypt function in order to create an encrypted version of the AES key, I receive a full buffer of zeroes, and I am unsure as to why. I have discovered that trying to use memcpy() to copy from trusted to untrusted memory will block the enclave, meaning that an OCALL needs to be used in order to fully copy the information. Any tips on this? What should I do in order to actually encrypt this correctly?
Use two different Ecalls? (one for Encryption size and another for the actual encryption? Do I need to allocate the memory fully outside of the enclave? Does this not stand as a huge security breach?)
As an additional note, why doesnt memset work inside the enclave? Returning the fully populated AES pointer back to untrusted memory seems like an issue.
Any help is appreciated, Thanks for your time