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 SHA1
46 //
47 // Contents:
48 // UpdateSHA1()
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_SHA1_)
59 //#pragma message("IPP_ALG_HASH_SHA1 disabled")
60
61 //#else
62 //#pragma message("IPP_ALG_HASH_SHA1 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 // Magic functions defined in FIPS 180-1
74 //
75 */
76 #define MAGIC_F0(B,C,D) (((B) & (C)) | ((~(B)) & (D)))
77 #define MAGIC_F1(B,C,D) ((B) ^ (C) ^ (D))
78 #define MAGIC_F2(B,C,D) (((B) & (C)) | ((B) & (D)) | ((C) & (D)))
79 #define MAGIC_F3(B,C,D) ((B) ^ (C) ^ (D))
80
81 #define SHA1_STEP(A,B,C,D,E, MAGIC_FUN, W,K) \
82 (E)+= ROL32((A),5) + MAGIC_FUN((B),(C),(D)) + (W) + (K); \
83 (B) = ROL32((B),30)
84
85 #define COMPACT_SHA1_STEP(A,B,C,D,E, MAGIC_FUN, W,K, t) { \
86 Ipp32u _T = ROL32((A),5) + MAGIC_FUN((t)/20, (B),(C),(D)) + (E) + (W)[(t)] + (K)[(t)/20]; \
87 (E) = (D); \
88 (D) = (C); \
89 (C) = ROL32((B),30); \
90 (B) = (A); \
91 (A) = _T; \
92 }
93
94 #if defined(_ALG_SHA1_COMPACT_)
MagicFun(int s,Ipp32u b,Ipp32u c,Ipp32u d)95 __INLINE Ipp32u MagicFun(int s, Ipp32u b, Ipp32u c, Ipp32u d)
96 {
97 switch(s) {
98 case 0: return MAGIC_F0(b,c,d);
99 case 2: return MAGIC_F2(b,c,d);
100 default:return MAGIC_F1(b,c,d);
101 }
102 }
103 #endif
104
105
106 /*F*
107 // Name: UpdateSHA1
108 //
109 // Purpose: Update internal hash according to input message stream.
110 //
111 // Parameters:
112 // uniHash pointer to in/out hash
113 // mblk pointer to message stream
114 // mlen message stream length (multiple by message block size)
115 // uniParam pointer to the optional parameter
116 //
117 *F*/
118 #if defined(_ALG_SHA1_COMPACT_)
119 #pragma message("SHA1 compact")
120 #endif
121
UpdateSHA1(void * uinHash,const Ipp8u * mblk,int mlen,const void * uniParam)122 void UpdateSHA1(void* uinHash, const Ipp8u* mblk, int mlen, const void *uniParam)
123 {
124 Ipp32u* data = (Ipp32u*)mblk;
125
126 Ipp32u* digest = (Ipp32u*)uinHash;
127 Ipp32u* SHA1_cnt_loc = (Ipp32u*)uniParam;
128
129 for(; mlen>=MBS_SHA1; data += MBS_SHA1/sizeof(Ipp32u), mlen -= MBS_SHA1) {
130 int t;
131
132 /*
133 // expand message block
134 */
135 Ipp32u W[80];
136 /* initialize the first 16 words in the array W (remember about endian) */
137 for(t=0; t<16; t++) {
138 #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
139 W[t] = data[t];
140 #else
141 W[t] = ENDIANNESS(data[t]);
142 #endif
143 }
144 /* schedule another 80-16 words in the array W */
145 for(; t<80; t++) {
146 W[t] = ROL32(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
147 }
148
149 /*
150 // update hash
151 */
152 {
153 /* init A, B, C, D, E by the the input hash */
154 Ipp32u A = digest[0];
155 Ipp32u B = digest[1];
156 Ipp32u C = digest[2];
157 Ipp32u D = digest[3];
158 Ipp32u E = digest[4];
159
160 #if defined(_ALG_SHA1_COMPACT_)
161 /* steps 0-79 */
162 for(t=0; t<80; t++)
163 COMPACT_SHA1_STEP(A,B,C,D,E, MagicFun, W, SHA1_cnt_loc, t);
164
165 #else
166 /* perform 0-19 steps */
167 for(t=0; t<20; t+=5) {
168 SHA1_STEP(A,B,C,D,E, MAGIC_F0, W[t ],SHA1_cnt_loc[0]);
169 SHA1_STEP(E,A,B,C,D, MAGIC_F0, W[t+1],SHA1_cnt_loc[0]);
170 SHA1_STEP(D,E,A,B,C, MAGIC_F0, W[t+2],SHA1_cnt_loc[0]);
171 SHA1_STEP(C,D,E,A,B, MAGIC_F0, W[t+3],SHA1_cnt_loc[0]);
172 SHA1_STEP(B,C,D,E,A, MAGIC_F0, W[t+4],SHA1_cnt_loc[0]);
173 }
174 /* perform 20-39 steps */
175 for(; t<40; t+=5) {
176 SHA1_STEP(A,B,C,D,E, MAGIC_F1, W[t ],SHA1_cnt_loc[1]);
177 SHA1_STEP(E,A,B,C,D, MAGIC_F1, W[t+1],SHA1_cnt_loc[1]);
178 SHA1_STEP(D,E,A,B,C, MAGIC_F1, W[t+2],SHA1_cnt_loc[1]);
179 SHA1_STEP(C,D,E,A,B, MAGIC_F1, W[t+3],SHA1_cnt_loc[1]);
180 SHA1_STEP(B,C,D,E,A, MAGIC_F1, W[t+4],SHA1_cnt_loc[1]);
181 }
182 /* perform 40-59 steps */
183 for(; t<60; t+=5) {
184 SHA1_STEP(A,B,C,D,E, MAGIC_F2, W[t ],SHA1_cnt_loc[2]);
185 SHA1_STEP(E,A,B,C,D, MAGIC_F2, W[t+1],SHA1_cnt_loc[2]);
186 SHA1_STEP(D,E,A,B,C, MAGIC_F2, W[t+2],SHA1_cnt_loc[2]);
187 SHA1_STEP(C,D,E,A,B, MAGIC_F2, W[t+3],SHA1_cnt_loc[2]);
188 SHA1_STEP(B,C,D,E,A, MAGIC_F2, W[t+4],SHA1_cnt_loc[2]);
189 }
190 /* perform 60-79 steps */
191 for(; t<80; t+=5) {
192 SHA1_STEP(A,B,C,D,E, MAGIC_F3, W[t ],SHA1_cnt_loc[3]);
193 SHA1_STEP(E,A,B,C,D, MAGIC_F3, W[t+1],SHA1_cnt_loc[3]);
194 SHA1_STEP(D,E,A,B,C, MAGIC_F3, W[t+2],SHA1_cnt_loc[3]);
195 SHA1_STEP(C,D,E,A,B, MAGIC_F3, W[t+3],SHA1_cnt_loc[3]);
196 SHA1_STEP(B,C,D,E,A, MAGIC_F3, W[t+4],SHA1_cnt_loc[3]);
197 }
198 #endif
199
200 /* update digest */
201 digest[0] += A;
202 digest[1] += B;
203 digest[2] += C;
204 digest[3] += D;
205 digest[4] += E;
206 }
207 }
208 }
209
210 #endif
211 //#endif /* IPP_ALG_HASH_SHA1 */
212