pcpshsmgfca.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "pcphash.h"
  34. #include "pcptool.h"
  35. /*F*
  36. // Name: ippsMGF_SHA1
  37. // ippsMGF_SHA224
  38. // ippsMGF_SHA256
  39. // ippsMGF_SHA384
  40. // ippsMGF_SHA512
  41. // ippsMGF_MD5
  42. //
  43. // Purpose: Mask Generation Functios.
  44. //
  45. // Returns: Reason:
  46. // ippStsNullPtrErr pMask == NULL
  47. // ippStsLengthErr seedLen <0
  48. // maskLen <0
  49. // ippStsNotSupportedModeErr if algID is not match to supported hash alg
  50. // ippStsNoErr no errors
  51. //
  52. // Parameters:
  53. // pSeed pointer to the input stream
  54. // seedLen input stream length (bytes)
  55. // pMaske pointer to the ouput mask
  56. // maskLen desired length of mask (bytes)
  57. //
  58. *F*/
  59. IPPFUN(IppStatus, ippsMGF,(const Ipp8u* pSeed, int seedLen, Ipp8u* pMask, int maskLen, IppHashAlgId hashAlg))
  60. {
  61. /* get algorithm id */
  62. hashAlg = cpValidHashAlg(hashAlg);
  63. /* test hash alg */
  64. IPP_BADARG_RET(ippHashAlg_Unknown==hashAlg, ippStsNotSupportedModeErr);
  65. IPP_BAD_PTR1_RET(pMask);
  66. IPP_BADARG_RET((seedLen<0)||(maskLen<0), ippStsLengthErr);
  67. {
  68. /* hash specific */
  69. int hashSize = cpHashSize(hashAlg);
  70. int i, outLen;
  71. IppsHashState hashCtx;
  72. ippsHashInit(&hashCtx, hashAlg);
  73. if(!pSeed)
  74. seedLen = 0;
  75. for(i=0,outLen=0; outLen<maskLen; i++) {
  76. Ipp8u cnt[4];
  77. cnt[0] = (Ipp8u)((i>>24) & 0xFF);
  78. cnt[1] = (Ipp8u)((i>>16) & 0xFF);
  79. cnt[2] = (Ipp8u)((i>>8) & 0xFF);
  80. cnt[3] = (Ipp8u)(i & 0xFF);
  81. cpReInitHash(&hashCtx, hashAlg);
  82. ippsHashUpdate(pSeed, seedLen, &hashCtx);
  83. ippsHashUpdate(cnt, 4, &hashCtx);
  84. if((outLen + hashSize) <= maskLen) {
  85. ippsHashFinal(pMask+outLen, &hashCtx);
  86. outLen += hashSize;
  87. }
  88. else {
  89. Ipp8u md[BITS2WORD8_SIZE(IPP_SHA512_DIGEST_BITSIZE)];
  90. ippsHashFinal(md, &hashCtx);
  91. CopyBlock(md, pMask+outLen, maskLen-outLen);
  92. outLen = maskLen;
  93. }
  94. }
  95. return ippStsNoErr;
  96. }
  97. }