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 // SHA512 message digest
46 //
47 // Contents:
48 // SHA512 stuff
49 //
50 //
51 */
52
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcphash.h"
56 #include "pcphash_rmf.h"
57 #include "pcptool.h"
58
59 #if !defined(_PCP_SHA512_STUFF_H)
60 #define _PCP_SHA512_STUFF_H
61
62 /* SHA-512, SHA-384, SHA512-224, SHA512 constants */
63 static const Ipp64u sha512_iv[] = {
64 CONST_64(0x6A09E667F3BCC908), CONST_64(0xBB67AE8584CAA73B),
65 CONST_64(0x3C6EF372FE94F82B), CONST_64(0xA54FF53A5F1D36F1),
66 CONST_64(0x510E527FADE682D1), CONST_64(0x9B05688C2B3E6C1F),
67 CONST_64(0x1F83D9ABFB41BD6B), CONST_64(0x5BE0CD19137E2179)};
68 static const Ipp64u sha512_384_iv[] = {
69 CONST_64(0xCBBB9D5DC1059ED8), CONST_64(0x629A292A367CD507),
70 CONST_64(0x9159015A3070DD17), CONST_64(0x152FECD8F70E5939),
71 CONST_64(0x67332667FFC00B31), CONST_64(0x8EB44A8768581511),
72 CONST_64(0xDB0C2E0D64F98FA7), CONST_64(0x47B5481DBEFA4FA4)};
73 static const Ipp64u sha512_256_iv[] = {
74 CONST_64(0x22312194FC2BF72C), CONST_64(0x9F555FA3C84C64C2),
75 CONST_64(0x2393B86B6F53B151), CONST_64(0x963877195940EABD),
76 CONST_64(0x96283EE2A88EFFE3), CONST_64(0xBE5E1E2553863992),
77 CONST_64(0x2B0199FC2C85B8AA), CONST_64(0x0EB72DDC81C52CA2)};
78 static const Ipp64u sha512_224_iv[] = {
79 CONST_64(0x8C3D37C819544DA2), CONST_64(0x73E1996689DCD4D6),
80 CONST_64(0x1DFAB7AE32FF9C82), CONST_64(0x679DD514582F9FCF),
81 CONST_64(0x0F6D2B697BD44DA8), CONST_64(0x77E36F7304C48942),
82 CONST_64(0x3F9D85A86A1D36C8), CONST_64(0x1112E6AD91D692A1)};
83
84 static __ALIGN16 const Ipp64u sha512_cnt[] = {
85 CONST_64(0x428A2F98D728AE22), CONST_64(0x7137449123EF65CD), CONST_64(0xB5C0FBCFEC4D3B2F), CONST_64(0xE9B5DBA58189DBBC),
86 CONST_64(0x3956C25BF348B538), CONST_64(0x59F111F1B605D019), CONST_64(0x923F82A4AF194F9B), CONST_64(0xAB1C5ED5DA6D8118),
87 CONST_64(0xD807AA98A3030242), CONST_64(0x12835B0145706FBE), CONST_64(0x243185BE4EE4B28C), CONST_64(0x550C7DC3D5FFB4E2),
88 CONST_64(0x72BE5D74F27B896F), CONST_64(0x80DEB1FE3B1696B1), CONST_64(0x9BDC06A725C71235), CONST_64(0xC19BF174CF692694),
89 CONST_64(0xE49B69C19EF14AD2), CONST_64(0xEFBE4786384F25E3), CONST_64(0x0FC19DC68B8CD5B5), CONST_64(0x240CA1CC77AC9C65),
90 CONST_64(0x2DE92C6F592B0275), CONST_64(0x4A7484AA6EA6E483), CONST_64(0x5CB0A9DCBD41FBD4), CONST_64(0x76F988DA831153B5),
91 CONST_64(0x983E5152EE66DFAB), CONST_64(0xA831C66D2DB43210), CONST_64(0xB00327C898FB213F), CONST_64(0xBF597FC7BEEF0EE4),
92 CONST_64(0xC6E00BF33DA88FC2), CONST_64(0xD5A79147930AA725), CONST_64(0x06CA6351E003826F), CONST_64(0x142929670A0E6E70),
93 CONST_64(0x27B70A8546D22FFC), CONST_64(0x2E1B21385C26C926), CONST_64(0x4D2C6DFC5AC42AED), CONST_64(0x53380D139D95B3DF),
94 CONST_64(0x650A73548BAF63DE), CONST_64(0x766A0ABB3C77B2A8), CONST_64(0x81C2C92E47EDAEE6), CONST_64(0x92722C851482353B),
95 CONST_64(0xA2BFE8A14CF10364), CONST_64(0xA81A664BBC423001), CONST_64(0xC24B8B70D0F89791), CONST_64(0xC76C51A30654BE30),
96 CONST_64(0xD192E819D6EF5218), CONST_64(0xD69906245565A910), CONST_64(0xF40E35855771202A), CONST_64(0x106AA07032BBD1B8),
97 CONST_64(0x19A4C116B8D2D0C8), CONST_64(0x1E376C085141AB53), CONST_64(0x2748774CDF8EEB99), CONST_64(0x34B0BCB5E19B48A8),
98 CONST_64(0x391C0CB3C5C95A63), CONST_64(0x4ED8AA4AE3418ACB), CONST_64(0x5B9CCA4F7763E373), CONST_64(0x682E6FF3D6B2B8A3),
99 CONST_64(0x748F82EE5DEFB2FC), CONST_64(0x78A5636F43172F60), CONST_64(0x84C87814A1F0AB72), CONST_64(0x8CC702081A6439EC),
100 CONST_64(0x90BEFFFA23631E28), CONST_64(0xA4506CEBDE82BDE9), CONST_64(0xBEF9A3F7B2C67915), CONST_64(0xC67178F2E372532B),
101 CONST_64(0xCA273ECEEA26619C), CONST_64(0xD186B8C721C0C207), CONST_64(0xEADA7DD6CDE0EB1E), CONST_64(0xF57D4F7FEE6ED178),
102 CONST_64(0x06F067AA72176FBA), CONST_64(0x0A637DC5A2C898A6), CONST_64(0x113F9804BEF90DAE), CONST_64(0x1B710B35131C471B),
103 CONST_64(0x28DB77F523047D84), CONST_64(0x32CAAB7B40C72493), CONST_64(0x3C9EBE0A15C9BEBC), CONST_64(0x431D67C49C100D4C),
104 CONST_64(0x4CC5D4BECB3E42B6), CONST_64(0x597F299CFC657E2A), CONST_64(0x5FCB6FAB3AD6FAEC), CONST_64(0x6C44198C4A475817)
105 };
106
107 /* setup init hash value */
hashInit(Ipp64u * pHash,const Ipp64u * iv)108 __INLINE void hashInit(Ipp64u* pHash, const Ipp64u* iv)
109 {
110 pHash[0] = iv[0];
111 pHash[1] = iv[1];
112 pHash[2] = iv[2];
113 pHash[3] = iv[3];
114 pHash[4] = iv[4];
115 pHash[5] = iv[5];
116 pHash[6] = iv[6];
117 pHash[7] = iv[7];
118 }
sha512_hashInit(void * pHash)119 static void sha512_hashInit(void* pHash)
120 {
121 hashInit((Ipp64u*)pHash, sha512_iv);
122 }
sha512_384_hashInit(void * pHash)123 static void sha512_384_hashInit(void* pHash)
124 {
125 hashInit((Ipp64u*)pHash, sha512_384_iv);
126 }
sha512_256_hashInit(void * pHash)127 static void sha512_256_hashInit(void* pHash)
128 {
129 hashInit((Ipp64u*)pHash, sha512_256_iv);
130 }
sha512_224_hashInit(void * pHash)131 static void sha512_224_hashInit(void* pHash)
132 {
133 hashInit((Ipp64u*)pHash, sha512_224_iv);
134 }
135
sha512_hashUpdate(void * pHash,const Ipp8u * pMsg,int msgLen)136 static void sha512_hashUpdate(void* pHash, const Ipp8u* pMsg, int msgLen)
137 {
138 UpdateSHA512(pHash, pMsg, msgLen, sha512_cnt);
139 }
140
141 /* convert hash into big endian */
sha512_hashOctString(Ipp8u * pMD,void * pHashVal)142 static void sha512_hashOctString(Ipp8u* pMD, void* pHashVal)
143 {
144 ((Ipp64u*)pMD)[0] = ENDIANNESS64(((Ipp64u*)pHashVal)[0]);
145 ((Ipp64u*)pMD)[1] = ENDIANNESS64(((Ipp64u*)pHashVal)[1]);
146 ((Ipp64u*)pMD)[2] = ENDIANNESS64(((Ipp64u*)pHashVal)[2]);
147 ((Ipp64u*)pMD)[3] = ENDIANNESS64(((Ipp64u*)pHashVal)[3]);
148 ((Ipp64u*)pMD)[4] = ENDIANNESS64(((Ipp64u*)pHashVal)[4]);
149 ((Ipp64u*)pMD)[5] = ENDIANNESS64(((Ipp64u*)pHashVal)[5]);
150 ((Ipp64u*)pMD)[6] = ENDIANNESS64(((Ipp64u*)pHashVal)[6]);
151 ((Ipp64u*)pMD)[7] = ENDIANNESS64(((Ipp64u*)pHashVal)[7]);
152 }
sha512_384_hashOctString(Ipp8u * pMD,void * pHashVal)153 static void sha512_384_hashOctString(Ipp8u* pMD, void* pHashVal)
154 {
155 ((Ipp64u*)pMD)[0] = ENDIANNESS64(((Ipp64u*)pHashVal)[0]);
156 ((Ipp64u*)pMD)[1] = ENDIANNESS64(((Ipp64u*)pHashVal)[1]);
157 ((Ipp64u*)pMD)[2] = ENDIANNESS64(((Ipp64u*)pHashVal)[2]);
158 ((Ipp64u*)pMD)[3] = ENDIANNESS64(((Ipp64u*)pHashVal)[3]);
159 ((Ipp64u*)pMD)[4] = ENDIANNESS64(((Ipp64u*)pHashVal)[4]);
160 ((Ipp64u*)pMD)[5] = ENDIANNESS64(((Ipp64u*)pHashVal)[5]);
161 }
sha512_256_hashOctString(Ipp8u * pMD,void * pHashVal)162 static void sha512_256_hashOctString(Ipp8u* pMD, void* pHashVal)
163 {
164 ((Ipp64u*)pMD)[0] = ENDIANNESS64(((Ipp64u*)pHashVal)[0]);
165 ((Ipp64u*)pMD)[1] = ENDIANNESS64(((Ipp64u*)pHashVal)[1]);
166 ((Ipp64u*)pMD)[2] = ENDIANNESS64(((Ipp64u*)pHashVal)[2]);
167 ((Ipp64u*)pMD)[3] = ENDIANNESS64(((Ipp64u*)pHashVal)[3]);
168 }
sha512_224_hashOctString(Ipp8u * pMD,void * pHashVal)169 static void sha512_224_hashOctString(Ipp8u* pMD, void* pHashVal)
170 {
171 ((Ipp64u*)pMD)[0] = ENDIANNESS64(((Ipp64u*)pHashVal)[0]);
172 ((Ipp64u*)pMD)[1] = ENDIANNESS64(((Ipp64u*)pHashVal)[1]);
173 ((Ipp64u*)pMD)[2] = ENDIANNESS64(((Ipp64u*)pHashVal)[2]);
174 ((Ipp32u*)pMD)[6] = ENDIANNESS32(((Ipp32u*)pHashVal)[7]);
175 }
176
sha512_msgRep(Ipp8u * pDst,Ipp64u lenLo,Ipp64u lenHi)177 static void sha512_msgRep(Ipp8u* pDst, Ipp64u lenLo, Ipp64u lenHi)
178 {
179 lenHi = LSL64(lenHi,3) | LSR64(lenLo,63-3);
180 lenLo = LSL64(lenLo,3);
181 ((Ipp64u*)(pDst))[0] = ENDIANNESS64(lenHi);
182 ((Ipp64u*)(pDst))[1] = ENDIANNESS64(lenLo);
183 }
184
GetSizeSHA512(int * pSize)185 static IppStatus GetSizeSHA512(int* pSize)
186 {
187 /* test pointer */
188 IPP_BAD_PTR1_RET(pSize);
189 *pSize = sizeof(IppsSHA512State) +(SHA512_ALIGNMENT-1);
190 return ippStsNoErr;
191 }
192
193 //#define cpFinalizeSHA512 OWNAPI(cpFinalizeSHA512)
194 //void cpFinalizeSHA512(DigestSHA512 pHash, const Ipp8u* inpBuffer, int inpLen, Ipp64u lenLo, Ipp64u lenHi);
195 #define cpSHA512MessageDigest OWNAPI(cpSHA512MessageDigest)
196 IppStatus cpSHA512MessageDigest(DigestSHA512 hash, const Ipp8u* pMsg, int msgLen, const DigestSHA512 IV);
197 #define InitSHA512 OWNAPI(InitSHA512)
198 IppStatus InitSHA512(IppsSHA512State* pState, const DigestSHA512 IV);
199
cpFinalizeSHA512(DigestSHA512 pHash,const Ipp8u * inpBuffer,int inpLen,Ipp64u lenLo,Ipp64u lenHi)200 static void cpFinalizeSHA512(DigestSHA512 pHash,
201 const Ipp8u* inpBuffer, int inpLen,
202 Ipp64u lenLo, Ipp64u lenHi)
203 {
204 /* local buffer and it length */
205 Ipp8u buffer[MBS_SHA512*2];
206 int bufferLen = inpLen < (MBS_SHA512-(int)MLR_SHA512)? MBS_SHA512 : MBS_SHA512*2;
207
208 /* copy rest of message into internal buffer */
209 CopyBlock(inpBuffer, buffer, inpLen);
210
211 /* padd message */
212 buffer[inpLen++] = 0x80;
213 PaddBlock(0, buffer+inpLen, bufferLen-inpLen-MLR_SHA512);
214
215 /* message length representation */
216 lenHi = LSL64(lenHi,3) | LSR64(lenLo,63-3);
217 lenLo = LSL64(lenLo,3);
218 ((Ipp64u*)(buffer+bufferLen))[-2] = ENDIANNESS64(lenHi);
219 ((Ipp64u*)(buffer+bufferLen))[-1] = ENDIANNESS64(lenLo);
220
221 /* copmplete hash computation */
222 UpdateSHA512(pHash, buffer, bufferLen, sha512_cnt);
223 }
224
225 #endif /* #if !defined(_PCP_SHA512_STUFF_H) */
226