Skip to content

Commit 5d59856

Browse files
committed
Update tlibcrypto to add new crypto wrapper function
New API: sgx_create_rsa_key_pair sgx_rsa_priv_decrypt_sha256 sgx_rsa_pub_encrypt_sha256 sgx_create_rsa_priv2_key sgx_create_rsa_priv1_key Signed-off-by: Li, Xun <[email protected]>
1 parent 646b2d3 commit 5d59856

File tree

9 files changed

+1500
-7
lines changed

9 files changed

+1500
-7
lines changed

common/inc/internal/ipp_wrapper.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434

3535
#include "ippcp.h"
3636

37+
#ifndef CLEAR_FREE_MEM
38+
#define CLEAR_FREE_MEM(address, size) { \
39+
if (address != NULL) { \
40+
if (size > 0) { \
41+
(void)memset_s(address, size, 0, size); \
42+
} \
43+
free(address); \
44+
} \
45+
}
46+
#endif
47+
3748
#ifndef SAFE_FREE_MM
3849
#define SAFE_FREE_MM(ptr) {\
3950
if(ptr != NULL) \
@@ -55,7 +66,8 @@ extern "C" {
5566
#endif
5667

5768
IppStatus newBN(const Ipp32u *data, int size_in_bytes, IppsBigNumState **p_new_BN);
58-
69+
IppStatus newPrimeGen(int nMaxBits, IppsPrimeState ** pPrimeG);
70+
IppStatus newPRNG(IppsPRNGState **pRandGen);
5971
IppStatus create_rsa_priv1_key(int n_byte_size, int d_byte_size, const Ipp32u *n, const Ipp32u *d, IppsRSAPrivateKeyState **new_pri_key1);
6072

6173
IppStatus create_rsa_priv2_key(int p_byte_size, const Ipp32u *p, const Ipp32u *q,

common/inc/internal/ssl_wrapper.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Intel Corporation nor the names of its
15+
* contributors may be used to endorse or promote products derived
16+
* from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
*/
31+
#ifndef _SSL_WRAPPER_H
32+
#define _SSL_WRAPPER_H
33+
34+
35+
#ifndef BN_CHECK_BREAK
36+
#define BN_CHECK_BREAK(x) if((x == NULL) || (BN_is_zero(x))){break;}
37+
#endif
38+
39+
#ifndef NULL_BREAK
40+
#define NULL_BREAK(x) if(!x){break;}
41+
#endif
42+
43+
44+
#endif
45+
46+

common/inc/sgx_tcrypto.h

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "sgx.h"
4444
#include "sgx_defs.h"
45+
#include "stdlib.h"
4546

4647
#define SGX_SHA256_HASH_SIZE 32
4748
#define SGX_ECP256_KEY_SIZE 32
@@ -138,6 +139,41 @@ typedef enum {
138139
SGX_RSA_INVALID_SIGNATURE /* invalid signature */
139140
} sgx_rsa_result_t;
140141

142+
typedef enum {
143+
SGX_RSA_PRIVATE_KEY, /* RSA private key state */
144+
145+
SGX_RSA_PUBLIC_KEY /* RSA public key state */
146+
} sgx_rsa_key_type_t;
147+
148+
#define N_SIZE_IN_BYTES 384
149+
#define E_SIZE_IN_BYTES 4
150+
#define D_SIZE_IN_BYTES 384
151+
#define P_SIZE_IN_BYTES 192
152+
#define Q_SIZE_IN_BYTES 192
153+
#define DMP1_SIZE_IN_BYTES 192
154+
#define DMQ1_SIZE_IN_BYTES 192
155+
#define IQMP_SIZE_IN_BYTES 192
156+
157+
#define N_SIZE_IN_UINT N_SIZE_IN_BYTES/sizeof(unsigned int)
158+
#define E_SIZE_IN_UINT E_SIZE_IN_BYTES/sizeof(unsigned int)
159+
#define D_SIZE_IN_UINT D_SIZE_IN_BYTES/sizeof(unsigned int)
160+
#define P_SIZE_IN_UINT P_SIZE_IN_BYTES/sizeof(unsigned int)
161+
#define Q_SIZE_IN_UINT Q_SIZE_IN_BYTES/sizeof(unsigned int)
162+
#define DMP1_SIZE_IN_UINT DMP1_SIZE_IN_BYTES/sizeof(unsigned int)
163+
#define DMQ1_SIZE_IN_UINT DMQ1_SIZE_IN_BYTES/sizeof(unsigned int)
164+
#define IQMP_SIZE_IN_UINT IQMP_SIZE_IN_BYTES/sizeof(unsigned int)
165+
166+
typedef struct _rsa_params_t {
167+
unsigned int n[N_SIZE_IN_UINT];
168+
unsigned int e[E_SIZE_IN_UINT];
169+
unsigned int d[D_SIZE_IN_UINT];
170+
unsigned int p[P_SIZE_IN_UINT];
171+
unsigned int q[Q_SIZE_IN_UINT];
172+
unsigned int dmp1[DMP1_SIZE_IN_UINT];
173+
unsigned int dmq1[DMQ1_SIZE_IN_UINT];
174+
unsigned int iqmp[IQMP_SIZE_IN_UINT];
175+
}rsa_params_t;
176+
141177
#ifdef __cplusplus
142178
extern "C" {
143179
#endif
@@ -656,8 +692,127 @@ extern "C" {
656692
const sgx_rsa3072_signature_t *p_signature,
657693
sgx_rsa_result_t *p_result);
658694

695+
/** Create RSA key pair with <n_byte_size> key size and <e_byte_size> public exponent.
696+
*
697+
* Parameters:
698+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
699+
* Inputs: p_e [In/Out] Pointer to the public exponent e.
700+
* n_byte_size [In] Size in bytes of the key modulus.
701+
* e_byte_size [In] Size in bytes of the key public exponent.
702+
* Output: p_* [Out] Pointer to the matching key parameter/factor buffer.
703+
*/
704+
sgx_status_t sgx_create_rsa_key_pair(int n_byte_size, int e_byte_size, unsigned char *p_n, unsigned char *p_d, unsigned char *p_e,
705+
unsigned char *p_p, unsigned char *p_q, unsigned char *p_dmp1,
706+
unsigned char *p_dmq1, unsigned char *p_iqmp);
707+
708+
/** Decrypt ciphertext [pin_data] using RSA private key, with OAEP SHA-256
709+
*
710+
* Parameters:
711+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
712+
* Inputs: rsa_key - Pointer to the EVP_PKEY struct containting RSA private key.
713+
* pin_data - Pointer to the input ciphertext buffer.
714+
* pin_len - Ciphertext buffer size.
715+
* Output: pout_data - Pointer to the output buffer.
716+
* pout_len - Pointer to amount of data written.
717+
*
718+
*/
719+
sgx_status_t sgx_rsa_priv_decrypt_sha256(void* rsa_key, unsigned char* pout_data, size_t* pout_len, const unsigned char* pin_data, const size_t pin_len);
720+
721+
/** Encrypt input data [pin_data] using RSA public key, with OAEP SHA-256
722+
*
723+
* Parameters:
724+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
725+
* Inputs: rsa_key - Pointer to the EVP_PKEY struct containting RSA public key.
726+
* pin_data - Pointer to the input data buffer.
727+
* pin_len - Input buffer size.
728+
* Output: pout_data - Pointer to the output buffer.
729+
* pout_len - Pointer to amount of data (ciphertext) written.
730+
*
731+
*/
732+
sgx_status_t sgx_rsa_pub_encrypt_sha256(void* rsa_key, unsigned char* pout_data, size_t* pout_len, const unsigned char* pin_data, const size_t pin_len);
733+
734+
/** Create RSA private key using input buffer factors in little endian.
735+
*
736+
* Parameters:
737+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
738+
* Inputs: prime_size - Pointer to the modulus size.
739+
* exp_size - Pointer to the public exponent e size.
740+
* g_rsa_key_e - Pointer to the public exponent e buffer.
741+
* g_rsa_key_p - Pointer to the prime number p.
742+
* g_rsa_key_q - Pointer to the prime number q.
743+
* g_rsa_key_dmp1 - Pointer to dmp1 [d mod (p-1)].
744+
* g_rsa_key_dmq1 - Pointer to dmq1 [d mod (q-1)].
745+
* g_rsa_key_iqmp - Pointer to iqmp [q^-1 mod p].
746+
* Output: new_pri_key2 - Pointer to the generated private key.
747+
*
748+
*/
749+
sgx_status_t sgx_create_rsa_priv2_key(int prime_size, int exp_size, const unsigned char *g_rsa_key_e, const unsigned char *g_rsa_key_p, const unsigned char *g_rsa_key_q,
750+
const unsigned char *g_rsa_key_dmp1, const unsigned char *g_rsa_key_dmq1, const unsigned char *g_rsa_key_iqmp,
751+
void **new_pri_key2);
752+
753+
/** Create RSA private key using input buffer factors in little endian.
754+
*
755+
* Parameters:
756+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
757+
* Inputs: n_byte_size - Pointer to the modulus size.
758+
* e_byte_size - Pointer to the public exponent e size.
759+
* d_byte_size - Pointer to the private exponent d size.
760+
* le_e - Pointer to the public exponent e buffer.
761+
* le_n - Pointer to the modulus n.
762+
* le_d - Pointer to the private exponent d.
763+
* Output: new_pri_key1 - Pointer to the generated private key.
764+
*
765+
*/
766+
sgx_status_t sgx_create_rsa_priv1_key(int n_byte_size, int e_byte_size, int d_byte_size, const unsigned char *le_n, const unsigned char *le_e,
767+
const unsigned char *le_d, void **new_pri_key1);
768+
769+
/** Create RSA public key using input buffer factors in little endian.
770+
*
771+
* Parameters:
772+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
773+
* Inputs: prime_size - Pointer to the modulus size.
774+
* exp_size - Pointer to the public exponent e size.
775+
* le_n - Pointer to the modulus n buffer.
776+
* le_e - Pointer to the public exponent e buffer.
777+
* Output: new_pub_key1 - Pointer to the generated public key.
778+
*
779+
*/
780+
sgx_status_t sgx_create_rsa_pub1_key(int prime_size, int exp_size, const unsigned char *le_n, const unsigned char *le_e, void **new_pub_key1);
781+
782+
/** Clear and free RSA key which was generated by one of the Tcrypto "sgx_create_rsa_*" APIs.
783+
*
784+
* Parameters:
785+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
786+
* Inputs: p_rsa_key - Pointer to the RSA key.
787+
* (Note: All input parameters below are relevant only when using IPP based tcrypto library)
788+
* key_type - key state type, relevant only when using IPP based tcrypto library.
789+
* Possible options {SGX_RSA_PRIVATE_KEY, SGX_RSA_PUBLIC_KEY}
790+
* mod_size - RSA key modulus size.
791+
* exp_size - RSA key public exponent size.
792+
* Output:
793+
*
794+
*/
795+
sgx_status_t sgx_free_rsa_key(void *p_rsa_key, sgx_rsa_key_type_t key_type, int mod_size, int exp_size);
796+
797+
/** Create an ECDSA private key based on input random seed.
798+
*
799+
* Parameters:
800+
* Return: sgx_status_t - SGX_SUCCESS or failure as defined in sgx_error.h
801+
* Inputs: hash_drg - Input seed
802+
* hash_drg_len - Seed len
803+
* sgx_nistp256_r_m1 -
804+
* sgx_nistp256_r_m1_len - nistp256 len
805+
* Output: out_key - ECDSA private key
806+
* out_key_len - ECDSA private key length
807+
*
808+
*/
809+
sgx_status_t sgx_calculate_ecdsa_priv_key(const unsigned char* hash_drg, int hash_drg_len,
810+
const unsigned char* sgx_nistp256_r_m1, int sgx_nistp256_r_m1_len,
811+
unsigned char* out_key, int out_key_len);
812+
659813
#ifdef __cplusplus
660814
}
661815
#endif
662816

663817
#endif
818+

sdk/tlibcrypto/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CPPFLAGS := -I$(COMMON_DIR)/inc/internal \
3737

3838
CXXFLAGS += $(ENCLAVE_CXXFLAGS) -Werror -fno-exceptions -fno-rtti
3939

40-
OBJ = init_tcrypto_lib.o sgx_aes_ctr.o sgx_aes_gcm.o sgx_cmac128.o sgx_ecc256.o sgx_ecc256_ecdsa.o sgx_sha256.o sgx_sha256_msg.o sgx_ecc256_internal.o sgx_rsa3072.o
40+
OBJ = init_tcrypto_lib.o sgx_aes_ctr.o sgx_rsa_encryption.o sgx_aes_gcm.o sgx_cmac128.o sgx_ecc256.o sgx_ecc256_ecdsa.o sgx_sha256.o sgx_sha256_msg.o sgx_ecc256_internal.o sgx_rsa3072.o
4141
SHARED_OBJ = tcrypto_version.o
4242

4343
ifneq ($(USE_OPT_LIBS), 1)
@@ -50,14 +50,14 @@ else
5050
endif #($(ARCH), x86_64)
5151

5252
ifdef DEBUG
53+
SGX_COMMON_CFLAGS += -O0
5354
OPENSSL_LIBRARY_PATH := $(OPENSSL_PACKAGE)/lib64/debug
5455
else
5556
OPENSSL_LIBRARY_PATH := $(OPENSSL_PACKAGE)/lib64/release
5657
endif
5758

58-
SGXSSL_Library_Name := sgx_tsgxssl
5959
OpenSSL_Crypto_Library_Name := sgx_tsgxssl_crypto
60-
60+
SGXSSL_Library_Name := sgx_tsgxssl
6161
PREPARE_SGXSSL := $(LINUX_EXTERNAL_DIR)/sgxssl/prepare_sgxssl.sh
6262
PREPRARE_SGX_SSL:
6363
chmod 755 $(PREPARE_SGXSSL)
@@ -72,7 +72,7 @@ LIB_NAME := libsgx_tcrypto_sgxssl.a
7272
else
7373

7474
CPPFLAGS += -I$(SGX_IPP_INC)
75-
OBJ += sgx_ecc256_common.o
75+
OBJ += sgx_ecc256_common.o sgx_rsa_internal.o
7676
SRCDIR := ipp
7777

7878
LIB_NAME := libsgx_tcrypto_ipp.a

0 commit comments

Comments
 (0)