pcpngrsakeypublic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2016 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. #include "owndefs.h"
  32. #include "owncp.h"
  33. #include "pcpbn.h"
  34. #include "pcpngrsa.h"
  35. #include "pcpngrsamontstuff.h"
  36. /*F*
  37. // Name: ippsRSA_GetSizePublicKey
  38. //
  39. // Purpose: Returns context size (bytes) of RSA public key context
  40. //
  41. // Returns: Reason:
  42. // ippStsNullPtrErr NULL == pSize
  43. //
  44. // ippStsNotSupportedModeErr MIN_RSA_SIZE > rsaModulusBitSize
  45. // MAX_RSA_SIZE < rsaModulusBitSize
  46. //
  47. // ippStsBadArgErr 0 >= publicExpBitSize
  48. // publicExpBitSize > rsaModulusBitSize
  49. //
  50. // ippStsNoErr no error
  51. //
  52. // Parameters:
  53. // rsaModulusBitSize bitsize of RSA modulus (bitsize of N)
  54. // publicExpBitSize bitsize of public exponent (bitsize of E)
  55. // pSize pointer to the size of RSA key context (bytes)
  56. *F*/
  57. static int cpSizeof_RSA_publicKey(int rsaModulusBitSize, int publicExpBitSize)
  58. {
  59. int pubExpLen = BITS_BNU_CHUNK(publicExpBitSize);
  60. int modulusLen32 = BITS2WORD32_SIZE(rsaModulusBitSize);
  61. int montNsize;
  62. gsMontGetSize(ippBinaryMethod, modulusLen32, &montNsize);
  63. return sizeof(IppsRSAPublicKeyState)
  64. + pubExpLen*sizeof(BNU_CHUNK_T)
  65. + sizeof(BNU_CHUNK_T)-1
  66. + montNsize
  67. + (RSA_PUBLIC_KEY_ALIGNMENT-1);
  68. }
  69. IPPFUN(IppStatus, ippsRSA_GetSizePublicKey,(int rsaModulusBitSize, int publicExpBitSize, int* pKeySize))
  70. {
  71. IPP_BAD_PTR1_RET(pKeySize);
  72. IPP_BADARG_RET((MIN_RSA_SIZE>rsaModulusBitSize) || (rsaModulusBitSize>MAX_RSA_SIZE), ippStsNotSupportedModeErr);
  73. IPP_BADARG_RET(!((0<publicExpBitSize) && (publicExpBitSize<=rsaModulusBitSize)), ippStsBadArgErr);
  74. *pKeySize = cpSizeof_RSA_publicKey(rsaModulusBitSize, publicExpBitSize);
  75. return ippStsNoErr;
  76. }
  77. /*F*
  78. // Name: ippsRSA_InitPublicKey
  79. //
  80. // Purpose: Init RSA public key context
  81. //
  82. // Returns: Reason:
  83. // ippStsNullPtrErr NULL == pKey
  84. //
  85. // ippStsNotSupportedModeErr MIN_RSA_SIZE > rsaModulusBitSize
  86. // MAX_RSA_SIZE < rsaModulusBitSize
  87. //
  88. // ippStsBadArgErr 0 >= publicExpBitSize
  89. // publicExpBitSize > rsaModulusBitSize
  90. //
  91. // ippStsMemAllocErr keyCtxSize is not enough for operation
  92. //
  93. // ippStsNoErr no error
  94. //
  95. // Parameters:
  96. // rsaModulusBitSize bitsize of RSA modulus (bitsize of N)
  97. // publicExpBitSize bitsize of public exponent (bitsize of E)
  98. // pKey pointer to the key context
  99. // keyCtxSize size of memmory accosizted with key comtext
  100. *F*/
  101. IPPFUN(IppStatus, ippsRSA_InitPublicKey,(int rsaModulusBitSize, int publicExpBitSize,
  102. IppsRSAPublicKeyState* pKey, int keyCtxSize))
  103. {
  104. IPP_BAD_PTR1_RET(pKey);
  105. pKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  106. IPP_BADARG_RET((MIN_RSA_SIZE>rsaModulusBitSize) || (rsaModulusBitSize>MAX_RSA_SIZE), ippStsNotSupportedModeErr);
  107. IPP_BADARG_RET(!((0<publicExpBitSize) && (publicExpBitSize<=rsaModulusBitSize)), ippStsBadArgErr);
  108. /* test available size of context buffer */
  109. IPP_BADARG_RET(keyCtxSize<cpSizeof_RSA_publicKey(rsaModulusBitSize, publicExpBitSize), ippStsMemAllocErr);
  110. RSA_PUB_KEY_ID(pKey) = idCtxRSA_PubKey;
  111. RSA_PUB_KEY_MAXSIZE_N(pKey) = rsaModulusBitSize;
  112. RSA_PUB_KEY_MAXSIZE_E(pKey) = publicExpBitSize;
  113. RSA_PUB_KEY_BITSIZE_N(pKey) = 0;
  114. RSA_PUB_KEY_BITSIZE_E(pKey) = 0;
  115. {
  116. Ipp8u* ptr = (Ipp8u*)pKey;
  117. int pubExpLen = BITS_BNU_CHUNK(publicExpBitSize);
  118. int modulusLen32 = BITS2WORD32_SIZE(rsaModulusBitSize);
  119. int montNsize;
  120. gsMontGetSize(ippBinaryMethod, modulusLen32, &montNsize);
  121. /* allocate internal contexts */
  122. ptr += sizeof(IppsRSAPublicKeyState);
  123. RSA_PUB_KEY_E(pKey) = (BNU_CHUNK_T*)( IPP_ALIGNED_PTR((ptr), (int)sizeof(BNU_CHUNK_T)) );
  124. ptr += pubExpLen*sizeof(BNU_CHUNK_T);
  125. RSA_PUB_KEY_NMONT(pKey) = (IppsMontState*)( IPP_ALIGNED_PTR((ptr), (MONT_ALIGNMENT)) );
  126. ptr += montNsize;
  127. ZEXPAND_BNU(RSA_PUB_KEY_E(pKey), 0, pubExpLen);
  128. gsMontInit(ippBinaryMethod, modulusLen32, RSA_PUB_KEY_NMONT(pKey));
  129. return ippStsNoErr;
  130. }
  131. }
  132. /*F*
  133. // Name: ippsRSA_SetPublicKey
  134. //
  135. // Purpose: Set up the RSA public key
  136. //
  137. // Returns: Reason:
  138. // ippStsNullPtrErr NULL == pKey
  139. // NULL == pPublicExp
  140. // NULL == pKey
  141. //
  142. // ippStsContextMatchErr !BN_VALID_ID(pModulus)
  143. // !BN_VALID_ID(pPublicExp)
  144. // !RSA_PUB_KEY_VALID_ID()
  145. //
  146. // ippStsOutOfRangeErr 0 >= pModulus
  147. // 0 >= pPublicExp
  148. //
  149. // ippStsSizeErr bitsize(pModulus) exceeds requested value
  150. // bitsize(pPublicExp) exceeds requested value
  151. //
  152. // ippStsNoErr no error
  153. //
  154. // Parameters:
  155. // pModulus pointer to modulus (N)
  156. // pPublicExp pointer to public exponent (E)
  157. // pKey pointer to the key context
  158. *F*/
  159. IPPFUN(IppStatus, ippsRSA_SetPublicKey,(const IppsBigNumState* pModulus,
  160. const IppsBigNumState* pPublicExp,
  161. IppsRSAPublicKeyState* pKey))
  162. {
  163. IPP_BAD_PTR1_RET(pKey);
  164. pKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  165. IPP_BADARG_RET(!RSA_PUB_KEY_VALID_ID(pKey), ippStsContextMatchErr);
  166. IPP_BAD_PTR1_RET(pModulus);
  167. pModulus = (IppsBigNumState*)( IPP_ALIGNED_PTR(pModulus, BN_ALIGNMENT) );
  168. IPP_BADARG_RET(!BN_VALID_ID(pModulus), ippStsContextMatchErr);
  169. IPP_BADARG_RET(!(0 < cpBN_tst(pModulus)), ippStsOutOfRangeErr);
  170. IPP_BADARG_RET(BITSIZE_BNU(BN_NUMBER(pModulus), BN_SIZE(pModulus)) > RSA_PUB_KEY_MAXSIZE_N(pKey), ippStsSizeErr);
  171. IPP_BAD_PTR1_RET(pPublicExp);
  172. pPublicExp = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPublicExp, BN_ALIGNMENT) );
  173. IPP_BADARG_RET(!BN_VALID_ID(pPublicExp), ippStsContextMatchErr);
  174. IPP_BADARG_RET(!(0 < cpBN_tst(pPublicExp)), ippStsOutOfRangeErr);
  175. IPP_BADARG_RET(BITSIZE_BNU(BN_NUMBER(pPublicExp), BN_SIZE(pPublicExp)) > RSA_PUB_KEY_MAXSIZE_E(pKey), ippStsSizeErr);
  176. {
  177. RSA_PUB_KEY_BITSIZE_N(pKey) = 0;
  178. RSA_PUB_KEY_BITSIZE_E(pKey) = 0;
  179. /* store E */
  180. ZEXPAND_COPY_BNU(RSA_PUB_KEY_E(pKey), BITS_BNU_CHUNK(RSA_PUB_KEY_MAXSIZE_E(pKey)), BN_NUMBER(pPublicExp), BN_SIZE(pPublicExp));
  181. /* setup montgomery engine */
  182. gsMontSet((Ipp32u*)BN_NUMBER(pModulus), BN_SIZE32(pModulus), RSA_PUB_KEY_NMONT(pKey));
  183. RSA_PUB_KEY_BITSIZE_N(pKey) = cpBN_bitsize(pModulus);
  184. RSA_PUB_KEY_BITSIZE_E(pKey) = cpBN_bitsize(pPublicExp);
  185. return ippStsNoErr;
  186. }
  187. }
  188. /*F*
  189. // Name: ippsRSA_GetPublicKey
  190. //
  191. // Purpose: Extract key component from the key context
  192. //
  193. // Returns: Reason:
  194. // ippStsNullPtrErr NULL == pKey
  195. //
  196. // ippStsContextMatchErr !RSA_PUB_KEY_VALID_ID()
  197. // !BN_VALID_ID(pModulus)
  198. // !BN_VALID_ID(pExp)
  199. //
  200. // ippStsIncompleteContextErr public key is not set up
  201. //
  202. // ippStsSizeErr BN_ROOM(pModulus), BN_ROOM(pExp) is not enough
  203. //
  204. // ippStsNoErr no error
  205. //
  206. // Parameters:
  207. // pModulus (optional) pointer to the modulus (N)
  208. // pExp (optional) pointer to the public exponent (E)
  209. // pKey pointer to the key context
  210. *F*/
  211. IPPFUN(IppStatus, ippsRSA_GetPublicKey,(IppsBigNumState* pModulus,
  212. IppsBigNumState* pExp,
  213. const IppsRSAPublicKeyState* pKey))
  214. {
  215. IPP_BAD_PTR1_RET(pKey);
  216. pKey = (IppsRSAPublicKeyState*)( IPP_ALIGNED_PTR(pKey, RSA_PUBLIC_KEY_ALIGNMENT) );
  217. IPP_BADARG_RET(!RSA_PUB_KEY_VALID_ID(pKey), ippStsContextMatchErr);
  218. if(pModulus) {
  219. pModulus = (IppsBigNumState*)( IPP_ALIGNED_PTR(pModulus, BN_ALIGNMENT) );
  220. IPP_BADARG_RET(!BN_VALID_ID(pModulus), ippStsContextMatchErr);
  221. IPP_BADARG_RET(!RSA_PUB_KEY_IS_SET(pKey), ippStsIncompleteContextErr);
  222. IPP_BADARG_RET(BN_ROOM(pModulus)<BITS_BNU_CHUNK(RSA_PUB_KEY_BITSIZE_N(pKey)), ippStsSizeErr);
  223. BN_Set(MNT_MODULUS(RSA_PUB_KEY_NMONT(pKey)),
  224. MNT_SIZE(RSA_PUB_KEY_NMONT(pKey)),
  225. pModulus);
  226. }
  227. if(pExp) {
  228. cpSize expLen = BITS_BNU_CHUNK(RSA_PUB_KEY_BITSIZE_E(pKey));
  229. FIX_BNU(RSA_PUB_KEY_E(pKey), expLen);
  230. pExp = (IppsBigNumState*)( IPP_ALIGNED_PTR(pExp, BN_ALIGNMENT) );
  231. IPP_BADARG_RET(!BN_VALID_ID(pExp), ippStsContextMatchErr);
  232. IPP_BADARG_RET(!RSA_PUB_KEY_IS_SET(pKey), ippStsIncompleteContextErr);
  233. IPP_BADARG_RET(BN_ROOM(pExp) < expLen, ippStsSizeErr);
  234. BN_Set(RSA_PUB_KEY_E(pKey), expLen, pExp);
  235. }
  236. return ippStsNoErr;
  237. }