pcphashsha256px.c 6.1 KB

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