• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //  Purpose:
43 //     Intel(R) Integrated Performance Primitives
44 //     Cryptographic Primitives
45 //     Internal GF(p) basic Definitions & Function Prototypes
46 //
47 */
48 
49 #if !defined(_PCP_GFP_H_)
50 #define _PCP_GFP_H_
51 
52 #include "owncp.h"
53 #include "pcpgfpmethod.h"
54 #include "pcpmontgomery.h"
55 
56 /* GF element */
57 typedef struct _cpGFpElement {
58    IppCtxId    idCtx;   /* GF() element ident */
59    int         length;  /* length of element (in BNU_CHUNK_T) */
60    BNU_CHUNK_T*  pData;
61 } cpGFpElement;
62 
63 #define GFPE_ID(pCtx)      ((pCtx)->idCtx)
64 #define GFPE_ROOM(pCtx)    ((pCtx)->length)
65 #define GFPE_DATA(pCtx)    ((pCtx)->pData)
66 
67 #define GFPE_TEST_ID(pCtx) (GFPE_ID((pCtx))==idCtxGFPE)
68 
69 
70 /* GF(p) context */
71 typedef struct _cpGFp {
72    IppCtxId       idCtx;   /* GFp spec ident     */
73    gsModEngine*   pGFE;    /* arithmethic engine */
74 } cpGFp;
75 
76 #define GFP_ALIGNMENT   ((int)(sizeof(void*)))
77 
78 /* Local definitions */
79 #define GFP_MAX_BITSIZE       (IPP_MAX_GF_BITSIZE)      /* max bitsize for GF element */
80 #define GFP_POOL_SIZE         (16)//(IPP_MAX_EXPONENT_NUM+3)  /* num of elements into the pool */
81 #define GFP_RAND_ADD_BITS     (128)                     /* parameter of random element generation ?? == febits/2 */
82 
83 #define GFP_ID(pCtx)          ((pCtx)->idCtx)
84 #define GFP_PMA(pCtx)         ((pCtx)->pGFE)
85 
86 #define GFP_PARENT(pCtx)      MOD_PARENT((pCtx))
87 #define GFP_EXTDEGREE(pCtx)   MOD_EXTDEG((pCtx))
88 #define GFP_FEBITLEN(pCtx)    MOD_BITSIZE((pCtx))
89 #define GFP_FELEN(pCtx)       MOD_LEN((pCtx))
90 #define GFP_FELEN32(pCtx)     MOD_LEN32((pCtx))
91 #define GFP_PELEN(pCtx)       MOD_PELEN((pCtx))
92 #define GFP_METHOD(pCtx)      MOD_METHOD((pCtx))
93 #define GFP_MODULUS(pCtx)     MOD_MODULUS((pCtx))
94 #define GFP_MNT_FACTOR(pCtx)  MOD_MNT_FACTOR((pCtx))
95 #define GFP_MNT_R(pCtx)       MOD_MNT_R((pCtx))
96 #define GFP_MNT_RR(pCtx)      MOD_MNT_R2((pCtx))
97 #define GFP_HMODULUS(pCtx)    MOD_HMODULUS((pCtx))
98 #define GFP_QNR(pCtx)         MOD_QNR((pCtx))
99 #define GFP_POOL(pCtx)        MOD_POOL_BUF((pCtx))
100 #define GFP_MAXPOOL(pCtx)     MOD_MAXPOOL((pCtx))
101 #define GFP_USEDPOOL(pCtx)    MOD_USEDPOOL((pCtx))
102 
103 #define GFP_IS_BASIC(pCtx)    (GFP_PARENT((pCtx))==NULL)
104 #define GFP_TEST_ID(pCtx)     (GFP_ID((pCtx))==idCtxGFP)
105 
106 /*
107 // get/release n element from/to the pool
108 */
109 #define cpGFpGetPool(n, gfe)     gsModPoolAlloc((gfe), (n))
110 #define cpGFpReleasePool(n, gfe) gsModPoolFree((gfe), (n))
111 
112 
cpGFpElementLen(const BNU_CHUNK_T * pE,int nsE)113 __INLINE int cpGFpElementLen(const BNU_CHUNK_T* pE, int nsE)
114 {
115    for(; nsE>1 && 0==pE[nsE-1]; nsE--) ;
116    return nsE;
117 }
cpGFpElementCopy(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pE,int nsE)118 __INLINE BNU_CHUNK_T* cpGFpElementCopy(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pE, int nsE)
119 {
120    int n;
121    for(n=0; n<nsE; n++) pR[n] = pE[n];
122    return pR;
123 }
cpGFpElementPadd(BNU_CHUNK_T * pE,int nsE,BNU_CHUNK_T filler)124 __INLINE BNU_CHUNK_T* cpGFpElementPadd(BNU_CHUNK_T* pE, int nsE, BNU_CHUNK_T filler)
125 {
126    int n;
127    for(n=0; n<nsE; n++) pE[n] = filler;
128    return pE;
129 }
cpGFpElementCopyPadd(BNU_CHUNK_T * pR,int nsR,const BNU_CHUNK_T * pE,int nsE)130 __INLINE BNU_CHUNK_T* cpGFpElementCopyPadd(BNU_CHUNK_T* pR, int nsR, const BNU_CHUNK_T* pE, int nsE)
131 {
132    int n;
133    for(n=0; n<nsE; n++) pR[n] = pE[n];
134    for(; n<nsR; n++) pR[n] = 0;
135    return pR;
136 }
cpGFpElementCmp(const BNU_CHUNK_T * pE,const BNU_CHUNK_T * pX,int nsE)137 __INLINE int cpGFpElementCmp(const BNU_CHUNK_T* pE, const BNU_CHUNK_T* pX, int nsE)
138 {
139    for(; nsE>1 && pE[nsE-1]==pX[nsE-1]; nsE--)
140       ;
141    return pE[nsE-1]==pX[nsE-1]? 0 : pE[nsE-1]>pX[nsE-1]? 1:-1;
142 }
143 
cpGFpElementIsEquChunk(const BNU_CHUNK_T * pE,int nsE,BNU_CHUNK_T x)144 __INLINE int cpGFpElementIsEquChunk(const BNU_CHUNK_T* pE, int nsE, BNU_CHUNK_T x)
145 {
146    int isEqu = (pE[0] == x);
147    return isEqu && (1==cpGFpElementLen(pE, nsE));
148 }
149 
cpGFpElementSetChunk(BNU_CHUNK_T * pR,int nsR,BNU_CHUNK_T x)150 __INLINE BNU_CHUNK_T* cpGFpElementSetChunk(BNU_CHUNK_T* pR, int nsR, BNU_CHUNK_T x)
151 {
152    return cpGFpElementCopyPadd(pR, nsR, &x, 1);
153 }
154 
cpGFpAdd(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsModEngine * pGFE)155 __INLINE BNU_CHUNK_T* cpGFpAdd(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFE)
156 {
157    return GFP_METHOD(pGFE)->add(pR, pA, pB, pGFE);
158 }
159 
cpGFpSub(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsModEngine * pGFE)160 __INLINE BNU_CHUNK_T* cpGFpSub(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFE)
161 {
162    return GFP_METHOD(pGFE)->sub(pR, pA, pB, pGFE);
163 }
164 
cpGFpNeg(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsModEngine * pGFE)165 __INLINE BNU_CHUNK_T* cpGFpNeg(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFE)
166 {
167    return GFP_METHOD(pGFE)->neg(pR, pA, pGFE);
168 }
169 
cpGFpMul(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsModEngine * pGFE)170 __INLINE BNU_CHUNK_T* cpGFpMul(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsModEngine* pGFE)
171 {
172    return GFP_METHOD(pGFE)->mul(pR, pA, pB, pGFE);
173 }
174 
cpGFpSqr(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsModEngine * pGFE)175 __INLINE BNU_CHUNK_T* cpGFpSqr(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFE)
176 {
177    return GFP_METHOD(pGFE)->sqr(pR, pA, pGFE);
178 }
179 
cpGFpHalve(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsModEngine * pGFE)180 __INLINE BNU_CHUNK_T* cpGFpHalve(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFE)
181 {
182    return GFP_METHOD(pGFE)->div2(pR, pA, pGFE);
183 }
184 
185 
186 #define GFP_LT(a,b,size)  (-1==cpGFpElementCmp((a),(b),(size)))
187 #define GFP_EQ(a,b,size)  ( 0==cpGFpElementCmp((a),(b),(size)))
188 #define GFP_GT(a,b,size)  ( 1==cpGFpElementCmp((a),(b),(size)))
189 
190 #define GFP_IS_ZERO(a,size)  cpGFpElementIsEquChunk((a),(size), 0)
191 #define GFP_IS_ONE(a,size)   cpGFpElementIsEquChunk((a),(size), 1)
192 
193 #define GFP_ZERO(a,size)      cpGFpElementSetChunk((a),(size), 0)
194 #define GFP_ONE(a,size)       cpGFpElementSetChunk((a),(size), 1)
195 
196 #define GFP_IS_EVEN(a)  (0==((a)[0]&1))
197 #define GFP_IS_ODD(a)   (1==((a)[0]&1))
198 
199 
200 /* construct GF element */
cpGFpElementConstruct(IppsGFpElement * pR,BNU_CHUNK_T * pDataBufer,int ns)201 __INLINE IppsGFpElement* cpGFpElementConstruct(IppsGFpElement* pR, BNU_CHUNK_T* pDataBufer, int ns)
202 {
203    GFPE_ID(pR) = idCtxGFPE;
204    GFPE_ROOM(pR) = ns;
205    GFPE_DATA(pR) = pDataBufer;
206    return pR;
207 }
208 
209 
210 /* size of GFp context, init and setup */
211 #define cpGFpGetSize OWNAPI(cpGFpGetSize)
212 int     cpGFpGetSize(int feBitSize, int peBitSize, int numpe);
213 
214 #define   cpGFpInitGFp OWNAPI(cpGFpInitGFp)
215 IppStatus cpGFpInitGFp(int primeBitSize, IppsGFpState* pGF);
216 
217 #define   cpGFpSetGFp OWNAPI(cpGFpSetGFp)
218 IppStatus cpGFpSetGFp(const BNU_CHUNK_T* pPrime, int primeBitSize, const IppsGFpMethod* method, IppsGFpState* pGF);
219 
220 /* operations */
221 #define      cpGFpRand OWNAPI(cpGFpRand)
222 BNU_CHUNK_T* cpGFpRand(BNU_CHUNK_T* pR, gsModEngine* pGFE, IppBitSupplier rndFunc, void* pRndParam);
223 
224 #define      cpGFpSet OWNAPI(cpGFpSet)
225 BNU_CHUNK_T* cpGFpSet (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pDataA, int nsA, gsModEngine* pGFE);
226 
227 #define      cpGFpGet OWNAPI(cpGFpGet)
228 BNU_CHUNK_T* cpGFpGet (BNU_CHUNK_T* pDataA, int nsA, const BNU_CHUNK_T* pR, gsModEngine* pGFE);
229 
230 #define      cpGFpSetOctString OWNAPI(cpGFpSetOctString)
231 BNU_CHUNK_T* cpGFpSetOctString(BNU_CHUNK_T* pR, const Ipp8u* pStr, int strSize, gsModEngine* pGFE);
232 
233 #define cpGFpGetOctString OWNAPI(cpGFpGetOctString)
234 Ipp8u*  cpGFpGetOctString(Ipp8u* pStr, int strSize, const BNU_CHUNK_T* pA, gsModEngine* pGFE);
235 
236 #define      cpGFpInv OWNAPI(cpGFpInv)
237 BNU_CHUNK_T* cpGFpInv  (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFE);
238 
239 #define      cpGFpExp OWNAPI(cpGFpExp)
240 BNU_CHUNK_T* cpGFpExp  (BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pE, int nsE, gsModEngine* pGFE);
241 
242 #define cpGFpSqrt OWNAPI(cpGFpSqrt)
243 int     cpGFpSqrt(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsModEngine* pGFE);
244 
245 #define cpGFEqnr OWNAPI(cpGFEqnr)
246 void cpGFEqnr(gsModEngine* pGFE);
247 
248 #endif /* _PCP_GFP_H_ */
249