<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re:Asymmetric cryptography in Enclave in Intel® Software Guard Extensions (Intel® SGX)</title>
    <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1198114#M3871</link>
    <description>&lt;P&gt;Hello daamgard,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You seem to be doing everything right and I cannot figure out why the output is only 8 bytes in length. I will confer with engineering and let you know what they find.&lt;/P&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 06 Aug 2020 21:31:12 GMT</pubDate>
    <dc:creator>JesusG_Intel</dc:creator>
    <dc:date>2020-08-06T21:31:12Z</dc:date>
    <item>
      <title>Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1196902#M3854</link>
      <description>&lt;P&gt;I have been trying to find a way to generate a public and private key inside an enclave to encrypt and decrypt incoming data.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I found&amp;nbsp;&lt;SPAN&gt;sgx_create_rsa_key_pair() in sgx_tcrypto, though i am having a hard time getting it working.&lt;BR /&gt;Do you have any examples of creating RSA keys or should i be doing this in another way?&lt;BR /&gt;&lt;BR /&gt;Best Regards,&lt;BR /&gt;Mads&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Aug 2020 12:40:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1196902#M3854</guid>
      <dc:creator>damgaard22</dc:creator>
      <dc:date>2020-08-03T12:40:57Z</dc:date>
    </item>
    <item>
      <title>Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197359#M3859</link>
      <description>&lt;P&gt;Hello Daamgard22,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Please provide source code and error messages to help us diagnose your issue.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 04 Aug 2020 19:25:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197359#M3859</guid>
      <dc:creator>JesusG_Intel</dc:creator>
      <dc:date>2020-08-04T19:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197506#M3861</link>
      <description>&lt;P&gt;Hi Jesus,&lt;BR /&gt;&lt;BR /&gt;Sure.&lt;BR /&gt;This is my code in Enclave.cpp&lt;BR /&gt;Everything seems to be linked and imported correctly since i spend some time getting that right.&lt;BR /&gt;I am importing sgx_tcrypto.h&lt;BR /&gt;ocall_print is just a call to printf that also appends a newline&lt;BR /&gt;&lt;BR /&gt;The code example prints:&lt;BR /&gt;"&lt;BR /&gt;Encryption failed&lt;BR /&gt;1&lt;BR /&gt;"&lt;BR /&gt;&lt;BR /&gt;Indicating an error of "&lt;SPAN&gt;SGX_ERROR_UNEXPECTED" according to sgx_error.h&lt;BR /&gt;It also seems that the private and public key were created successfully as it fails at encryption.&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;unsigned char p_n[256];
unsigned char p_d[256];
unsigned char p_p[256];
unsigned char p_q[256];
unsigned char p_dmp1[256];
unsigned char p_dmq1[256];
unsigned char p_iqmp[256];

int n_byte_size = 256;
int e_byte_size = 4;
long e = 65537;


sgx_status_t ret_create_key_params = sgx_create_rsa_key_pair(n_byte_size, e_byte_size, p_n, p_d, (unsigned char*)&amp;amp;e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp);

if (ret_create_key_params != SGX_SUCCESS) {
    ocall_print("Key param generation failed");
    ocall_print(std::to_string(ret_create_key_params).c_str());
} else {
    ocall_print((char *) p_q);
}

void *private_key[256];

sgx_status_t ret_create_private_key = sgx_create_rsa_priv2_key(n_byte_size, e_byte_size, (unsigned char*)&amp;amp;e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp, private_key);

if ( ret_create_private_key != SGX_SUCCESS) {
    ocall_print("Private key generation failed");
    ocall_print(std::to_string(ret_create_private_key).c_str());
}

void *public_key[256];

sgx_status_t ret_create_public_key = sgx_create_rsa_pub1_key(n_byte_size, e_byte_size, p_n, (unsigned char*)&amp;amp;e, public_key);

if ( ret_create_public_key != SGX_SUCCESS) {
    ocall_print("Public key generation failed");
    ocall_print(std::to_string(ret_create_public_key).c_str());
}

const char * pin_data = "Hello World!";
size_t out_len = 256;
unsigned char * pout_data = NULL;
ocall_print("Public key");

sgx_status_t ret_encrypt = sgx_rsa_pub_encrypt_sha256(&amp;amp;public_key, pout_data, &amp;amp;out_len, (unsigned char *)pin_data, sizeof(pin_data));

if ( ret_encrypt != SGX_SUCCESS) {
    ocall_print("Encryption failed");
    ocall_print(std::to_string(ret_encrypt).c_str());
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Aug 2020 06:47:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197506#M3861</guid>
      <dc:creator>damgaard22</dc:creator>
      <dc:date>2020-08-05T06:47:00Z</dc:date>
    </item>
    <item>
      <title>Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197757#M3866</link>
      <description>&lt;P&gt;Hello daamgard22,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I found this example, &lt;A href="https://cpp.hotexamples.com/examples/-/-/sgx_sha256_msg/cpp-sgx_sha256_msg-function-examples.html#0xbe1627ef0ca4806adec49545040c9a3e11a5f3b49b8fbf268df2aa46f9e6fd9a-307,,836," rel="noopener noreferrer" target="_blank"&gt;https://cpp.hotexamples.com/examples/-/-/sgx_sha256_msg/cpp-sgx_sha256_msg-function-examples.html#0xbe1627ef0ca4806adec49545040c9a3e11a5f3b49b8fbf268df2aa46f9e6fd9a-307,,836,&lt;/A&gt; that demonstrates how to use sgx_rsa_pub_encrypt_sha256.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You just need to make three small changes to make your code work:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier;"&gt;//void *public_key[256]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;B style="font-family: courier;"&gt;void *public_key = NULL;&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier;"&gt;sgx_status_t ret_create_public_key = sgx_create_rsa_pub1_key(n_byte_size, e_byte_size, p_n, (unsigned char*)&amp;amp;e, &lt;/SPAN&gt;&lt;B style="font-family: courier;"&gt;&amp;amp;public_key&lt;/B&gt;&lt;SPAN style="font-family: courier;"&gt;); //add "&amp;amp;" to public_key&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier;"&gt;sgx_status_t ret_encrypt = sgx_rsa_pub_encrypt_sha256(&lt;/SPAN&gt;&lt;B style="font-family: courier;"&gt;public_key,&lt;/B&gt;&lt;SPAN style="font-family: courier;"&gt; pout_data, &amp;amp;out_len, (unsigned char *)pin_data, sizeof(pin_data)); //remove "&amp;amp;" from public_key&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Aug 2020 20:23:38 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197757#M3866</guid>
      <dc:creator>JesusG_Intel</dc:creator>
      <dc:date>2020-08-05T20:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197960#M3868</link>
      <description>&lt;P&gt;Thanks, that got me a lot further!&lt;BR /&gt;&lt;BR /&gt;Though i am now having trouble decrypting it correctly.&lt;BR /&gt;When decrypting, the original "Hello World!" becomes "Hello Wo" which means it is missing 4 chars.&lt;BR /&gt;Looking at the output it does also seem that the "decrypted_out_len" that indicates the length of the decrypted data is 8 bytes instead of the expected 12 bytes.&lt;BR /&gt;&lt;BR /&gt;Here is the updated code:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;unsigned char p_n[256];
unsigned char p_d[256];
unsigned char p_p[256];
unsigned char p_q[256];
unsigned char p_dmp1[256];
unsigned char p_dmq1[256];
unsigned char p_iqmp[256];

int n_byte_size = 256;
long e = 65537;


sgx_status_t ret_create_key_params = sgx_create_rsa_key_pair(n_byte_size, sizeof(e), p_n, p_d, (unsigned char*)&amp;amp;e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp);

if (ret_create_key_params != SGX_SUCCESS) {
    ocall_print("Key param generation failed");
    ocall_print(std::to_string(ret_create_key_params).c_str());
} else {
    ocall_print((char *) p_q);
}

void *private_key = NULL;

sgx_status_t ret_create_private_key = sgx_create_rsa_priv2_key(n_byte_size, sizeof(e), (unsigned char*)&amp;amp;e, p_p, p_q, p_dmp1, p_dmq1, p_iqmp, &amp;amp;private_key);

if ( ret_create_private_key != SGX_SUCCESS) {
    ocall_print("Private key generation failed");
    ocall_print(std::to_string(ret_create_private_key).c_str());
}

void *public_key = NULL;

sgx_status_t ret_create_public_key = sgx_create_rsa_pub1_key(n_byte_size, sizeof(e), p_n, (unsigned char*)&amp;amp;e, &amp;amp;public_key);

if ( ret_create_public_key != SGX_SUCCESS) {
    ocall_print("Public key generation failed");
    ocall_print(std::to_string(ret_create_public_key).c_str());
}

char * pin_data = "Hello World!";
size_t out_len = 0;

sgx_status_t ret_get_output_len = sgx_rsa_pub_encrypt_sha256(public_key, NULL, &amp;amp;out_len, (unsigned char *)pin_data, sizeof(pin_data));

if ( ret_get_output_len != SGX_SUCCESS) {
    ocall_print("Determination of output length failed");
    ocall_print(std::to_string(ret_get_output_len).c_str());
}

unsigned char pout_data[out_len];

sgx_status_t ret_encrypt = sgx_rsa_pub_encrypt_sha256(public_key, pout_data, &amp;amp;out_len, (unsigned char *)pin_data, sizeof(pin_data));

if ( ret_encrypt != SGX_SUCCESS) {
    ocall_print("Encryption failed");
    ocall_print(std::to_string(ret_encrypt).c_str());
} else {
    ocall_print(std::to_string(out_len).c_str());
}

size_t decrypted_out_len = 0;

sgx_status_t ret_determine_decrypt_len = sgx_rsa_priv_decrypt_sha256(private_key, NULL, &amp;amp;decrypted_out_len, pout_data, sizeof(pout_data));

if ( ret_determine_decrypt_len != SGX_SUCCESS) {
    ocall_print("Determination of decrypted output length failed");
    ocall_print(std::to_string(ret_determine_decrypt_len).c_str());
}

unsigned char decrypted_pout_data[decrypted_out_len];

sgx_status_t ret_decrypt = sgx_rsa_priv_decrypt_sha256(private_key, decrypted_pout_data, &amp;amp;decrypted_out_len, pout_data, sizeof(pout_data));

if ( ret_decrypt != SGX_SUCCESS) {
    ocall_print("Decryption failed");
    ocall_print(std::to_string(ret_decrypt).c_str());
} else {
    ocall_print("Decrypted MESSAGE:");
    ocall_print((char *) decrypted_pout_data);
    ocall_print(std::to_string(decrypted_out_len).c_str());
}
&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;This outputs (With comments):&lt;BR /&gt;256 # out_len&lt;BR /&gt;Decrypted MESSAGE:&lt;BR /&gt;Hello Wop28c�&amp;#127; # Seems like it correctly decrypted 8 bytes and the rest looks weird&lt;BR /&gt;8 # decrypted_out_len (Should be 12 bytes afaik as there are 12 bytes in "Hello World!"&lt;/P&gt;</description>
      <pubDate>Thu, 06 Aug 2020 09:47:31 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1197960#M3868</guid>
      <dc:creator>damgaard22</dc:creator>
      <dc:date>2020-08-06T09:47:31Z</dc:date>
    </item>
    <item>
      <title>Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1198114#M3871</link>
      <description>&lt;P&gt;Hello daamgard,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You seem to be doing everything right and I cannot figure out why the output is only 8 bytes in length. I will confer with engineering and let you know what they find.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 06 Aug 2020 21:31:12 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1198114#M3871</guid>
      <dc:creator>JesusG_Intel</dc:creator>
      <dc:date>2020-08-06T21:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1201670#M3879</link>
      <description>&lt;P&gt;Hi Jesus,&lt;BR /&gt;&lt;BR /&gt;Any updates on this?&lt;BR /&gt;&lt;BR /&gt;Also is there a way to get the length of the keys?&lt;BR /&gt;For example i want to send the public key to another party but i do not know how to find the size of the&amp;nbsp; public key.&lt;BR /&gt;&lt;BR /&gt;All your help is greatly appreciated.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 13:39:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1201670#M3879</guid>
      <dc:creator>damgaard22</dc:creator>
      <dc:date>2020-08-19T13:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1201804#M3882</link>
      <description>&lt;P&gt;Hello damgaard22,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We finally figured it out. When you encrypt the message using sgx_rsa_pub_encrypt_sha256 and you send the size of the input in the last parameter, you are using sizeof(pin_data). In your code, pin_data is a "char *" so the size of "char *" is 8 bytes. For this parameter, you have to send the size of the input itself. In this case, you can use strlen(pin_data) instead of sizeof(pin_data).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a good &lt;A href="https://security.stackexchange.com/questions/90169/rsa-public-key-and-private-key-lengths" target="_self"&gt;discussion&lt;/A&gt;&amp;nbsp; on private and public key lengths.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 20:07:39 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1201804#M3882</guid>
      <dc:creator>JesusG_Intel</dc:creator>
      <dc:date>2020-08-19T20:07:39Z</dc:date>
    </item>
    <item>
      <title>Re:Asymmetric cryptography in Enclave</title>
      <link>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1202550#M3899</link>
      <description>&lt;P&gt;&lt;B&gt;This thread has been marked as answered and Intel will no longer monitor this thread. If you want a response from Intel in a follow-up question, please open a new thread.&lt;/B&gt;&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 21 Aug 2020 17:13:48 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Software-Guard-Extensions/Asymmetric-cryptography-in-Enclave/m-p/1202550#M3899</guid>
      <dc:creator>JesusG_Intel</dc:creator>
      <dc:date>2020-08-21T17:13:48Z</dc:date>
    </item>
  </channel>
</rss>

