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 p384r1_add OWNAPI(p384r1_add)
61 #define p384r1_sub OWNAPI(p384r1_sub)
62 #define p384r1_neg OWNAPI(p384r1_neg)
63 #define p384r1_div_by_2 OWNAPI(p384r1_div_by_2)
64 #define p384r1_mul_by_2 OWNAPI(p384r1_mul_by_2)
65 #define p384r1_mul_by_3 OWNAPI(p384r1_mul_by_3)
66
67 /* arithmetic over P-384r1 NIST modulus */
68 BNU_CHUNK_T* p384r1_add(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
69 BNU_CHUNK_T* p384r1_sub(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
70 BNU_CHUNK_T* p384r1_neg(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
71 BNU_CHUNK_T* p384r1_div_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
72 BNU_CHUNK_T* p384r1_mul_by_2 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
73 BNU_CHUNK_T* p384r1_mul_by_3 (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
74 #if(_IPP_ARCH ==_IPP_ARCH_EM64T)
75 //BNU_CHUNK_T* p384r1_mul_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
76 //BNU_CHUNK_T* p384r1_sqr_montl(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
77 //BNU_CHUNK_T* p384r1_mul_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
78 //BNU_CHUNK_T* p384r1_sqr_montx(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
79 //BNU_CHUNK_T* p384r1_to_mont (BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
80 //BNU_CHUNK_T* p384r1_mont_back(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
81 #endif
82
83 #define p384r1_mred OWNAPI(p384r1_mred)
84 BNU_CHUNK_T* p384r1_mred(BNU_CHUNK_T* res, BNU_CHUNK_T* product);
85
86 #if(_IPP_ARCH ==_IPP_ARCH_IA32)
87
88 #define p384r1_mul_mont_slm OWNAPI(p384r1_mul_mont_slm)
89 #define p384r1_sqr_mont_slm OWNAPI(p384r1_sqr_mont_slm)
90
91 BNU_CHUNK_T* p384r1_mul_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, const BNU_CHUNK_T* b, gsEngine* pGFE);
92 BNU_CHUNK_T* p384r1_sqr_mont_slm(BNU_CHUNK_T* res, const BNU_CHUNK_T* a, gsEngine* pGFE);
93 #endif
94
95 #define OPERAND_BITSIZE (384)
96 #define LEN_P384 (BITS_BNU_CHUNK(OPERAND_BITSIZE))
97
98
99 /*
100 // multiplicative methods
101 */
p384r1_mul_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,const BNU_CHUNK_T * pB,gsEngine * pGFE)102 static BNU_CHUNK_T* p384r1_mul_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
103 {
104 BNU_CHUNK_T* product = cpGFpGetPool(2, pGFE);
105 //tbcd: temporary excluded: assert(NULL!=product);
106
107 cpMul_BNU_school(product, pA,LEN_P384, pB,LEN_P384);
108 p384r1_mred(pR, product);
109
110 cpGFpReleasePool(2, pGFE);
111 return pR;
112 }
113
p384r1_sqr_montl(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)114 static BNU_CHUNK_T* p384r1_sqr_montl(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
115 {
116 BNU_CHUNK_T* product = gsModPoolAlloc((gsModEngine*)pGFE, 2);
117
118 cpSqr_BNU_school(product, pA,LEN_P384);
119 p384r1_mred(pR, product);
120
121 cpGFpReleasePool(2, pGFE);
122 return pR;
123 }
124
125
126 /*
127 // Montgomery domain conversion constants
128 */
129 static Ipp64u RR[] = {
130 0xfffffffe00000001,0x0000000200000000,0xfffffffe00000000,
131 0x0000000200000000,0x0000000000000001,0x0000000000000000
132 };
133
134 static BNU_CHUNK_T one[] = {
135 #if(_IPP_ARCH == _IPP_ARCH_EM64T)
136 1,0,0,0,0,0};
137 #elif(_IPP_ARCH == _IPP_ARCH_IA32)
138 1,0,0,0,0,0,0,0,0,0,0,0};
139 #endif
140
p384r1_to_mont(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)141 static BNU_CHUNK_T* p384r1_to_mont(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
142 {
143 return p384r1_mul_montl(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
144 }
145
p384r1_mont_back(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)146 static BNU_CHUNK_T* p384r1_mont_back(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
147 {
148 return p384r1_mul_montl(pR, pA, (BNU_CHUNK_T*)one, pGFE);
149 }
150
151 #if (_ADCOX_NI_ENABLING_==_FEATURE_ON_) || (_ADCOX_NI_ENABLING_==_FEATURE_TICKTOCK_)
152 //BNU_CHUNK_T* p384r1_mul_montx(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
153 //BNU_CHUNK_T* p384r1_sqr_montx(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, const BNU_CHUNK_T* pB, gsEngine* pGFE)
154 #endif
155
156 #if(_IPP_ARCH ==_IPP_ARCH_IA32)
p384r1_to_mont_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)157 static BNU_CHUNK_T* p384r1_to_mont_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
158 {
159 return p384r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)RR, pGFE);
160 }
161
p384r1_mont_back_slm(BNU_CHUNK_T * pR,const BNU_CHUNK_T * pA,gsEngine * pGFE)162 static BNU_CHUNK_T* p384r1_mont_back_slm(BNU_CHUNK_T* pR, const BNU_CHUNK_T* pA, gsEngine* pGFE)
163 {
164 return p384r1_mul_mont_slm(pR, pA, (BNU_CHUNK_T*)one, pGFE);
165 }
166 #endif /* _IPP_ARCH ==_IPP_ARCH_IA32 */
167
168 /*
169 // return specific gf p384r1 arith methods,
170 // p384r1 = 2^384 -2^128 -2^96 +2^32 -1 (NIST P384r1)
171 */
gsArithGF_p384r1(void)172 static gsModMethod* gsArithGF_p384r1(void)
173 {
174 static gsModMethod m = {
175 p384r1_to_mont,
176 p384r1_mont_back,
177 p384r1_mul_montl,
178 p384r1_sqr_montl,
179 NULL,
180 p384r1_add,
181 p384r1_sub,
182 p384r1_neg,
183 p384r1_div_by_2,
184 p384r1_mul_by_2,
185 p384r1_mul_by_3,
186 };
187
188 #if(_IPP_ARCH==_IPP_ARCH_IA32)
189 if(IsFeatureEnabled(ippCPUID_SSSE3|ippCPUID_MOVBE) && !IsFeatureEnabled(ippCPUID_AVX)) {
190 m.mul = p384r1_mul_mont_slm;
191 m.sqr = p384r1_sqr_mont_slm;
192 m.encode = p384r1_to_mont_slm;
193 m.decode = p384r1_mont_back_slm;
194 }
195 #endif
196
197 return &m;
198 }
199 #endif /* (_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7) */
200
201 /*F*
202 // Name: ippsGFpMethod_p384r1
203 //
204 // Purpose: Returns a reference to an implementation of
205 // arithmetic operations over GF(q).
206 //
207 // Returns: Pointer to a structure containing an implementation of arithmetic
208 // operations over GF(q). q = 2^384 - 2^128 - 2^96 + 2^32 - 1
209 *F*/
210
211 IPPFUN( const IppsGFpMethod*, ippsGFpMethod_p384r1, (void) )
212 {
213 static IppsGFpMethod method = {
214 cpID_PrimeP384r1,
215 384,
216 secp384r1_p,
217 NULL
218 };
219
220 #if(_IPP >= _IPP_P8) || (_IPP32E >= _IPP32E_M7)
221 method.arith = gsArithGF_p384r1();
222 #else
223 method.arith = gsArithGFp();
224 #endif
225
226 return &method;
227 }
228
229 #undef LEN_P384
230 #undef OPERAND_BITSIZE
231