1 /*******************************************************************************
2 * Copyright 2016-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 //     GF(p) methods
44 //
45 */
46 #include "owndefs.h"
47 #include "owncp.h"
48 
49 #include "pcpbnumisc.h"
50 #include "gsmodstuff.h"
51 #include "pcpgfpstuff.h"
52 #include "pcpgfpmethod.h"
53 #include "pcpbnuarith.h"
54 #include "pcpecprime.h"
55 
56 //tbcd: temporary excluded: #include <assert.h>
57 
58 #if(_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7)
59 
60 #define      p521r1_add      OWNAPI(p521r1_add)
61 #define      p521r1_sub      OWNAPI(p521r1_sub)
62 #define      p521r1_neg      OWNAPI(p521r1_neg)
63 #define      p521r1_div_by_2 OWNAPI(p521r1_div_by_2)
64 #define      p521r1_mul_by_2 OWNAPI(p521r1_mul_by_2)
65 #define      p521r1_mul_by_3 OWNAPI(p521r1_mul_by_3)
66 
67 /* arithmetic over P-521r1 NIST modulus */
68 BNU_CHUNK_T* p521r1_add(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
69 BNU_CHUNK_T* p521r1_sub(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
70 BNU_CHUNK_T* p521r1_neg(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
71 BNU_CHUNK_T* p521r1_div_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
72 BNU_CHUNK_T* p521r1_mul_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
73 BNU_CHUNK_T* p521r1_mul_by_3 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
74 
75 #if(_IPP_ARCH ==_IPP_ARCH_EM64T)
76 
77 //BNU_CHUNK_T* p521r1_to_mont  (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
78 //BNU_CHUNK_T* p521r1_mont_back(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
79 //BNU_CHUNK_T* p521r1_mul_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
80 //BNU_CHUNK_T* p521r1_sqr_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
81 //BNU_CHUNK_T* p521r1_mul_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
82 //BNU_CHUNK_T* p521r1_sqr_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
83 #endif
84 
85 #define      p521r1_mred OWNAPI(p521r1_mred)
86 BNU_CHUNK_T* p521r1_mred(BNU_CHUNK_T* res, BNU_CHUNK_T* product);
87 
88 #if(_IPP_ARCH ==_IPP_ARCH_IA32)
89 #define      p521r1_mul_mont_slm OWNAPI(p521r1_mul_mont_slm)
90 #define      p521r1_sqr_mont_slm OWNAPI(p521r1_sqr_mont_slm)
91 
92 BNU_CHUNK_T* p521r1_mul_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
93 BNU_CHUNK_T* p521r1_sqr_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
94 #endif
95 
96 #define OPERAND_BITSIZE (521)
97 #define LEN_P521        (BITS_BNU_CHUNK(OPERAND_BITSIZE))
98 
99 
100 /*
101 // multiplicative methods
102 */
p521r1_mul_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)103 static BNU_CHUNK_T* p521r1_mul_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
104 {
105    BNU_CHUNK_T* product = cpGFpGetPool(2, pGFE);
106    //tbcd: temporary excluded: assert(NULL!=product);
107 
108    cpMul_BNU_school(product, pA,LEN_P521, pB,LEN_P521);
109    p521r1_mred(pR, product);
110 
111    cpGFpReleasePool(2, pGFE);
112    return pR;
113 }
114 
p521r1_sqr_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)115 static BNU_CHUNK_T* p521r1_sqr_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
116 {
117    BNU_CHUNK_T* product = cpGFpGetPool(2, pGFE);
118    //tbcd: temporary excluded: assert(NULL!=product);
119 
120    cpSqr_BNU_school(product, pA,LEN_P521);
121    p521r1_mred(pR, product);
122 
123    cpGFpReleasePool(2, pGFE);
124    return pR;
125 }
126 
127 
128 /*
129 // Montgomery domain conversion constants
130 */
131 static BNU_CHUNK_T RR[] = {
132 #if(_IPP_ARCH == _IPP_ARCH_EM64T)
133    0x0000000000000000,0x0000400000000000,0x0000000000000000,
134    0x0000000000000000,0x0000000000000000,0x0000000000000000,
135    0x0000000000000000,0x0000000000000000,0x0000000000000000};
136 #elif(_IPP_ARCH == _IPP_ARCH_IA32)
137    0x00000000,0x00004000,0x00000000,0x00000000,0x00000000,0x00000000,
138    0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
139    0x00000000,0x00000000,0x00000000,0x00000000,0x00000000};
140 #endif
141 
142 static BNU_CHUNK_T one[] = {
143 #if(_IPP_ARCH == _IPP_ARCH_EM64T)
144    1,0,0,0,0,0,0,0,0};
145 #elif(_IPP_ARCH == _IPP_ARCH_IA32)
146    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
147 #endif
148 
p521r1_to_mont(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)149 static BNU_CHUNK_T* p521r1_to_mont(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
150 {
151    return p521r1_mul_montl(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
152 }
153 
p521r1_mont_back(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)154 static BNU_CHUNK_T* p521r1_mont_back(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
155 {
156    return p521r1_mul_montl(pR, pA, (BNU_CHUNK_T*)one, pGFE);
157 }
158 
159 #if (_ADCOX_NI_ENABLING_==_FEATURE_ON_) || (_ADCOX_NI_ENABLING_==_FEATURE_TICKTOCK_)
160 //BNU_CHUNK_T* p521r1_mul_montx(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
161 //BNU_CHUNK_T* p521r1_sqr_montx(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
162 #endif
163 
164 #if(_IPP_ARCH ==_IPP_ARCH_IA32)
p521r1_to_mont_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)165 static BNU_CHUNK_T* p521r1_to_mont_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
166 {
167    return p521r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
168 }
169 
p521r1_mont_back_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)170 static BNU_CHUNK_T* p521r1_mont_back_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
171 {
172    return p521r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)one, pGFE);
173 }
174 #endif /* _IPP_ARCH ==_IPP_ARCH_IA32*/
175 
176 /*
177 // return specific gf p521r1 arith methods,
178 //    p521r1 = 2^521 -1 (NIST P521r1)
179 */
gsArithGF_p521r1(void)180 static gsModMethod* gsArithGF_p521r1(void)
181 {
182    static gsModMethod m = {
183       p521r1_to_mont,
184       p521r1_mont_back,
185       p521r1_mul_montl,
186       p521r1_sqr_montl,
187       NULL,
188       p521r1_add,
189       p521r1_sub,
190       p521r1_neg,
191       p521r1_div_by_2,
192       p521r1_mul_by_2,
193       p521r1_mul_by_3,
194    };
195 
196    #if(_IPP_ARCH==_IPP_ARCH_IA32)
197    if(IsFeatureEnabled(ippCPUID_SSSE3|ippCPUID_MOVBE) && !IsFeatureEnabled(ippCPUID_AVX)) {
198       m.mul = p521r1_mul_mont_slm;
199       m.sqr = p521r1_sqr_mont_slm;
200       m.encode = p521r1_to_mont_slm;
201       m.decode = p521r1_mont_back_slm;
202    }
203    #endif
204 
205    return &m;
206 }
207 #endif /* (_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7) */
208 
209 /*F*
210 // Name: ippsGFpMethod_p521r1
211 //
212 // Purpose: Returns a reference to an implementation of
213 //          arithmetic operations over GF(q).
214 //
215 // Returns:  Pointer to a structure containing an implementation of arithmetic
216 //           operations over GF(q). q = 2^521 - 1
217 *F*/
218 
219 IPPFUN( const IppsGFpMethod*, ippsGFpMethod_p521r1, (void) )
220 {
221    static IppsGFpMethod method = {
222       cpID_PrimeP521r1,
223       521,
224       secp521r1_p,
225       NULL
226    };
227 
228    #if(_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7)
229    method.arith = gsArithGF_p521r1();
230    #else
231    method.arith = gsArithGFp();
232    #endif
233 
234    return &method;
235 }
236 
237 #undef LEN_P521
238 #undef OPERAND_BITSIZE
239