1 /*******************************************************************************
2 * Copyright 2005-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 //     HASH based Mask Generation Functions
46 //
47 //  Contents:
48 //     ippsMGF_SHA1()
49 //     ippsMGF_SHA224()
50 //     ippsMGF_SHA256()
51 //     ippsMGF_SHA384()
52 //     ippsMGF_SHA512()
53 //     ippsMGF_MD5()
54 //
55 //
56 */
57 
58 #include "owndefs.h"
59 #include "owncp.h"
60 #include "pcphash.h"
61 #include "pcptool.h"
62 
63 
64 /*F*
65 //    Name: ippsMGF_SHA1
66 //          ippsMGF_SHA224
67 //          ippsMGF_SHA256
68 //          ippsMGF_SHA384
69 //          ippsMGF_SHA512
70 //          ippsMGF_MD5
71 //
72 // Purpose: Mask Generation Functions.
73 //
74 // Returns:                Reason:
75 //    ippStsNullPtrErr           pMask == NULL
76 //    ippStsLengthErr            seedLen <0
77 //                               maskLen <0
78 //    ippStsNotSupportedModeErr  if algID is not match to supported hash alg
79 //    ippStsNoErr                no errors
80 //
81 // Parameters:
82 //    pSeed       pointer to the input stream
83 //    seedLen     input stream length (bytes)
84 //    pMask       pointer to the ouput mask
85 //    maskLen     desired length of mask (bytes)
86 //    hashAlg     identifier of the hash algorithm
87 //
88 *F*/
89 IPPFUN(IppStatus, ippsMGF,(const Ipp8u* pSeed, int seedLen, Ipp8u* pMask, int maskLen, IppHashAlgId hashAlg))
90 {
91    /* get algorithm id */
92    hashAlg = cpValidHashAlg(hashAlg);
93    /* test hash alg */
94    IPP_BADARG_RET(ippHashAlg_Unknown==hashAlg, ippStsNotSupportedModeErr);
95 
96    IPP_BAD_PTR1_RET(pMask);
97    IPP_BADARG_RET((seedLen<0)||(maskLen<0), ippStsLengthErr);
98 
99    {
100       /* hash specific */
101       int hashSize = cpHashSize(hashAlg);
102 
103       int i, outLen;
104 
105       IppsHashState hashCtx;
106       ippsHashInit(&hashCtx, hashAlg);
107 
108       if(!pSeed)
109          seedLen = 0;
110 
111       for(i=0,outLen=0; outLen<maskLen; i++) {
112          Ipp8u cnt[4];
113          cnt[0] = (Ipp8u)((i>>24) & 0xFF);
114          cnt[1] = (Ipp8u)((i>>16) & 0xFF);
115          cnt[2] = (Ipp8u)((i>>8)  & 0xFF);
116          cnt[3] = (Ipp8u)(i & 0xFF);
117 
118          cpReInitHash(&hashCtx, hashAlg);
119          ippsHashUpdate(pSeed, seedLen, &hashCtx);
120          ippsHashUpdate(cnt,   4,       &hashCtx);
121 
122          if((outLen + hashSize) <= maskLen) {
123             ippsHashFinal(pMask+outLen, &hashCtx);
124             outLen += hashSize;
125          }
126          else {
127             Ipp8u md[MAX_HASH_SIZE];
128             ippsHashFinal(md, &hashCtx);
129             CopyBlock(md, pMask+outLen, maskLen-outLen);
130             outLen = maskLen;
131          }
132       }
133 
134       return ippStsNoErr;
135    }
136 }
137