pcpeccpinitca.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "pcpeccp.h"
  34. #include "pcpeccppoint.h"
  35. #include "pcpbnresource.h"
  36. #include "pcpeccpmethod.h"
  37. #include "pcpeccpsscm.h"
  38. #include "pcptool.h"
  39. /*F*
  40. // Name: ippsECCPGetSize
  41. //
  42. // Purpose: Returns size of ECC context (bytes).
  43. //
  44. // Returns: Reason:
  45. // ippStsNullPtrErr NULL == pSize
  46. //
  47. // ippStsSizeErr 2>feBitSize
  48. //
  49. // ippStsNoErr no errors
  50. //
  51. // Parameters:
  52. // feBitSize size of field element (bits)
  53. // pSize pointer to the size of internal ECC context
  54. //
  55. *F*/
  56. IPPFUN(IppStatus, ippsECCPGetSize, (int feBitSize, int *pSize))
  57. {
  58. /* test size's pointer */
  59. IPP_BAD_PTR1_RET(pSize);
  60. /* test size of field element */
  61. IPP_BADARG_RET((2>feBitSize || feBitSize>EC_GFP_MAXBITSIZE), ippStsSizeErr);
  62. {
  63. int bn1Size;
  64. int bn2Size;
  65. int pointSize;
  66. int mont1Size;
  67. int mont2Size;
  68. int primeSize;
  69. int listSize;
  70. /* size of field element */
  71. int gfeSize = BITS2WORD32_SIZE(feBitSize);
  72. /* size of order */
  73. int ordSize = BITS2WORD32_SIZE(feBitSize+1);
  74. /* size of sscm buffer */
  75. int w = cpECCP_OptimalWinSize(feBitSize+1);
  76. int nPrecomputed = 1<<w;
  77. int sscmBuffSize = nPrecomputed*(BITS_BNU_CHUNK(feBitSize)*3*sizeof(BNU_CHUNK_T)) +(CACHE_LINE_SIZE-1);
  78. /* size of BigNum over GF(p) */
  79. ippsBigNumGetSize(gfeSize, &bn1Size);
  80. /* size of BigNum over GF(r) */
  81. ippsBigNumGetSize(ordSize, &bn2Size);
  82. /* size of EC point over GF(p) */
  83. ippsECCPPointGetSize(feBitSize, &pointSize);
  84. /* size of montgomery engine over GF(p) */
  85. ippsMontGetSize(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize), &mont1Size);
  86. /* size of montgomery engine over GF(r) */
  87. ippsMontGetSize(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize+1), &mont2Size);
  88. /* size of prime engine */
  89. ippsPrimeGetSize(feBitSize+1, &primeSize);
  90. /* size of big num list (big num in the list preserve 32 bit word) */
  91. listSize = cpBigNumListGetSize(feBitSize+1, BNLISTSIZE);
  92. *pSize = sizeof(IppsECCPState)
  93. +sizeof(ECCP_METHOD) /* methods */
  94. +bn1Size /* prime */
  95. +bn1Size /* A */
  96. +bn1Size /* B */
  97. +bn1Size /* GX */
  98. +bn1Size /* GY */
  99. +bn2Size /* order */
  100. +bn1Size /* Aenc */
  101. +bn1Size /* Benc */
  102. +mont1Size /* montgomery(p) */
  103. +pointSize /* Genc */
  104. +bn2Size /* cofactor */
  105. +mont2Size /* montgomery(r) */
  106. +bn2Size /* private */
  107. +pointSize /* public */
  108. +bn2Size /* eph private */
  109. +pointSize /* eph public */
  110. +primeSize /* prime engine */
  111. +sscmBuffSize /* sscm buffer */
  112. +listSize /* temp big num */
  113. +(ALIGN_VAL-1);
  114. }
  115. return ippStsNoErr;
  116. }
  117. /*F*
  118. // Name: ippsECCPInit
  119. //
  120. // Purpose: Init ECC context.
  121. //
  122. // Returns: Reason:
  123. // ippStsNullPtrErr NULL == pECC
  124. //
  125. // ippStsSizeErr 2>feBitSize
  126. //
  127. // ippStsNoErr no errors
  128. //
  129. // Parameters:
  130. // feBitSize size of field element (bits)
  131. // pECC pointer to the ECC context
  132. //
  133. *F*/
  134. IPPFUN(IppStatus, ippsECCPInit, (int feBitSize, IppsECCPState* pECC))
  135. {
  136. /* test pECC pointer */
  137. IPP_BAD_PTR1_RET(pECC);
  138. /* use aligned EC context */
  139. pECC = (IppsECCPState*)( IPP_ALIGNED_PTR(pECC, ALIGN_VAL) );
  140. /* test size of field element */
  141. IPP_BADARG_RET((2>feBitSize || feBitSize>EC_GFP_MAXBITSIZE), ippStsSizeErr);
  142. /* clear context */
  143. PaddBlock(0, pECC, sizeof(IppsECCPState));
  144. /* context ID */
  145. ECP_ID(pECC) = idCtxECCP;
  146. /* generic EC */
  147. ECP_TYPE(pECC) = IppECCArbitrary;
  148. /* size of field element & BP order */
  149. ECP_GFEBITS(pECC) = feBitSize;
  150. ECP_ORDBITS(pECC) = feBitSize+1;
  151. /*
  152. // init other context fields
  153. */
  154. {
  155. int bn1Size;
  156. int bn2Size;
  157. int pointSize;
  158. int mont1Size;
  159. int mont2Size;
  160. int primeSize;
  161. /* size of field element */
  162. int gfeSize = BITS2WORD32_SIZE(feBitSize);
  163. /* size of order */
  164. int ordSize = BITS2WORD32_SIZE(feBitSize+1);
  165. /* size of sscm buffer */
  166. int w = cpECCP_OptimalWinSize(feBitSize+1);
  167. int nPrecomputed = 1<<w;
  168. int sscmBuffSize = nPrecomputed*(BITS_BNU_CHUNK(feBitSize)*3*sizeof(BNU_CHUNK_T)) +(CACHE_LINE_SIZE-1);
  169. Ipp8u* ptr = (Ipp8u*)pECC;
  170. /* size of BigNum over GF(p) */
  171. ippsBigNumGetSize(gfeSize, &bn1Size);
  172. /* size of BigNum over GF(r) */
  173. ippsBigNumGetSize(ordSize, &bn2Size);
  174. /* size of EC point over GF(p) */
  175. ippsECCPPointGetSize(feBitSize, &pointSize);
  176. /* size of montgomery engine over GF(p) */
  177. ippsMontGetSize(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize), &mont1Size);
  178. /* size of montgomery engine over GF(r) */
  179. ippsMontGetSize(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize+1), &mont2Size);
  180. /* size of prime engine */
  181. ippsPrimeGetSize(feBitSize+1, &primeSize);
  182. /* size of big num list */
  183. /* listSize = cpBigNumListGetSize(feBitSize+1+32, BNLISTSIZE); */
  184. /* allocate buffers */
  185. ptr += sizeof(IppsECCPState);
  186. ECP_METHOD(pECC) = (ECCP_METHOD*) (ptr);
  187. ptr += sizeof(ECCP_METHOD);
  188. ECP_PRIME(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  189. ptr += bn1Size;
  190. ECP_A(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  191. ptr += bn1Size;
  192. ECP_B(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  193. ptr += bn1Size;
  194. ECP_GX(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  195. ptr += bn1Size;
  196. ECP_GY(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  197. ptr += bn1Size;
  198. ECP_ORDER(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  199. ptr += bn2Size;
  200. ECP_AENC(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  201. ptr += bn1Size;
  202. ECP_BENC(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  203. ptr += bn1Size;
  204. ECP_PMONT(pECC) = (IppsMontState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  205. ptr += mont1Size;
  206. ECP_GENC(pECC) = (IppsECCPPointState*)( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  207. ptr += pointSize;
  208. ECP_COFACTOR(pECC)= (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  209. ptr += bn2Size;
  210. ECP_RMONT(pECC) = (IppsMontState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  211. ptr += mont2Size;
  212. ECP_PRIVATE(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  213. ptr += bn2Size;
  214. ECP_PUBLIC(pECC) = (IppsECCPPointState*)( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  215. ptr += pointSize;
  216. ECP_PRIVATE_E(pECC) = (IppsBigNumState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  217. ptr += bn2Size;
  218. ECP_PUBLIC_E(pECC) =(IppsECCPPointState*)( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  219. ptr += pointSize;
  220. ECP_PRIMARY(pECC) = (IppsPrimeState*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  221. ptr += primeSize;
  222. ECP_SCCMBUFF(pECC) = (Ipp8u*) ( IPP_ALIGNED_PTR(ptr,CACHE_LINE_SIZE) );
  223. ptr += sscmBuffSize;
  224. ECP_BNCTX(pECC) = (BigNumNode*) ( IPP_ALIGNED_PTR(ptr,ALIGN_VAL) );
  225. /* init buffers */
  226. ippsBigNumInit(gfeSize, ECP_PRIME(pECC));
  227. ippsBigNumInit(gfeSize, ECP_A(pECC));
  228. ippsBigNumInit(gfeSize, ECP_B(pECC));
  229. ippsBigNumInit(gfeSize, ECP_GX(pECC));
  230. ippsBigNumInit(gfeSize, ECP_GY(pECC));
  231. ippsBigNumInit(ordSize, ECP_ORDER(pECC));
  232. ippsBigNumInit(gfeSize, ECP_AENC(pECC));
  233. ippsBigNumInit(gfeSize, ECP_BENC(pECC));
  234. ippsMontInit(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize), ECP_PMONT(pECC));
  235. ippsECCPPointInit(feBitSize, ECP_GENC(pECC));
  236. ippsBigNumInit(ordSize, ECP_COFACTOR(pECC));
  237. ippsMontInit(ippBinaryMethod, BITS2WORD32_SIZE(feBitSize+1), ECP_RMONT(pECC));
  238. ippsBigNumInit(ordSize, ECP_PRIVATE(pECC));
  239. ippsECCPPointInit(feBitSize,ECP_PUBLIC(pECC));
  240. ippsBigNumInit(ordSize, ECP_PRIVATE_E(pECC));
  241. ippsECCPPointInit(feBitSize,ECP_PUBLIC_E(pECC));
  242. cpBigNumListInit(feBitSize+1, BNLISTSIZE, ECP_BNCTX(pECC));
  243. }
  244. return ippStsNoErr;
  245. }