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 //        ippsHashUpdate()
50 //
51 */
52 
53 #include "owndefs.h"
54 #include "owncp.h"
55 #include "pcphash.h"
56 #include "pcptool.h"
57 
58 
59 /*F*
60 //    Name: ippsHashUpdate
61 //
62 // Purpose: Updates intermediate hash value based on input stream.
63 //
64 // Returns:                   Reason:
65 //    ippStsNullPtrErr           pState == NULL
66 //                               pSrc==0 but len!=0
67 //    ippStsContextMatchErr      pState->idCtx != idCtxHash
68 //    ippStsLengthErr            len <0
69 //    ippStsNoErr                no errors
70 //
71 // Parameters:
72 //    pSrc     pointer to the input stream
73 //    len      input stream length
74 //    pState   pointer to the Hash context
75 //
76 *F*/
IsExceedMsgLen(Ipp64u maxLo,Ipp64u maxHi,Ipp64u lenLo,Ipp64u lenHi)77 __INLINE int IsExceedMsgLen(Ipp64u maxLo, Ipp64u maxHi, Ipp64u lenLo, Ipp64u lenHi)
78 {
79    int isExceed = lenLo > maxLo;
80    isExceed = (lenHi+isExceed) > maxHi;
81    return isExceed;
82 }
83 
84 IPPFUN(IppStatus, ippsHashUpdate,(const Ipp8u* pSrc, int len, IppsHashState* pState))
85 {
86    /* test state pointer and ID */
87    IPP_BAD_PTR1_RET(pState);
88    /* test the context */
89    IPP_BADARG_RET(!HASH_VALID_ID(pState), ippStsContextMatchErr);
90    /* test input length */
91    IPP_BADARG_RET((len<0 && pSrc), ippStsLengthErr);
92    /* test source pointer */
93    IPP_BADARG_RET((len && !pSrc), ippStsNullPtrErr);
94 
95    /* handle non empty input */
96    if(len) {
97       const cpHashAttr* pAttr = &cpHashAlgAttr[HASH_ALG_ID(pState)];
98 
99       /* test if size of message is being processed not exceeded yet */
100       Ipp64u lenLo = HASH_LENLO(pState);
101       Ipp64u lenHi = HASH_LENHI(pState);
102       lenLo += len;
103       if(lenLo < HASH_LENLO(pState)) lenHi++;
104       if(IsExceedMsgLen(pAttr->msgLenMax[0],pAttr->msgLenMax[1], lenLo,lenHi))
105          IPP_ERROR_RET(ippStsLengthErr);
106 
107       else {
108          cpHashProc hashFunc = HASH_FUNC(pState);    /* processing function */
109          const void* pParam = HASH_FUNC_PAR(pState); /* and it's addition params */
110          int mbs = pAttr->msgBlkSize;              /* data block size */
111 
112          /*
113          // processing
114          */
115          {
116             int procLen;
117 
118             /* test if internal buffer is not empty */
119             int n = HAHS_BUFFIDX(pState);
120             if(n) {
121                procLen = IPP_MIN(len, (mbs-n));
122                CopyBlock(pSrc, HASH_BUFF(pState)+n, procLen);
123                HAHS_BUFFIDX(pState) = n += procLen;
124 
125                /* block processing */
126                if(mbs==n) {
127                   hashFunc(HASH_VALUE(pState), HASH_BUFF(pState), mbs, pParam);
128                   HAHS_BUFFIDX(pState) = 0;
129                }
130 
131                /* update message pointer and length */
132                pSrc += procLen;
133                len  -= procLen;
134             }
135 
136             /* main processing part */
137             procLen = len & ~(mbs-1);
138             if(procLen) {
139                hashFunc(HASH_VALUE(pState), pSrc, procLen, pParam);
140                pSrc += procLen;
141                len  -= procLen;
142             }
143 
144             /* rest of input message */
145             if(len) {
146                CopyBlock(pSrc, HASH_BUFF(pState), len);
147                HAHS_BUFFIDX(pState) += len;
148             }
149          }
150 
151          /* update length of processed message */
152          HASH_LENLO(pState) = lenLo;
153          HASH_LENHI(pState) = lenHi;
154 
155          return ippStsNoErr;
156       }
157    }
158 
159    return ippStsNoErr;
160 }
161