Debian Codegraph @

mpi/ec-nist.c

raw
1 /* ec-nist.c - NIST optimized elliptic curve functions
2 * Copyright (C) 2021 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25
26 #ifndef ASM_DISABLED
27
28
29 #include "mpi-internal.h"
30 #include "longlong.h"
31 #include "g10lib.h"
32 #include "context.h"
33 #include "ec-context.h"
34 #include "ec-inline.h"
35 #include "const-time.h"
36 #include "cipher.h" /* for GCRYECC_FLAG_LEAST_LEAK */
37
38
39 static inline
40 void prefetch(const void *tab, size_t len)
41 {
42 const volatile byte *vtab = tab;
43
44 if (len > 0 * 64)
45 (void)vtab[0 * 64];
46 if (len > 1 * 64)
47 (void)vtab[1 * 64];
48 if (len > 2 * 64)
49 (void)vtab[2 * 64];
50 if (len > 3 * 64)
51 (void)vtab[3 * 64];
52 if (len > 4 * 64)
53 (void)vtab[4 * 64];
54 if (len > 5 * 64)
55 (void)vtab[5 * 64];
56 if (len > 6 * 64)
57 (void)vtab[6 * 64];
58 if (len > 7 * 64)
59 (void)vtab[7 * 64];
60 if (len > 8 * 64)
61 (void)vtab[8 * 64];
62 if (len > 9 * 64)
63 (void)vtab[9 * 64];
64 if (len > 10 * 64)
65 (void)vtab[10 * 64];
66 (void)vtab[len - 1];
67 }
68
69
70 /* Fast reduction routines for NIST curves. */
71
72 void
73 _gcry_mpi_ec_nist192_mod (gcry_mpi_t w, mpi_ec_t ctx)
74 {
75 static const mpi_limb64_t p_mult[3][4] =
76 {
77 { /* P * 1 */
78 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xfffffffeU),
79 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0x00000000U)
80 },
81 { /* P * 2 */
82 LIMB64_C(0xffffffffU, 0xfffffffeU), LIMB64_C(0xffffffffU, 0xfffffffdU),
83 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0x00000001U)
84 },
85 { /* P * 3 */
86 LIMB64_C(0xffffffffU, 0xfffffffdU), LIMB64_C(0xffffffffU, 0xfffffffcU),
87 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0x00000002U)
88 }
89 };
90 const mpi_limb64_t zero = LIMB_TO64(0);
91 mpi_ptr_t wp;
92 mpi_limb64_t s[192 / BITS_PER_MPI_LIMB64 + 1];
93 mpi_limb64_t o[DIM(s)];
94 const mpi_size_t wsize = DIM(s) - 1;
95 mpi_limb_t mask1;
96 mpi_limb_t mask2;
97 mpi_limb_t s_is_negative;
98 int carry;
99
100 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2 * LIMBS_PER_LIMB64);
101 RESIZE_AND_CLEAR_IF_NEEDED (ctx->p, wsize * LIMBS_PER_LIMB64);
102
103 wp = w->d;
104
105 prefetch (p_mult, sizeof(p_mult));
106
107 /* See "FIPS 186-4, D.2.1 Curve P-192". */
108
109 s[0] = LOAD64(wp, 3);
110 ADD3_LIMB64 (s[3], s[2], s[1],
111 zero, zero, LOAD64(wp, 3),
112 zero, LOAD64(wp, 4), LOAD64(wp, 4));
113
114 ADD4_LIMB64 (s[3], s[2], s[1], s[0],
115 s[3], s[2], s[1], s[0],
116 zero, LOAD64(wp, 5), LOAD64(wp, 5), LOAD64(wp, 5));
117
118 ADD4_LIMB64 (s[3], s[2], s[1], s[0],
119 s[3], s[2], s[1], s[0],
120 zero, LOAD64(wp, 2), LOAD64(wp, 1), LOAD64(wp, 0));
121
122 /* mod p:
123 * 's[3]' holds carry value (0..2). Subtract (carry + 1) * p. Result will be
124 * with in range -p...p. Handle result being negative with addition and
125 * conditional store. */
126
127 carry = LO32_LIMB64(s[3]);
128
129 SUB4_LIMB64 (s[3], s[2], s[1], s[0],
130 s[3], s[2], s[1], s[0],
131 p_mult[carry][3], p_mult[carry][2],
132 p_mult[carry][1], p_mult[carry][0]);
133
134 ADD4_LIMB64 (o[3], o[2], o[1], o[0],
135 s[3], s[2], s[1], s[0],
136 zero,
137 p_mult[0][2], p_mult[0][1], p_mult[0][0]);
138
139 s_is_negative = LO32_LIMB64(s[3]) >> 31;
140
141 mask2 = ct_limb_gen_mask(s_is_negative);
142 mask1 = ct_limb_gen_inv_mask(s_is_negative);
143
144 STORE64_COND(wp, 0, mask2, o[0], mask1, s[0]);
145 STORE64_COND(wp, 1, mask2, o[1], mask1, s[1]);
146 STORE64_COND(wp, 2, mask2, o[2], mask1, s[2]);
147
148 w->nlimbs = 192 / BITS_PER_MPI_LIMB;
149 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
150 MPN_NORMALIZE (wp, w->nlimbs);
151 }
152
153 void
154 _gcry_mpi_ec_nist224_mod (gcry_mpi_t w, mpi_ec_t ctx)
155 {
156 static const mpi_limb64_t p_mult[5][4] =
157 {
158 { /* P * -1 */
159 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0xffffffffU),
160 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xffffffffU, 0x00000000U)
161 },
162 { /* P * 0 */
163 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
164 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U)
165 },
166 { /* P * 1 */
167 LIMB64_C(0x00000000U, 0x00000001U), LIMB64_C(0xffffffffU, 0x00000000U),
168 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0xffffffffU)
169 },
170 { /* P * 2 */
171 LIMB64_C(0x00000000U, 0x00000002U), LIMB64_C(0xfffffffeU, 0x00000000U),
172 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000001U, 0xffffffffU)
173 },
174 { /* P * 3 */
175 LIMB64_C(0x00000000U, 0x00000003U), LIMB64_C(0xfffffffdU, 0x00000000U),
176 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000002U, 0xffffffffU)
177 }
178 };
179 const mpi_limb64_t zero = LIMB_TO64(0);
180 mpi_ptr_t wp;
181 mpi_limb64_t s[(224 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64];
182 mpi_limb64_t d[DIM(s)];
183 const mpi_size_t wsize = DIM(s);
184 mpi_size_t psize = ctx->p->nlimbs;
185 mpi_limb_t mask1;
186 mpi_limb_t mask2;
187 mpi_limb_t s_is_negative;
188 int carry;
189
190 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2 * LIMBS_PER_LIMB64);
191 RESIZE_AND_CLEAR_IF_NEEDED (ctx->p, wsize * LIMBS_PER_LIMB64);
192 ctx->p->nlimbs = psize;
193
194 wp = w->d;
195
196 prefetch (p_mult, sizeof(p_mult));
197
198 /* See "FIPS 186-4, D.2.2 Curve P-224". */
199
200 /* "S1 + S2" with 64-bit limbs:
201 * [0:A10]:[ A9: A8]:[ A7:0]:[0:0]
202 * + [0:0]:[A13:A12]:[A11:0]:[0:0]
203 * => s[3]:s[2]:s[1]:s[0]
204 */
205 s[0] = zero;
206 ADD3_LIMB64 (s[3], s[2], s[1],
207 LIMB64_HILO(0, LOAD32(wp, 10)),
208 LOAD64(wp, 8 / 2),
209 LIMB64_HILO(LOAD32(wp, 7), 0),
210 zero,
211 LOAD64(wp, 12 / 2),
212 LIMB64_HILO(LOAD32(wp, 11), 0));
213
214 /* "T + S1 + S2" */
215 ADD4_LIMB64 (s[3], s[2], s[1], s[0],
216 s[3], s[2], s[1], s[0],
217 LIMB64_HILO(0, LOAD32(wp, 6)),
218 LOAD64(wp, 4 / 2),
219 LOAD64(wp, 2 / 2),
220 LOAD64(wp, 0 / 2));
221
222 /* "D1 + D2" with 64-bit limbs:
223 * [0:A13]:[A12:A11]:[A10: A9]:[ A8: A7]
224 * + [0:0]:[ 0: 0]:[ 0:A13]:[A12:A11]
225 * => d[3]:d[2]:d[1]:d[0]
226 */
227 ADD4_LIMB64 (d[3], d[2], d[1], d[0],
228 LIMB64_HILO(0, LOAD32(wp, 13)),
229 LOAD64_UNALIGNED(wp, 11 / 2),
230 LOAD64_UNALIGNED(wp, 9 / 2),
231 LOAD64_UNALIGNED(wp, 7 / 2),
232 zero,
233 zero,
234 LIMB64_HILO(0, LOAD32(wp, 13)),
235 LOAD64_UNALIGNED(wp, 11 / 2));
236
237 /* "T + S1 + S2 - D1 - D2" */
238 SUB4_LIMB64 (s[3], s[2], s[1], s[0],
239 s[3], s[2], s[1], s[0],
240 d[3], d[2], d[1], d[0]);
241
242 /* mod p:
243 * Upper 32-bits of 's[3]' holds carry value (-2..2).
244 * Subtract (carry + 1) * p. Result will be with in range -p...p.
245 * Handle result being negative with addition and conditional store. */
246
247 carry = HI32_LIMB64(s[3]);
248
249 SUB4_LIMB64 (s[3], s[2], s[1], s[0],
250 s[3], s[2], s[1], s[0],
251 p_mult[carry + 2][3], p_mult[carry + 2][2],
252 p_mult[carry + 2][1], p_mult[carry + 2][0]);
253
254 ADD4_LIMB64 (d[3], d[2], d[1], d[0],
255 s[3], s[2], s[1], s[0],
256 p_mult[0 + 2][3], p_mult[0 + 2][2],
257 p_mult[0 + 2][1], p_mult[0 + 2][0]);
258
259 s_is_negative = (HI32_LIMB64(s[3]) >> 31);
260
261 mask2 = ct_limb_gen_mask(s_is_negative);
262 mask1 = ct_limb_gen_inv_mask(s_is_negative);
263
264 STORE64_COND(wp, 0, mask2, d[0], mask1, s[0]);
265 STORE64_COND(wp, 1, mask2, d[1], mask1, s[1]);
266 STORE64_COND(wp, 2, mask2, d[2], mask1, s[2]);
267 STORE64_COND(wp, 3, mask2, d[3], mask1, s[3]);
268
269 w->nlimbs = (224 + BITS_PER_MPI_LIMB- 1) / BITS_PER_MPI_LIMB;
270 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
271 MPN_NORMALIZE (wp, w->nlimbs);
272 }
273
274 void
275 _gcry_mpi_ec_nist256_mod (gcry_mpi_t w, mpi_ec_t ctx)
276 {
277 static const mpi_limb64_t p_mult[12][5] =
278 {
279 { /* P * -3 */
280 LIMB64_C(0x00000000U, 0x00000003U), LIMB64_C(0xfffffffdU, 0x00000000U),
281 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000002U, 0xfffffffcU),
282 LIMB64_C(0xffffffffU, 0xfffffffdU)
283 },
284 { /* P * -2 */
285 LIMB64_C(0x00000000U, 0x00000002U), LIMB64_C(0xfffffffeU, 0x00000000U),
286 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000001U, 0xfffffffdU),
287 LIMB64_C(0xffffffffU, 0xfffffffeU)
288 },
289 { /* P * -1 */
290 LIMB64_C(0x00000000U, 0x00000001U), LIMB64_C(0xffffffffU, 0x00000000U),
291 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0xfffffffeU),
292 LIMB64_C(0xffffffffU, 0xffffffffU)
293 },
294 { /* P * 0 */
295 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
296 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
297 LIMB64_C(0x00000000U, 0x00000000U)
298 },
299 { /* P * 1 */
300 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0x00000000U, 0xffffffffU),
301 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xffffffffU, 0x00000001U),
302 LIMB64_C(0x00000000U, 0x00000000U)
303 },
304 { /* P * 2 */
305 LIMB64_C(0xffffffffU, 0xfffffffeU), LIMB64_C(0x00000001U, 0xffffffffU),
306 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffffeU, 0x00000002U),
307 LIMB64_C(0x00000000U, 0x00000001U)
308 },
309 { /* P * 3 */
310 LIMB64_C(0xffffffffU, 0xfffffffdU), LIMB64_C(0x00000002U, 0xffffffffU),
311 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffffdU, 0x00000003U),
312 LIMB64_C(0x00000000U, 0x00000002U)
313 },
314 { /* P * 4 */
315 LIMB64_C(0xffffffffU, 0xfffffffcU), LIMB64_C(0x00000003U, 0xffffffffU),
316 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffffcU, 0x00000004U),
317 LIMB64_C(0x00000000U, 0x00000003U)
318 },
319 { /* P * 5 */
320 LIMB64_C(0xffffffffU, 0xfffffffbU), LIMB64_C(0x00000004U, 0xffffffffU),
321 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffffbU, 0x00000005U),
322 LIMB64_C(0x00000000U, 0x00000004U)
323 },
324 { /* P * 6 */
325 LIMB64_C(0xffffffffU, 0xfffffffaU), LIMB64_C(0x00000005U, 0xffffffffU),
326 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffffaU, 0x00000006U),
327 LIMB64_C(0x00000000U, 0x00000005U)
328 },
329 { /* P * 7 */
330 LIMB64_C(0xffffffffU, 0xfffffff9U), LIMB64_C(0x00000006U, 0xffffffffU),
331 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0xfffffff9U, 0x00000007U),
332 LIMB64_C(0x00000000U, 0x00000006U)
333 }
334 };
335 const mpi_limb64_t zero = LIMB_TO64(0);
336 mpi_ptr_t wp;
337 mpi_limb64_t s[(256 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64 + 1];
338 mpi_limb64_t t[DIM(s)];
339 mpi_limb64_t d[DIM(s)];
340 mpi_limb64_t e[DIM(s)];
341 const mpi_size_t wsize = DIM(s) - 1;
342 mpi_size_t psize = ctx->p->nlimbs;
343 mpi_limb_t mask1;
344 mpi_limb_t mask2;
345 mpi_limb_t mask3;
346 mpi_limb_t s_is_negative;
347 mpi_limb_t d_is_negative;
348 int carry;
349
350 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2 * LIMBS_PER_LIMB64);
351 RESIZE_AND_CLEAR_IF_NEEDED (ctx->p, wsize * LIMBS_PER_LIMB64);
352 ctx->p->nlimbs = psize;
353
354 wp = w->d;
355
356 prefetch (p_mult, sizeof(p_mult));
357
358 /* See "FIPS 186-4, D.2.3 Curve P-256". */
359
360 /* "S1 + S2" with 64-bit limbs:
361 * [A15:A14]:[A13:A12]:[A11:0]:[0:0]
362 * + [0:A15]:[A14:A13]:[A12:0]:[0:0]
363 * => s[4]:s[3]:s[2]:s[1]:s[0]
364 */
365 s[0] = zero;
366 ADD4_LIMB64 (s[4], s[3], s[2], s[1],
367 zero,
368 LOAD64(wp, 14 / 2),
369 LOAD64(wp, 12 / 2),
370 LIMB64_HILO(LOAD32(wp, 11), 0),
371 zero,
372 LIMB64_HILO(0, LOAD32(wp, 15)),
373 LOAD64_UNALIGNED(wp, 13 / 2),
374 LIMB64_HILO(LOAD32(wp, 12), 0));
375
376 /* "S3 + S4" with 64-bit limbs:
377 * [A15:A14]:[ 0: 0]:[ 0:A10]:[ A9:A8]
378 * + [A8:A13]:[A15:A14]:[A13:A11]:[A10:A9]
379 * => t[4]:t[3]:t[2]:t[1]:t[0]
380 */
381 ADD5_LIMB64 (t[4], t[3], t[2], t[1], t[0],
382 zero,
383 LOAD64(wp, 14 / 2),
384 zero,
385 LIMB64_HILO(0, LOAD32(wp, 10)),
386 LOAD64(wp, 8 / 2),
387 zero,
388 LIMB64_HILO(LOAD32(wp, 8), LOAD32(wp, 13)),
389 LOAD64(wp, 14 / 2),
390 LIMB64_HILO(LOAD32(wp, 13), LOAD32(wp, 11)),
391 LOAD64_UNALIGNED(wp, 9 / 2));
392
393 /* "2*S1 + 2*S2" */
394 ADD5_LIMB64 (s[4], s[3], s[2], s[1], s[0],
395 s[4], s[3], s[2], s[1], s[0],
396 s[4], s[3], s[2], s[1], s[0]);
397
398 /* "T + S3 + S4" */
399 ADD5_LIMB64 (t[4], t[3], t[2], t[1], t[0],
400 t[4], t[3], t[2], t[1], t[0],
401 zero,
402 LOAD64(wp, 6 / 2),
403 LOAD64(wp, 4 / 2),
404 LOAD64(wp, 2 / 2),
405 LOAD64(wp, 0 / 2));
406
407 /* "2*S1 + 2*S2 - D3" with 64-bit limbs:
408 * s[4]: s[3]: s[2]: s[1]: s[0]
409 * - [A12:0]:[A10:A9]:[A8:A15]:[A14:A13]
410 * => s[4]:s[3]:s[2]:s[1]:s[0]
411 */
412 SUB5_LIMB64 (s[4], s[3], s[2], s[1], s[0],
413 s[4], s[3], s[2], s[1], s[0],
414 zero,
415 LIMB64_HILO(LOAD32(wp, 12), 0),
416 LOAD64_UNALIGNED(wp, 9 / 2),
417 LIMB64_HILO(LOAD32(wp, 8), LOAD32(wp, 15)),
418 LOAD64_UNALIGNED(wp, 13 / 2));
419
420 /* "T + 2*S1 + 2*S2 + S3 + S4 - D3" */
421 ADD5_LIMB64 (s[4], s[3], s[2], s[1], s[0],
422 s[4], s[3], s[2], s[1], s[0],
423 t[4], t[3], t[2], t[1], t[0]);
424
425 /* "D1 + D2" with 64-bit limbs:
426 * [0:A13]:[A12:A11] + [A15:A14]:[A13:A12] => d[2]:d[1]:d[0]
427 * [A10:A8] + [A11:A9] => d[4]:d[3]
428 */
429 ADD3_LIMB64 (d[2], d[1], d[0],
430 zero,
431 LIMB64_HILO(0, LOAD32(wp, 13)),
432 LOAD64_UNALIGNED(wp, 11 / 2),
433 zero,
434 LOAD64(wp, 14 / 2),
435 LOAD64(wp, 12 / 2));
436 ADD2_LIMB64 (d[4], d[3],
437 zero, LIMB64_HILO(LOAD32(wp, 10), LOAD32(wp, 8)),
438 zero, LIMB64_HILO(LOAD32(wp, 11), LOAD32(wp, 9)));
439
440 /* "D1 + D2 + D4" with 64-bit limbs:
441 * d[4]: d[3]: d[2]: d[1]: d[0]
442 * - [A13:0]:[A11:A10]:[A9:0]:[A15:A14]
443 * => d[4]:d[3]:d[2]:d[1]:d[0]
444 */
445 ADD5_LIMB64 (d[4], d[3], d[2], d[1], d[0],
446 d[4], d[3], d[2], d[1], d[0],
447 zero,
448 LIMB64_HILO(LOAD32(wp, 13), 0),
449 LOAD64(wp, 10 / 2),
450 LIMB64_HILO(LOAD32(wp, 9), 0),
451 LOAD64(wp, 14 / 2));
452
453 /* "T + 2*S1 + 2*S2 + S3 + S4 - D1 - D2 - D3 - D4" */
454 SUB5_LIMB64 (s[4], s[3], s[2], s[1], s[0],
455 s[4], s[3], s[2], s[1], s[0],
456 d[4], d[3], d[2], d[1], d[0]);
457
458 /* mod p:
459 * 's[4]' holds carry value (-4..6). Subtract (carry + 1) * p. Result
460 * will be with in range -2*p...p. Handle result being negative with
461 * addition and conditional store. */
462
463 carry = LO32_LIMB64(s[4]);
464
465 /* Load values to stack to ease register pressure on i386. */
466 e[0] = p_mult[carry + 4][0];
467 e[1] = p_mult[carry + 4][1];
468 e[2] = p_mult[carry + 4][2];
469 e[3] = p_mult[carry + 4][3];
470 e[4] = p_mult[carry + 4][4];
471 SUB5_LIMB64 (s[4], s[3], s[2], s[1], s[0],
472 s[4], s[3], s[2], s[1], s[0],
473 e[4], e[3], e[2], e[1], e[0]);
474
475 /* Add 1*P */
476 ADD5_LIMB64 (d[4], d[3], d[2], d[1], d[0],
477 s[4], s[3], s[2], s[1], s[0],
478 zero,
479 p_mult[0 + 4][3], p_mult[0 + 4][2],
480 p_mult[0 + 4][1], p_mult[0 + 4][0]);
481
482 /* Add 2*P */
483 ADD5_LIMB64 (e[4], e[3], e[2], e[1], e[0],
484 s[4], s[3], s[2], s[1], s[0],
485 zero,
486 p_mult[1 + 4][3], p_mult[1 + 4][2],
487 p_mult[1 + 4][1], p_mult[1 + 4][0]);
488
489 s_is_negative = LO32_LIMB64(s[4]) >> 31;
490 d_is_negative = LO32_LIMB64(d[4]) >> 31;
491 mask3 = ct_limb_gen_mask(d_is_negative);
492 mask2 = ct_limb_gen_mask(s_is_negative) & ~mask3;
493 mask1 = ct_limb_gen_inv_mask(s_is_negative) & ~mask3;
494
495 s[0] = LIMB_OR64(MASK_AND64(mask2, d[0]), MASK_AND64(mask1, s[0]));
496 s[1] = LIMB_OR64(MASK_AND64(mask2, d[1]), MASK_AND64(mask1, s[1]));
497 s[2] = LIMB_OR64(MASK_AND64(mask2, d[2]), MASK_AND64(mask1, s[2]));
498 s[3] = LIMB_OR64(MASK_AND64(mask2, d[3]), MASK_AND64(mask1, s[3]));
499 s[0] = LIMB_OR64(MASK_AND64(mask3, e[0]), s[0]);
500 s[1] = LIMB_OR64(MASK_AND64(mask3, e[1]), s[1]);
501 s[2] = LIMB_OR64(MASK_AND64(mask3, e[2]), s[2]);
502 s[3] = LIMB_OR64(MASK_AND64(mask3, e[3]), s[3]);
503
504 STORE64(wp, 0, s[0]);
505 STORE64(wp, 1, s[1]);
506 STORE64(wp, 2, s[2]);
507 STORE64(wp, 3, s[3]);
508
509 w->nlimbs = wsize * LIMBS_PER_LIMB64;
510 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
511 MPN_NORMALIZE (wp, w->nlimbs);
512 }
513
514 void
515 _gcry_mpi_ec_nist384_mod (gcry_mpi_t w, mpi_ec_t ctx)
516 {
517 static const mpi_limb64_t p_mult[11][7] =
518 {
519 { /* P * -2 */
520 LIMB64_C(0xfffffffeU, 0x00000002U), LIMB64_C(0x00000001U, 0xffffffffU),
521 LIMB64_C(0x00000000U, 0x00000002U), LIMB64_C(0x00000000U, 0x00000000U),
522 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
523 LIMB64_C(0xffffffffU, 0xfffffffeU)
524 },
525 { /* P * -1 */
526 LIMB64_C(0xffffffffU, 0x00000001U), LIMB64_C(0x00000000U, 0xffffffffU),
527 LIMB64_C(0x00000000U, 0x00000001U), LIMB64_C(0x00000000U, 0x00000000U),
528 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
529 LIMB64_C(0xffffffffU, 0xffffffffU)
530 },
531 { /* P * 0 */
532 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
533 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
534 LIMB64_C(0x00000000U, 0x00000000U), LIMB64_C(0x00000000U, 0x00000000U),
535 LIMB64_C(0x00000000U, 0x00000000U)
536 },
537 { /* P * 1 */
538 LIMB64_C(0x00000000U, 0xffffffffU), LIMB64_C(0xffffffffU, 0x00000000U),
539 LIMB64_C(0xffffffffU, 0xfffffffeU), LIMB64_C(0xffffffffU, 0xffffffffU),
540 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
541 LIMB64_C(0x00000000U, 0x00000000U)
542 },
543 { /* P * 2 */
544 LIMB64_C(0x00000001U, 0xfffffffeU), LIMB64_C(0xfffffffeU, 0x00000000U),
545 LIMB64_C(0xffffffffU, 0xfffffffdU), LIMB64_C(0xffffffffU, 0xffffffffU),
546 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
547 LIMB64_C(0x00000000U, 0x00000001U)
548 },
549 { /* P * 3 */
550 LIMB64_C(0x00000002U, 0xfffffffdU), LIMB64_C(0xfffffffdU, 0x00000000U),
551 LIMB64_C(0xffffffffU, 0xfffffffcU), LIMB64_C(0xffffffffU, 0xffffffffU),
552 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
553 LIMB64_C(0x00000000U, 0x00000002U)
554 },
555 { /* P * 4 */
556 LIMB64_C(0x00000003U, 0xfffffffcU), LIMB64_C(0xfffffffcU, 0x00000000U),
557 LIMB64_C(0xffffffffU, 0xfffffffbU), LIMB64_C(0xffffffffU, 0xffffffffU),
558 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
559 LIMB64_C(0x00000000U, 0x00000003U)
560 },
561 { /* P * 5 */
562 LIMB64_C(0x00000004U, 0xfffffffbU), LIMB64_C(0xfffffffbU, 0x00000000U),
563 LIMB64_C(0xffffffffU, 0xfffffffaU), LIMB64_C(0xffffffffU, 0xffffffffU),
564 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
565 LIMB64_C(0x00000000U, 0x00000004U)
566 },
567 { /* P * 6 */
568 LIMB64_C(0x00000005U, 0xfffffffaU), LIMB64_C(0xfffffffaU, 0x00000000U),
569 LIMB64_C(0xffffffffU, 0xfffffff9U), LIMB64_C(0xffffffffU, 0xffffffffU),
570 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
571 LIMB64_C(0x00000000U, 0x00000005U)
572 },
573 { /* P * 7 */
574 LIMB64_C(0x00000006U, 0xfffffff9U), LIMB64_C(0xfffffff9U, 0x00000000U),
575 LIMB64_C(0xffffffffU, 0xfffffff8U), LIMB64_C(0xffffffffU, 0xffffffffU),
576 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
577 LIMB64_C(0x00000000U, 0x00000006U)
578 },
579 { /* P * 8 */
580 LIMB64_C(0x00000007U, 0xfffffff8U), LIMB64_C(0xfffffff8U, 0x00000000U),
581 LIMB64_C(0xffffffffU, 0xfffffff7U), LIMB64_C(0xffffffffU, 0xffffffffU),
582 LIMB64_C(0xffffffffU, 0xffffffffU), LIMB64_C(0xffffffffU, 0xffffffffU),
583 LIMB64_C(0x00000000U, 0x00000007U)
584 },
585 };
586 const mpi_limb64_t zero = LIMB_TO64(0);
587 mpi_ptr_t wp;
588 mpi_limb64_t s[(384 + BITS_PER_MPI_LIMB64 - 1) / BITS_PER_MPI_LIMB64 + 1];
589 mpi_limb64_t t[DIM(s)];
590 mpi_limb64_t d[DIM(s)];
591 mpi_limb64_t x[DIM(s)];
592 #if (BITS_PER_MPI_LIMB64 == BITS_PER_MPI_LIMB) && defined(WORDS_BIGENDIAN)
593 mpi_limb_t wp_shr32[(DIM(s) - 1) * LIMBS_PER_LIMB64];
594 #endif
595 const mpi_size_t wsize = DIM(s) - 1;
596 mpi_size_t psize = ctx->p->nlimbs;
597 mpi_limb_t mask1;
598 mpi_limb_t mask2;
599 mpi_limb_t s_is_negative;
600 int carry;
601
602 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2 * LIMBS_PER_LIMB64);
603 RESIZE_AND_CLEAR_IF_NEEDED (ctx->p, wsize * LIMBS_PER_LIMB64);
604 ctx->p->nlimbs = psize;
605
606 wp = w->d;
607
608 prefetch (p_mult, sizeof(p_mult));
609
610 /* See "FIPS 186-4, D.2.4 Curve P-384". */
611
612 #if BITS_PER_MPI_LIMB64 == BITS_PER_MPI_LIMB
613 # ifdef WORDS_BIGENDIAN
614 # define LOAD64_SHR32(idx) LOAD64(wp_shr32, ((idx) / 2 - wsize))
615 _gcry_mpih_rshift (wp_shr32, wp + 384 / BITS_PER_MPI_LIMB,
616 wsize * LIMBS_PER_LIMB64, 32);
617 # else
618 # define LOAD64_SHR32(idx) LOAD64_UNALIGNED(wp, idx / 2)
619 #endif
620 #else
621 # define LOAD64_SHR32(idx) LIMB64_HILO(LOAD32(wp, (idx) + 1), LOAD32(wp, idx))
622 #endif
623
624 /* "S1 + S1" with 64-bit limbs:
625 * [0:A23]:[A22:A21]
626 * + [0:A23]:[A22:A21]
627 * => s[3]:s[2]
628 */
629 ADD2_LIMB64 (s[3], s[2],
630 LIMB64_HILO(0, LOAD32(wp, 23)),
631 LOAD64_SHR32(21),
632 LIMB64_HILO(0, LOAD32(wp, 23)),
633 LOAD64_SHR32(21));
634
635 /* "S5 + S6" with 64-bit limbs:
636 * [A23:A22]:[A21:A20]:[ 0:0]:[0: 0]
637 * + [ 0: 0]:[A23:A22]:[A21:0]:[0:A20]
638 * => x[4]:x[3]:x[2]:x[1]:x[0]
639 */
640 x[0] = LIMB64_HILO(0, LOAD32(wp, 20));
641 x[1] = LIMB64_HILO(LOAD32(wp, 21), 0);
642 ADD3_LIMB64 (x[4], x[3], x[2],
643 zero, LOAD64(wp, 22 / 2), LOAD64(wp, 20 / 2),
644 zero, zero, LOAD64(wp, 22 / 2));
645
646 /* "D2 + D3" with 64-bit limbs:
647 * [0:A23]:[A22:A21]:[A20:0]
648 * + [0:A23]:[A23:0]:[0:0]
649 * => d[2]:d[1]:d[0]
650 */
651 d[0] = LIMB64_HILO(LOAD32(wp, 20), 0);
652 ADD2_LIMB64 (d[2], d[1],
653 LIMB64_HILO(0, LOAD32(wp, 23)),
654 LOAD64_SHR32(21),
655 LIMB64_HILO(0, LOAD32(wp, 23)),
656 LIMB64_HILO(LOAD32(wp, 23), 0));
657
658 /* "2*S1 + S5 + S6" with 64-bit limbs:
659 * s[4]:s[3]:s[2]:s[1]:s[0]
660 * + x[4]:x[3]:x[2]:x[1]:x[0]
661 * => s[4]:s[3]:s[2]:s[1]:s[0]
662 */
663 s[0] = x[0];
664 s[1] = x[1];
665 ADD3_LIMB64(s[4], s[3], s[2],
666 zero, s[3], s[2],
667 x[4], x[3], x[2]);
668
669 /* "T + S2" with 64-bit limbs:
670 * [A11:A10]:[ A9: A8]:[ A7: A6]:[ A5: A4]:[ A3: A2]:[ A1: A0]
671 * + [A23:A22]:[A21:A20]:[A19:A18]:[A17:A16]:[A15:A14]:[A13:A12]
672 * => t[6]:t[5]:t[4]:t[3]:t[2]:t[1]:t[0]
673 */
674 ADD7_LIMB64 (t[6], t[5], t[4], t[3], t[2], t[1], t[0],
675 zero,
676 LOAD64(wp, 10 / 2), LOAD64(wp, 8 / 2), LOAD64(wp, 6 / 2),
677 LOAD64(wp, 4 / 2), LOAD64(wp, 2 / 2), LOAD64(wp, 0 / 2),
678 zero,
679 LOAD64(wp, 22 / 2), LOAD64(wp, 20 / 2), LOAD64(wp, 18 / 2),
680 LOAD64(wp, 16 / 2), LOAD64(wp, 14 / 2), LOAD64(wp, 12 / 2));
681
682 /* "2*S1 + S4 + S5 + S6" with 64-bit limbs:
683 * s[6]: s[5]: s[4]: s[3]: s[2]: s[1]: s[0]
684 * + [A19:A18]:[A17:A16]:[A15:A14]:[A13:A12]:[A20:0]:[A23:0]
685 * => s[6]:s[5]:s[4]:s[3]:s[2]:s[1]:s[0]
686 */
687 ADD7_LIMB64 (s[6], s[5], s[4], s[3], s[2], s[1], s[0],
688 zero, zero, s[4], s[3], s[2], s[1], s[0],
689 zero,
690 LOAD64(wp, 18 / 2), LOAD64(wp, 16 / 2),
691 LOAD64(wp, 14 / 2), LOAD64(wp, 12 / 2),
692 LIMB64_HILO(LOAD32(wp, 20), 0),
693 LIMB64_HILO(LOAD32(wp, 23), 0));
694
695 /* "D1 + D2 + D3" with 64-bit limbs:
696 * d[6]: d[5]: d[4]: d[3]: d[2]: d[1]: d[0]
697 * + [A22:A21]:[A20:A19]:[A18:A17]:[A16:A15]:[A14:A13]:[A12:A23]
698 * => d[6]:d[5]:d[4]:d[3]:d[2]:d[1]:d[0]
699 */
700 ADD7_LIMB64 (d[6], d[5], d[4], d[3], d[2], d[1], d[0],
701 zero, zero, zero, zero, d[2], d[1], d[0],
702 zero,
703 LOAD64_SHR32(21),
704 LOAD64_SHR32(19),
705 LOAD64_SHR32(17),
706 LOAD64_SHR32(15),
707 LOAD64_SHR32(13),
708 LIMB64_HILO(LOAD32(wp, 12), LOAD32(wp, 23)));
709
710 /* "2*S1 + S3 + S4 + S5 + S6" with 64-bit limbs:
711 * s[6]: s[5]: s[4]: s[3]: s[2]: s[1]: s[0]
712 * + [A20:A19]:[A18:A17]:[A16:A15]:[A14:A13]:[A12:A23]:[A22:A21]
713 * => s[6]:s[5]:s[4]:s[3]:s[2]:s[1]:s[0]
714 */
715 ADD7_LIMB64 (s[6], s[5], s[4], s[3], s[2], s[1], s[0],
716 s[6], s[5], s[4], s[3], s[2], s[1], s[0],
717 zero,
718 LOAD64_SHR32(19),
719 LOAD64_SHR32(17),
720 LOAD64_SHR32(15),
721 LOAD64_SHR32(13),
722 LIMB64_HILO(LOAD32(wp, 12), LOAD32(wp, 23)),
723 LOAD64_SHR32(21));
724
725 /* "T + 2*S1 + S2 + S3 + S4 + S5 + S6" */
726 ADD7_LIMB64 (s[6], s[5], s[4], s[3], s[2], s[1], s[0],
727 s[6], s[5], s[4], s[3], s[2], s[1], s[0],
728 t[6], t[5], t[4], t[3], t[2], t[1], t[0]);
729
730 /* "T + 2*S1 + S2 + S3 + S4 + S5 + S6 - D1 - D2 - D3" */
731 SUB7_LIMB64 (s[6], s[5], s[4], s[3], s[2], s[1], s[0],
732 s[6], s[5], s[4], s[3], s[2], s[1], s[0],
733 d[6], d[5], d[4], d[3], d[2], d[1], d[0]);
734
735 #undef LOAD64_SHR32
736
737 /* mod p:
738 * 's[6]' holds carry value (-3..7). Subtract (carry + 1) * p. Result
739 * will be with in range -p...p. Handle result being negative with
740 * addition and conditional store. */
741
742 carry = LO32_LIMB64(s[6]);
743
744 /* Load values to stack to ease register pressure on i386. */
745 x[0] = p_mult[carry + 3][0];
746 x[1] = p_mult[carry + 3][1];
747 x[2] = p_mult[carry + 3][2];
748 x[3] = p_mult[carry + 3][3];
749 x[4] = p_mult[carry + 3][4];
750 x[5] = p_mult[carry + 3][5];
751 x[6] = p_mult[carry + 3][6];
752 SUB7_LIMB64 (s[6], s[5], s[4], s[3], s[2], s[1], s[0],
753 s[6], s[5], s[4], s[3], s[2], s[1], s[0],
754 x[6], x[5], x[4], x[3], x[2], x[1], x[0]);
755
756 ADD7_LIMB64 (d[6], d[5], d[4], d[3], d[2], d[1], d[0],
757 s[6], s[5], s[4], s[3], s[2], s[1], s[0],
758 zero,
759 p_mult[0 + 3][5], p_mult[0 + 3][4],
760 p_mult[0 + 3][3], p_mult[0 + 3][2],
761 p_mult[0 + 3][1], p_mult[0 + 3][0]);
762
763 s_is_negative = LO32_LIMB64(s[6]) >> 31;
764 mask2 = ct_limb_gen_mask(s_is_negative);
765 mask1 = ct_limb_gen_inv_mask(s_is_negative);
766
767 STORE64_COND(wp, 0, mask2, d[0], mask1, s[0]);
768 STORE64_COND(wp, 1, mask2, d[1], mask1, s[1]);
769 STORE64_COND(wp, 2, mask2, d[2], mask1, s[2]);
770 STORE64_COND(wp, 3, mask2, d[3], mask1, s[3]);
771 STORE64_COND(wp, 4, mask2, d[4], mask1, s[4]);
772 STORE64_COND(wp, 5, mask2, d[5], mask1, s[5]);
773
774 w->nlimbs = wsize * LIMBS_PER_LIMB64;
775 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
776 MPN_NORMALIZE (wp, w->nlimbs);
777
778 #if (BITS_PER_MPI_LIMB64 == BITS_PER_MPI_LIMB) && defined(WORDS_BIGENDIAN)
779 wipememory(wp_shr32, sizeof(wp_shr32));
780 #endif
781 }
782
783 void
784 _gcry_mpi_ec_nist521_mod (gcry_mpi_t w, mpi_ec_t ctx)
785 {
786 mpi_limb_t s[(521 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB];
787 const mpi_size_t wsize = DIM(s);
788 mpi_limb_t cy;
789 mpi_ptr_t wp;
790
791 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2);
792
793 wp = w->d;
794
795 /* See "FIPS 186-4, D.2.5 Curve P-521". */
796
797 _gcry_mpih_rshift (s, wp + wsize - 1, wsize, 521 % BITS_PER_MPI_LIMB);
798 s[wsize - 1] &= (1 << (521 % BITS_PER_MPI_LIMB)) - 1;
799 wp[wsize - 1] &= (1 << (521 % BITS_PER_MPI_LIMB)) - 1;
800 _gcry_mpih_add_n (wp, wp, s, wsize);
801
802 /* "mod p" */
803 cy = _gcry_mpih_sub_n (wp, wp, ctx->p->d, wsize);
804 _gcry_mpih_add_n (s, wp, ctx->p->d, wsize);
805 mpih_set_cond (wp, s, wsize, mpih_limb_is_not_zero (cy));
806
807 w->nlimbs = wsize;
808 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
809 MPN_NORMALIZE (wp, w->nlimbs);
810 }
811
812 #endif /* !ASM_DISABLED */