0

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

  • `out_data` is enclave memory, you need to pass a non-enclave buffer that you then copy `out_data` into after you finish. The enclave was never intended to be *fast* just *secure* – Mgetz Mar 03 '22 at 19:30
  • 1
    Also note that SGX is removed in newer processors due to security vulns. – Mgetz Mar 03 '22 at 19:37
  • Passing a non-enclave Buffer in order to memcpy the information from out_data to it blocks the enclave and stops the program from continuing. Is there something wrong in my thinking? – mute_police Mar 04 '22 at 10:55
  • My suggestion came literally from the Intel samples. However I question the value of SGX at all given it is removed in newer silicon – Mgetz Mar 04 '22 at 12:20
  • It was the previously given decision by the board for my Masters Dissertation, I do question it a bit now that I am aware Intel no longer supports it for private Pcs, however, due to my project having the objective of running in multiple servers, since Xeon processors still support it, it was the decision for it not to be scrapped – mute_police Mar 04 '22 at 15:09

0 Answers0