1 /*******************************************************************************
2 * Copyright 2002-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 // Digesting message according to SHA256
46 //
47 // Contents:
48 // ippsSHA256GetSize()
49 // ippsSHA256Init()
50 // ippsSHA256Pack()
51 // ippsSHA256Unpack()
52 // ippsSHA256Duplicate()
53 // ippsSHA256Update()
54 // ippsSHA256GetTag()
55 // ippsSHA256Final()
56 // ippsSHA256MessageDigest()
57 //
58 //
59 */
60
61 #include "owndefs.h"
62 #include "owncp.h"
63 #include "pcphash.h"
64 #include "pcphash_rmf.h"
65
66 #if !defined(_PCP_SHA256_STUFF_H)
67 #define _PCP_SHA256_STUFF_H
68
69 /* SHA-256, SHA-224 constants */
70 static const Ipp32u sha256_iv[] = {
71 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
72 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19};
73 static const Ipp32u sha224_iv[] = {
74 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939,
75 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4};
76
77 static __ALIGN16 const Ipp32u sha256_cnt[] = {
78 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
79 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
80 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
81 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
82 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
83 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
84 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
85 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
86 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
87 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
88 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
89 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
90 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
91 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
92 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
93 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
94 };
95
96
97 /* setup init hash value */
hashInit(Ipp32u * pHash,const Ipp32u * iv)98 __INLINE void hashInit(Ipp32u* pHash, const Ipp32u* iv)
99 {
100 pHash[0] = iv[0];
101 pHash[1] = iv[1];
102 pHash[2] = iv[2];
103 pHash[3] = iv[3];
104 pHash[4] = iv[4];
105 pHash[5] = iv[5];
106 pHash[6] = iv[6];
107 pHash[7] = iv[7];
108 }
sha256_hashInit(void * pHash)109 static void sha256_hashInit(void* pHash)
110 {
111 hashInit((Ipp32u*)pHash, sha256_iv);
112 }
sha224_hashInit(void * pHash)113 static void sha224_hashInit(void* pHash)
114 {
115 hashInit((Ipp32u*)pHash, sha224_iv);
116 }
117
sha256_hashUpdate(void * pHash,const Ipp8u * pMsg,int msgLen)118 static void sha256_hashUpdate(void* pHash, const Ipp8u* pMsg, int msgLen)
119 {
120 UpdateSHA256(pHash, pMsg, msgLen, sha256_cnt);
121 }
122 #if (_SHA_NI_ENABLING_==_FEATURE_TICKTOCK_ || _SHA_NI_ENABLING_==_FEATURE_ON_)
sha256_ni_hashUpdate(void * pHash,const Ipp8u * pMsg,int msgLen)123 static void sha256_ni_hashUpdate(void* pHash, const Ipp8u* pMsg, int msgLen)
124 {
125 UpdateSHA256ni(pHash, pMsg, msgLen, sha256_cnt);
126 }
127 #endif
128
129 /* convert hash into big endian */
sha256_hashOctString(Ipp8u * pMD,void * pHashVal)130 static void sha256_hashOctString(Ipp8u* pMD, void* pHashVal)
131 {
132 /* convert hash into big endian */
133 ((Ipp32u*)pMD)[0] = ENDIANNESS32(((Ipp32u*)pHashVal)[0]);
134 ((Ipp32u*)pMD)[1] = ENDIANNESS32(((Ipp32u*)pHashVal)[1]);
135 ((Ipp32u*)pMD)[2] = ENDIANNESS32(((Ipp32u*)pHashVal)[2]);
136 ((Ipp32u*)pMD)[3] = ENDIANNESS32(((Ipp32u*)pHashVal)[3]);
137 ((Ipp32u*)pMD)[4] = ENDIANNESS32(((Ipp32u*)pHashVal)[4]);
138 ((Ipp32u*)pMD)[5] = ENDIANNESS32(((Ipp32u*)pHashVal)[5]);
139 ((Ipp32u*)pMD)[6] = ENDIANNESS32(((Ipp32u*)pHashVal)[6]);
140 ((Ipp32u*)pMD)[7] = ENDIANNESS32(((Ipp32u*)pHashVal)[7]);
141 }
sha224_hashOctString(Ipp8u * pMD,void * pHashVal)142 static void sha224_hashOctString(Ipp8u* pMD, void* pHashVal)
143 {
144 /* convert hash into big endian */
145 ((Ipp32u*)pMD)[0] = ENDIANNESS32(((Ipp32u*)pHashVal)[0]);
146 ((Ipp32u*)pMD)[1] = ENDIANNESS32(((Ipp32u*)pHashVal)[1]);
147 ((Ipp32u*)pMD)[2] = ENDIANNESS32(((Ipp32u*)pHashVal)[2]);
148 ((Ipp32u*)pMD)[3] = ENDIANNESS32(((Ipp32u*)pHashVal)[3]);
149 ((Ipp32u*)pMD)[4] = ENDIANNESS32(((Ipp32u*)pHashVal)[4]);
150 ((Ipp32u*)pMD)[5] = ENDIANNESS32(((Ipp32u*)pHashVal)[5]);
151 ((Ipp32u*)pMD)[6] = ENDIANNESS32(((Ipp32u*)pHashVal)[6]);
152 }
153
sha256_msgRep(Ipp8u * pDst,Ipp64u lenLo,Ipp64u lenHi)154 static void sha256_msgRep(Ipp8u* pDst, Ipp64u lenLo, Ipp64u lenHi)
155 {
156 UNREFERENCED_PARAMETER(lenHi);
157 lenLo = ENDIANNESS64(lenLo<<3);
158 ((Ipp64u*)(pDst))[0] = lenLo;
159 }
160
161 /*
162 // SHA256 init context
163 */
GetSizeSHA256(int * pSize)164 static IppStatus GetSizeSHA256(int* pSize)
165 {
166 IPP_BAD_PTR1_RET(pSize);
167 *pSize = sizeof(IppsSHA256State) +(SHA256_ALIGNMENT-1);
168 return ippStsNoErr;
169 }
170
InitSHA256(IppsSHA256State * pState,const DigestSHA256 IV)171 static IppStatus InitSHA256(IppsSHA256State* pState, const DigestSHA256 IV)
172 {
173 /* test state pointer */
174 IPP_BAD_PTR1_RET(pState);
175 pState = (IppsSHA256State*)( IPP_ALIGNED_PTR(pState, SHA256_ALIGNMENT) );
176
177 HASH_CTX_ID(pState) = idCtxSHA256;
178 HASH_LENLO(pState) = 0;
179 HAHS_BUFFIDX(pState) = 0;
180
181 /* setup initial digest */
182 HASH_VALUE(pState)[0] = IV[0];
183 HASH_VALUE(pState)[1] = IV[1];
184 HASH_VALUE(pState)[2] = IV[2];
185 HASH_VALUE(pState)[3] = IV[3];
186 HASH_VALUE(pState)[4] = IV[4];
187 HASH_VALUE(pState)[5] = IV[5];
188 HASH_VALUE(pState)[6] = IV[6];
189 HASH_VALUE(pState)[7] = IV[7];
190
191 return ippStsNoErr;
192 }
193
194 #define cpSHA256MessageDigest OWNAPI(cpSHA256MessageDigest)
195 IppStatus cpSHA256MessageDigest(DigestSHA256 hash, const Ipp8u* pMsg, int msgLen, const DigestSHA256 IV);
196
197 #define cpFinalizeSHA256 OWNAPI(cpFinalizeSHA256)
198 void cpFinalizeSHA256(DigestSHA256 pHash, const Ipp8u* inpBuffer, int inpLen, Ipp64u processedMsgLen);
199
200 #endif /* #if !defined(_PCP_SHA256_STUFF_H) */
201