• 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 //     Security Hash Standard
46 //     General Functionality
47 //
48 //  Contents:
49 //        cpComputeDigest()
50 //
51 */
52 
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcphash.h"
56 #include "pcptool.h"
57 
58 
cpComputeDigest(Ipp8u * pHashTag,int hashTagLen,const IppsHashState * pCtx)59 void cpComputeDigest(Ipp8u* pHashTag, int hashTagLen, const IppsHashState* pCtx)
60 {
61    /* hash alg and parameters */
62    cpHashProc hashFunc = HASH_FUNC(pCtx);    /* processing function */
63    const void* pParam = HASH_FUNC_PAR(pCtx); /* and it's addition params */
64 
65    /* attributes */
66    const cpHashAttr* pAttr = &cpHashAlgAttr[HASH_ALG_ID(pCtx)];
67    int mbs = pAttr->msgBlkSize;              /* data block size */
68    int ivSize = pAttr->ivSize;               /* size of hash's IV */
69    int msgLenRepSize = pAttr->msgLenRepSize; /* length of the message representation */
70 
71    /* number of bytes in context buffer */
72    int n = HAHS_BUFFIDX(pCtx);
73    /* buffer and it actual length */
74    Ipp8u buffer[MBS_HASH_MAX*2];
75    int bufferLen = n < (mbs-msgLenRepSize)? mbs : mbs*2;
76 
77    /* copy current hash value */
78    cpHash hash;
79    CopyBlock(HASH_VALUE(pCtx), hash, ivSize);
80 
81    /* copy of state's buffer */
82    CopyBlock(HASH_BUFF(pCtx), buffer, n);
83    /* end of message bit */
84    buffer[n++] = 0x80;
85    /* padd buffer */
86    PaddBlock(0, buffer+n, bufferLen-n-msgLenRepSize);
87 
88    /* message length representation in bits (remember about big endian) */
89    {
90       /* convert processed message length bytes ->bits */
91       Ipp64u lo = HASH_LENLO(pCtx);
92       Ipp64u hi = HASH_LENHI(pCtx);
93       hi = LSL64(hi,3) | LSR64(lo,63-3);
94       lo = LSL64(lo,3);
95 
96       if(msgLenRepSize>(int)(sizeof(Ipp64u))) {
97       #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
98          ((Ipp64u*)(buffer+bufferLen))[-2] = hi;
99       #else
100          ((Ipp64u*)(buffer+bufferLen))[-2] = ENDIANNESS64(hi);
101       #endif
102       }
103 
104       /* recall about MD5 specific */
105       if(ippHashAlg_MD5!=HASH_ALG_ID(pCtx)) {
106          #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
107          ((Ipp64u*)(buffer+bufferLen))[-1] = lo;
108          #else
109          ((Ipp64u*)(buffer+bufferLen))[-1] = ENDIANNESS64(lo);
110          #endif
111       }
112       else {
113          #if (IPP_ENDIAN == IPP_BIG_ENDIAN)
114          ((Ipp64u*)(buffer+bufferLen))[-1] = ENDIANNESS64(lo);
115          #else
116          ((Ipp64u*)(buffer+bufferLen))[-1] = lo;
117          #endif
118       }
119    }
120 
121    /* copmplete hash computation */
122    hashFunc(hash, buffer, bufferLen, pParam);
123 
124    /* store digest into the user buffer (remember digest in big endian) */
125    if(msgLenRepSize>(int)(sizeof(Ipp64u))) {
126       /* ippHashAlg_SHA384, ippHashAlg_SHA512, ippHashAlg_SHA512_224 and ippHashAlg_SHA512_256 */
127       hash[0] = ENDIANNESS64(hash[0]);
128       hash[1] = ENDIANNESS64(hash[1]);
129       hash[2] = ENDIANNESS64(hash[2]);
130       hash[3] = ENDIANNESS64(hash[3]);
131       hash[4] = ENDIANNESS64(hash[4]);
132       hash[5] = ENDIANNESS64(hash[5]);
133       hash[6] = ENDIANNESS64(hash[6]);
134       hash[7] = ENDIANNESS64(hash[7]);
135    }
136    else if(ippHashAlg_MD5!=HASH_ALG_ID(pCtx)) {
137       ((Ipp32u*)hash)[0] = ENDIANNESS32(((Ipp32u*)hash)[0]);
138       ((Ipp32u*)hash)[1] = ENDIANNESS32(((Ipp32u*)hash)[1]);
139       ((Ipp32u*)hash)[2] = ENDIANNESS32(((Ipp32u*)hash)[2]);
140       ((Ipp32u*)hash)[3] = ENDIANNESS32(((Ipp32u*)hash)[3]);
141       ((Ipp32u*)hash)[4] = ENDIANNESS32(((Ipp32u*)hash)[4]);
142       if(ippHashAlg_SHA1!=HASH_ALG_ID(pCtx)) {
143          ((Ipp32u*)hash)[5] = ENDIANNESS32(((Ipp32u*)hash)[5]);
144          ((Ipp32u*)hash)[6] = ENDIANNESS32(((Ipp32u*)hash)[6]);
145          ((Ipp32u*)hash)[7] = ENDIANNESS32(((Ipp32u*)hash)[7]);
146       }
147    }
148    CopyBlock(hash, pHashTag, hashTagLen);
149 }
150