• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2 * Copyright 2014-2018 Intel Corporation
3 * All Rights Reserved.
4 *
5 * If this  software was obtained  under the  Intel Simplified  Software License,
6 * the following terms apply:
7 *
8 * The source code,  information  and material  ("Material") contained  herein is
9 * owned by Intel Corporation or its  suppliers or licensors,  and  title to such
10 * Material remains with Intel  Corporation or its  suppliers or  licensors.  The
11 * Material  contains  proprietary  information  of  Intel or  its suppliers  and
12 * licensors.  The Material is protected by  worldwide copyright  laws and treaty
13 * provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
14 * modified, published,  uploaded, posted, transmitted,  distributed or disclosed
15 * in any way without Intel's prior express written permission.  No license under
16 * any patent,  copyright or other  intellectual property rights  in the Material
17 * is granted to  or  conferred  upon  you,  either   expressly,  by implication,
18 * inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
19 * property rights must be express and approved by Intel in writing.
20 *
21 * Unless otherwise agreed by Intel in writing,  you may not remove or alter this
22 * notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
23 * suppliers or licensors in any way.
24 *
25 *
26 * If this  software  was obtained  under the  Apache License,  Version  2.0 (the
27 * "License"), the following terms apply:
28 *
29 * You may  not use this  file except  in compliance  with  the License.  You may
30 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
31 *
32 *
33 * Unless  required  by   applicable  law  or  agreed  to  in  writing,  software
34 * distributed under the License  is distributed  on an  "AS IS"  BASIS,  WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 *
37 * See the   License  for the   specific  language   governing   permissions  and
38 * limitations under the License.
39 *******************************************************************************/
40 
41 /*
42 //
43 //  Purpose:
44 //     Cryptography Primitive.
45 //     Message block processing according to SHA256
46 //
47 //  Contents:
48 //     UpdateSHA256()
49 //
50 //
51 */
52 
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcphash.h"
56 #include "pcptool.h"
57 
58 #if !defined(_ENABLE_ALG_SHA256_) && !defined(_ENABLE_ALG_SHA224_)
59 #pragma message("IPP_ALG_HASH_SHA256 disabled")
60 
61 #else
62 #pragma message("IPP_ALG_HASH_SHA256 enabled")
63 
64 #if !((_IPP==_IPP_M5) || \
65       (_IPP==_IPP_W7) || (_IPP==_IPP_T7) || \
66       (_IPP==_IPP_V8) || (_IPP==_IPP_P8) || \
67       (_IPP==_IPP_S8) || (_IPP>=_IPP_G9) || \
68       (_IPP32E==_IPP32E_M7) || \
69       (_IPP32E==_IPP32E_U8) || (_IPP32E==_IPP32E_Y8) || \
70       (_IPP32E==_IPP32E_N8) || (_IPP32E>=_IPP32E_E9))
71 
72 /*
73 // SHA256 Specific Macros (reference proposal 256-384-512)
74 */
75 #define CH(x,y,z)    (((x) & (y)) ^ (~(x) & (z)))
76 #define MAJ(x,y,z)   (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
77 
78 #define SUM0(x)   (ROR32((x), 2) ^ ROR32((x),13) ^ ROR32((x),22))
79 #define SUM1(x)   (ROR32((x), 6) ^ ROR32((x),11) ^ ROR32((x),25))
80 
81 #define SIG0(x)   (ROR32((x), 7) ^ ROR32((x),18) ^ LSR32((x), 3))
82 #define SIG1(x)   (ROR32((x),17) ^ ROR32((x),19) ^ LSR32((x),10))
83 
84 #define SHA256_UPDATE(i) \
85    wdat[i & 15] += SIG1(wdat[(i+14)&15]) + wdat[(i+9)&15] + SIG0(wdat[(i+1)&15])
86 
87 #define SHA256_STEP(i,j)  \
88    v[(7 - i) & 7] += (j ? SHA256_UPDATE(i) : wdat[i&15])    \
89                   + SHA256_cnt_loc[i + j]                       \
90                   + SUM1(v[(4-i)&7])                        \
91                   + CH(v[(4-i)&7], v[(5-i)&7], v[(6-i)&7]); \
92    v[(3-i)&7] += v[(7-i)&7];                                \
93    v[(7-i)&7] += SUM0(v[(0-i)&7]) + MAJ(v[(0-i)&7], v[(1-i)&7], v[(2-i)&7])
94 
95 #define COMPACT_SHA256_STEP(A,B,C,D,E,F,G,H, W,K, r) { \
96    Ipp32u _T1 = (H) + SUM1((E)) + CH((E),(F),(G)) + (W)[(r)] + (K)[(r)]; \
97    Ipp32u _T2 = SUM0((A)) + MAJ((A),(B),(C)); \
98    (H) = (G); \
99    (G) = (F); \
100    (F) = (E); \
101    (E) = (D)+_T1; \
102    (D) = (C); \
103    (C) = (B); \
104    (B) = (A); \
105    (A) = _T1+_T2; \
106 }
107 
108 /*F*
109 //    Name: UpdateSHA256
110 //
111 // Purpose: Update internal hash according to input message stream.
112 //
113 // Parameters:
114 //    uniHash  pointer to in/out hash
115 //    mblk     pointer to message stream
116 //    mlen     message stream length (multiple by message block size)
117 //    uniParam pointer to the optional parameter
118 //
119 *F*/
120 #if defined(_ALG_SHA256_COMPACT_)
121 #pragma message("SHA256 compact")
122 
UpdateSHA256(void * uniHash,const Ipp8u * mblk,int mlen,const void * uniParam)123 void UpdateSHA256(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniParam)
124 {
125    Ipp32u* data = (Ipp32u*)mblk;
126 
127    Ipp32u* digest = (Ipp32u*)uniHash;
128    Ipp32u* SHA256_cnt_loc = (Ipp32u*)uniParam;
129 
130    for(; mlen>=MBS_SHA256; data += MBS_SHA256/sizeof(Ipp32u), mlen -= MBS_SHA256) {
131       int t;
132 
133       /*
134       // expand message block
135       */
136       Ipp32u W[64];
137       /* initialize the first 16 words in the array W (remember about endian) */
138       for(t=0; t<16; t++) {
139          #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
140          W[t] = data[t];
141          #else
142          W[t] = ENDIANNESS( data[t] );
143          #endif
144       }
145       for(; t<64; t++)
146          W[t] = SIG1(W[t-2]) + W[t-7] + SIG0(W[t-15]) + W[t-16];
147 
148       /*
149       // update hash
150       */
151       {
152          /* init A, B, C, D, E, F, G, H by the input hash */
153          Ipp32u A = digest[0];
154          Ipp32u B = digest[1];
155          Ipp32u C = digest[2];
156          Ipp32u D = digest[3];
157          Ipp32u E = digest[4];
158          Ipp32u F = digest[5];
159          Ipp32u G = digest[6];
160          Ipp32u H = digest[7];
161 
162          for(t=0; t<64; t++)
163          COMPACT_SHA256_STEP(A,B,C,D,E,F,G,H, W,SHA256_cnt_loc, t);
164 
165          /* update hash*/
166          digest[0] += A;
167          digest[1] += B;
168          digest[2] += C;
169          digest[3] += D;
170          digest[4] += E;
171          digest[5] += F;
172          digest[6] += G;
173          digest[7] += H;
174       }
175    }
176 }
177 
178 #else
UpdateSHA256(void * uniHash,const Ipp8u * mblk,int mlen,const void * uniParam)179 void UpdateSHA256(void* uniHash, const Ipp8u* mblk, int mlen, const void* uniParam)
180 {
181    Ipp32u* data = (Ipp32u*)mblk;
182 
183    Ipp32u* digest = (Ipp32u*)uniHash;
184    Ipp32u* SHA256_cnt_loc = (Ipp32u*)uniParam;
185 
186    for(; mlen>=MBS_SHA256; data += MBS_SHA256/sizeof(Ipp32u), mlen -= MBS_SHA256) {
187       Ipp32u wdat[16];
188       int j;
189 
190       /* copy digest */
191       Ipp32u v[8];
192       CopyBlock(digest, v, IPP_SHA256_DIGEST_BITSIZE/BYTESIZE);
193 
194       /* initialize the first 16 words in the array W (remember about endian) */
195       for(j=0; j<16; j++) {
196          #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
197          wdat[j] = data[j];
198          #else
199          wdat[j] = ENDIANNESS( data[j] );
200          #endif
201       }
202 
203       for(j=0; j<64; j+=16) {
204          SHA256_STEP( 0, j);
205          SHA256_STEP( 1, j);
206          SHA256_STEP( 2, j);
207          SHA256_STEP( 3, j);
208          SHA256_STEP( 4, j);
209          SHA256_STEP( 5, j);
210          SHA256_STEP( 6, j);
211          SHA256_STEP( 7, j);
212          SHA256_STEP( 8, j);
213          SHA256_STEP( 9, j);
214          SHA256_STEP(10, j);
215          SHA256_STEP(11, j);
216          SHA256_STEP(12, j);
217          SHA256_STEP(13, j);
218          SHA256_STEP(14, j);
219          SHA256_STEP(15, j);
220       }
221 
222       /* update digest */
223       digest[0] += v[0];
224       digest[1] += v[1];
225       digest[2] += v[2];
226       digest[3] += v[3];
227       digest[4] += v[4];
228       digest[5] += v[5];
229       digest[6] += v[6];
230       digest[7] += v[7];
231    }
232 }
233 #endif
234 
235 #endif
236 #endif /* IPP_ALG_HASH_SHA256 */
237