xref: /illumos-gate/usr/src/common/crypto/ecc/ec2_193.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1f9fbec18Smcpowers /*
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 binary polynomial 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  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
24f9fbec18Smcpowers  *   Stephen Fung <fungstep@hotmail.com>, and
25f9fbec18Smcpowers  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
26f9fbec18Smcpowers  *
27f9fbec18Smcpowers  * Alternatively, the contents of this file may be used under the terms of
28f9fbec18Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
29f9fbec18Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30f9fbec18Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
31f9fbec18Smcpowers  * of those above. If you wish to allow use of your version of this file only
32f9fbec18Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
33f9fbec18Smcpowers  * use your version of this file under the terms of the MPL, indicate your
34f9fbec18Smcpowers  * decision by deleting the provisions above and replace them with the notice
35f9fbec18Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
36f9fbec18Smcpowers  * the provisions above, a recipient may use your version of this file under
37f9fbec18Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
38f9fbec18Smcpowers  *
39f9fbec18Smcpowers  * ***** END LICENSE BLOCK ***** */
40f9fbec18Smcpowers /*
41f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
42f9fbec18Smcpowers  * Use is subject to license terms.
43f9fbec18Smcpowers  *
44f9fbec18Smcpowers  * Sun elects to use this software under the MPL license.
45f9fbec18Smcpowers  */
46f9fbec18Smcpowers 
47f9fbec18Smcpowers #include "ec2.h"
48f9fbec18Smcpowers #include "mp_gf2m.h"
49f9fbec18Smcpowers #include "mp_gf2m-priv.h"
50f9fbec18Smcpowers #include "mpi.h"
51f9fbec18Smcpowers #include "mpi-priv.h"
52f9fbec18Smcpowers #ifndef _KERNEL
53f9fbec18Smcpowers #include <stdlib.h>
54f9fbec18Smcpowers #endif
55f9fbec18Smcpowers 
56f9fbec18Smcpowers /* Fast reduction for polynomials over a 193-bit curve. Assumes reduction
57f9fbec18Smcpowers  * polynomial with terms {193, 15, 0}. */
58f9fbec18Smcpowers mp_err
ec_GF2m_193_mod(const mp_int * a,mp_int * r,const GFMethod * meth)59f9fbec18Smcpowers ec_GF2m_193_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60f9fbec18Smcpowers {
61f9fbec18Smcpowers 	mp_err res = MP_OKAY;
62f9fbec18Smcpowers 	mp_digit *u, z;
63f9fbec18Smcpowers 
64f9fbec18Smcpowers 	if (a != r) {
65f9fbec18Smcpowers 		MP_CHECKOK(mp_copy(a, r));
66f9fbec18Smcpowers 	}
67f9fbec18Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
68f9fbec18Smcpowers 	if (MP_USED(r) < 7) {
69f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 7));
70f9fbec18Smcpowers 	}
71f9fbec18Smcpowers 	u = MP_DIGITS(r);
72f9fbec18Smcpowers 	MP_USED(r) = 7;
73f9fbec18Smcpowers 
74f9fbec18Smcpowers 	/* u[6] only has 2 significant bits */
75f9fbec18Smcpowers 	z = u[6];
76f9fbec18Smcpowers 	u[3] ^= (z << 14) ^ (z >> 1);
77f9fbec18Smcpowers 	u[2] ^= (z << 63);
78f9fbec18Smcpowers 	z = u[5];
79f9fbec18Smcpowers 	u[3] ^= (z >> 50);
80f9fbec18Smcpowers 	u[2] ^= (z << 14) ^ (z >> 1);
81f9fbec18Smcpowers 	u[1] ^= (z << 63);
82f9fbec18Smcpowers 	z = u[4];
83f9fbec18Smcpowers 	u[2] ^= (z >> 50);
84f9fbec18Smcpowers 	u[1] ^= (z << 14) ^ (z >> 1);
85f9fbec18Smcpowers 	u[0] ^= (z << 63);
86f9fbec18Smcpowers 	z = u[3] >> 1;				/* z only has 63 significant bits */
87f9fbec18Smcpowers 	u[1] ^= (z >> 49);
88f9fbec18Smcpowers 	u[0] ^= (z << 15) ^ z;
89f9fbec18Smcpowers 	/* clear bits above 193 */
90f9fbec18Smcpowers 	u[6] = u[5] = u[4] = 0;
91f9fbec18Smcpowers 	u[3] ^= z << 1;
92f9fbec18Smcpowers #else
93f9fbec18Smcpowers 	if (MP_USED(r) < 13) {
94f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 13));
95f9fbec18Smcpowers 	}
96f9fbec18Smcpowers 	u = MP_DIGITS(r);
97f9fbec18Smcpowers 	MP_USED(r) = 13;
98f9fbec18Smcpowers 
99f9fbec18Smcpowers 	/* u[12] only has 2 significant bits */
100f9fbec18Smcpowers 	z = u[12];
101f9fbec18Smcpowers 	u[6] ^= (z << 14) ^ (z >> 1);
102f9fbec18Smcpowers 	u[5] ^= (z << 31);
103f9fbec18Smcpowers 	z = u[11];
104f9fbec18Smcpowers 	u[6] ^= (z >> 18);
105f9fbec18Smcpowers 	u[5] ^= (z << 14) ^ (z >> 1);
106f9fbec18Smcpowers 	u[4] ^= (z << 31);
107f9fbec18Smcpowers 	z = u[10];
108f9fbec18Smcpowers 	u[5] ^= (z >> 18);
109f9fbec18Smcpowers 	u[4] ^= (z << 14) ^ (z >> 1);
110f9fbec18Smcpowers 	u[3] ^= (z << 31);
111f9fbec18Smcpowers 	z = u[9];
112f9fbec18Smcpowers 	u[4] ^= (z >> 18);
113f9fbec18Smcpowers 	u[3] ^= (z << 14) ^ (z >> 1);
114f9fbec18Smcpowers 	u[2] ^= (z << 31);
115f9fbec18Smcpowers 	z = u[8];
116f9fbec18Smcpowers 	u[3] ^= (z >> 18);
117f9fbec18Smcpowers 	u[2] ^= (z << 14) ^ (z >> 1);
118f9fbec18Smcpowers 	u[1] ^= (z << 31);
119f9fbec18Smcpowers 	z = u[7];
120f9fbec18Smcpowers 	u[2] ^= (z >> 18);
121f9fbec18Smcpowers 	u[1] ^= (z << 14) ^ (z >> 1);
122f9fbec18Smcpowers 	u[0] ^= (z << 31);
123f9fbec18Smcpowers 	z = u[6] >> 1;				/* z only has 31 significant bits */
124f9fbec18Smcpowers 	u[1] ^= (z >> 17);
125f9fbec18Smcpowers 	u[0] ^= (z << 15) ^ z;
126f9fbec18Smcpowers 	/* clear bits above 193 */
127f9fbec18Smcpowers 	u[12] = u[11] = u[10] = u[9] = u[8] = u[7] = 0;
128f9fbec18Smcpowers 	u[6] ^= z << 1;
129f9fbec18Smcpowers #endif
130f9fbec18Smcpowers 	s_mp_clamp(r);
131f9fbec18Smcpowers 
132f9fbec18Smcpowers   CLEANUP:
133f9fbec18Smcpowers 	return res;
134f9fbec18Smcpowers }
135f9fbec18Smcpowers 
136f9fbec18Smcpowers /* Fast squaring for polynomials over a 193-bit curve. Assumes reduction
137f9fbec18Smcpowers  * polynomial with terms {193, 15, 0}. */
138f9fbec18Smcpowers mp_err
ec_GF2m_193_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)139f9fbec18Smcpowers ec_GF2m_193_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
140f9fbec18Smcpowers {
141f9fbec18Smcpowers 	mp_err res = MP_OKAY;
142f9fbec18Smcpowers 	mp_digit *u, *v;
143f9fbec18Smcpowers 
144f9fbec18Smcpowers 	v = MP_DIGITS(a);
145f9fbec18Smcpowers 
146f9fbec18Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
147f9fbec18Smcpowers 	if (MP_USED(a) < 4) {
148f9fbec18Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
149f9fbec18Smcpowers 	}
150f9fbec18Smcpowers 	if (MP_USED(r) < 7) {
151f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 7));
152f9fbec18Smcpowers 	}
153f9fbec18Smcpowers 	MP_USED(r) = 7;
154f9fbec18Smcpowers #else
155f9fbec18Smcpowers 	if (MP_USED(a) < 7) {
156f9fbec18Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
157f9fbec18Smcpowers 	}
158f9fbec18Smcpowers 	if (MP_USED(r) < 13) {
159f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 13));
160f9fbec18Smcpowers 	}
161f9fbec18Smcpowers 	MP_USED(r) = 13;
162f9fbec18Smcpowers #endif
163f9fbec18Smcpowers 	u = MP_DIGITS(r);
164f9fbec18Smcpowers 
165f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
166f9fbec18Smcpowers 	u[12] = gf2m_SQR0(v[6]);
167f9fbec18Smcpowers 	u[11] = gf2m_SQR1(v[5]);
168f9fbec18Smcpowers 	u[10] = gf2m_SQR0(v[5]);
169f9fbec18Smcpowers 	u[9] = gf2m_SQR1(v[4]);
170f9fbec18Smcpowers 	u[8] = gf2m_SQR0(v[4]);
171f9fbec18Smcpowers 	u[7] = gf2m_SQR1(v[3]);
172f9fbec18Smcpowers #endif
173f9fbec18Smcpowers 	u[6] = gf2m_SQR0(v[3]);
174f9fbec18Smcpowers 	u[5] = gf2m_SQR1(v[2]);
175f9fbec18Smcpowers 	u[4] = gf2m_SQR0(v[2]);
176f9fbec18Smcpowers 	u[3] = gf2m_SQR1(v[1]);
177f9fbec18Smcpowers 	u[2] = gf2m_SQR0(v[1]);
178f9fbec18Smcpowers 	u[1] = gf2m_SQR1(v[0]);
179f9fbec18Smcpowers 	u[0] = gf2m_SQR0(v[0]);
180f9fbec18Smcpowers 	return ec_GF2m_193_mod(r, r, meth);
181f9fbec18Smcpowers 
182f9fbec18Smcpowers   CLEANUP:
183f9fbec18Smcpowers 	return res;
184f9fbec18Smcpowers }
185f9fbec18Smcpowers 
186f9fbec18Smcpowers /* Fast multiplication for polynomials over a 193-bit curve. Assumes
187f9fbec18Smcpowers  * reduction polynomial with terms {193, 15, 0}. */
188f9fbec18Smcpowers mp_err
ec_GF2m_193_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)189f9fbec18Smcpowers ec_GF2m_193_mul(const mp_int *a, const mp_int *b, mp_int *r,
190f9fbec18Smcpowers 				const GFMethod *meth)
191f9fbec18Smcpowers {
192f9fbec18Smcpowers 	mp_err res = MP_OKAY;
193f9fbec18Smcpowers 	mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
194f9fbec18Smcpowers 
195f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
196f9fbec18Smcpowers 	mp_digit a6 = 0, a5 = 0, a4 = 0, b6 = 0, b5 = 0, b4 = 0;
197f9fbec18Smcpowers 	mp_digit rm[8];
198f9fbec18Smcpowers #endif
199f9fbec18Smcpowers 
200f9fbec18Smcpowers 	if (a == b) {
201f9fbec18Smcpowers 		return ec_GF2m_193_sqr(a, r, meth);
202f9fbec18Smcpowers 	} else {
203f9fbec18Smcpowers 		switch (MP_USED(a)) {
204f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
205f9fbec18Smcpowers 		case 7:
206f9fbec18Smcpowers 			a6 = MP_DIGIT(a, 6);
207*38a641c5SToomas Soome 			/* FALLTHROUGH */
208f9fbec18Smcpowers 		case 6:
209f9fbec18Smcpowers 			a5 = MP_DIGIT(a, 5);
210*38a641c5SToomas Soome 			/* FALLTHROUGH */
211f9fbec18Smcpowers 		case 5:
212f9fbec18Smcpowers 			a4 = MP_DIGIT(a, 4);
213f9fbec18Smcpowers #endif
214*38a641c5SToomas Soome 			/* FALLTHROUGH */
215f9fbec18Smcpowers 		case 4:
216f9fbec18Smcpowers 			a3 = MP_DIGIT(a, 3);
217*38a641c5SToomas Soome 			/* FALLTHROUGH */
218f9fbec18Smcpowers 		case 3:
219f9fbec18Smcpowers 			a2 = MP_DIGIT(a, 2);
220*38a641c5SToomas Soome 			/* FALLTHROUGH */
221f9fbec18Smcpowers 		case 2:
222f9fbec18Smcpowers 			a1 = MP_DIGIT(a, 1);
223*38a641c5SToomas Soome 			/* FALLTHROUGH */
224f9fbec18Smcpowers 		default:
225f9fbec18Smcpowers 			a0 = MP_DIGIT(a, 0);
226f9fbec18Smcpowers 		}
227f9fbec18Smcpowers 		switch (MP_USED(b)) {
228f9fbec18Smcpowers #ifdef ECL_THIRTY_TWO_BIT
229f9fbec18Smcpowers 		case 7:
230f9fbec18Smcpowers 			b6 = MP_DIGIT(b, 6);
231*38a641c5SToomas Soome 			/* FALLTHROUGH */
232f9fbec18Smcpowers 		case 6:
233f9fbec18Smcpowers 			b5 = MP_DIGIT(b, 5);
234*38a641c5SToomas Soome 			/* FALLTHROUGH */
235f9fbec18Smcpowers 		case 5:
236f9fbec18Smcpowers 			b4 = MP_DIGIT(b, 4);
237f9fbec18Smcpowers #endif
238*38a641c5SToomas Soome 			/* FALLTHROUGH */
239f9fbec18Smcpowers 		case 4:
240f9fbec18Smcpowers 			b3 = MP_DIGIT(b, 3);
241*38a641c5SToomas Soome 			/* FALLTHROUGH */
242f9fbec18Smcpowers 		case 3:
243f9fbec18Smcpowers 			b2 = MP_DIGIT(b, 2);
244*38a641c5SToomas Soome 			/* FALLTHROUGH */
245f9fbec18Smcpowers 		case 2:
246f9fbec18Smcpowers 			b1 = MP_DIGIT(b, 1);
247*38a641c5SToomas Soome 			/* FALLTHROUGH */
248f9fbec18Smcpowers 		default:
249f9fbec18Smcpowers 			b0 = MP_DIGIT(b, 0);
250f9fbec18Smcpowers 		}
251f9fbec18Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
252f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 8));
253f9fbec18Smcpowers 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
254f9fbec18Smcpowers 		MP_USED(r) = 8;
255f9fbec18Smcpowers 		s_mp_clamp(r);
256f9fbec18Smcpowers #else
257f9fbec18Smcpowers 		MP_CHECKOK(s_mp_pad(r, 14));
258f9fbec18Smcpowers 		s_bmul_3x3(MP_DIGITS(r) + 8, a6, a5, a4, b6, b5, b4);
259f9fbec18Smcpowers 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
260f9fbec18Smcpowers 		s_bmul_4x4(rm, a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b3, b6 ^ b2, b5 ^ b1,
261f9fbec18Smcpowers 				   b4 ^ b0);
262f9fbec18Smcpowers 		rm[7] ^= MP_DIGIT(r, 7);
263f9fbec18Smcpowers 		rm[6] ^= MP_DIGIT(r, 6);
264f9fbec18Smcpowers 		rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
265f9fbec18Smcpowers 		rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
266f9fbec18Smcpowers 		rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
267f9fbec18Smcpowers 		rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
268f9fbec18Smcpowers 		rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
269f9fbec18Smcpowers 		rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
270f9fbec18Smcpowers 		MP_DIGIT(r, 11) ^= rm[7];
271f9fbec18Smcpowers 		MP_DIGIT(r, 10) ^= rm[6];
272f9fbec18Smcpowers 		MP_DIGIT(r, 9) ^= rm[5];
273f9fbec18Smcpowers 		MP_DIGIT(r, 8) ^= rm[4];
274f9fbec18Smcpowers 		MP_DIGIT(r, 7) ^= rm[3];
275f9fbec18Smcpowers 		MP_DIGIT(r, 6) ^= rm[2];
276f9fbec18Smcpowers 		MP_DIGIT(r, 5) ^= rm[1];
277f9fbec18Smcpowers 		MP_DIGIT(r, 4) ^= rm[0];
278f9fbec18Smcpowers 		MP_USED(r) = 14;
279f9fbec18Smcpowers 		s_mp_clamp(r);
280f9fbec18Smcpowers #endif
281f9fbec18Smcpowers 		return ec_GF2m_193_mod(r, r, meth);
282f9fbec18Smcpowers 	}
283f9fbec18Smcpowers 
284f9fbec18Smcpowers   CLEANUP:
285f9fbec18Smcpowers 	return res;
286f9fbec18Smcpowers }
287f9fbec18Smcpowers 
288f9fbec18Smcpowers /* Wire in fast field arithmetic for 193-bit curves. */
289f9fbec18Smcpowers mp_err
ec_group_set_gf2m193(ECGroup * group,ECCurveName name)290f9fbec18Smcpowers ec_group_set_gf2m193(ECGroup *group, ECCurveName name)
291f9fbec18Smcpowers {
292f9fbec18Smcpowers 	group->meth->field_mod = &ec_GF2m_193_mod;
293f9fbec18Smcpowers 	group->meth->field_mul = &ec_GF2m_193_mul;
294f9fbec18Smcpowers 	group->meth->field_sqr = &ec_GF2m_193_sqr;
295f9fbec18Smcpowers 	return MP_OKAY;
296f9fbec18Smcpowers }
297