1/*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * Use is subject to license terms.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* *********************************************************************
25 *
26 * The Original Code is the elliptic curve math library for prime field curves.
27 *
28 * The Initial Developer of the Original Code is
29 * Sun Microsystems, Inc.
30 * Portions created by the Initial Developer are Copyright (C) 2003
31 * the Initial Developer. All Rights Reserved.
32 *
33 * Contributor(s):
34 * Douglas Stebila <douglas@stebila.ca>
35 *
36 *********************************************************************** */
37
38#include "ecp.h"
39#include "mpi.h"
40#include "mplogic.h"
41#include "mpi-priv.h"
42#ifndef _KERNEL
43#include <stdlib.h>
44#endif
45
46#define ECP521_DIGITS ECL_CURVE_DIGITS(521)
47
48/* Fast modular reduction for p521 = 2^521 - 1. a can be r. Uses
49 * algorithm 2.31 from Hankerson, Menezes, Vanstone. Guide to
50 * Elliptic Curve Cryptography. */
51mp_err
52ec_GFp_nistp521_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
53{
54 mp_err res = MP_OKAY;
55 int a_bits = mpl_significant_bits(a);
56 unsigned int i;
57
58 /* m1, m2 are statically-allocated mp_int of exactly the size we need */
59 mp_int m1;
60
61 mp_digit s1[ECP521_DIGITS] = { 0 };
62
63 MP_SIGN(&m1) = MP_ZPOS;
64 MP_ALLOC(&m1) = ECP521_DIGITS;
65 MP_USED(&m1) = ECP521_DIGITS;
66 MP_DIGITS(&m1) = s1;
67
68 if (a_bits < 521) {
69 if (a==r) return MP_OKAY;
70 return mp_copy(a, r);
71 }
72 /* for polynomials larger than twice the field size or polynomials
73 * not using all words, use regular reduction */
74 if (a_bits > (521*2)) {
75 MP_CHECKOK(mp_mod(a, &meth->irr, r));
76 } else {
77#define FIRST_DIGIT (ECP521_DIGITS-1)
78 for (i = FIRST_DIGIT; i < MP_USED(a)-1; i++) {
79 s1[i-FIRST_DIGIT] = (MP_DIGIT(a, i) >> 9)
80 | (MP_DIGIT(a, 1+i) << (MP_DIGIT_BIT-9));
81 }
82 s1[i-FIRST_DIGIT] = MP_DIGIT(a, i) >> 9;
83
84 if ( a != r ) {
85 MP_CHECKOK(s_mp_pad(r,ECP521_DIGITS));
86 for (i = 0; i < ECP521_DIGITS; i++) {
87 MP_DIGIT(r,i) = MP_DIGIT(a, i);
88 }
89 }
90 MP_USED(r) = ECP521_DIGITS;
91 MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
92
93 MP_CHECKOK(s_mp_add(r, &m1));
94 if (MP_DIGIT(r, FIRST_DIGIT) & 0x200) {
95 MP_CHECKOK(s_mp_add_d(r,1));
96 MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
97 }
98 s_mp_clamp(r);
99 }
100
101 CLEANUP:
102 return res;
103}
104
105/* Compute the square of polynomial a, reduce modulo p521. Store the
106 * result in r. r could be a. Uses optimized modular reduction for p521.
107 */
108mp_err
109ec_GFp_nistp521_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
110{
111 mp_err res = MP_OKAY;
112
113 MP_CHECKOK(mp_sqr(a, r));
114 MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
115 CLEANUP:
116 return res;
117}
118
119/* Compute the product of two polynomials a and b, reduce modulo p521.
120 * Store the result in r. r could be a or b; a could be b. Uses
121 * optimized modular reduction for p521. */
122mp_err
123ec_GFp_nistp521_mul(const mp_int *a, const mp_int *b, mp_int *r,
124 const GFMethod *meth)
125{
126 mp_err res = MP_OKAY;
127
128 MP_CHECKOK(mp_mul(a, b, r));
129 MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
130 CLEANUP:
131 return res;
132}
133
134/* Divides two field elements. If a is NULL, then returns the inverse of
135 * b. */
136mp_err
137ec_GFp_nistp521_div(const mp_int *a, const mp_int *b, mp_int *r,
138 const GFMethod *meth)
139{
140 mp_err res = MP_OKAY;
141 mp_int t;
142
143 /* If a is NULL, then return the inverse of b, otherwise return a/b. */
144 if (a == NULL) {
145 return mp_invmod(b, &meth->irr, r);
146 } else {
147 /* MPI doesn't support divmod, so we implement it using invmod and
148 * mulmod. */
149 MP_CHECKOK(mp_init(&t, FLAG(b)));
150 MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
151 MP_CHECKOK(mp_mul(a, &t, r));
152 MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
153 CLEANUP:
154 mp_clear(&t);
155 return res;
156 }
157}
158
159/* Wire in fast field arithmetic and precomputation of base point for
160 * named curves. */
161mp_err
162ec_group_set_gfp521(ECGroup *group, ECCurveName name)
163{
164 if (name == ECCurve_NIST_P521) {
165 group->meth->field_mod = &ec_GFp_nistp521_mod;
166 group->meth->field_mul = &ec_GFp_nistp521_mul;
167 group->meth->field_sqr = &ec_GFp_nistp521_sqr;
168 group->meth->field_div = &ec_GFp_nistp521_div;
169 }
170 return MP_OKAY;
171}
172