pcpmontgomery.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #if !defined(_CP_MONTGOMETRY_H)
  32. #define _CP_MONTGOMETRY_H
  33. /*
  34. // Montgomery spec structure
  35. */
  36. struct _cpMontgomery
  37. {
  38. IppCtxId idCtx; /* Montgomery spec identifier */
  39. cpSize maxLen; /* maximum length of modulus being stored */
  40. cpSize modLen; /* length of modulus (and R = b^modLen) */
  41. BNU_CHUNK_T m0; /* low word of (1/modulus) mod R */
  42. BNU_CHUNK_T* pModulus; /* modulus (of modLen BNU_CHUNK_T size) */
  43. BNU_CHUNK_T* pIdentity; /* mont_enc(1) */
  44. BNU_CHUNK_T* pSquare; /* mont_enc(R^2) */
  45. BNU_CHUNK_T* pCube; /* mont_enc(R^3) */
  46. BNU_CHUNK_T* pTBuffer; /* internal buffer modLen BNU_CHUNK_T */
  47. BNU_CHUNK_T* pSBuffer; /* internal buffer modLen BNU_CHUNK_T */
  48. BNU_CHUNK_T* pProduct; /* internal product (2*modLen BNU_CHUNK_T)*/
  49. BNU_CHUNK_T* pKBuffer; /* mul/sqr buffer (Karatsuba method used) */
  50. };
  51. /* accessory macros */
  52. #define MNT_ID(eng) ((eng)->idCtx)
  53. #define MNT_ROOM(eng) ((eng)->maxLen)
  54. #define MNT_SIZE(eng) ((eng)->modLen)
  55. #define MNT_HELPER(eng) ((eng)->m0)
  56. #define MNT_MODULUS(eng) ((eng)->pModulus)
  57. #define MNT_1(eng) ((eng)->pIdentity)
  58. #define MNT_IDENT_R(eng) (MNT_1((eng)))
  59. #define MNT_SQUARE_R(eng) ((eng)->pSquare)
  60. #define MNT_CUBE_R(eng) ((eng)->pCube)
  61. #define MNT_TBUFFER(eng) ((eng)->pTBuffer)
  62. #define MNT_SBUFFER(eng) ((eng)->pSBuffer)
  63. #define MNT_PRODUCT(eng) ((eng)->pProduct)
  64. #define MNT_KBUFFER(eng) ((eng)->pKBuffer)
  65. #define MNT_VALID_ID(eng) (MNT_ID((eng))==idCtxMontgomery)
  66. /* default methos */
  67. #define EXPONENT_METHOD (ippBinaryMethod)
  68. /* alignment */
  69. #define MONT_ALIGNMENT ((int)(sizeof(void*)))
  70. /*
  71. // Pacp/unpack Montgomery context
  72. */
  73. void cpPackMontCtx(const IppsMontState* pCtx, Ipp8u* pBuffer);
  74. void cpUnpackMontCtx(const Ipp8u* pBuffer, IppsMontState* pCtx);
  75. /*
  76. // Montgomery reduction, multiplication and squaring
  77. */
  78. void cpMontRedAdc_BNU(BNU_CHUNK_T* pR,
  79. BNU_CHUNK_T* pProduct,
  80. const BNU_CHUNK_T* pModulus, cpSize nsM, BNU_CHUNK_T m0);
  81. __INLINE void cpMontRed_BNU(BNU_CHUNK_T* pR,
  82. BNU_CHUNK_T* pProduct,
  83. const BNU_CHUNK_T* pModulus, cpSize nsM, BNU_CHUNK_T m0)
  84. {
  85. cpMontRedAdc_BNU(pR, pProduct, pModulus, nsM, m0);
  86. }
  87. __INLINE void cpMontMul_BNU(BNU_CHUNK_T* pR,
  88. const BNU_CHUNK_T* pX, cpSize nsX,
  89. const BNU_CHUNK_T* pY, cpSize nsY,
  90. const BNU_CHUNK_T* pModulus, cpSize nsM, BNU_CHUNK_T m0,
  91. BNU_CHUNK_T* pProduct, BNU_CHUNK_T* pKBuffer)
  92. {
  93. cpMul_BNU(pProduct, pX,nsX, pY,nsY, pKBuffer);
  94. ZEXPAND_BNU(pProduct,nsX+nsY, 2*nsM);
  95. cpMontRed_BNU(pR, pProduct, pModulus, nsM, m0);
  96. }
  97. __INLINE void cpMontSqr_BNU(BNU_CHUNK_T* pR,
  98. const BNU_CHUNK_T* pX, cpSize nsX,
  99. const BNU_CHUNK_T* pModulus, cpSize nsM, BNU_CHUNK_T m0,
  100. BNU_CHUNK_T* pProduct, BNU_CHUNK_T* pKBuffer)
  101. {
  102. cpSqr_BNU(pProduct, pX,nsX, pKBuffer);
  103. ZEXPAND_BNU(pProduct, 2*nsX, 2*nsM);
  104. cpMontRed_BNU(pR, pProduct, pModulus, nsM, m0);
  105. }
  106. /*
  107. // Montgomery encoding/decoding
  108. */
  109. __INLINE cpSize cpMontEnc_BNU(BNU_CHUNK_T* pR,
  110. const BNU_CHUNK_T* pXreg, cpSize nsX,
  111. IppsMontState* pMont)
  112. {
  113. cpSize nsM = MNT_SIZE(pMont);
  114. cpMontMul_BNU(pR,
  115. pXreg, nsX, MNT_SQUARE_R(pMont), nsM,
  116. MNT_MODULUS(pMont), nsM, MNT_HELPER(pMont),
  117. MNT_PRODUCT(pMont), MNT_KBUFFER(pMont));
  118. FIX_BNU(pR, nsM);
  119. return nsM;
  120. }
  121. __INLINE cpSize cpMontDec_BNU(BNU_CHUNK_T* pR,
  122. const BNU_CHUNK_T* pXmont, cpSize nsX,
  123. IppsMontState* pMont)
  124. {
  125. cpSize nsM = MNT_SIZE(pMont);
  126. ZEXPAND_COPY_BNU(MNT_PRODUCT(pMont), 2*nsM, pXmont, nsX);
  127. cpMontRed_BNU(pR, MNT_PRODUCT(pMont), MNT_MODULUS(pMont), nsM, MNT_HELPER(pMont));
  128. FIX_BNU(pR, nsM);
  129. return nsM;
  130. }
  131. __INLINE void cpMontEnc_BN(IppsBigNumState* pRbn,
  132. const IppsBigNumState* pXbn,
  133. IppsMontState* pMont)
  134. {
  135. BNU_CHUNK_T* pR = BN_NUMBER(pRbn);
  136. cpSize nsM = MNT_SIZE(pMont);
  137. cpMontMul_BNU(pR,
  138. BN_NUMBER(pXbn), BN_SIZE(pXbn),
  139. MNT_SQUARE_R(pMont), nsM,
  140. MNT_MODULUS(pMont), nsM, MNT_HELPER(pMont),
  141. MNT_PRODUCT(pMont), MNT_KBUFFER(pMont));
  142. FIX_BNU(pR, nsM);
  143. BN_SIZE(pRbn) = nsM;
  144. BN_SIGN(pRbn) = ippBigNumPOS;
  145. }
  146. __INLINE void cpMontDec_BN(IppsBigNumState* pRbn,
  147. const IppsBigNumState* pXbn,
  148. IppsMontState* pMont)
  149. {
  150. BNU_CHUNK_T* pR = BN_NUMBER(pRbn);
  151. cpSize nsM = MNT_SIZE(pMont);
  152. ZEXPAND_COPY_BNU(MNT_PRODUCT(pMont), 2*nsM, BN_NUMBER(pXbn), BN_SIZE(pXbn));
  153. cpMontRed_BNU(pR, MNT_PRODUCT(pMont), MNT_MODULUS(pMont), nsM, MNT_HELPER(pMont));
  154. FIX_BNU(pR, nsM);
  155. BN_SIZE(pRbn) = nsM;
  156. BN_SIGN(pRbn) = ippBigNumPOS;
  157. }
  158. /*
  159. // Montgomery exponentiation (binary)
  160. */
  161. cpSize cpMontExpBin_BNU(BNU_CHUNK_T* pY,
  162. const BNU_CHUNK_T* pX, cpSize nsX,
  163. const BNU_CHUNK_T* pE, cpSize nsE,
  164. IppsMontState* pMont);
  165. #endif /* _CP_MONTGOMETRY_H */