• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2 * Copyright 2017-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 #if !defined(_GS_MOD_STUFF_H)
42 #define _GS_MOD_STUFF_H
43 
44 //#define MONTMUL_ONESTAGE
45 
46 #include "owncp.h"
47 
48 #include "pcpbnuimpl.h"
49 #include "gsmodmethod.h"
50 
51 #define MOD_ENGINE_MIN_POOL_SIZE 1
52 
53 typedef struct _gsModEngine gsModEngine_T;
54 
55 typedef struct _gsModEngine
56 {
57    gsModEngine_T*       pParentME;     /* pointer to parent stuff          */
58    int                  extdegree;     /* parent modulus extension (deg)   */
59    int                  modBitLen;     /* length of modulus in bits        */
60    int                  modLen;        /* length of modulus  (BNU_CHUNK_T) */
61    int                  modLen32;      /* length of modulus  (Ipp32u)      */
62    int                  peLen;         /* length of pool element (BNU_CHUNK_T) */
63    const gsModMethod*   method;        /* modular arithmetic methods       */
64    BNU_CHUNK_T*         pModulus;      /* modulus                          */
65    BNU_CHUNK_T          k0;            /* low word of (1/modulus) mod R    */
66    BNU_CHUNK_T*         pMontR;        /* mont_enc(1)                      */
67    BNU_CHUNK_T*         pMontR2;       /* mont_enc(1)^2                    */
68    BNU_CHUNK_T*         pHalfModulus;  /* modulus/2                        */
69    BNU_CHUNK_T*         pQnr;          /* quadratic non-residue            */
70    int                  poolLenUsed;   /* number of reserved temporary BNU */
71    int                  poolLen;       /* max number of temporary BNU      */
72    BNU_CHUNK_T*         pBuffer;       /* buffer of modLen*nBuffers length */
73 } gsModEngine;
74 
75 /* accessory macros */
76 #define MOD_PARENT(eng)      ((eng)->pParentME)
77 #define MOD_EXTDEG(eng)      ((eng)->extdegree)
78 #define MOD_BITSIZE(eng)     ((eng)->modBitLen)
79 #define MOD_LEN(eng)         ((eng)->modLen)
80 #define MOD_LEN32(eng)       ((eng)->modLen32)
81 #define MOD_PELEN(eng)       ((eng)->peLen)
82 #define MOD_METHOD(eng)      ((eng)->method)
83 #define MOD_MODULUS(eng)     ((eng)->pModulus)
84 #define MOD_MNT_FACTOR(eng)  ((eng)->k0)
85 #define MOD_MNT_R(eng)       ((eng)->pMontR)
86 #define MOD_MNT_R2(eng)      ((eng)->pMontR2)
87 #define MOD_HMODULUS(eng)    ((eng)->pHalfModulus)
88 #define MOD_QNR(eng)         ((eng)->pQnr)
89 #define MOD_POOL_BUF(eng)    ((eng)->pBuffer)
90 #define MOD_MAXPOOL(eng)     ((eng)->poolLen)
91 #define MOD_USEDPOOL(eng)    ((eng)->poolLenUsed)
92 
93 #define MOD_BUFFER(eng,n)    ((eng)->pBuffer+(MOD_PELEN(eng))*(n))
94 
95 #define MOD_ENGINE_ALIGNMENT ((int)sizeof(void*))
96 
97 /*
98 // size of context and it initialization
99 */
100 #define   gsModEngineGetSize OWNAPI(gsModEngineGetSize)
101 IppStatus gsModEngineGetSize(int modulusBitSIze, int numpe, int* pSize);
102 
103 #define   gsModEngineInit OWNAPI(gsModEngineInit)
104 IppStatus gsModEngineInit(gsModEngine* pME, const Ipp32u* pModulus, int modulusBitSize, int numpe, const gsModMethod* method);
105 
106 #define     gsMontFactor OWNAPI(gsMontFactor)
107 BNU_CHUNK_T gsMontFactor(BNU_CHUNK_T m0);
108 
109 
110 /*
111 // pool management methods
112 */
113 
114 /*F*
115 // Name: gsModPoolAlloc
116 //
117 // Purpose: Allocation pool.
118 //
119 // Returns:                        Reason:
120 //       pointer to allocate Pool       enough of pool
121 //       NULL                           required pool more than pME have
122 //
123 // Parameters:
124 //    pME       ModEngine
125 //    poolReq   Required pool
126 *F*/
127 
gsModPoolAlloc(gsModEngine * pME,int poolReq)128 __INLINE BNU_CHUNK_T* gsModPoolAlloc(gsModEngine* pME, int poolReq)
129 {
130    BNU_CHUNK_T* pPool = MOD_BUFFER(pME, pME->poolLenUsed);
131 
132    if(pME->poolLenUsed + poolReq > pME->poolLen)
133       pPool = NULL;
134    else
135       pME->poolLenUsed += poolReq;
136 
137    return pPool;
138 }
139 
140 /*F*
141 // Name: gsModPoolFree
142 //
143 // Purpose: Delete pool.
144 //
145 // Returns:
146 //    nothing
147 //
148 // Parameters:
149 //    pME       ModEngine
150 //    poolReq   Required pool
151 *F*/
152 
gsModPoolFree(gsModEngine * pME,int poolReq)153 __INLINE void gsModPoolFree(gsModEngine* pME, int poolReq)
154 {
155    if(pME->poolLenUsed < poolReq)
156       poolReq = pME->poolLenUsed;
157    pME->poolLenUsed -= poolReq;
158 }
159 
160 /* return pointer to the top pool buffer */
161 #define      gsModGetPool OWNAPI(gsModGetPool)
162 BNU_CHUNK_T* gsModGetPool(gsModEngine* pME);
163 
164 /*
165 // advanced operations
166 */
167 typedef int (*alm_inv)(BNU_CHUNK_T* pr, const BNU_CHUNK_T* pa, gsModEngine* pMA);
168 
169 #define alm_mont_inv OWNAPI(alm_mont_inv)
170 int     alm_mont_inv(BNU_CHUNK_T* pr, const BNU_CHUNK_T* pa, gsModEngine* pMA);
171 
172 #define alm_mont_inv_ct OWNAPI(alm_mont_inv_ct)
173 int     alm_mont_inv_ct(BNU_CHUNK_T* pr, const BNU_CHUNK_T* pa, gsModEngine* pMA);
174 
175 #define      gs_mont_inv OWNAPI(gs_mont_inv)
176 BNU_CHUNK_T* gs_mont_inv(BNU_CHUNK_T* pr, const BNU_CHUNK_T* pa, gsModEngine* pMA, alm_inv invf);
177 
178 #define      gs_inv OWNAPI(gs_inv)
179 BNU_CHUNK_T* gs_inv(BNU_CHUNK_T* pr, const BNU_CHUNK_T* pa, gsModEngine* pMA, alm_inv invf);
180 
181 /*
182 // Pack/Unpack methods
183 */
184 #define gsPackModEngineCtx OWNAPI(gsPackModEngineCtx)
185 void    gsPackModEngineCtx(const gsModEngine* pCtx, Ipp8u* pBuffer);
186 
187 #define gsUnpackModEngineCtx OWNAPI(gsUnpackModEngineCtx)
188 void    gsUnpackModEngineCtx(const Ipp8u* pBuffer, gsModEngine* pCtx);
189 
190 #endif /* _GS_MOD_STUFF_H */
191