sgx_rsa_internal.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "sgx_tcrypto.h"
  46. #include "ipp_wrapper.h"
  47. #include "sgx_trts.h"
  48. #define RSA_SEED_SIZE_SHA256 32
  49. extern "C" IppStatus newPrimeGen(int nMaxBits, IppsPrimeState ** pPrimeG)
  50. {
  51. if (pPrimeG == NULL || nMaxBits <= 0) {
  52. return ippStsBadArgErr;
  53. }
  54. int ctxSize = 0;
  55. IppStatus error_code = ippsPrimeGetSize(nMaxBits, &ctxSize);
  56. if (error_code != ippStsNoErr) {
  57. return error_code;
  58. }
  59. IppsPrimeState* pCtx = (IppsPrimeState *)malloc(ctxSize);
  60. if (pCtx == NULL) {
  61. return ippStsMemAllocErr;
  62. }
  63. error_code = ippsPrimeInit(nMaxBits, pCtx);
  64. if (error_code != ippStsNoErr) {
  65. free(pCtx);
  66. return error_code;
  67. }
  68. *pPrimeG = pCtx;
  69. return error_code;
  70. }
  71. extern "C" IppStatus newPRNG(IppsPRNGState **pRandGen)
  72. {
  73. if (pRandGen == NULL) {
  74. return ippStsBadArgErr;
  75. }
  76. int ctxSize = 0;
  77. IppStatus error_code = ippsPRNGGetSize(&ctxSize);
  78. if (error_code != ippStsNoErr) {
  79. return error_code;
  80. }
  81. IppsPRNGState* pCtx = (IppsPRNGState *)malloc(ctxSize);
  82. if (pCtx == NULL) {
  83. return ippStsMemAllocErr;
  84. }
  85. error_code = ippsPRNGInit(160, pCtx);
  86. if (error_code != ippStsNoErr) {
  87. free(pCtx);
  88. return error_code;
  89. }
  90. *pRandGen = pCtx;
  91. return error_code;
  92. }
  93. extern "C" IppStatus newBN(const Ipp32u *data, int size_in_bytes, IppsBigNumState **p_new_BN)
  94. {
  95. IppsBigNumState *pBN = 0;
  96. int bn_size = 0;
  97. if (p_new_BN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u))
  98. return ippStsBadArgErr;
  99. /* Get the size of the IppsBigNumState context in bytes */
  100. IppStatus error_code = ippsBigNumGetSize(size_in_bytes / (int)sizeof(Ipp32u), &bn_size);
  101. if (error_code != ippStsNoErr) {
  102. *p_new_BN = 0;
  103. return error_code;
  104. }
  105. pBN = (IppsBigNumState *)malloc(bn_size);
  106. if (!pBN) {
  107. error_code = ippStsMemAllocErr;
  108. *p_new_BN = 0;
  109. return error_code;
  110. }
  111. /* Initializes context and partitions allocated buffer */
  112. error_code = ippsBigNumInit(size_in_bytes / (int)sizeof(Ipp32u), pBN);
  113. if (error_code != ippStsNoErr) {
  114. SAFE_FREE_MM(pBN);
  115. *p_new_BN = 0;
  116. return error_code;
  117. }
  118. if (data) {
  119. error_code = ippsSet_BN(IppsBigNumPOS, size_in_bytes / (int)sizeof(Ipp32u), data, pBN);
  120. if (error_code != ippStsNoErr) {
  121. SAFE_FREE_MM(pBN);
  122. *p_new_BN = 0;
  123. return error_code;
  124. }
  125. }
  126. *p_new_BN = pBN;
  127. return error_code;
  128. }
  129. extern "C" void secure_free_rsa_pri1_key(int n_byte_size, int d_byte_size, IppsRSAPrivateKeyState *pri_key1)
  130. {
  131. if (n_byte_size <= 0 || d_byte_size <= 0 || pri_key1 == NULL) {
  132. if (pri_key1)
  133. free(pri_key1);
  134. return;
  135. }
  136. int rsa1_size = 0;
  137. if (ippsRSA_GetSizePrivateKeyType1(n_byte_size * 8, d_byte_size * 8, &rsa1_size) != ippStsNoErr) {
  138. free(pri_key1);
  139. return;
  140. }
  141. /* Clear the buffer before free. */
  142. memset_s(pri_key1, rsa1_size, 0, rsa1_size);
  143. free(pri_key1);
  144. return;
  145. }
  146. extern "C" void secure_free_rsa_pri2_key(int p_byte_size, IppsRSAPrivateKeyState *pri_key2)
  147. {
  148. if (p_byte_size <= 0 || pri_key2 == NULL) {
  149. if (pri_key2)
  150. free(pri_key2);
  151. return;
  152. }
  153. int rsa2_size = 0;
  154. if (ippsRSA_GetSizePrivateKeyType2(p_byte_size / 2 * 8, p_byte_size / 2 * 8, &rsa2_size) != ippStsNoErr) {
  155. free(pri_key2);
  156. return;
  157. }
  158. /* Clear the buffer before free. */
  159. memset_s(pri_key2, rsa2_size, 0, rsa2_size);
  160. free(pri_key2);
  161. return;
  162. }
  163. extern "C" void secure_free_BN(IppsBigNumState *pBN, int size_in_bytes)
  164. {
  165. if (pBN == NULL || size_in_bytes <= 0 || size_in_bytes % sizeof(Ipp32u)) {
  166. if (pBN) {
  167. free(pBN);
  168. }
  169. return;
  170. }
  171. int bn_size = 0;
  172. /* Get the size of the IppsBigNumState context in bytes
  173. * Since we have checked the size_in_bytes before and the &bn_size is not NULL,
  174. * ippsBigNumGetSize never returns failure
  175. */
  176. if (ippsBigNumGetSize(size_in_bytes / (int)sizeof(Ipp32u), &bn_size) != ippStsNoErr) {
  177. free(pBN);
  178. return;
  179. }
  180. /* Clear the buffer before free. */
  181. memset_s(pBN, bn_size, 0, bn_size);
  182. free(pBN);
  183. return;
  184. }
  185. extern "C" void secure_free_rsa_pub_key(int n_byte_size, int e_byte_size, IppsRSAPublicKeyState *pub_key)
  186. {
  187. if (n_byte_size <= 0 || e_byte_size <= 0 || pub_key == NULL) {
  188. if (pub_key)
  189. free(pub_key);
  190. return;
  191. }
  192. int rsa_size = 0;
  193. if (ippsRSA_GetSizePublicKey(n_byte_size * 8, e_byte_size * 8, &rsa_size) != ippStsNoErr) {
  194. free(pub_key);
  195. return;
  196. }
  197. /* Clear the buffer before free. */
  198. memset_s(pub_key, rsa_size, 0, rsa_size);
  199. free(pub_key);
  200. return;
  201. }