3 /* nettle, low-level cryptographics library
5 * Copyright (C) 2013 Niels Möller
7 * The nettle library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
12 * The nettle library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the nettle library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
25 #ifndef NETTLE_ECC_H_INCLUDED
26 #define NETTLE_ECC_H_INCLUDED
30 #include "nettle-types.h"
37 #define ecc_point_init nettle_ecc_point_init
38 #define ecc_point_clear nettle_ecc_point_clear
39 #define ecc_point_set nettle_ecc_point_set
40 #define ecc_point_get nettle_ecc_point_get
41 #define ecc_point_mul nettle_ecc_point_mul
42 #define ecc_point_mul_g nettle_ecc_point_mul_g
43 #define ecc_scalar_init nettle_ecc_scalar_init
44 #define ecc_scalar_clear nettle_ecc_scalar_clear
45 #define ecc_scalar_set nettle_ecc_scalar_set
46 #define ecc_scalar_get nettle_ecc_scalar_get
47 #define ecc_scalar_random nettle_ecc_scalar_random
48 #define ecc_point_mul nettle_ecc_point_mul
49 #define ecc_size nettle_ecc_size
50 #define ecc_size_a nettle_ecc_size_a
51 #define ecc_size_j nettle_ecc_size_j
52 #define ecc_a_to_a_itch nettle_ecc_a_to_a_itch
53 #define ecc_a_to_a nettle_ecc_a_to_a
54 #define ecc_a_to_j nettle_ecc_a_to_j
55 #define ecc_j_to_a_itch nettle_ecc_j_to_a_itch
56 #define ecc_j_to_a nettle_ecc_j_to_a
57 #define ecc_dup_ja_itch nettle_ecc_dup_ja_itch
58 #define ecc_dup_ja nettle_ecc_dup_ja
59 #define ecc_dup_jj_itch nettle_ecc_dup_jj_itch
60 #define ecc_dup_jj nettle_ecc_dup_jj
61 #define ecc_add_jja_itch nettle_ecc_add_jja_itch
62 #define ecc_add_jja nettle_ecc_add_jja
63 #define ecc_add_jjj_itch nettle_ecc_add_jjj_itch
64 #define ecc_add_jjj nettle_ecc_add_jjj
65 #define ecc_mul_g_itch nettle_ecc_mul_g_itch
66 #define ecc_mul_g nettle_ecc_mul_g
67 #define ecc_mul_a_itch nettle_ecc_mul_a_itch
68 #define ecc_mul_a nettle_ecc_mul_a
72 /* High level interface, for ECDSA, DH, etc */
74 /* Represents a point on the ECC curve */
77 const struct ecc_curve
*ecc
;
78 /* Allocated using the same allocation function as GMP. */
82 /* Represents a non-zero scalar, an element of Z_q^*, where q is the
83 group order of the curve. */
86 const struct ecc_curve
*ecc
;
87 /* Allocated using the same allocation function as GMP. */
92 ecc_point_init (struct ecc_point
*p
, const struct ecc_curve
*ecc
);
94 ecc_point_clear (struct ecc_point
*p
);
96 /* Fails and returns zero if the point is not on the curve. */
98 ecc_point_set (struct ecc_point
*p
, const mpz_t x
, const mpz_t y
);
100 ecc_point_get (const struct ecc_point
*p
, mpz_t x
, mpz_t y
);
103 ecc_scalar_init (struct ecc_scalar
*s
, const struct ecc_curve
*ecc
);
105 ecc_scalar_clear (struct ecc_scalar
*s
);
107 /* Fails and returns zero if the scalar is not in the proper range. */
109 ecc_scalar_set (struct ecc_scalar
*s
, const mpz_t z
);
111 ecc_scalar_get (const struct ecc_scalar
*s
, mpz_t z
);
112 /* Generates a random scalar, suitable as an ECDSA private key or a
115 ecc_scalar_random (struct ecc_scalar
*s
,
116 void *random_ctx
, nettle_random_func
*random
);
118 /* Computes r = n p */
120 ecc_point_mul (struct ecc_point
*r
, const struct ecc_scalar
*n
,
121 const struct ecc_point
*p
);
123 /* Computes r = n g */
125 ecc_point_mul_g (struct ecc_point
*r
, const struct ecc_scalar
*n
);
128 /* Low-level interface */
130 /* Points on a curve are represented as arrays of mp_limb_t. For some
131 curves, point coordinates are represented in montgomery form. We
132 use either affine coordinates x,y, or Jacobian coordinates X, Y, Z,
133 where x = X/Z^2 and y = X/Z^2.
135 Since we use additive notation for the groups, the infinity point
136 on the curve is denoted 0. The infinity point can be represented
137 with x = y = 0 in affine coordinates, and Z = 0 in Jacobian
138 coordinates. However, note that most of the ECC functions do *not*
139 support infinity as an input or output.
142 /* FIXME: Also provided some compile time constants? */
144 /* Returns the size of a single coordinate. */
146 ecc_size (const struct ecc_curve
*ecc
);
148 /* Size of a point, using affine coordinates x, y. */
150 ecc_size_a (const struct ecc_curve
*ecc
);
152 /* Size of a point, using jacobian coordinates X, Y and Z. */
154 ecc_size_j (const struct ecc_curve
*ecc
);
156 /* FIXME: Rename the low-level (and side-channel silent) functions to
157 _ecc_*, and provide public ecc_* functions which handle the
158 infinity points properly? */
160 /* Converts the affine coordinates of a point into montgomery form, if
161 used for this curve. */
163 ecc_a_to_a_itch (const struct ecc_curve
*ecc
);
165 ecc_a_to_a (const struct ecc_curve
*ecc
,
166 mp_limb_t
*r
, const mp_limb_t
*p
,
169 /* Converts a point P in affine coordinates into a point R in jacobian
170 coordinates. If INITIAL is non-zero, and the curve uses montgomery
171 coordinates, also convert coordinates to montgomery form. */
173 ecc_a_to_j (const struct ecc_curve
*ecc
,
175 mp_limb_t
*r
, const mp_limb_t
*p
);
177 /* Converts a point P in jacobian coordinates into a point R in affine
178 coordinates. If FLAGS has bit 0 set, and the curve uses montgomery
179 coordinates, also undo the montgomery conversion. If flags has bit
180 1 set, produce x coordinate only. */
182 ecc_j_to_a_itch (const struct ecc_curve
*ecc
);
184 ecc_j_to_a (const struct ecc_curve
*ecc
,
186 mp_limb_t
*r
, const mp_limb_t
*p
,
189 /* Group operations */
192 /* Point doubling, with jacobian output and affine input. Corner
193 cases: Correctly sets R = 0 (r_Z = 0) if p = 0 or 2p = 0. */
195 ecc_dup_ja_itch (const struct ecc_curve
*ecc
);
197 ecc_dup_ja (const struct ecc_curve
*ecc
,
198 mp_limb_t
*r
, const mp_limb_t
*p
,
201 /* Point doubling, with jacobian input and output. Corner cases:
202 Correctly sets R = 0 (r_Z = 0) if p = 0 or 2p = 0. */
204 ecc_dup_jj_itch (const struct ecc_curve
*ecc
);
206 ecc_dup_jj (const struct ecc_curve
*ecc
,
207 mp_limb_t
*r
, const mp_limb_t
*p
,
211 /* Point addition, with jacobian output, one jacobian input and one
212 affine input. Corner cases: Fails for the cases
214 P = Q != 0 Duplication of non-zero point
215 P = 0, Q != 0 or P != 0, Q = 0 One input zero
217 Correctly gives R = 0 if P = Q = 0 or P = -Q. */
219 ecc_add_jja_itch (const struct ecc_curve
*ecc
);
221 ecc_add_jja (const struct ecc_curve
*ecc
,
222 mp_limb_t
*r
, const mp_limb_t
*p
, const mp_limb_t
*q
,
225 /* Point addition with Jacobian input and output. */
227 ecc_add_jjj_itch (const struct ecc_curve
*ecc
);
229 ecc_add_jjj (const struct ecc_curve
*ecc
,
230 mp_limb_t
*r
, const mp_limb_t
*p
, const mp_limb_t
*q
,
234 /* Computes N * the group generator. N is an array of ecc_size()
235 limbs. It must be in the range 0 < N < group order, then R != 0,
236 and the algorithm can work without any intermediate values getting
239 ecc_mul_g_itch (const struct ecc_curve
*ecc
);
241 ecc_mul_g (const struct ecc_curve
*ecc
, mp_limb_t
*r
,
242 const mp_limb_t
*np
, mp_limb_t
*scratch
);
244 /* Computes N * P. The scalar N is the same as for ecc_mul_g. P is a
245 non-zero point on the curve, in affine coordinates. Pass a non-zero
246 INITIAL if the point coordinates have not previously been converted
247 to Montgomery representation. Output R is a non-zero point, in
248 Jacobian coordinates. */
250 ecc_mul_a_itch (const struct ecc_curve
*ecc
);
252 ecc_mul_a (const struct ecc_curve
*ecc
,
253 int initial
, mp_limb_t
*r
,
254 const mp_limb_t
*np
, const mp_limb_t
*p
,
261 #endif /* NETTLE_ECC_H_INCLUDED */