Intel® Software Guard Extensions (Intel® SGX)
Discussion board focused on hardware-based isolation and memory encryption to provide extended code protection in solutions.

sgx_rsa_pub_encrypt_sha256 in Enclave

qianbao
Beginner
317 Views

I want use sgx_create_rsa_key_pair() in sgx_tcrypto and I have found some examples of creating RSA keys and encrypt my data,but I still have some problem. Maybe the example code I found already unused.

 

my code:

#include "sgx_tcrypto.h"
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/core_names.h>
#include <openssl/param_build.h>

 

void ecall_test_rsa() {
void *public_key_rsa = NULL;
void *private_key_rsa = NULL;
const int n_byte_size = 384;
long e = 65537;

unsigned char p_n[n_byte_size] = {0};
unsigned char p_d[n_byte_size] = {0};
unsigned char p_p[n_byte_size] = {0};
unsigned char p_q[n_byte_size] = {0};
unsigned char p_dmp1[n_byte_size] = {0};
unsigned char p_dmq1[n_byte_size] = {0};
unsigned char p_iqmp[n_byte_size] = {0};

sgx_status_t ret_create_key_params = sgx_create_rsa_key_pair(n_byte_size, sizeof(e), p_n, p_d, (unsigned char*)&e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp);
if (ret_create_key_params != SGX_SUCCESS) {
printf("Error creting keys (%d)\n", ret_create_key_params);
return;
}

sgx_status_t ret_create_private_key = sgx_create_rsa_priv2_key(n_byte_size, sizeof(e), (unsigned char*)&e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp, &private_key_rsa);

if ( ret_create_private_key != SGX_SUCCESS) {
printf("Private key generation failed (%d)\n", ret_create_private_key);

}

sgx_status_t ret_create_public_key = sgx_create_rsa_pub1_key(n_byte_size, sizeof(e), p_n, (unsigned char*)&e, &public_key_rsa);

if ( ret_create_public_key != SGX_SUCCESS) {
printf("Public key generation failed (%d)\n", ret_create_public_key);

}

printf("public_key_rsa:%s\n", (char *)public_key_rsa);
printf("private_key_rsa:%s\n", (char *)private_key_rsa);

const char * pin_data = "Hello World!";
size_t out_len = 256;
unsigned char * pout_data = NULL;

sgx_status_t ret_encrypt = sgx_rsa_pub_encrypt_sha256(public_key_rsa, pout_data, &out_len, (unsigned char *)pin_data, strlen(pin_data));
if ( ret_encrypt != SGX_SUCCESS) {
printf("Encryption failed\n");
printf("ret_encrypt = %d\n", ret_encrypt);
} else {
printf("Encryption success\n");
printf("ret_encrypt = %d\n", ret_encrypt);
printf("out_len = %d\n", out_len);
printf("pout_data: %s\n", pout_data);
}


size_t decrypted_pout_data_len;
unsigned char decrypted_pout_data[n_byte_size];

sgx_status_t ret_decrypt = sgx_rsa_priv_decrypt_sha256(private_key_rsa, decrypted_pout_data, &decrypted_pout_data_len, (unsigned char *)pout_data, out_len);

if ( ret_decrypt != SGX_SUCCESS) {
printf("Decryption failed\n");
printf("ret_decrypt = %d\n", ret_decrypt);
} else {
printf("Decrypted MESSAGE:\n");
printf("len = %d, msg = %s\n", decrypted_pout_data_len, decrypted_pout_data);
}
}

 

 

output is:

(base) baoqian@XEON8368:~/workspace/rsatest/ppmlac-llm/src/lib/sgx$ ./test-rsa
public_key_rsa:P�R
private_key_rsa:R�R
Encryption success
ret_encrypt = 0
out_len = 384
pout_data: (null)
Decryption failed
ret_decrypt = 2

 

it seems sgx_rsa_pub_encrypt_sha256 and sgx_rsa_priv_decrypt_sha256 all failed, pout_data is null, plese give some advice

0 Kudos
0 Replies
Reply