xref: /illumos-gate/usr/src/common/crypto/ecc/ecp_384.c (revision c40a6cd7)
1*c40a6cd7SToomas Soome /*
2f9fbec18Smcpowers  * ***** BEGIN LICENSE BLOCK *****
3f9fbec18Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4f9fbec18Smcpowers  *
5f9fbec18Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
6f9fbec18Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
7f9fbec18Smcpowers  * the License. You may obtain a copy of the License at
8f9fbec18Smcpowers  * http://www.mozilla.org/MPL/
9f9fbec18Smcpowers  *
10f9fbec18Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
11f9fbec18Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12f9fbec18Smcpowers  * for the specific language governing rights and limitations under the
13f9fbec18Smcpowers  * License.
14f9fbec18Smcpowers  *
15f9fbec18Smcpowers  * The Original Code is the elliptic curve math library for prime field curves.
16f9fbec18Smcpowers  *
17f9fbec18Smcpowers  * The Initial Developer of the Original Code is
18f9fbec18Smcpowers  * Sun Microsystems, Inc.
19f9fbec18Smcpowers  * Portions created by the Initial Developer are Copyright (C) 2003
20f9fbec18Smcpowers  * the Initial Developer. All Rights Reserved.
21f9fbec18Smcpowers  *
22f9fbec18Smcpowers  * Contributor(s):
23f9fbec18Smcpowers  *   Douglas Stebila <douglas@stebila.ca>
24f9fbec18Smcpowers  *
25f9fbec18Smcpowers  * Alternatively, the contents of this file may be used under the terms of
26f9fbec18Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
27f9fbec18Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28f9fbec18Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
29f9fbec18Smcpowers  * of those above. If you wish to allow use of your version of this file only
30f9fbec18Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
31f9fbec18Smcpowers  * use your version of this file under the terms of the MPL, indicate your
32f9fbec18Smcpowers  * decision by deleting the provisions above and replace them with the notice
33f9fbec18Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
34f9fbec18Smcpowers  * the provisions above, a recipient may use your version of this file under
35f9fbec18Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
36f9fbec18Smcpowers  *
37f9fbec18Smcpowers  * ***** END LICENSE BLOCK ***** */
38f9fbec18Smcpowers /*
39f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
40f9fbec18Smcpowers  * Use is subject to license terms.
41f9fbec18Smcpowers  *
42f9fbec18Smcpowers  * Sun elects to use this software under the MPL license.
43f9fbec18Smcpowers  */
44f9fbec18Smcpowers 
45f9fbec18Smcpowers #include "ecp.h"
46f9fbec18Smcpowers #include "mpi.h"
47f9fbec18Smcpowers #include "mplogic.h"
48f9fbec18Smcpowers #include "mpi-priv.h"
49f9fbec18Smcpowers #ifndef _KERNEL
50f9fbec18Smcpowers #include <stdlib.h>
51f9fbec18Smcpowers #endif
52f9fbec18Smcpowers 
53*c40a6cd7SToomas Soome /* Fast modular reduction for p384 = 2^384 - 2^128 - 2^96 + 2^32 - 1.  a can be r.
54*c40a6cd7SToomas Soome  * Uses algorithm 2.30 from Hankerson, Menezes, Vanstone. Guide to
55f9fbec18Smcpowers  * Elliptic Curve Cryptography. */
56f9fbec18Smcpowers mp_err
ec_GFp_nistp384_mod(const mp_int * a,mp_int * r,const GFMethod * meth)57f9fbec18Smcpowers ec_GFp_nistp384_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
58f9fbec18Smcpowers {
59f9fbec18Smcpowers 	mp_err res = MP_OKAY;
60f9fbec18Smcpowers 	int a_bits = mpl_significant_bits(a);
61f9fbec18Smcpowers 	int i;
62f9fbec18Smcpowers 
63f9fbec18Smcpowers 	/* m1, m2 are statically-allocated mp_int of exactly the size we need */
64f9fbec18Smcpowers 	mp_int m[10];
65f9fbec18Smcpowers 
66f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
67f9fbec18Smcpowers 	mp_digit s[10][12];
68f9fbec18Smcpowers 	for (i = 0; i < 10; i++) {
69f9fbec18Smcpowers 		MP_SIGN(&m[i]) = MP_ZPOS;
70f9fbec18Smcpowers 		MP_ALLOC(&m[i]) = 12;
71f9fbec18Smcpowers 		MP_USED(&m[i]) = 12;
72f9fbec18Smcpowers 		MP_DIGITS(&m[i]) = s[i];
73f9fbec18Smcpowers 	}
74f9fbec18Smcpowers #else
75f9fbec18Smcpowers 	mp_digit s[10][6];
76f9fbec18Smcpowers 	for (i = 0; i < 10; i++) {
77f9fbec18Smcpowers 		MP_SIGN(&m[i]) = MP_ZPOS;
78f9fbec18Smcpowers 		MP_ALLOC(&m[i]) = 6;
79f9fbec18Smcpowers 		MP_USED(&m[i]) = 6;
80f9fbec18Smcpowers 		MP_DIGITS(&m[i]) = s[i];
81f9fbec18Smcpowers 	}
82f9fbec18Smcpowers #endif
83f9fbec18Smcpowers 
84f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
85*c40a6cd7SToomas Soome 	/* for polynomials larger than twice the field size or polynomials
86f9fbec18Smcpowers 	 * not using all words, use regular reduction */
87f9fbec18Smcpowers 	if ((a_bits > 768) || (a_bits <= 736)) {
88f9fbec18Smcpowers 		MP_CHECKOK(mp_mod(a, &meth->irr, r));
89f9fbec18Smcpowers 	} else {
90f9fbec18Smcpowers 		for (i = 0; i < 12; i++) {
91f9fbec18Smcpowers 			s[0][i] = MP_DIGIT(a, i);
92f9fbec18Smcpowers 		}
93f9fbec18Smcpowers 		s[1][0] = 0;
94f9fbec18Smcpowers 		s[1][1] = 0;
95f9fbec18Smcpowers 		s[1][2] = 0;
96f9fbec18Smcpowers 		s[1][3] = 0;
97f9fbec18Smcpowers 		s[1][4] = MP_DIGIT(a, 21);
98f9fbec18Smcpowers 		s[1][5] = MP_DIGIT(a, 22);
99f9fbec18Smcpowers 		s[1][6] = MP_DIGIT(a, 23);
100f9fbec18Smcpowers 		s[1][7] = 0;
101f9fbec18Smcpowers 		s[1][8] = 0;
102f9fbec18Smcpowers 		s[1][9] = 0;
103f9fbec18Smcpowers 		s[1][10] = 0;
104f9fbec18Smcpowers 		s[1][11] = 0;
105f9fbec18Smcpowers 		for (i = 0; i < 12; i++) {
106f9fbec18Smcpowers 			s[2][i] = MP_DIGIT(a, i+12);
107f9fbec18Smcpowers 		}
108f9fbec18Smcpowers 		s[3][0] = MP_DIGIT(a, 21);
109f9fbec18Smcpowers 		s[3][1] = MP_DIGIT(a, 22);
110f9fbec18Smcpowers 		s[3][2] = MP_DIGIT(a, 23);
111f9fbec18Smcpowers 		for (i = 3; i < 12; i++) {
112f9fbec18Smcpowers 			s[3][i] = MP_DIGIT(a, i+9);
113f9fbec18Smcpowers 		}
114f9fbec18Smcpowers 		s[4][0] = 0;
115f9fbec18Smcpowers 		s[4][1] = MP_DIGIT(a, 23);
116f9fbec18Smcpowers 		s[4][2] = 0;
117f9fbec18Smcpowers 		s[4][3] = MP_DIGIT(a, 20);
118f9fbec18Smcpowers 		for (i = 4; i < 12; i++) {
119f9fbec18Smcpowers 			s[4][i] = MP_DIGIT(a, i+8);
120f9fbec18Smcpowers 		}
121f9fbec18Smcpowers 		s[5][0] = 0;
122f9fbec18Smcpowers 		s[5][1] = 0;
123f9fbec18Smcpowers 		s[5][2] = 0;
124f9fbec18Smcpowers 		s[5][3] = 0;
125f9fbec18Smcpowers 		s[5][4] = MP_DIGIT(a, 20);
126f9fbec18Smcpowers 		s[5][5] = MP_DIGIT(a, 21);
127f9fbec18Smcpowers 		s[5][6] = MP_DIGIT(a, 22);
128f9fbec18Smcpowers 		s[5][7] = MP_DIGIT(a, 23);
129f9fbec18Smcpowers 		s[5][8] = 0;
130f9fbec18Smcpowers 		s[5][9] = 0;
131f9fbec18Smcpowers 		s[5][10] = 0;
132f9fbec18Smcpowers 		s[5][11] = 0;
133f9fbec18Smcpowers 		s[6][0] = MP_DIGIT(a, 20);
134f9fbec18Smcpowers 		s[6][1] = 0;
135f9fbec18Smcpowers 		s[6][2] = 0;
136f9fbec18Smcpowers 		s[6][3] = MP_DIGIT(a, 21);
137f9fbec18Smcpowers 		s[6][4] = MP_DIGIT(a, 22);
138f9fbec18Smcpowers 		s[6][5] = MP_DIGIT(a, 23);
139f9fbec18Smcpowers 		s[6][6] = 0;
140f9fbec18Smcpowers 		s[6][7] = 0;
141f9fbec18Smcpowers 		s[6][8] = 0;
142f9fbec18Smcpowers 		s[6][9] = 0;
143f9fbec18Smcpowers 		s[6][10] = 0;
144f9fbec18Smcpowers 		s[6][11] = 0;
145f9fbec18Smcpowers 		s[7][0] = MP_DIGIT(a, 23);
146f9fbec18Smcpowers 		for (i = 1; i < 12; i++) {
147f9fbec18Smcpowers 			s[7][i] = MP_DIGIT(a, i+11);
148f9fbec18Smcpowers 		}
149f9fbec18Smcpowers 		s[8][0] = 0;
150f9fbec18Smcpowers 		s[8][1] = MP_DIGIT(a, 20);
151f9fbec18Smcpowers 		s[8][2] = MP_DIGIT(a, 21);
152f9fbec18Smcpowers 		s[8][3] = MP_DIGIT(a, 22);
153f9fbec18Smcpowers 		s[8][4] = MP_DIGIT(a, 23);
154f9fbec18Smcpowers 		s[8][5] = 0;
155f9fbec18Smcpowers 		s[8][6] = 0;
156f9fbec18Smcpowers 		s[8][7] = 0;
157f9fbec18Smcpowers 		s[8][8] = 0;
158f9fbec18Smcpowers 		s[8][9] = 0;
159f9fbec18Smcpowers 		s[8][10] = 0;
160f9fbec18Smcpowers 		s[8][11] = 0;
161f9fbec18Smcpowers 		s[9][0] = 0;
162f9fbec18Smcpowers 		s[9][1] = 0;
163f9fbec18Smcpowers 		s[9][2] = 0;
164f9fbec18Smcpowers 		s[9][3] = MP_DIGIT(a, 23);
165f9fbec18Smcpowers 		s[9][4] = MP_DIGIT(a, 23);
166f9fbec18Smcpowers 		s[9][5] = 0;
167f9fbec18Smcpowers 		s[9][6] = 0;
168f9fbec18Smcpowers 		s[9][7] = 0;
169f9fbec18Smcpowers 		s[9][8] = 0;
170f9fbec18Smcpowers 		s[9][9] = 0;
171f9fbec18Smcpowers 		s[9][10] = 0;
172f9fbec18Smcpowers 		s[9][11] = 0;
173f9fbec18Smcpowers 
174f9fbec18Smcpowers 		MP_CHECKOK(mp_add(&m[0], &m[1], r));
175f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[1], r));
176f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[2], r));
177f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[3], r));
178f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[4], r));
179f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[5], r));
180f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[6], r));
181f9fbec18Smcpowers 		MP_CHECKOK(mp_sub(r, &m[7], r));
182f9fbec18Smcpowers 		MP_CHECKOK(mp_sub(r, &m[8], r));
183f9fbec18Smcpowers 		MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
184f9fbec18Smcpowers 		s_mp_clamp(r);
185f9fbec18Smcpowers 	}
186f9fbec18Smcpowers #else
187*c40a6cd7SToomas Soome 	/* for polynomials larger than twice the field size or polynomials
188f9fbec18Smcpowers 	 * not using all words, use regular reduction */
189f9fbec18Smcpowers 	if ((a_bits > 768) || (a_bits <= 736)) {
190f9fbec18Smcpowers 		MP_CHECKOK(mp_mod(a, &meth->irr, r));
191f9fbec18Smcpowers 	} else {
192f9fbec18Smcpowers 		for (i = 0; i < 6; i++) {
193f9fbec18Smcpowers 			s[0][i] = MP_DIGIT(a, i);
194f9fbec18Smcpowers 		}
195f9fbec18Smcpowers 		s[1][0] = 0;
196f9fbec18Smcpowers 		s[1][1] = 0;
197f9fbec18Smcpowers 		s[1][2] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
198f9fbec18Smcpowers 		s[1][3] = MP_DIGIT(a, 11) >> 32;
199f9fbec18Smcpowers 		s[1][4] = 0;
200f9fbec18Smcpowers 		s[1][5] = 0;
201f9fbec18Smcpowers 		for (i = 0; i < 6; i++) {
202f9fbec18Smcpowers 			s[2][i] = MP_DIGIT(a, i+6);
203f9fbec18Smcpowers 		}
204f9fbec18Smcpowers 		s[3][0] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
205f9fbec18Smcpowers 		s[3][1] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
206f9fbec18Smcpowers 		for (i = 2; i < 6; i++) {
207f9fbec18Smcpowers 			s[3][i] = (MP_DIGIT(a, i+4) >> 32) | (MP_DIGIT(a, i+5) << 32);
208f9fbec18Smcpowers 		}
209f9fbec18Smcpowers 		s[4][0] = (MP_DIGIT(a, 11) >> 32) << 32;
210f9fbec18Smcpowers 		s[4][1] = MP_DIGIT(a, 10) << 32;
211f9fbec18Smcpowers 		for (i = 2; i < 6; i++) {
212f9fbec18Smcpowers 			s[4][i] = MP_DIGIT(a, i+4);
213f9fbec18Smcpowers 		}
214f9fbec18Smcpowers 		s[5][0] = 0;
215f9fbec18Smcpowers 		s[5][1] = 0;
216f9fbec18Smcpowers 		s[5][2] = MP_DIGIT(a, 10);
217f9fbec18Smcpowers 		s[5][3] = MP_DIGIT(a, 11);
218f9fbec18Smcpowers 		s[5][4] = 0;
219f9fbec18Smcpowers 		s[5][5] = 0;
220f9fbec18Smcpowers 		s[6][0] = (MP_DIGIT(a, 10) << 32) >> 32;
221f9fbec18Smcpowers 		s[6][1] = (MP_DIGIT(a, 10) >> 32) << 32;
222f9fbec18Smcpowers 		s[6][2] = MP_DIGIT(a, 11);
223f9fbec18Smcpowers 		s[6][3] = 0;
224f9fbec18Smcpowers 		s[6][4] = 0;
225f9fbec18Smcpowers 		s[6][5] = 0;
226f9fbec18Smcpowers 		s[7][0] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
227f9fbec18Smcpowers 		for (i = 1; i < 6; i++) {
228f9fbec18Smcpowers 			s[7][i] = (MP_DIGIT(a, i+5) >> 32) | (MP_DIGIT(a, i+6) << 32);
229f9fbec18Smcpowers 		}
230f9fbec18Smcpowers 		s[8][0] = MP_DIGIT(a, 10) << 32;
231f9fbec18Smcpowers 		s[8][1] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
232f9fbec18Smcpowers 		s[8][2] = MP_DIGIT(a, 11) >> 32;
233f9fbec18Smcpowers 		s[8][3] = 0;
234f9fbec18Smcpowers 		s[8][4] = 0;
235f9fbec18Smcpowers 		s[8][5] = 0;
236f9fbec18Smcpowers 		s[9][0] = 0;
237f9fbec18Smcpowers 		s[9][1] = (MP_DIGIT(a, 11) >> 32) << 32;
238f9fbec18Smcpowers 		s[9][2] = MP_DIGIT(a, 11) >> 32;
239f9fbec18Smcpowers 		s[9][3] = 0;
240f9fbec18Smcpowers 		s[9][4] = 0;
241f9fbec18Smcpowers 		s[9][5] = 0;
242f9fbec18Smcpowers 
243f9fbec18Smcpowers 		MP_CHECKOK(mp_add(&m[0], &m[1], r));
244f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[1], r));
245f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[2], r));
246f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[3], r));
247f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[4], r));
248f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[5], r));
249f9fbec18Smcpowers 		MP_CHECKOK(mp_add(r, &m[6], r));
250f9fbec18Smcpowers 		MP_CHECKOK(mp_sub(r, &m[7], r));
251f9fbec18Smcpowers 		MP_CHECKOK(mp_sub(r, &m[8], r));
252f9fbec18Smcpowers 		MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
253f9fbec18Smcpowers 		s_mp_clamp(r);
254f9fbec18Smcpowers 	}
255f9fbec18Smcpowers #endif
256f9fbec18Smcpowers 
257f9fbec18Smcpowers   CLEANUP:
258f9fbec18Smcpowers 	return res;
259f9fbec18Smcpowers }
260f9fbec18Smcpowers 
261f9fbec18Smcpowers /* Compute the square of polynomial a, reduce modulo p384. Store the
262*c40a6cd7SToomas Soome  * result in r.  r could be a.  Uses optimized modular reduction for p384.
263f9fbec18Smcpowers  */
264f9fbec18Smcpowers mp_err
ec_GFp_nistp384_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)265f9fbec18Smcpowers ec_GFp_nistp384_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
266f9fbec18Smcpowers {
267f9fbec18Smcpowers 	mp_err res = MP_OKAY;
268f9fbec18Smcpowers 
269f9fbec18Smcpowers 	MP_CHECKOK(mp_sqr(a, r));
270f9fbec18Smcpowers 	MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
271f9fbec18Smcpowers   CLEANUP:
272f9fbec18Smcpowers 	return res;
273f9fbec18Smcpowers }
274f9fbec18Smcpowers 
275f9fbec18Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p384.
276f9fbec18Smcpowers  * Store the result in r.  r could be a or b; a could be b.  Uses
277f9fbec18Smcpowers  * optimized modular reduction for p384. */
278f9fbec18Smcpowers mp_err
ec_GFp_nistp384_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)279f9fbec18Smcpowers ec_GFp_nistp384_mul(const mp_int *a, const mp_int *b, mp_int *r,
280f9fbec18Smcpowers 					const GFMethod *meth)
281f9fbec18Smcpowers {
282f9fbec18Smcpowers 	mp_err res = MP_OKAY;
283f9fbec18Smcpowers 
284f9fbec18Smcpowers 	MP_CHECKOK(mp_mul(a, b, r));
285f9fbec18Smcpowers 	MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
286f9fbec18Smcpowers   CLEANUP:
287f9fbec18Smcpowers 	return res;
288f9fbec18Smcpowers }
289f9fbec18Smcpowers 
290f9fbec18Smcpowers /* Wire in fast field arithmetic and precomputation of base point for
291f9fbec18Smcpowers  * named curves. */
292f9fbec18Smcpowers mp_err
ec_group_set_gfp384(ECGroup * group,ECCurveName name)293f9fbec18Smcpowers ec_group_set_gfp384(ECGroup *group, ECCurveName name)
294f9fbec18Smcpowers {
295f9fbec18Smcpowers 	if (name == ECCurve_NIST_P384) {
296f9fbec18Smcpowers 		group->meth->field_mod = &ec_GFp_nistp384_mod;
297f9fbec18Smcpowers 		group->meth->field_mul = &ec_GFp_nistp384_mul;
298f9fbec18Smcpowers 		group->meth->field_sqr = &ec_GFp_nistp384_sqr;
299f9fbec18Smcpowers 	}
300f9fbec18Smcpowers 	return MP_OKAY;
301f9fbec18Smcpowers }
302