1 /*******************************************************************************
2 * Copyright 2004-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 //     PRNG Functions
46 //
47 //  Contents:
48 //        cpPRNGen()
49 //
50 */
51 
52 #include "owndefs.h"
53 #include "owncp.h"
54 #include "pcpbn.h"
55 #include "pcphash.h"
56 #include "pcpprng.h"
57 #include "pcptool.h"
58 
59 
60 /*
61 // G() function based on SHA1
62 //
63 // Parameters:
64 //    T           160 bit parameter
65 //    pHexStr     input hex string
66 //    hexStrLen   size of hex string (Ipp8u segnments)
67 //    xBNU        160 bit BNU result
68 //
69 // Note 1:
70 //    must to be hexStrLen <= 64 (512 bits)
71 */
72 static
SHA1_G(Ipp32u * xBNU,const Ipp32u * T,Ipp8u * pHexStr,int hexStrLen)73 void SHA1_G(Ipp32u* xBNU, const Ipp32u* T, Ipp8u* pHexStr, int hexStrLen)
74 {
75    /* select processing function */
76    cpHashProc updateFunc;
77    #if (_SHA_NI_ENABLING_==_FEATURE_ON_)
78    updateFunc = UpdateSHA1ni;
79    #elif (_SHA_NI_ENABLING_==_FEATURE_TICKTOCK_)
80    updateFunc = IsFeatureEnabled(ippCPUID_SHA)? UpdateSHA1ni : UpdateSHA1;
81    #else
82    updateFunc = UpdateSHA1;
83    #endif
84 
85    /* pad HexString zeros */
86    PaddBlock(0, pHexStr+hexStrLen, BITS2WORD8_SIZE(MAX_XKEY_SIZE)-hexStrLen);
87 
88    /* reset initial HASH value */
89    xBNU[0] = T[0];
90    xBNU[1] = T[1];
91    xBNU[2] = T[2];
92    xBNU[3] = T[3];
93    xBNU[4] = T[4];
94 
95    /* SHA1 */
96    //UpdateSHA1(xBNU, pHexStr, BITS2WORD8_SIZE(MAX_XKEY_SIZE), SHA1_cnt);
97    updateFunc(xBNU, pHexStr, BITS2WORD8_SIZE(MAX_XKEY_SIZE), SHA1_cnt);
98 
99    /* swap back */
100    SWAP(xBNU[0],xBNU[4]);
101    SWAP(xBNU[1],xBNU[3]);
102 }
103 
104 /*F*
105 // Name: cpPRNGen
106 //
107 // Purpose: Returns bitsize of the bitstring has beed added
108 //
109 // Returns:
110 //    bitsize of the bitstring has beed added
111 //
112 // Parameters:
113 //    pRand    pointer to the buffer
114 //    nBits    number of bits be requested
115 //    pRnd     pointer to the context
116 *F*/
117 
cpPRNGen(Ipp32u * pRand,cpSize nBits,IppsPRNGState * pRnd)118 int cpPRNGen(Ipp32u* pRand, cpSize nBits, IppsPRNGState* pRnd)
119 {
120    BNU_CHUNK_T Xj  [BITS_BNU_CHUNK(MAX_XKEY_SIZE)];
121    BNU_CHUNK_T XVAL[BITS_BNU_CHUNK(MAX_XKEY_SIZE)];
122 
123    Ipp8u  TXVAL[BITS2WORD8_SIZE(MAX_XKEY_SIZE)];
124 
125    /* XKEY length in BNU_CHUNK_T */
126    cpSize xKeyLen = BITS_BNU_CHUNK(RAND_SEEDBITS(pRnd));
127    /* XKEY length in bytes */
128    cpSize xKeySize= BITS2WORD8_SIZE(RAND_SEEDBITS(pRnd));
129    /* XKEY word's mask */
130    BNU_CHUNK_T xKeyMsk = MASK_BNU_CHUNK(RAND_SEEDBITS(pRnd));
131 
132    /* number of Ipp32u chunks to be generated */
133    cpSize genlen = BITS2WORD32_SIZE(nBits);
134 
135    ZEXPAND_BNU(Xj, 0, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
136    ZEXPAND_BNU(XVAL, 0, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
137 
138    while(genlen) {
139       cpSize len;
140 
141       /* Step 1: XVAL=(Xkey+Xseed) mod 2^b */
142       BNU_CHUNK_T carry = cpAdd_BNU(XVAL, RAND_XKEY(pRnd), RAND_XAUGMENT(pRnd), xKeyLen);
143       XVAL[xKeyLen-1] &= xKeyMsk;
144 
145       /* Step 2: xj=G(t, XVAL) mod Q */
146       cpToOctStr_BNU(TXVAL, xKeySize, XVAL, xKeyLen);
147       SHA1_G((Ipp32u*)Xj, (Ipp32u*)RAND_T(pRnd), TXVAL, xKeySize);
148 
149       {
150          cpSize sizeXj = BITS_BNU_CHUNK(160);
151          if(0 <= cpCmp_BNU(Xj, BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE), RAND_Q(pRnd),BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE)) )
152             sizeXj = cpMod_BNU(Xj, BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE), RAND_Q(pRnd), BITS_BNU_CHUNK(IPP_SHA1_DIGEST_BITSIZE));
153          FIX_BNU(Xj, sizeXj);
154          ZEXPAND_BNU(Xj, sizeXj, BITS_BNU_CHUNK(MAX_XKEY_SIZE));
155       }
156 
157       /* Step 3: Xkey=(1+Xkey+Xj) mod 2^b */
158       cpInc_BNU(RAND_XKEY(pRnd), RAND_XKEY(pRnd), xKeyLen, 1);
159       carry = cpAdd_BNU(RAND_XKEY(pRnd), RAND_XKEY(pRnd), Xj, xKeyLen);
160       RAND_XKEY(pRnd)[xKeyLen-1] &= xKeyMsk;
161 
162       /* fill out result */
163       len = genlen<BITS2WORD32_SIZE(IPP_SHA1_DIGEST_BITSIZE)? genlen : BITS2WORD32_SIZE(IPP_SHA1_DIGEST_BITSIZE);
164       COPY_BNU(pRand, (Ipp32u*)Xj, len);
165 
166       pRand  += len;
167       genlen -= len;
168    }
169 
170    return nBits;
171 }
172