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 "pcpecprime.h"
54
55 //tbcd: temporary excluded: #include <assert.h>
56
57 #if(_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7)
58
59 /* arithmetic over P-192r1 NIST modulus */
60 #define p192r1_add OWNAPI(p192r1_add)
61 BNU_CHUNK_T* p192r1_add(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
62
63 #define p192r1_sub OWNAPI(p192r1_sub)
64 BNU_CHUNK_T* p192r1_sub(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
65
66 #define p192r1_neg OWNAPI(p192r1_neg)
67 BNU_CHUNK_T* p192r1_neg(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
68
69 #define p192r1_div_by_2 OWNAPI(p192r1_div_by_2)
70 BNU_CHUNK_T* p192r1_div_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
71
72 #define p192r1_mul_by_2 OWNAPI(p192r1_mul_by_2)
73 BNU_CHUNK_T* p192r1_mul_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
74
75 #define p192r1_mul_by_3 OWNAPI(p192r1_mul_by_3)
76 BNU_CHUNK_T* p192r1_mul_by_3 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
77
78 #if(_IPP_ARCH ==_IPP_ARCH_EM64T)
79 #define p192r1_mul_montl OWNAPI(p192r1_mul_montl)
80 BNU_CHUNK_T* p192r1_mul_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
81
82 #define p192r1_mul_montx OWNAPI(p192r1_mul_montx)
83 BNU_CHUNK_T* p192r1_mul_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
84
85 #define p192r1_sqr_montl OWNAPI(p192r1_sqr_montl)
86 BNU_CHUNK_T* p192r1_sqr_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
87
88 #define p192r1_sqr_montx OWNAPI(p192r1_sqr_montx)
89 BNU_CHUNK_T* p192r1_sqr_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
90
91 #define p192r1_to_mont OWNAPI(p192r1_to_mont)
92 BNU_CHUNK_T* p192r1_to_mont (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
93
94 #define p192r1_mont_back OWNAPI(p192r1_mont_back)
95 BNU_CHUNK_T* p192r1_mont_back(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
96 #endif
97 #if(_IPP_ARCH ==_IPP_ARCH_IA32)
98 #define p192r1_mul_mont_slm OWNAPI(p192r1_mul_mont_slm)
99 BNU_CHUNK_T* p192r1_mul_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
100
101 #define p192r1_sqr_mont_slm OWNAPI(p192r1_sqr_mont_slm)
102 BNU_CHUNK_T* p192r1_sqr_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
103
104 #define p192r1_mred OWNAPI(p192r1_mred)
105 BNU_CHUNK_T* p192r1_mred(BNU_CHUNK_T* res, BNU_CHUNK_T* product);
106 #endif
107
108 #define OPERAND_BITSIZE (192)
109 #define LEN_P192 (BITS_BNU_CHUNK(OPERAND_BITSIZE))
110
111
112 /*
113 // ia32 multiplicative methods
114 */
115 #if (_IPP_ARCH ==_IPP_ARCH_IA32)
p192r1_mul_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)116 static BNU_CHUNK_T* p192r1_mul_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
117 {
118 BNU_CHUNK_T* product = cpGFpGetPool(2, pGFE);
119 //tbcd: temporary excluded: assert(NULL!=product);
120
121 cpMulAdc_BNU_school(product, pA,LEN_P192, pB,LEN_P192);
122 p192r1_mred(pR, product);
123
124 cpGFpReleasePool(2, pGFE);
125 return pR;
126 }
127
p192r1_sqr_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)128 static BNU_CHUNK_T* p192r1_sqr_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
129 {
130 BNU_CHUNK_T* product = cpGFpGetPool(2, pGFE);
131 //tbcd: temporary excluded: assert(NULL!=product);
132
133 cpSqrAdc_BNU_school(product, pA,LEN_P192);
134 p192r1_mred(pR, product);
135
136 cpGFpReleasePool(2, pGFE);
137 return pR;
138 }
139
140
141 /*
142 // Montgomery domain conversion constants
143 */
144 static BNU_CHUNK_T RR[] = {
145 0x00000001,0x00000000,0x00000002,0x00000000,
146 0x00000001,0x00000000};
147
148 static BNU_CHUNK_T one[] = {
149 1,0,0,0,0,0};
150
p192r1_to_mont(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)151 static BNU_CHUNK_T* p192r1_to_mont(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
152 {
153 return p192r1_mul_montl(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
154 }
155
p192r1_mont_back(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)156 static BNU_CHUNK_T* p192r1_mont_back(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
157 {
158 return p192r1_mul_montl(pR, pA, (BNU_CHUNK_T*)one, pGFE);
159 }
160
p192r1_to_mont_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)161 static BNU_CHUNK_T* p192r1_to_mont_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
162 {
163 return p192r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
164 }
165
p192r1_mont_back_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)166 static BNU_CHUNK_T* p192r1_mont_back_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
167 {
168 return p192r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)one, pGFE);
169 }
170 #endif /* _IPP >= _IPP_P8 */
171
172 /*
173 // return specific gf p192r1 arith methods,
174 // p192r1 = 2^192 -2^64 -1 (NIST P192r1)
175 */
gsArithGF_p192r1(void)176 static gsModMethod* gsArithGF_p192r1(void)
177 {
178 static gsModMethod m = {
179 p192r1_to_mont,
180 p192r1_mont_back,
181 p192r1_mul_montl,
182 p192r1_sqr_montl,
183 NULL,
184 p192r1_add,
185 p192r1_sub,
186 p192r1_neg,
187 p192r1_div_by_2,
188 p192r1_mul_by_2,
189 p192r1_mul_by_3,
190 };
191
192 #if(_IPP_ARCH==_IPP_ARCH_EM64T) && ((_ADCOX_NI_ENABLING_==_FEATURE_ON_) || (_ADCOX_NI_ENABLING_==_FEATURE_TICKTOCK_))
193 if(IsFeatureEnabled(ippCPUID_ADCOX)) {
194 m.mul = p192r1_mul_montx;
195 m.sqr = p192r1_sqr_montx;
196 }
197 #endif
198
199 #if(_IPP_ARCH==_IPP_ARCH_IA32)
200 if(IsFeatureEnabled(ippCPUID_SSSE3|ippCPUID_MOVBE) && !IsFeatureEnabled(ippCPUID_AVX)) {
201 m.mul = p192r1_mul_mont_slm;
202 m.sqr = p192r1_sqr_mont_slm;
203 m.encode = p192r1_to_mont_slm;
204 m.decode = p192r1_mont_back_slm;
205 }
206 #endif
207
208 return &m;
209 }
210 #endif /* (_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7) */
211
212 /*F*
213 // Name: ippsGFpMethod_p192r1
214 //
215 // Purpose: Returns a reference to an implementation of
216 // arithmetic operations over GF(q).
217 //
218 // Returns: Pointer to a structure containing an implementation of arithmetic
219 // operations over GF(q). q = 2^192 - 2^64 - 1
220 *F*/
221
222 IPPFUN( const IppsGFpMethod*, ippsGFpMethod_p192r1, (void) )
223 {
224 static IppsGFpMethod method = {
225 cpID_PrimeP192r1,
226 192,
227 secp192r1_p,
228 NULL
229 };
230
231 #if(_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7)
232 method.arith = gsArithGF_p192r1();
233 #else
234 method.arith = gsArithGFp();
235 #endif
236
237 return &method;
238 }
239
240 #undef LEN_P192
241 #undef OPERAND_BITSIZE
242