xref: /illumos-gate/usr/src/common/crypto/ecc/ecp.h (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>, Sun Microsystems Laboratories
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 #ifndef _ECP_H
46f9fbec18Smcpowers #define _ECP_H
47f9fbec18Smcpowers 
48f9fbec18Smcpowers #include "ecl-priv.h"
49f9fbec18Smcpowers 
50f9fbec18Smcpowers /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
51f9fbec18Smcpowers mp_err ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py);
52f9fbec18Smcpowers 
53f9fbec18Smcpowers /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
54f9fbec18Smcpowers mp_err ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py);
55f9fbec18Smcpowers 
56f9fbec18Smcpowers /* Computes R = P + Q where R is (rx, ry), P is (px, py) and Q is (qx,
57f9fbec18Smcpowers  * qy). Uses affine coordinates. */
58f9fbec18Smcpowers mp_err ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py,
59f9fbec18Smcpowers 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
60f9fbec18Smcpowers 						 mp_int *ry, const ECGroup *group);
61f9fbec18Smcpowers 
62f9fbec18Smcpowers /* Computes R = P - Q.  Uses affine coordinates. */
63f9fbec18Smcpowers mp_err ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py,
64f9fbec18Smcpowers 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
65f9fbec18Smcpowers 						 mp_int *ry, const ECGroup *group);
66f9fbec18Smcpowers 
67f9fbec18Smcpowers /* Computes R = 2P.  Uses affine coordinates. */
68f9fbec18Smcpowers mp_err ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
69f9fbec18Smcpowers 						 mp_int *ry, const ECGroup *group);
70f9fbec18Smcpowers 
71f9fbec18Smcpowers /* Validates a point on a GFp curve. */
72f9fbec18Smcpowers mp_err ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group);
73f9fbec18Smcpowers 
74f9fbec18Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
75f9fbec18Smcpowers /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters
76f9fbec18Smcpowers  * a, b and p are the elliptic curve coefficients and the prime that
77f9fbec18Smcpowers  * determines the field GFp.  Uses affine coordinates. */
78f9fbec18Smcpowers mp_err ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px,
79f9fbec18Smcpowers 						 const mp_int *py, mp_int *rx, mp_int *ry,
80f9fbec18Smcpowers 						 const ECGroup *group);
81f9fbec18Smcpowers #endif
82f9fbec18Smcpowers 
83f9fbec18Smcpowers /* Converts a point P(px, py) from affine coordinates to Jacobian
84f9fbec18Smcpowers  * projective coordinates R(rx, ry, rz). */
85f9fbec18Smcpowers mp_err ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx,
86f9fbec18Smcpowers 						 mp_int *ry, mp_int *rz, const ECGroup *group);
87f9fbec18Smcpowers 
88f9fbec18Smcpowers /* Converts a point P(px, py, pz) from Jacobian projective coordinates to
89f9fbec18Smcpowers  * affine coordinates R(rx, ry). */
90f9fbec18Smcpowers mp_err ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py,
91f9fbec18Smcpowers 						 const mp_int *pz, mp_int *rx, mp_int *ry,
92f9fbec18Smcpowers 						 const ECGroup *group);
93f9fbec18Smcpowers 
94f9fbec18Smcpowers /* Checks if point P(px, py, pz) is at infinity.  Uses Jacobian
95f9fbec18Smcpowers  * coordinates. */
96f9fbec18Smcpowers mp_err ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py,
97f9fbec18Smcpowers 							const mp_int *pz);
98f9fbec18Smcpowers 
99f9fbec18Smcpowers /* Sets P(px, py, pz) to be the point at infinity.  Uses Jacobian
100f9fbec18Smcpowers  * coordinates. */
101f9fbec18Smcpowers mp_err ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz);
102f9fbec18Smcpowers 
103f9fbec18Smcpowers /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
104f9fbec18Smcpowers  * (qx, qy, qz).  Uses Jacobian coordinates. */
105f9fbec18Smcpowers mp_err ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py,
106f9fbec18Smcpowers 							 const mp_int *pz, const mp_int *qx,
107f9fbec18Smcpowers 							 const mp_int *qy, mp_int *rx, mp_int *ry,
108f9fbec18Smcpowers 							 mp_int *rz, const ECGroup *group);
109f9fbec18Smcpowers 
110f9fbec18Smcpowers /* Computes R = 2P.  Uses Jacobian coordinates. */
111f9fbec18Smcpowers mp_err ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py,
112f9fbec18Smcpowers 						 const mp_int *pz, mp_int *rx, mp_int *ry,
113f9fbec18Smcpowers 						 mp_int *rz, const ECGroup *group);
114f9fbec18Smcpowers 
115f9fbec18Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_JAC
116f9fbec18Smcpowers /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters
117f9fbec18Smcpowers  * a, b and p are the elliptic curve coefficients and the prime that
118f9fbec18Smcpowers  * determines the field GFp.  Uses Jacobian coordinates. */
119f9fbec18Smcpowers mp_err ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px,
120f9fbec18Smcpowers 						 const mp_int *py, mp_int *rx, mp_int *ry,
121f9fbec18Smcpowers 						 const ECGroup *group);
122f9fbec18Smcpowers #endif
123f9fbec18Smcpowers 
124f9fbec18Smcpowers /* Computes R(x, y) = k1 * G + k2 * P(x, y), where G is the generator
125f9fbec18Smcpowers  * (base point) of the group of points on the elliptic curve. Allows k1 =
126f9fbec18Smcpowers  * NULL or { k2, P } = NULL.  Implemented using mixed Jacobian-affine
127f9fbec18Smcpowers  * coordinates. Input and output values are assumed to be NOT
128f9fbec18Smcpowers  * field-encoded and are in affine form. */
129f9fbec18Smcpowers mp_err
130f9fbec18Smcpowers  ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px,
131f9fbec18Smcpowers 					const mp_int *py, mp_int *rx, mp_int *ry,
132f9fbec18Smcpowers 					const ECGroup *group);
133f9fbec18Smcpowers 
134f9fbec18Smcpowers /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic
135f9fbec18Smcpowers  * curve points P and R can be identical. Uses mixed Modified-Jacobian
136f9fbec18Smcpowers  * co-ordinates for doubling and Chudnovsky Jacobian coordinates for
137f9fbec18Smcpowers  * additions. Assumes input is already field-encoded using field_enc, and
138f9fbec18Smcpowers  * returns output that is still field-encoded. Uses 5-bit window NAF
139f9fbec18Smcpowers  * method (algorithm 11) for scalar-point multiplication from Brown,
140*c40a6cd7SToomas Soome  * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic
141f9fbec18Smcpowers  * Curves Over Prime Fields. */
142f9fbec18Smcpowers mp_err
143f9fbec18Smcpowers  ec_GFp_pt_mul_jm_wNAF(const mp_int *n, const mp_int *px, const mp_int *py,
144f9fbec18Smcpowers 					   mp_int *rx, mp_int *ry, const ECGroup *group);
145f9fbec18Smcpowers 
146f9fbec18Smcpowers #endif /* _ECP_H */
147