pcpprimeginitca.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "owncp.h"
  32. #include "pcpprimeg.h"
  33. #include "pcptool.h"
  34. /*F*
  35. // Name: ippsPrimeGetSize
  36. //
  37. // Purpose: Returns size of Prime Number Generator context (bytes).
  38. //
  39. // Returns: Reason:
  40. // ippStsNullPtrErr NULL == pSize
  41. // ippStsLengthErr 1 > maxBits
  42. // ippStsNoErr no error
  43. //
  44. // Parameters:
  45. // maxBits max length of a prime number
  46. // pSize pointer to the size of internal context
  47. *F*/
  48. IPPFUN(IppStatus, ippsPrimeGetSize, (cpSize maxBits, cpSize* pSize))
  49. {
  50. IPP_BAD_PTR1_RET(pSize);
  51. IPP_BADARG_RET(maxBits<1, ippStsLengthErr);
  52. {
  53. cpSize len = BITS_BNU_CHUNK(maxBits);
  54. cpSize len32 = BITS2WORD32_SIZE(maxBits);
  55. cpSize montSize;
  56. ippsMontGetSize(ippBinaryMethod, len32, &montSize);
  57. *pSize = sizeof(IppsPrimeState)
  58. +len*sizeof(BNU_CHUNK_T)
  59. +len*sizeof(BNU_CHUNK_T)
  60. +len*sizeof(BNU_CHUNK_T)
  61. +len*sizeof(BNU_CHUNK_T)
  62. +montSize
  63. +PRIME_ALIGNMENT-1;
  64. return ippStsNoErr;
  65. }
  66. }
  67. /*F*
  68. // Name: ippsPrimeInit
  69. //
  70. // Purpose: Initializes Prime Number Generator context
  71. //
  72. // Returns: Reason:
  73. // ippStsNullPtrErr NULL == pCtx
  74. // ippStsLengthErr 1 > maxBits
  75. // ippStsNoErr no error
  76. //
  77. // Parameters:
  78. // maxBits max length of a prime number
  79. // pCtx pointer to the context to be initialized
  80. *F*/
  81. IPPFUN(IppStatus, ippsPrimeInit, (cpSize maxBits, IppsPrimeState* pCtx))
  82. {
  83. IPP_BAD_PTR1_RET(pCtx);
  84. IPP_BADARG_RET(maxBits<1, ippStsLengthErr);
  85. /* use aligned PRNG context */
  86. pCtx = (IppsPrimeState*)( IPP_ALIGNED_PTR(pCtx, PRIME_ALIGNMENT) );
  87. {
  88. Ipp8u* ptr = (Ipp8u*)pCtx;
  89. cpSize len = BITS_BNU_CHUNK(maxBits);
  90. cpSize len32 = BITS2WORD32_SIZE(maxBits);
  91. PRIME_ID(pCtx) = idCtxPrimeNumber;
  92. PRIME_MAXBITSIZE(pCtx) = maxBits;
  93. ptr += sizeof(IppsPrimeState);
  94. PRIME_NUMBER(pCtx) = (BNU_CHUNK_T*)ptr;
  95. ptr += len*sizeof(BNU_CHUNK_T);
  96. PRIME_TEMP1(pCtx) = (BNU_CHUNK_T*)ptr;
  97. ptr += len*sizeof(BNU_CHUNK_T);
  98. PRIME_TEMP2(pCtx) = (BNU_CHUNK_T*)ptr;
  99. ptr += len*sizeof(BNU_CHUNK_T);
  100. PRIME_TEMP3(pCtx) = (BNU_CHUNK_T*)ptr;
  101. ptr += len*sizeof(BNU_CHUNK_T);
  102. PRIME_MONT(pCtx) = (IppsMontState*)( IPP_ALIGNED_PTR((ptr), MONT_ALIGNMENT) );
  103. ippsMontInit(ippBinaryMethod, len32, PRIME_MONT(pCtx));
  104. return ippStsNoErr;
  105. }
  106. }