pcpprnginitca.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "pcpprng.h"
  35. #include "pcphash.h"
  36. #include "pcptool.h"
  37. /*F*
  38. // Name: ippsPRNGGetSize
  39. //
  40. // Purpose: Returns size of PRNG context (bytes).
  41. //
  42. // Returns: Reason:
  43. // ippStsNullPtrErr NULL == pSize
  44. //
  45. // ippStsNoErr no error
  46. //
  47. // Parameters:
  48. // pSize pointer to the size of internal context
  49. *F*/
  50. IPPFUN(IppStatus, ippsPRNGGetSize, (int* pSize))
  51. {
  52. IPP_BAD_PTR1_RET(pSize);
  53. *pSize = sizeof(IppsPRNGState)
  54. +PRNG_ALIGNMENT-1;
  55. return ippStsNoErr;
  56. }
  57. /*F*
  58. // Name: ippsPRNGInit
  59. //
  60. // Purpose: Initializes PRNG context
  61. //
  62. // Returns: Reason:
  63. // ippStsNullPtrErr NULL == pRnd
  64. //
  65. // ippStsLengthErr seedBits < 1
  66. // seedBits < MAX_XKEY_SIZE
  67. // seedBits%8 !=0
  68. //
  69. // ippStsNoErr no error
  70. //
  71. // Parameters:
  72. // seedBits seed bitsize
  73. // pRnd pointer to the context to be initialized
  74. *F*/
  75. IPPFUN(IppStatus, ippsPRNGInit, (int seedBits, IppsPRNGState* pRnd))
  76. {
  77. /* test PRNG context */
  78. IPP_BAD_PTR1_RET(pRnd);
  79. pRnd = (IppsPRNGState*)( IPP_ALIGNED_PTR(pRnd, PRNG_ALIGNMENT) );
  80. /* test sizes */
  81. IPP_BADARG_RET((1>seedBits) || (seedBits>MAX_XKEY_SIZE) ||(seedBits&7), ippStsLengthErr);
  82. {
  83. int hashIvSize = cpHashIvSize(ippHashAlg_SHA1);
  84. const Ipp8u* iv = cpHashIV[ippHashAlg_SHA1];
  85. /* cleanup context */
  86. ZEXPAND_BNU((Ipp8u*)pRnd, 0, (cpSize)(sizeof(IppsPRNGState)));
  87. RAND_ID(pRnd) = idCtxPRNG;
  88. RAND_SEEDBITS(pRnd) = seedBits;
  89. /* default Q parameter */
  90. ((Ipp32u*)RAND_Q(pRnd))[0] = 0xFFFFFFFF;
  91. ((Ipp32u*)RAND_Q(pRnd))[1] = 0xFFFFFFFF;
  92. ((Ipp32u*)RAND_Q(pRnd))[2] = 0xFFFFFFFF;
  93. ((Ipp32u*)RAND_Q(pRnd))[3] = 0xFFFFFFFF;
  94. ((Ipp32u*)RAND_Q(pRnd))[4] = 0xFFFFFFFF;
  95. /* default T parameter */
  96. CopyBlock(iv, RAND_T(pRnd), hashIvSize);
  97. return ippStsNoErr;
  98. }
  99. }