aboutsummaryrefslogtreecommitdiff
path: root/ext/ipp/sources/ippcp/src/pcphashsha256px.c
blob: d0a733091f98f29fc8c24297edeed8d421d49440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*############################################################################
  # Copyright 1999-2018 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/* 
// 
//  Purpose:
//     Cryptography Primitive.
//     Message block processing according to SHA256
// 
//  Contents:
//     UpdateSHA256()
// 
// 
*/

#include "owndefs.h"
#include "owncp.h"
#include "pcphash.h"
#include "pcptool.h"

#if !defined(_ENABLE_ALG_SHA256_) && !defined(_ENABLE_ALG_SHA224_)
#pragma message("IPP_ALG_HASH_SHA256 disabled")

#else
//#pragma message("IPP_ALG_HASH_SHA256 enabled")

#if !((_IPP==_IPP_M5) || \
      (_IPP==_IPP_W7) || (_IPP==_IPP_T7) || \
      (_IPP==_IPP_V8) || (_IPP==_IPP_P8) || \
      (_IPP==_IPP_S8) || (_IPP>=_IPP_G9) || \
      (_IPP32E==_IPP32E_M7) || \
      (_IPP32E==_IPP32E_U8) || (_IPP32E==_IPP32E_Y8) || \
      (_IPP32E==_IPP32E_N8) || (_IPP32E>=_IPP32E_E9) || \
      (_IPP64==_IPP64_I7) )

/*
// SHA256 Specific Macros (reference proposal 256-384-512)
*/
#define CH(x,y,z)    (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z)   (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

#define SUM0(x)   (ROR32((x), 2) ^ ROR32((x),13) ^ ROR32((x),22))
#define SUM1(x)   (ROR32((x), 6) ^ ROR32((x),11) ^ ROR32((x),25))

#define SIG0(x)   (ROR32((x), 7) ^ ROR32((x),18) ^ LSR32((x), 3))
#define SIG1(x)   (ROR32((x),17) ^ ROR32((x),19) ^ LSR32((x),10))

#define SHA256_UPDATE(i) \
   wdat[i & 15] += SIG1(wdat[(i+14)&15]) + wdat[(i+9)&15] + SIG0(wdat[(i+1)&15])

#define SHA256_STEP(i,j)  \
   v[(7 - i) & 7] += (j ? SHA256_UPDATE(i) : wdat[i&15])    \
                  + SHA256_cnt_loc[i + j]                       \
                  + SUM1(v[(4-i)&7])                        \
                  + CH(v[(4-i)&7], v[(5-i)&7], v[(6-i)&7]); \
   v[(3-i)&7] += v[(7-i)&7];                                \
   v[(7-i)&7] += SUM0(v[(0-i)&7]) + MAJ(v[(0-i)&7], v[(1-i)&7], v[(2-i)&7])

#define COMPACT_SHA256_STEP(A,B,C,D,E,F,G,H, W,K, r) { \
   Ipp32u _T1 = (H) + SUM1((E)) + CH((E),(F),(G)) + (W)[(r)] + (K)[(r)]; \
   Ipp32u _T2 = SUM0((A)) + MAJ((A),(B),(C)); \
   (H) = (G); \
   (G) = (F); \
   (F) = (E); \
   (E) = (D)+_T1; \
   (D) = (C); \
   (C) = (B); \
   (B) = (A); \
   (A) = _T1+_T2; \
}

/*F*
//    Name: UpdateSHA256
//
// Purpose: Update internal hash according to input message stream.
//
// Parameters:
//    uniHash  pointer to in/out hash
//    mblk     pointer to message stream
//    mlen     message stream length (multiple by message block size)
//    uniParam pointer to the optional parameter
//
*F*/
#if defined(_ALG_SHA256_COMPACT_)
#pragma message("SHA256 compact")

void UpdateSHA256(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniParam)
{
   Ipp32u* data = (Ipp32u*)mblk;

   Ipp32u* digest = (Ipp32u*)uniHash;
   Ipp32u* SHA256_cnt_loc = (Ipp32u*)uniParam;

   for(; mlen>=MBS_SHA256; data += MBS_SHA256/sizeof(Ipp32u), mlen -= MBS_SHA256) {
      int t;

      /*
      // expand message block
      */
      Ipp32u W[64];
      /* initialize the first 16 words in the array W (remember about endian) */
      for(t=0; t<16; t++) {
         #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
         W[t] = data[t];
         #else
         W[t] = ENDIANNESS( data[t] );
         #endif
      }
      for(; t<64; t++)
         W[t] = SIG1(W[t-2]) + W[t-7] + SIG0(W[t-15]) + W[t-16];

      /*
      // update hash
      */
      {
         /* init A, B, C, D, E, F, G, H by the input hash */
         Ipp32u A = digest[0];
         Ipp32u B = digest[1];
         Ipp32u C = digest[2];
         Ipp32u D = digest[3];
         Ipp32u E = digest[4];
         Ipp32u F = digest[5];
         Ipp32u G = digest[6];
         Ipp32u H = digest[7];

         for(t=0; t<64; t++)
         COMPACT_SHA256_STEP(A,B,C,D,E,F,G,H, W,SHA256_cnt_loc, t);

         /* update hash*/
         digest[0] += A;
         digest[1] += B;
         digest[2] += C;
         digest[3] += D;
         digest[4] += E;
         digest[5] += F;
         digest[6] += G;
         digest[7] += H;
      }
   }
}

#else
void UpdateSHA256(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniParam)
{
   Ipp32u* data = (Ipp32u*)mblk;

   Ipp32u* digest = (Ipp32u*)uniHash;
   Ipp32u* SHA256_cnt_loc = (Ipp32u*)uniParam;

   for(; mlen>=MBS_SHA256; data += MBS_SHA256/sizeof(Ipp32u), mlen -= MBS_SHA256) {
      Ipp32u wdat[16];
      int j;

      /* copy digest */
      Ipp32u v[8];
      CopyBlock(digest, v, IPP_SHA256_DIGEST_BITSIZE/BYTESIZE);

      /* initialize the first 16 words in the array W (remember about endian) */
      for(j=0; j<16; j++) {
         #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
         wdat[j] = data[j];
         #else
         wdat[j] = ENDIANNESS( data[j] );
         #endif
      }

      for(j=0; j<64; j+=16) {
         SHA256_STEP( 0, j);
         SHA256_STEP( 1, j);
         SHA256_STEP( 2, j);
         SHA256_STEP( 3, j);
         SHA256_STEP( 4, j);
         SHA256_STEP( 5, j);
         SHA256_STEP( 6, j);
         SHA256_STEP( 7, j);
         SHA256_STEP( 8, j);
         SHA256_STEP( 9, j);
         SHA256_STEP(10, j);
         SHA256_STEP(11, j);
         SHA256_STEP(12, j);
         SHA256_STEP(13, j);
         SHA256_STEP(14, j);
         SHA256_STEP(15, j);
      }

      /* update digest */
      digest[0] += v[0];
      digest[1] += v[1];
      digest[2] += v[2];
      digest[3] += v[3];
      digest[4] += v[4];
      digest[5] += v[5];
      digest[6] += v[6];
      digest[7] += v[7];
   }
}
#endif

#endif
#endif /* IPP_ALG_HASH_SHA256 */