1 /*******************************************************************************
2 * Copyright 2010-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 //     Intel(R) Integrated Performance Primitives. Cryptography Primitives.
43 //     Operations over GF(p).
44 //
45 //     Context:
46 //        cpGFpGetSize()
47 //        cpGFpInitGFp()
48 //
49 //
50 */
51 #include "owndefs.h"
52 #include "owncp.h"
53 
54 #include "pcpgfpstuff.h"
55 #include "pcpgfpxstuff.h"
56 #include "pcptool.h"
57 
58 
59 /*
60 // size of GFp engine context (Montgomery)
61 */
cpGFpGetSize(int feBitSize,int peBitSize,int numpe)62 int cpGFpGetSize(int feBitSize, int peBitSize, int numpe)
63 {
64    int ctxSize = 0;
65    int elemLen = BITS_BNU_CHUNK(feBitSize);
66    int pelmLen = BITS_BNU_CHUNK(peBitSize);
67 
68    /* size of GFp engine */
69    ctxSize = sizeof(gsModEngine)
70             + elemLen*sizeof(BNU_CHUNK_T)    /* modulus  */
71             + elemLen*sizeof(BNU_CHUNK_T)    /* mont_R   */
72             + elemLen*sizeof(BNU_CHUNK_T)    /* mont_R^2 */
73             + elemLen*sizeof(BNU_CHUNK_T)    /* half of modulus */
74             + elemLen*sizeof(BNU_CHUNK_T)    /* quadratic non-residue */
75             + pelmLen*sizeof(BNU_CHUNK_T)*numpe; /* pool */
76 
77    ctxSize += sizeof(IppsGFpState);   /* size of IppsGFPState */
78    return ctxSize;
79 }
80 
81 /*
82 // init GFp engine context (Montgomery)
83 */
cpGFEInit(gsModEngine * pGFE,int modulusBitSize,int peBitSize,int numpe)84 static void cpGFEInit(gsModEngine* pGFE, int modulusBitSize, int peBitSize, int numpe)
85 {
86    int modLen  = BITS_BNU_CHUNK(modulusBitSize);
87    int pelmLen = BITS_BNU_CHUNK(peBitSize);
88 
89    Ipp8u* ptr = (Ipp8u*)pGFE;
90 
91    /* clear whole context */
92    PaddBlock(0, ptr, sizeof(gsModEngine));
93    ptr += sizeof(gsModEngine);
94 
95    GFP_PARENT(pGFE)    = NULL;
96    GFP_EXTDEGREE(pGFE) = 1;
97    GFP_FEBITLEN(pGFE)  = modulusBitSize;
98    GFP_FELEN(pGFE)     = modLen;
99    GFP_FELEN32(pGFE)   = BITS2WORD32_SIZE(modulusBitSize);
100    GFP_PELEN(pGFE)     = pelmLen;
101  //GFP_METHOD(pGFE)    = method;
102    GFP_MODULUS(pGFE)   = (BNU_CHUNK_T*)(ptr);   ptr += modLen*sizeof(BNU_CHUNK_T);
103    GFP_MNT_R(pGFE)     = (BNU_CHUNK_T*)(ptr);   ptr += modLen*sizeof(BNU_CHUNK_T);
104    GFP_MNT_RR(pGFE)    = (BNU_CHUNK_T*)(ptr);   ptr += modLen*sizeof(BNU_CHUNK_T);
105    GFP_HMODULUS(pGFE)  = (BNU_CHUNK_T*)(ptr);   ptr += modLen*sizeof(BNU_CHUNK_T);
106    GFP_QNR(pGFE)       = (BNU_CHUNK_T*)(ptr);   ptr += modLen*sizeof(BNU_CHUNK_T);
107    GFP_POOL(pGFE)      = (BNU_CHUNK_T*)(ptr);/* ptr += modLen*sizeof(BNU_CHUNK_T);*/
108    GFP_MAXPOOL(pGFE)   = numpe;
109    GFP_USEDPOOL(pGFE)  = 0;
110 
111    cpGFpElementPadd(GFP_MODULUS(pGFE), modLen, 0);
112    cpGFpElementPadd(GFP_MNT_R(pGFE), modLen, 0);
113    cpGFpElementPadd(GFP_MNT_RR(pGFE), modLen, 0);
114    cpGFpElementPadd(GFP_HMODULUS(pGFE), modLen, 0);
115    cpGFpElementPadd(GFP_QNR(pGFE), modLen, 0);
116 }
117 
cpGFpInitGFp(int primeBitSize,IppsGFpState * pGF)118 IppStatus cpGFpInitGFp(int primeBitSize, IppsGFpState* pGF)
119 {
120    IPP_BADARG_RET((primeBitSize< IPP_MIN_GF_BITSIZE) || (primeBitSize> IPP_MAX_GF_BITSIZE), ippStsSizeErr);
121    IPP_BAD_PTR1_RET(pGF);
122    pGF = (IppsGFpState*)( IPP_ALIGNED_PTR(pGF, GFP_ALIGNMENT) );
123 
124    {
125       Ipp8u* ptr = (Ipp8u*)pGF;
126 
127       GFP_ID(pGF)      = idCtxGFP;
128       GFP_PMA(pGF) = (gsModEngine*)(ptr+sizeof(IppsGFpState));
129       cpGFEInit(GFP_PMA(pGF), primeBitSize, primeBitSize+BITSIZE(BNU_CHUNK_T), GFP_POOL_SIZE);
130 
131       return ippStsNoErr;
132    }
133 }
134