pcpprngenca.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "pcphash.h"
  35. #include "pcpprng.h"
  36. #include "pcptool.h"
  37. /*
  38. // G() function based on SHA1
  39. //
  40. // Parameters:
  41. // T 160 bit parameter
  42. // pHexStr input hex string
  43. // hexStrLen size of hex string (Ipp8u segnments)
  44. // xBNU 160 bit BNU result
  45. //
  46. // Note 1:
  47. // must to be hexStrLen <= 64 (512 bits)
  48. */
  49. static
  50. void SHA1_G(Ipp32u* xBNU, const Ipp32u* T, Ipp8u* pHexStr, int hexStrLen)
  51. {
  52. /* select processing function */
  53. cpHashProc updateFunc = UpdateSHA1;
  54. /* pad HexString zeros */
  55. PaddBlock(0, pHexStr+hexStrLen, BITS2WORD8_SIZE(MAX_XKEY_SIZE)-hexStrLen);
  56. /* reset initial HASH value */
  57. xBNU[0] = T[0];
  58. xBNU[1] = T[1];
  59. xBNU[2] = T[2];
  60. xBNU[3] = T[3];
  61. xBNU[4] = T[4];
  62. /* SHA1 */
  63. //UpdateSHA1(xBNU, pHexStr, BITS2WORD8_SIZE(MAX_XKEY_SIZE), SHA1_cnt);
  64. updateFunc(xBNU, pHexStr, BITS2WORD8_SIZE(MAX_XKEY_SIZE), SHA1_cnt);
  65. /* swap back */
  66. SWAP(xBNU[0],xBNU[4]);
  67. SWAP(xBNU[1],xBNU[3]);
  68. }
  69. /*
  70. // Returns bitsize of the bitstring has beed added
  71. */
  72. int cpPRNGen(Ipp32u* pRand, cpSize nBits, IppsPRNGState* pRnd)
  73. {
  74. BNU_CHUNK_T Xj [BITS_BNU_CHUNK(MAX_XKEY_SIZE)];
  75. BNU_CHUNK_T XVAL[BITS_BNU_CHUNK(MAX_XKEY_SIZE)];
  76. Ipp8u TXVAL[BITS2WORD8_SIZE(MAX_XKEY_SIZE)];
  77. /* XKEY length in BNU_CHUNK_T */
  78. cpSize xKeyLen = BITS_BNU_CHUNK(RAND_SEEDBITS(pRnd));
  79. /* XKEY length in bytes */
  80. cpSize xKeySize= BITS2WORD8_SIZE(RAND_SEEDBITS(pRnd));
  81. /* XKEY word's mask */
  82. BNU_CHUNK_T xKeyMsk = MASK_BNU_CHUNK(RAND_SEEDBITS(pRnd));
  83. /* number of Ipp32u chunks to be generated */
  84. cpSize genlen = BITS2WORD32_SIZE(nBits);
  85. ZEXPAND_BNU(Xj, 0, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
  86. ZEXPAND_BNU(XVAL, 0, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
  87. while(genlen) {
  88. cpSize len;
  89. /* Step 1: XVAL=(Xkey+Xseed) mod 2^b */
  90. BNU_CHUNK_T carry = cpAdd_BNU(XVAL, RAND_XKEY(pRnd), RAND_XAUGMENT(pRnd), xKeyLen);
  91. XVAL[xKeyLen-1] &= xKeyMsk;
  92. /* Step 2: xj=G(t, XVAL) mod Q */
  93. cpToOctStr_BNU(TXVAL, xKeySize, XVAL, xKeyLen);
  94. SHA1_G((Ipp32u*)Xj, (Ipp32u*)RAND_T(pRnd), TXVAL, xKeySize);
  95. {
  96. cpSize sizeXj = BITS_BNU_CHUNK(160);
  97. if(0 <= cpCmp_BNU(Xj, BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE), RAND_Q(pRnd),BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE)) )
  98. sizeXj = cpMod_BNU(Xj, BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE), RAND_Q(pRnd), BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE));
  99. FIX_BNU(Xj, sizeXj);
  100. ZEXPAND_BNU(Xj, sizeXj, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
  101. }
  102. /* Step 3: Xkey=(1+Xkey+Xj) mod 2^b */
  103. cpInc_BNU(RAND_XKEY(pRnd), RAND_XKEY(pRnd), xKeyLen, 1);
  104. carry = cpAdd_BNU(RAND_XKEY(pRnd), RAND_XKEY(pRnd), Xj, xKeyLen);
  105. RAND_XKEY(pRnd)[xKeyLen-1] &= xKeyMsk;
  106. /* fill out result */
  107. len = genlen<BITS2WORD32_SIZE(IPP_SHA1_DIGEST_BITSIZE)? genlen : BITS2WORD32_SIZE(IPP_SHA1_DIGEST_BITSIZE);
  108. COPY_BNU(pRand, (Ipp32u*)Xj, len);
  109. pRand += len;
  110. genlen -= len;
  111. }
  112. return nBits;
  113. }
  114. /*F*
  115. // Name: ippsPRNGen
  116. //
  117. // Purpose: Generates a pseudorandom bit sequence of the specified nBits length.
  118. //
  119. // Returns: Reason:
  120. // ippStsNullPtrErr NULL == pRnd
  121. // NULL == pBuffer
  122. //
  123. // ippStsContextMatchErr illegal pRnd->idCtx
  124. //
  125. // ippStsLengthErr 1 > nBits
  126. //
  127. // ippStsNoErr no error
  128. //
  129. // Parameters:
  130. // pBuffer pointer to the buffer
  131. // nBits number of bits be requested
  132. // pRndCtx pointer to the context
  133. *F*/
  134. IPPFUN(IppStatus, ippsPRNGen,(Ipp32u* pBuffer, cpSize nBits, void* pRnd))
  135. {
  136. IppsPRNGState* pRndCtx = (IppsPRNGState*)pRnd;
  137. /* test PRNG context */
  138. IPP_BAD_PTR2_RET(pBuffer, pRnd);
  139. pRndCtx = (IppsPRNGState*)( IPP_ALIGNED_PTR(pRndCtx, PRNG_ALIGNMENT) );
  140. IPP_BADARG_RET(!RAND_VALID_ID(pRndCtx), ippStsContextMatchErr);
  141. /* test sizes */
  142. IPP_BADARG_RET(nBits< 1, ippStsLengthErr);
  143. {
  144. cpSize rndSize = BITS2WORD32_SIZE(nBits);
  145. Ipp32u rndMask = MAKEMASK32(nBits);
  146. cpPRNGen(pBuffer, nBits, pRndCtx);
  147. pBuffer[rndSize-1] &= rndMask;
  148. return ippStsNoErr;
  149. }
  150. }