pcphashsha512px.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /*
  36. // SHA512 Specific Macros (reference proposal 256-384-512)
  37. //
  38. // Note: All operations act on DWORDs (64-bits)
  39. */
  40. #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  41. #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  42. #define SUM0(x) (ROR64((x),28) ^ ROR64((x),34) ^ ROR64((x),39))
  43. #define SUM1(x) (ROR64((x),14) ^ ROR64((x),18) ^ ROR64((x),41))
  44. #define SIG0(x) (ROR64((x), 1) ^ ROR64((x), 8) ^ LSR64((x), 7))
  45. #define SIG1(x) (ROR64((x),19) ^ ROR64((x),61) ^ LSR64((x), 6))
  46. #define SHA512_UPDATE(i) \
  47. wdat[i&15] += SIG1(wdat[(i+14)&15]) + wdat[(i+9)&15] + SIG0(wdat[(i+1)&15])
  48. #define SHA512_STEP(i,j) \
  49. v[(7-i)&7] += (j ? SHA512_UPDATE(i) : wdat[i&15]) \
  50. + SHA512_cnt_loc[i+j] \
  51. + SUM1(v[(4-i)&7]) \
  52. + CH(v[(4-i)&7], v[(5-i)&7], v[(6-i)&7]); \
  53. v[(3-i)&7] += v[(7-i)&7]; \
  54. v[(7-i)&7] += SUM0(v[(0-i)&7]) + MAJ(v[(0-i)&7], v[(1-i)&7], v[(2-i)&7])
  55. #define COMPACT_SHA512_STEP(A,B,C,D,E,F,G,H, W,K, r) { \
  56. Ipp64u _T1 = (H) + SUM1((E)) + CH((E),(F),(G)) + (W)[(r)] + (K)[(r)]; \
  57. Ipp64u _T2 = SUM0((A)) + MAJ((A),(B),(C)); \
  58. (H) = (G); \
  59. (G) = (F); \
  60. (F) = (E); \
  61. (E) = (D)+_T1; \
  62. (D) = (C); \
  63. (C) = (B); \
  64. (B) = (A); \
  65. (A) = _T1+_T2; \
  66. }
  67. /*F*
  68. // Name: UpdateSHA512
  69. //
  70. // Purpose: Update internal hash according to input message stream.
  71. //
  72. // Parameters:
  73. // uniHash pointer to in/out hash
  74. // mblk pointer to message stream
  75. // mlen message stream length (multiple by message block size)
  76. // uniParam pointer to the optional parameter
  77. //
  78. *F*/
  79. #if defined(_ALG_SHA512_COMPACT_)
  80. #pragma message("SHA512 compact")
  81. void UpdateSHA512(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniPraram)
  82. {
  83. Ipp32u* data = (Ipp32u*)mblk;
  84. Ipp64u* digest = (Ipp64u*)uniHash;
  85. Ipp64u* SHA512_cnt_loc = (Ipp64u*)uniPraram;
  86. for(; mlen>=MBS_SHA512; data += MBS_SHA512/sizeof(Ipp32u), mlen -= MBS_SHA512) {
  87. int t;
  88. Ipp64u W[80];
  89. /*
  90. // expand message block
  91. */
  92. /* initialize the first 16 words in the array W (remember about endian) */
  93. for(t=0; t<16; t++) {
  94. Ipp32u hiX = data[2*t];
  95. Ipp32u loX = data[2*t+1];
  96. #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
  97. W[t] = MAKEDWORD(loX, hiX);
  98. #else
  99. W[t] = MAKEDWORD( ENDIANNESS(loX), ENDIANNESS(hiX) );
  100. #endif
  101. }
  102. for(; t<80; t++)
  103. W[t] = SIG1(W[t-2]) + W[t-7] + SIG0(W[t-15]) + W[t-16];
  104. /*
  105. // update hash
  106. */
  107. {
  108. /* init A, B, C, D, E, F, G, H by the input hash */
  109. Ipp64u A = digest[0];
  110. Ipp64u B = digest[1];
  111. Ipp64u C = digest[2];
  112. Ipp64u D = digest[3];
  113. Ipp64u E = digest[4];
  114. Ipp64u F = digest[5];
  115. Ipp64u G = digest[6];
  116. Ipp64u H = digest[7];
  117. for(t=0; t<80; t++)
  118. COMPACT_SHA512_STEP(A,B,C,D,E,F,G,H, W,SHA512_cnt_loc, t);
  119. /* update hash*/
  120. digest[0] += A;
  121. digest[1] += B;
  122. digest[2] += C;
  123. digest[3] += D;
  124. digest[4] += E;
  125. digest[5] += F;
  126. digest[6] += G;
  127. digest[7] += H;
  128. }
  129. }
  130. }
  131. #else
  132. void UpdateSHA512(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniPraram)
  133. {
  134. Ipp32u* data = (Ipp32u*)mblk;
  135. Ipp64u* digest = (Ipp64u*)uniHash;
  136. Ipp64u* SHA512_cnt_loc = (Ipp64u*)uniPraram;
  137. for(; mlen>=MBS_SHA512; data += MBS_SHA512/sizeof(Ipp32u), mlen -= MBS_SHA512) {
  138. Ipp64u wdat[16];
  139. int j;
  140. Ipp64u v[8];
  141. /* initialize the first 16 words in the array W (remember about endian) */
  142. for(j=0; j<16; j++) {
  143. Ipp32u hiX = data[2*j];
  144. Ipp32u loX = data[2*j+1];
  145. #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
  146. wdat[j] = MAKEDWORD(loX, hiX);
  147. #else
  148. wdat[j] = MAKEDWORD( ENDIANNESS(loX), ENDIANNESS(hiX) );
  149. #endif
  150. }
  151. /* copy digest */
  152. CopyBlock(digest, v, IPP_SHA512_DIGEST_BITSIZE/BYTESIZE);
  153. for(j=0; j<80; j+=16) {
  154. SHA512_STEP( 0, j);
  155. SHA512_STEP( 1, j);
  156. SHA512_STEP( 2, j);
  157. SHA512_STEP( 3, j);
  158. SHA512_STEP( 4, j);
  159. SHA512_STEP( 5, j);
  160. SHA512_STEP( 6, j);
  161. SHA512_STEP( 7, j);
  162. SHA512_STEP( 8, j);
  163. SHA512_STEP( 9, j);
  164. SHA512_STEP(10, j);
  165. SHA512_STEP(11, j);
  166. SHA512_STEP(12, j);
  167. SHA512_STEP(13, j);
  168. SHA512_STEP(14, j);
  169. SHA512_STEP(15, j);
  170. }
  171. /* update digest */
  172. digest[0] += v[0];
  173. digest[1] += v[1];
  174. digest[2] += v[2];
  175. digest[3] += v[3];
  176. digest[4] += v[4];
  177. digest[5] += v[5];
  178. digest[6] += v[6];
  179. digest[7] += v[7];
  180. }
  181. }
  182. #endif