Updates for SGX 2.17 reproducible build.

Signed-off-by: Zhang Lili <lili.z.zhang@intel.com>
This commit is contained in:
Zhang Lili
2022-06-15 11:49:48 +08:00
parent 0f61c27a40
commit 1deb73848a
119 changed files with 2122 additions and 880 deletions
+103 -82
View File
@@ -28,8 +28,8 @@
//
// Generate_Key_Pair function:
// type1: RSA
// type2: EC-P384
// type1: RSA
// type2: EC-P384
// currently all hardware independant
//
#include <openssl/bio.h>
@@ -45,26 +45,29 @@
int get_pkey_by_rsa(EVP_PKEY *pk)
{
int res = -1;
RSA* rsa = nullptr;
int res = -1;
RSA* rsa = nullptr;
BIGNUM* e = nullptr;
e = BN_new();
if (!e)
e = BN_new();
if (!e) {
t_print("BN_new failed\n");
return res;
}
res = BN_set_word(e, (BN_ULONG)RSA_F4);
if (!res) {
if (!res) {
t_print("BN_set_word failed (%d)\n", res);
return res;
}
}
rsa = RSA_new();
if (!rsa) {
t_print("RSA_new failed\n");
res = -1;
return res;
}
}
res = RSA_generate_key_ex(
rsa,
@@ -73,16 +76,16 @@ int get_pkey_by_rsa(EVP_PKEY *pk)
nullptr /* callback argument - not needed in this case */
);
if (!res)
{
if (!res)
{
t_print("RSA_generate_key failed (%d)\n", res);
return res;
}
// Assign RSA key to EVP_PKEY structure
}
// Assign RSA key to EVP_PKEY structure
EVP_PKEY_assign_RSA(pk, rsa);
return res;
return res;
}
int get_pkey_by_ec(EVP_PKEY *pk)
@@ -93,15 +96,29 @@ int get_pkey_by_ec(EVP_PKEY *pk)
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
if (ctx == NULL)
return res;
if (EVP_PKEY_keygen_init(ctx) <= 0)
res = EVP_PKEY_keygen_init(ctx);
if (res <= 0)
{
t_print("EC_generate_key failed (%d)\n", res);
return res;
if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_secp384r1) <= 0)
}
res = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_secp384r1);
if (res <= 0)
{
t_print("EC_generate_key failed (%d)\n", res);
return res;
}
/* Generate key */
if (EVP_PKEY_keygen(ctx, &pk) <= 0)
res = EVP_PKEY_keygen(ctx, &pk);
if (res <= 0)
{
t_print("EC_generate_key failed (%d)\n", res);
return res;
return 0;
}
return res;
}
@@ -121,110 +138,112 @@ sgx_status_t generate_key_pair(
EVP_PKEY* pkey = nullptr;
BIO* bio = nullptr;
pkey = EVP_PKEY_new();
pkey = EVP_PKEY_new();
if (!pkey)
{
t_print("EVP_PKEY_new failed\n");
result = SGX_ERROR_UNEXPECTED;
goto done;
}
if (type != RSA_TYPE || type != EC_TYPE)
if (type != RSA_TYPE && type != EC_TYPE)
{
type = RSA_TYPE; // by default, we use RSA_TYPE
}
switch(type)
{
case RSA_TYPE:
res = get_pkey_by_rsa(pkey);
break;
case EC_TYPE:
res = get_pkey_by_ec(pkey);
break;
}
switch(type)
{
case RSA_TYPE:
res = get_pkey_by_rsa(pkey);
break;
case EC_TYPE:
res = get_pkey_by_ec(pkey);
break;
}
if (!res)
{
if (res <= 0)
{
t_print("get_pkey failed (%d)\n", res);
result = SGX_ERROR_UNEXPECTED;
goto done;
}
}
// Allocate memory
local_public_key = (uint8_t*)malloc(RSA_3072_PUBLIC_KEY_SIZE);
memset(local_public_key, 0x00, RSA_3072_PUBLIC_KEY_SIZE);
if (!local_public_key)
{
t_print("out-of-memory:calloc(local_public_key failed\n");
result = SGX_ERROR_UNEXPECTED;
goto done;
}
if (!local_public_key)
{
t_print("out-of-memory:calloc(local_public_key failed\n");
result = SGX_ERROR_OUT_OF_EPC;
goto done;
}
memset(local_public_key, 0x00, RSA_3072_PUBLIC_KEY_SIZE);
local_private_key = (uint8_t*)malloc(RSA_3072_PRIVATE_KEY_SIZE);
memset(local_private_key, 0x00, RSA_3072_PRIVATE_KEY_SIZE);
local_private_key = (uint8_t*)malloc(RSA_3072_PRIVATE_KEY_SIZE);
if (!local_private_key)
{
t_print("out-of-memory: calloc(local_private_key) failed\n");
result = SGX_ERROR_UNEXPECTED;
goto done;
}
{
t_print("out-of-memory: calloc(local_private_key) failed\n");
result = SGX_ERROR_OUT_OF_EPC;
goto done;
}
memset(local_private_key, 0x00, RSA_3072_PRIVATE_KEY_SIZE);
// Write out the public/private key in PEM format for exchange with
// other enclaves.
bio = BIO_new(BIO_s_mem());
if (!bio)
{
bio = BIO_new(BIO_s_mem());
if (!bio)
{
t_print("BIO_new for local_public_key failed\n");
goto done;
}
}
res = PEM_write_bio_PUBKEY(bio, pkey);
if (!res)
{
t_print("PEM_write_bio_PUBKEY failed (%d)\n", res);
goto done;
}
{
t_print("PEM_write_bio_PUBKEY failed (%d)\n", res);
goto done;
}
res = BIO_read(bio, local_public_key, RSA_3072_PUBLIC_KEY_SIZE);
if (!res)
{
t_print("BIO_read public key failed (%d)\n", res);
goto done;
}
{
t_print("BIO_read public key failed (%d)\n", res);
goto done;
}
BIO_free(bio);
bio = nullptr;
bio = BIO_new(BIO_s_mem());
if (!bio)
{
t_print("BIO_new for local_public_key failed\n");
goto done;
}
{
t_print("BIO_new for local_public_key failed\n");
goto done;
}
res = PEM_write_bio_PrivateKey(
bio, pkey, nullptr, nullptr, 0, nullptr, nullptr);
if (!res)
{
t_print("PEM_write_bio_PrivateKey failed (%d)\n", res);
goto done;
}
{
t_print("PEM_write_bio_PrivateKey failed (%d)\n", res);
goto done;
}
res = BIO_read(bio, local_private_key, RSA_3072_PRIVATE_KEY_SIZE);
if (!res)
{
t_print("BIO_read private key failed (%d)\n", res);
goto done;
}
{
t_print("BIO_read private key failed (%d)\n", res);
goto done;
}
BIO_free(bio);
BIO_free(bio);
bio = nullptr;
*public_key = local_public_key;
//*public_key_size = RSA_3072_PUBLIC_KEY_SIZE;
*private_key = local_private_key;
// *private_key_size = RSA_3072_PRIVATE_KEY_SIZE;
*public_key_size = strlen(reinterpret_cast<const char *>(local_public_key)) + 1;
*public_key_size = strlen(reinterpret_cast<const char *>(local_public_key)) + 1;
*private_key_size = strlen(reinterpret_cast<const char *>(local_private_key)) + 1;
t_print("public_key_size %d, private_key_size %d\n", *public_key_size, *private_key_size);
t_print("public_key_size %d, private_key_size %d\n", *public_key_size, *private_key_size);
result = SGX_SUCCESS;
done:
@@ -232,10 +251,12 @@ done:
BIO_free(bio);
if (pkey)
EVP_PKEY_free(pkey); // When this is called, rsa is also freed
if (result != SGX_SUCCESS)
if (result != SGX_SUCCESS)
{
free(local_public_key);
free(local_private_key);
}
if (local_public_key)
free(local_public_key);
if (local_private_key)
free(local_private_key);
}
return result;
}