sgx_rsa_encryption.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /**
  32. * File:
  33. * sgx_rsa_encryption.cpp
  34. * Description:
  35. * Wrapper for rsa operation functions
  36. *
  37. */
  38. #include "util.h"
  39. #include <assert.h>
  40. #include <limits.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "sgx_error.h"
  45. #include "ipp_wrapper.h"
  46. #include "sgx_trts.h"
  47. #define RSA_SEED_SIZE_SHA256 32
  48. 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,
  49. unsigned char *p_p, unsigned char *p_q, unsigned char *p_dmp1,
  50. unsigned char *p_dmq1, unsigned char *p_iqmp)
  51. {
  52. if (n_byte_size <= 0 || e_byte_size <= 0 || p_n == NULL || p_d == NULL || p_e == NULL ||
  53. p_p == NULL || p_q == NULL || p_dmp1 == NULL || p_dmq1 == NULL || p_iqmp == NULL) {
  54. return SGX_ERROR_INVALID_PARAMETER;
  55. }
  56. IppsRSAPrivateKeyState *p_pri_key = NULL;
  57. IppsRSAPublicKeyState *p_pub_key = NULL;
  58. IppStatus error_code = ippStsNoErr;
  59. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  60. IppsPRNGState *p_rand = NULL;
  61. IppsPrimeState *p_prime = NULL;
  62. Ipp8u * scratch_buffer = NULL;
  63. int pri_size = 0;
  64. IppsBigNumState *bn_n = NULL, *bn_e = NULL, *bn_d = NULL, *bn_e_s = NULL, *bn_p = NULL, *bn_q = NULL, *bn_dmp1 = NULL, *bn_dmq1 = NULL, *bn_iqmp = NULL;
  65. int validate_keys = IPP_IS_INVALID;
  66. int size;
  67. IppsBigNumSGN sgn;
  68. do {
  69. //create a new PRNG
  70. //
  71. error_code = newPRNG(&p_rand);
  72. ERROR_BREAK(error_code);
  73. //create a new prime number generator
  74. //
  75. error_code = newPrimeGen(n_byte_size * 8 / 2, &p_prime);
  76. ERROR_BREAK(error_code);
  77. //allocate and init private key of type 2
  78. //
  79. error_code = ippsRSA_GetSizePrivateKeyType2(n_byte_size / 2 * 8, n_byte_size / 2 * 8, &pri_size);
  80. ERROR_BREAK(error_code);
  81. p_pri_key = (IppsRSAPrivateKeyState *)malloc(pri_size);
  82. NULL_BREAK(p_pri_key);
  83. error_code = ippsRSA_InitPrivateKeyType2(n_byte_size / 2 * 8, n_byte_size / 2 * 8, p_pri_key, pri_size);
  84. ERROR_BREAK(error_code);
  85. //allocate scratch buffer, to be used as temp buffer
  86. //
  87. scratch_buffer = (Ipp8u *)malloc(pri_size);
  88. NULL_BREAK(scratch_buffer);
  89. memset(scratch_buffer, 0, pri_size);
  90. //allocate and initialize RSA BNs
  91. //
  92. error_code = newBN((const Ipp32u*)p_e, e_byte_size, &bn_e_s);
  93. ERROR_BREAK(error_code);
  94. error_code = newBN(NULL, n_byte_size, &bn_n);
  95. ERROR_BREAK(error_code);
  96. error_code = newBN(NULL, e_byte_size, &bn_e);
  97. ERROR_BREAK(error_code);
  98. error_code = newBN(NULL, n_byte_size, &bn_d);
  99. ERROR_BREAK(error_code);
  100. error_code = newBN(NULL, n_byte_size / 2, &bn_p);
  101. ERROR_BREAK(error_code);
  102. error_code = newBN(NULL, n_byte_size / 2, &bn_q);
  103. ERROR_BREAK(error_code);
  104. error_code = newBN(NULL, n_byte_size / 2, &bn_dmp1);
  105. ERROR_BREAK(error_code);
  106. error_code = newBN(NULL, n_byte_size / 2, &bn_dmq1);
  107. ERROR_BREAK(error_code);
  108. error_code = newBN(NULL, n_byte_size / 2, &bn_iqmp);
  109. ERROR_BREAK(error_code);
  110. //generate RSA key components with n_byte_size modulus and p_e public exponent
  111. //
  112. error_code = ippsRSA_GenerateKeys(bn_e_s,
  113. bn_n,
  114. bn_e,
  115. bn_d,
  116. p_pri_key,
  117. scratch_buffer,
  118. 1,
  119. p_prime,
  120. ippsPRNGen,
  121. p_rand);
  122. ERROR_BREAK(error_code);
  123. //extract private key components into BNs
  124. //
  125. error_code = ippsRSA_GetPrivateKeyType2(bn_p,
  126. bn_q,
  127. bn_dmp1,
  128. bn_dmq1,
  129. bn_iqmp,
  130. p_pri_key);
  131. ERROR_BREAK(error_code);
  132. //allocate and initialize public key
  133. //
  134. error_code = ippsRSA_GetSizePublicKey(n_byte_size * 8, e_byte_size * 8, &pri_size);
  135. ERROR_BREAK(error_code);
  136. p_pub_key = (IppsRSAPublicKeyState *)malloc(pri_size);
  137. NULL_BREAK(p_pub_key);
  138. error_code = ippsRSA_InitPublicKey(n_byte_size * 8, e_byte_size * 8, p_pub_key, pri_size);
  139. ERROR_BREAK(error_code);
  140. error_code = ippsRSA_SetPublicKey(bn_n, bn_e, p_pub_key);
  141. ERROR_BREAK(error_code);
  142. //validate generated keys
  143. //
  144. ippsRSA_ValidateKeys(&validate_keys, p_pub_key, p_pri_key, NULL, scratch_buffer, 10, p_prime, ippsPRNGen, p_rand);
  145. if (validate_keys != IPP_IS_VALID) {
  146. break;
  147. }
  148. //extract RSA components from BNs into output buffers
  149. //
  150. error_code = ippsGetSize_BN(bn_n, &size);
  151. ERROR_BREAK(error_code);
  152. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_n, bn_n);
  153. ERROR_BREAK(error_code);
  154. error_code = ippsGetSize_BN(bn_e, &size);
  155. ERROR_BREAK(error_code);
  156. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_e, bn_e);
  157. ERROR_BREAK(error_code);
  158. error_code = ippsGetSize_BN(bn_d, &size);
  159. ERROR_BREAK(error_code);
  160. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_d, bn_d);
  161. ERROR_BREAK(error_code);
  162. error_code = ippsGetSize_BN(bn_p, &size);
  163. ERROR_BREAK(error_code);
  164. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_p, bn_p);
  165. ERROR_BREAK(error_code);
  166. error_code = ippsGetSize_BN(bn_q, &size);
  167. ERROR_BREAK(error_code);
  168. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_q, bn_q);
  169. ERROR_BREAK(error_code);
  170. error_code = ippsGetSize_BN(bn_dmp1, &size);
  171. ERROR_BREAK(error_code);
  172. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_dmp1, bn_dmp1);
  173. ERROR_BREAK(error_code);
  174. error_code = ippsGetSize_BN(bn_dmq1, &size);
  175. ERROR_BREAK(error_code);
  176. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_dmq1, bn_dmq1);
  177. ERROR_BREAK(error_code);
  178. error_code = ippsGetSize_BN(bn_iqmp, &size);
  179. ERROR_BREAK(error_code);
  180. error_code = ippsGet_BN(&sgn, &size, (Ipp32u*)p_iqmp, bn_iqmp);
  181. ERROR_BREAK(error_code);
  182. ret_code = SGX_SUCCESS;
  183. } while (0);
  184. secure_free_BN(bn_e_s, e_byte_size);
  185. secure_free_BN(bn_e, e_byte_size);
  186. secure_free_BN(bn_d, n_byte_size);
  187. secure_free_BN(bn_n, n_byte_size);
  188. secure_free_BN(bn_p, n_byte_size / 2);
  189. secure_free_BN(bn_q, n_byte_size / 2);
  190. secure_free_BN(bn_dmp1, n_byte_size / 2);
  191. secure_free_BN(bn_dmq1, n_byte_size / 2);
  192. secure_free_BN(bn_iqmp, n_byte_size / 2);
  193. SAFE_FREE_MM(p_rand);
  194. SAFE_FREE_MM(p_prime);
  195. secure_free_rsa_pri2_key(n_byte_size / 2, p_pri_key);
  196. secure_free_rsa_pub_key(n_byte_size, e_byte_size, p_pub_key);
  197. SAFE_FREE_MM(scratch_buffer);
  198. return ret_code;
  199. }
  200. 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,
  201. const unsigned char *g_rsa_key_dmp1, const unsigned char *g_rsa_key_dmq1, const unsigned char *g_rsa_key_iqmp,
  202. void **new_pri_key2)
  203. {
  204. (void)(exp_size);
  205. (void)(g_rsa_key_e);
  206. IppsRSAPrivateKeyState *p_rsa2 = NULL;
  207. IppsBigNumState *p_p = NULL, *p_q = NULL, *p_dmp1 = NULL, *p_dmq1 = NULL, *p_iqmp = NULL;
  208. int rsa2_size = 0;
  209. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  210. if (prime_size <= 0 || g_rsa_key_p == NULL || g_rsa_key_q == NULL || g_rsa_key_dmp1 == NULL || g_rsa_key_dmq1 == NULL || g_rsa_key_iqmp == NULL || new_pri_key2 == NULL) {
  211. return SGX_ERROR_INVALID_PARAMETER;
  212. }
  213. IppStatus error_code = ippStsNoErr;
  214. do {
  215. //generate and assign RSA components BNs
  216. //
  217. error_code = newBN((const Ipp32u*)g_rsa_key_p, prime_size / 2, &p_p);
  218. ERROR_BREAK(error_code);
  219. error_code = newBN((const Ipp32u*)g_rsa_key_q, prime_size / 2, &p_q);
  220. ERROR_BREAK(error_code);
  221. error_code = newBN((const Ipp32u*)g_rsa_key_dmp1, prime_size / 2, &p_dmp1);
  222. ERROR_BREAK(error_code);
  223. error_code = newBN((const Ipp32u*)g_rsa_key_dmq1, prime_size / 2, &p_dmq1);
  224. ERROR_BREAK(error_code);
  225. error_code = newBN((const Ipp32u*)g_rsa_key_iqmp, prime_size / 2, &p_iqmp);
  226. ERROR_BREAK(error_code);
  227. //allocate and initialize private key of type 2
  228. //
  229. error_code = ippsRSA_GetSizePrivateKeyType2(prime_size / 2 * 8, prime_size / 2 * 8, &rsa2_size);
  230. ERROR_BREAK(error_code);
  231. p_rsa2 = (IppsRSAPrivateKeyState *)malloc(rsa2_size);
  232. NULL_BREAK(p_rsa2);
  233. error_code = ippsRSA_InitPrivateKeyType2(prime_size / 2 * 8, prime_size / 2 * 8, p_rsa2, rsa2_size);
  234. ERROR_BREAK(error_code);
  235. //setup private key with values of input components
  236. //
  237. error_code = ippsRSA_SetPrivateKeyType2(p_p, p_q, p_dmp1, p_dmq1, p_iqmp, p_rsa2);
  238. ERROR_BREAK(error_code);
  239. *new_pri_key2 = (void*)p_rsa2;
  240. ret_code = SGX_SUCCESS;
  241. } while (0);
  242. secure_free_BN(p_p, prime_size / 2);
  243. secure_free_BN(p_q, prime_size / 2);
  244. secure_free_BN(p_dmp1, prime_size / 2);
  245. secure_free_BN(p_dmq1, prime_size / 2);
  246. secure_free_BN(p_iqmp, prime_size / 2);
  247. if (ret_code != SGX_SUCCESS) {
  248. secure_free_rsa_pri2_key(prime_size, p_rsa2);
  249. }
  250. return ret_code;
  251. }
  252. 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)
  253. {
  254. if (new_pub_key1 == NULL || prime_size <= 0 || exp_size <= 0 || le_n == NULL || le_e == NULL) {
  255. return SGX_ERROR_INVALID_PARAMETER;
  256. }
  257. IppsRSAPublicKeyState *p_pub_key = NULL;
  258. IppsBigNumState *p_n = NULL, *p_e = NULL;
  259. int rsa_size = 0;
  260. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  261. IppStatus error_code = ippStsNoErr;
  262. do {
  263. //generate and assign RSA components BNs
  264. //
  265. error_code = newBN((const Ipp32u*)le_n, prime_size, &p_n);
  266. ERROR_BREAK(error_code);
  267. error_code = newBN((const Ipp32u*)le_e, exp_size, &p_e);
  268. ERROR_BREAK(error_code);
  269. //allocate and initialize public key
  270. //
  271. error_code = ippsRSA_GetSizePublicKey(prime_size * 8, exp_size * 8, &rsa_size);
  272. ERROR_BREAK(error_code);
  273. p_pub_key = (IppsRSAPublicKeyState *)malloc(rsa_size);
  274. NULL_BREAK(p_pub_key);
  275. error_code = ippsRSA_InitPublicKey(prime_size * 8, exp_size * 8, p_pub_key, rsa_size);
  276. ERROR_BREAK(error_code);
  277. //setup public key with values of input components
  278. //
  279. error_code = ippsRSA_SetPublicKey(p_n, p_e, p_pub_key);
  280. ERROR_BREAK(error_code);
  281. *new_pub_key1 = (void*)p_pub_key;
  282. ret_code = SGX_SUCCESS;
  283. } while (0);
  284. secure_free_BN(p_n, prime_size);
  285. secure_free_BN(p_e, exp_size);
  286. if (ret_code != SGX_SUCCESS) {
  287. secure_free_rsa_pub_key(prime_size, exp_size, p_pub_key);
  288. }
  289. return ret_code;
  290. }
  291. sgx_status_t sgx_rsa_pub_encrypt_sha256(void* rsa_key, unsigned char* pout_data, size_t* pout_len, const unsigned char* pin_data,
  292. const size_t pin_len) {
  293. (void)(pout_len);
  294. if (rsa_key == NULL || pin_data == NULL || pin_len < 1 || pin_len >= INT_MAX) {
  295. return SGX_ERROR_INVALID_PARAMETER;
  296. }
  297. uint8_t *p_scratch_buffer = NULL;
  298. Ipp8u seeds[RSA_SEED_SIZE_SHA256] = { 0 };
  299. int scratch_buff_size = 0;
  300. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  301. if (pout_data == NULL) {
  302. return SGX_SUCCESS;
  303. }
  304. do {
  305. //get scratch buffer size, to be used as temp buffer, and allocate it
  306. //
  307. if (ippsRSA_GetBufferSizePublicKey(&scratch_buff_size, (IppsRSAPublicKeyState*)rsa_key) != ippStsNoErr) {
  308. break;
  309. }
  310. p_scratch_buffer = (uint8_t *)malloc(8 * scratch_buff_size);
  311. NULL_BREAK(p_scratch_buffer);
  312. //get random seed
  313. //
  314. if (sgx_read_rand(seeds, RSA_SEED_SIZE_SHA256) != SGX_SUCCESS) {
  315. break;
  316. }
  317. //encrypt input data with public rsa_key and SHA256 padding
  318. //
  319. if (ippsRSAEncrypt_OAEP(pin_data, (int)pin_len, NULL, 0, seeds,
  320. pout_data, (IppsRSAPublicKeyState*)rsa_key, IPP_ALG_HASH_SHA256, p_scratch_buffer) != ippStsNoErr) {
  321. break;
  322. }
  323. ret_code = SGX_SUCCESS;
  324. } while (0);
  325. memset_s(seeds, RSA_SEED_SIZE_SHA256, 0, RSA_SEED_SIZE_SHA256);
  326. SAFE_FREE_MM(p_scratch_buffer);
  327. return ret_code;
  328. }
  329. sgx_status_t sgx_rsa_priv_decrypt_sha256(void* rsa_key, unsigned char* pout_data, size_t* pout_len, const unsigned char* pin_data,
  330. const size_t pin_len) {
  331. (void)(pin_len);
  332. if (rsa_key == NULL || pout_len == NULL || pin_data == NULL) {
  333. return SGX_ERROR_INVALID_PARAMETER;
  334. }
  335. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  336. uint8_t *p_scratch_buffer = NULL;
  337. int scratch_buff_size = 0;
  338. if (pout_data == NULL) {
  339. return SGX_SUCCESS;
  340. }
  341. do {
  342. //get scratch buffer size, to be used as temp buffer, and allocate it
  343. //
  344. if (ippsRSA_GetBufferSizePrivateKey(&scratch_buff_size, (IppsRSAPrivateKeyState*)rsa_key) != ippStsNoErr) {
  345. break;
  346. }
  347. p_scratch_buffer = (uint8_t *)malloc(scratch_buff_size);
  348. NULL_BREAK(p_scratch_buffer);
  349. //decrypt input ciphertext using private key rsa_key
  350. if (ippsRSADecrypt_OAEP(pin_data, NULL, 0, pout_data, (int*)pout_len, (IppsRSAPrivateKeyState*)rsa_key,
  351. IPP_ALG_HASH_SHA256, p_scratch_buffer) != ippStsNoErr) {
  352. break;
  353. }
  354. ret_code = SGX_SUCCESS;
  355. } while (0);
  356. SAFE_FREE_MM(p_scratch_buffer);
  357. return ret_code;
  358. }
  359. 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,
  360. const unsigned char *le_d, void **new_pri_key1)
  361. {
  362. if (n_byte_size <= 0 || e_byte_size <= 0 || d_byte_size <= 0 || new_pri_key1 == NULL ||
  363. le_n == NULL || le_e == NULL || le_d == NULL) {
  364. return SGX_ERROR_INVALID_PARAMETER;
  365. }
  366. IppsRSAPrivateKeyState *p_rsa1 = NULL;
  367. IppsBigNumState *p_n = NULL, *p_d = NULL;
  368. int rsa1_size = 0;
  369. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  370. IppStatus error_code = ippStsNoErr;
  371. do {
  372. //generate and assign RSA components BNs
  373. //
  374. error_code = newBN((const Ipp32u*)le_n, n_byte_size, &p_n);
  375. ERROR_BREAK(error_code);
  376. error_code = newBN((const Ipp32u*)le_d, d_byte_size, &p_d);
  377. ERROR_BREAK(error_code);
  378. //allocate and init private key of type 1
  379. //
  380. error_code = ippsRSA_GetSizePrivateKeyType1(n_byte_size * 8, d_byte_size * 8, &rsa1_size);
  381. ERROR_BREAK(error_code);
  382. p_rsa1 = (IppsRSAPrivateKeyState *)malloc(rsa1_size);
  383. NULL_BREAK(p_rsa1);
  384. error_code = ippsRSA_InitPrivateKeyType1(n_byte_size * 8, d_byte_size * 8, p_rsa1, rsa1_size);
  385. ERROR_BREAK(error_code);
  386. //setup private key with values of input components
  387. //
  388. error_code = ippsRSA_SetPrivateKeyType1(p_n, p_d, p_rsa1);
  389. ERROR_BREAK(error_code);
  390. *new_pri_key1 = p_rsa1;
  391. ret_code = SGX_SUCCESS;
  392. } while (0);
  393. secure_free_BN(p_n, n_byte_size);
  394. secure_free_BN(p_d, d_byte_size);
  395. if (ret_code != SGX_SUCCESS) {
  396. secure_free_rsa_pri1_key(n_byte_size, d_byte_size, p_rsa1);
  397. }
  398. return ret_code;
  399. }
  400. sgx_status_t sgx_free_rsa_key(void *p_rsa_key, sgx_rsa_key_type_t key_type, int mod_size, int exp_size) {
  401. if (key_type == SGX_RSA_PRIVATE_KEY) {
  402. (void)(exp_size);
  403. secure_free_rsa_pri2_key(mod_size, (IppsRSAPrivateKeyState*)p_rsa_key);
  404. } else if (key_type == SGX_RSA_PUBLIC_KEY) {
  405. secure_free_rsa_pub_key(mod_size, exp_size, (IppsRSAPublicKeyState*)p_rsa_key);
  406. }
  407. return SGX_SUCCESS;
  408. }
  409. sgx_status_t sgx_calculate_ecdsa_priv_key(const unsigned char* hash_drg, int hash_drg_len,
  410. const unsigned char* sgx_nistp256_r_m1, int sgx_nistp256_r_m1_len,
  411. unsigned char* out_key, int out_key_len) {
  412. if (out_key == NULL || hash_drg_len <= 0 || sgx_nistp256_r_m1_len <= 0 ||
  413. out_key_len <= 0 || hash_drg == NULL || sgx_nistp256_r_m1 == NULL) {
  414. return SGX_ERROR_INVALID_PARAMETER;
  415. }
  416. sgx_status_t ret_code = SGX_ERROR_UNEXPECTED;
  417. IppStatus ipp_status = ippStsNoErr;
  418. IppsBigNumState *bn_d = NULL;
  419. IppsBigNumState *bn_m = NULL;
  420. IppsBigNumState *bn_o = NULL;
  421. IppsBigNumState *bn_one = NULL;
  422. Ipp32u i = 1;
  423. do {
  424. //allocate and initialize BNs
  425. //
  426. ipp_status = newBN(reinterpret_cast<const Ipp32u *>(hash_drg), hash_drg_len, &bn_d);
  427. ERROR_BREAK(ipp_status);
  428. //generate mod to be n-1 where n is order of ECC Group
  429. //
  430. ipp_status = newBN(reinterpret_cast<const Ipp32u *>(sgx_nistp256_r_m1), sgx_nistp256_r_m1_len, &bn_m);
  431. ERROR_BREAK(ipp_status);
  432. //allocate memory for output BN
  433. //
  434. ipp_status = newBN(NULL, sgx_nistp256_r_m1_len, &bn_o);
  435. ERROR_BREAK(ipp_status);
  436. //create big number with value of 1
  437. //
  438. ipp_status = newBN(&i, sizeof(Ipp32u), &bn_one);
  439. ERROR_BREAK(ipp_status);
  440. //calculate output's BN value
  441. ipp_status = ippsMod_BN(bn_d, bn_m, bn_o);
  442. ERROR_BREAK(ipp_status)
  443. //increase by 1
  444. //
  445. ipp_status = ippsAdd_BN(bn_o, bn_one, bn_o);
  446. ERROR_BREAK(ipp_status);
  447. /*Unmatched size*/
  448. if (sgx_nistp256_r_m1_len != sizeof(sgx_ec256_private_t)) {
  449. break;
  450. }
  451. //convert BN_o into octet string
  452. ipp_status = ippsGetOctString_BN(reinterpret_cast<Ipp8u *>(out_key), sgx_nistp256_r_m1_len, bn_o);//output data in bigendian order
  453. ERROR_BREAK(ipp_status);
  454. ret_code = SGX_SUCCESS;
  455. } while (0);
  456. if (NULL != bn_d) {
  457. secure_free_BN(bn_d, hash_drg_len);
  458. }
  459. if (NULL != bn_m) {
  460. secure_free_BN(bn_m, sgx_nistp256_r_m1_len);
  461. }
  462. if (NULL != bn_o) {
  463. secure_free_BN(bn_o, sgx_nistp256_r_m1_len);
  464. }
  465. if (NULL != bn_one) {
  466. secure_free_BN(bn_one, sizeof(uint32_t));
  467. }
  468. if (ret_code != SGX_SUCCESS) {
  469. (void)memset_s(out_key, out_key_len, 0, out_key_len);
  470. }
  471. return ret_code;
  472. }