pcphash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(_PCP_HASH_H)
  32. #define _PCP_HASH_H
  33. /* messge block size */
  34. #define MBS_SHA1 (64) /* SHA1 message block size (bytes) */
  35. #define MBS_SHA256 (64) /* SHA256 and SHA224 */
  36. #define MBS_SHA224 (64) /* SHA224 */
  37. #define MBS_SHA512 (128) /* SHA512 and SHA384 */
  38. #define MBS_SHA384 (128) /* SHA384 */
  39. #define MBS_MD5 (64) /* MD5 */
  40. #define MBS_HASH_MAX (MBS_SHA512) /* max message block size (bytes) */
  41. #define MAX_HASH_SIZE (IPP_SHA512_DIGEST_BITSIZE/8) /* hash of the max len (bytes) */
  42. /*
  43. // Useful macros
  44. */
  45. #define SHS_ID(stt) ((stt)->idCtx)
  46. #define SHS_INDX(stt) ((stt)->index)
  47. #define SHS_LENL(stt) ((stt)->msgLenLo)
  48. #define SHS_LENH(stt) ((stt)->msgLenHi)
  49. #define SHS_BUFF(stt) ((stt)->msgBuffer)
  50. #define SHS_HASH(stt) ((stt)->msgHash)
  51. /* initial hash values */
  52. extern const Ipp32u SHA1_IV[];
  53. extern const Ipp32u SHA256_IV[];
  54. extern const Ipp32u SHA224_IV[];
  55. extern const Ipp64u SHA512_IV[];
  56. extern const Ipp64u SHA384_IV[];
  57. extern const Ipp32u MD5_IV[];
  58. extern const Ipp64u SHA512_224_IV[];
  59. extern const Ipp64u SHA512_256_IV[];
  60. /* hash alg additive constants */
  61. extern __ALIGN16 const Ipp32u SHA1_cnt[];
  62. extern __ALIGN16 const Ipp32u SHA256_cnt[];
  63. extern __ALIGN16 const Ipp64u SHA512_cnt[];
  64. extern __ALIGN16 const Ipp32u MD5_cnt[];
  65. /* hash alg attributes */
  66. typedef struct _cpHashAttr {
  67. int ivSize; /* attr: length (bytes) of initial value cpHashIV */
  68. int hashSize; /* attr: length (bytes) of hash */
  69. int msgBlkSize; /* attr: length (bytes) of message block */
  70. int msgLenRepSize; /* attr: length (bytes) in representation of processed message length */
  71. Ipp64u msgLenMax[2]; /* attr: max message length (bytes) (low high) */
  72. } cpHashAttr;
  73. /* hash value */
  74. typedef Ipp64u cpHash[IPP_SHA512_DIGEST_BITSIZE/BITSIZE(Ipp64u)]; /* hash value */
  75. /* hash update function */
  76. typedef void (*cpHashProc)(void* pHash, const Ipp8u* pMsg, int msgLen, const void* pParam);
  77. /* hash context */
  78. struct _cpHashCtx {
  79. IppCtxId idCtx; /* hash identifier */
  80. IppHashAlgId algID; /* hash algorithm ID */
  81. Ipp64u msgLenLo; /* length (bytes) of processed message: */
  82. Ipp64u msgLenHi; /* low and high parts */
  83. cpHashProc hashProc; /* hash update function */
  84. const void* pParam; /* optional hashProc's parameter */
  85. cpHash hashVal; /* intermadiate has value */
  86. int buffOffset; /* current buffer position */
  87. Ipp8u msgBuffer[MBS_HASH_MAX]; /* buffer */
  88. };
  89. /* accessors */
  90. #define HASH_CTX_ID(stt) ((stt)->idCtx)
  91. #define HASH_ALG_ID(stt) ((stt)->algID)
  92. #define HASH_LENLO(stt) ((stt)->msgLenLo)
  93. #define HASH_LENHI(stt) ((stt)->msgLenHi)
  94. #define HASH_FUNC(stt) ((stt)->hashProc)
  95. #define HASH_FUNC_PAR(stt) ((stt)->pParam)
  96. #define HASH_VALUE(stt) ((stt)->hashVal)
  97. #define HAHS_BUFFIDX(stt) ((stt)->buffOffset)
  98. #define HASH_BUFF(stt) ((stt)->msgBuffer)
  99. #define HASH_VALID_ID(pCtx) (HASH_CTX_ID((pCtx))==idCtxHash)
  100. /* hash alg opt argument */
  101. extern const void* cpHashProcFuncOpt[];
  102. /* enabled hash alg */
  103. extern const IppHashAlgId cpEnabledHashAlgID[];
  104. /* hash alg IV (init value) */
  105. extern const Ipp8u* cpHashIV[];
  106. /* hash alg attribute DB */
  107. extern const cpHashAttr cpHashAlgAttr[];
  108. /* IV size helper */
  109. __INLINE int cpHashIvSize(IppHashAlgId algID)
  110. { return cpHashAlgAttr[algID].ivSize; }
  111. /* hash size helper */
  112. __INLINE int cpHashSize(IppHashAlgId algID)
  113. { return cpHashAlgAttr[algID].hashSize; }
  114. /* message block size helper */
  115. __INLINE int cpHashMBS(IppHashAlgId algID)
  116. { return cpHashAlgAttr[algID].msgBlkSize; }
  117. /* maps algID into enabled IppHashAlgId value */
  118. __INLINE IppHashAlgId cpValidHashAlg(IppHashAlgId algID)
  119. {
  120. /* maps algID into the valid range */
  121. algID = (((int)ippHashAlg_Unknown < (int)algID) && ((int)algID < (int)ippHashAlg_MaxNo))? algID : ippHashAlg_Unknown;
  122. return cpEnabledHashAlgID[algID];
  123. }
  124. /* processing functions */
  125. void UpdateSHA1 (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
  126. void UpdateSHA256(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
  127. void UpdateSHA512(void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
  128. void UpdateMD5 (void* pHash, const Ipp8u* mblk, int mlen, const void* pParam);
  129. /* general methods */
  130. int cpReInitHash(IppsHashState* pCtx, IppHashAlgId algID);
  131. #endif /* _PCP_HASH_H */