|
42 | 42 |
|
43 | 43 | #include "sgx.h" |
44 | 44 | #include "sgx_defs.h" |
| 45 | +#include "stdlib.h" |
45 | 46 |
|
46 | 47 | #define SGX_SHA256_HASH_SIZE 32 |
47 | 48 | #define SGX_ECP256_KEY_SIZE 32 |
@@ -138,6 +139,41 @@ typedef enum { |
138 | 139 | SGX_RSA_INVALID_SIGNATURE /* invalid signature */ |
139 | 140 | } sgx_rsa_result_t; |
140 | 141 |
|
| 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 | + |
141 | 177 | #ifdef __cplusplus |
142 | 178 | extern "C" { |
143 | 179 | #endif |
@@ -656,8 +692,127 @@ extern "C" { |
656 | 692 | const sgx_rsa3072_signature_t *p_signature, |
657 | 693 | sgx_rsa_result_t *p_result); |
658 | 694 |
|
| 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 | + |
659 | 813 | #ifdef __cplusplus |
660 | 814 | } |
661 | 815 | #endif |
662 | 816 |
|
663 | 817 | #endif |
| 818 | + |
0 commit comments