blob: f7e5bf8a8c7715591101f611c41aa0bfde5c49fb [file] [log] [blame]
Janos Follathb0697532016-08-18 12:38:46 +01001/**
Gilles Peskine6a2fb612021-05-24 22:25:04 +02002 * \file ecp_internal_alt.h
Janos Follathb0697532016-08-18 12:38:46 +01003 *
Janos Follath372697b2016-10-28 16:53:11 +01004 * \brief Function declarations for alternative implementation of elliptic curve
5 * point arithmetic.
Darryl Greena40a1012018-01-05 15:33:17 +00006 */
7/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Janos Follathb0697532016-08-18 12:38:46 +01009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Janos Follathb0697532016-08-18 12:38:46 +010022 */
Janos Follathaab9efb2016-12-02 13:49:21 +000023
24/*
25 * References:
26 *
Janos Follath5634b862016-12-08 16:15:51 +000027 * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.
28 * <http://cr.yp.to/ecdh/curve25519-20060209.pdf>
Janos Follathaab9efb2016-12-02 13:49:21 +000029 *
30 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
31 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
32 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
33 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
34 *
35 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
36 * render ECC resistant against Side Channel Attacks. IACR Cryptology
37 * ePrint Archive, 2004, vol. 2004, p. 342.
38 * <http://eprint.iacr.org/2004/342.pdf>
Janos Follath5634b862016-12-08 16:15:51 +000039 *
40 * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.
41 * <http://www.secg.org/sec2-v2.pdf>
42 *
43 * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic
44 * Curve Cryptography.
45 *
46 * [6] Digital Signature Standard (DSS), FIPS 186-4.
47 * <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
48 *
Darryl Green11999bb2018-03-13 15:22:58 +000049 * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer
Janos Follath5634b862016-12-08 16:15:51 +000050 * Security (TLS), RFC 4492.
51 * <https://tools.ietf.org/search/rfc4492>
52 *
53 * [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>
54 *
55 * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.
56 * Springer Science & Business Media, 1 Aug 2000
Janos Follathaab9efb2016-12-02 13:49:21 +000057 */
58
Janos Follathc44ab972016-11-18 16:38:23 +000059#ifndef MBEDTLS_ECP_INTERNAL_H
60#define MBEDTLS_ECP_INTERNAL_H
Janos Follathb0697532016-08-18 12:38:46 +010061
Bence Szépkútic662b362021-05-27 11:25:03 +020062#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063
Janos Follathc44ab972016-11-18 16:38:23 +000064#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Janos Follathb0697532016-08-18 12:38:46 +010065
Janos Follathaab9efb2016-12-02 13:49:21 +000066/**
Janos Follath5634b862016-12-08 16:15:51 +000067 * \brief Indicate if the Elliptic Curve Point module extension can
68 * handle the group.
Janos Follathaab9efb2016-12-02 13:49:21 +000069 *
Janos Follath5634b862016-12-08 16:15:51 +000070 * \param grp The pointer to the elliptic curve group that will be the
71 * basis of the cryptographic computations.
Janos Follathaab9efb2016-12-02 13:49:21 +000072 *
73 * \return Non-zero if successful.
74 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020075unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +010076
Janos Follathaab9efb2016-12-02 13:49:21 +000077/**
Janos Follath5634b862016-12-08 16:15:51 +000078 * \brief Initialise the Elliptic Curve Point module extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000079 *
80 * If mbedtls_internal_ecp_grp_capable returns true for a
81 * group, this function has to be able to initialise the
Janos Follath5634b862016-12-08 16:15:51 +000082 * module for it.
Janos Follathaab9efb2016-12-02 13:49:21 +000083 *
Janos Follath5634b862016-12-08 16:15:51 +000084 * This module can be a driver to a crypto hardware
85 * accelerator, for which this could be an initialise function.
86 *
87 * \param grp The pointer to the group the module needs to be
Janos Follathaab9efb2016-12-02 13:49:21 +000088 * initialised for.
89 *
90 * \return 0 if successful.
91 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092int mbedtls_internal_ecp_init(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +010093
Janos Follathaab9efb2016-12-02 13:49:21 +000094/**
Janos Follath5634b862016-12-08 16:15:51 +000095 * \brief Frees and deallocates the Elliptic Curve Point module
96 * extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000097 *
Janos Follath5634b862016-12-08 16:15:51 +000098 * \param grp The pointer to the group the module was initialised for.
Janos Follathaab9efb2016-12-02 13:49:21 +000099 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100void mbedtls_internal_ecp_free(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +0100101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102# if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathaab9efb2016-12-02 13:49:21 +0000103
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104# if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000105/**
106 * \brief Randomize jacobian coordinates:
107 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
108 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000109 * \param grp Pointer to the group representing the curve.
110 *
111 * \param pt The point on the curve to be randomised, given with Jacobian
112 * coordinates.
113 *
114 * \param f_rng A function pointer to the random number generator.
115 *
116 * \param p_rng A pointer to the random number generator state.
117 *
118 * \return 0 if successful.
119 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group *grp,
121 mbedtls_ecp_point *pt,
122 int (*f_rng)(void *,
123 unsigned char *,
124 size_t),
125 void *p_rng);
126# endif
Janos Follathb0697532016-08-18 12:38:46 +0100127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128# if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000129/**
130 * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
131 *
132 * The coordinates of Q must be normalized (= affine),
133 * but those of P don't need to. R is not normalized.
134 *
Janos Follath5634b862016-12-08 16:15:51 +0000135 * This function is used only as a subrutine of
136 * ecp_mul_comb().
137 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000138 * Special cases: (1) P or Q is zero, (2) R is zero,
139 * (3) P == Q.
140 * None of these cases can happen as intermediate step in
141 * ecp_mul_comb():
142 * - at each step, P, Q and R are multiples of the base
143 * point, the factor being less than its order, so none of
144 * them is zero;
145 * - Q is an odd multiple of the base point, P an even
146 * multiple, due to the choice of precomputed points in the
147 * modified comb method.
148 * So branches for these cases do not leak secret information.
149 *
150 * We accept Q->Z being unset (saving memory in tables) as
151 * meaning 1.
152 *
Janos Follath5634b862016-12-08 16:15:51 +0000153 * Cost in field operations if done by [5] 3.22:
Janos Follathaab9efb2016-12-02 13:49:21 +0000154 * 1A := 8M + 3S
155 *
156 * \param grp Pointer to the group representing the curve.
157 *
158 * \param R Pointer to a point structure to hold the result.
159 *
160 * \param P Pointer to the first summand, given with Jacobian
161 * coordinates
162 *
163 * \param Q Pointer to the second summand, given with affine
164 * coordinates.
165 *
166 * \return 0 if successful.
167 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group *grp,
169 mbedtls_ecp_point *R,
170 const mbedtls_ecp_point *P,
171 const mbedtls_ecp_point *Q);
172# endif
Janos Follathb0697532016-08-18 12:38:46 +0100173
Janos Follathaab9efb2016-12-02 13:49:21 +0000174/**
175 * \brief Point doubling R = 2 P, Jacobian coordinates.
176 *
177 * Cost: 1D := 3M + 4S (A == 0)
178 * 4M + 4S (A == -3)
179 * 3M + 6S + 1a otherwise
Janos Follath5634b862016-12-08 16:15:51 +0000180 * when the implementation is based on the "dbl-1998-cmo-2"
181 * doubling formulas in [8] and standard optimizations are
182 * applied when curve parameter A is one of { 0, -3 }.
Janos Follathaab9efb2016-12-02 13:49:21 +0000183 *
184 * \param grp Pointer to the group representing the curve.
185 *
186 * \param R Pointer to a point structure to hold the result.
187 *
188 * \param P Pointer to the point that has to be doubled, given with
189 * Jacobian coordinates.
190 *
191 * \return 0 if successful.
192 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193# if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
194int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group *grp,
195 mbedtls_ecp_point *R,
196 const mbedtls_ecp_point *P);
197# endif
Janos Follathb0697532016-08-18 12:38:46 +0100198
Janos Follathaab9efb2016-12-02 13:49:21 +0000199/**
200 * \brief Normalize jacobian coordinates of an array of (pointers to)
201 * points.
202 *
203 * Using Montgomery's trick to perform only one inversion mod P
204 * the cost is:
205 * 1N(t) := 1I + (6t - 3)M + 1S
Janos Follath5634b862016-12-08 16:15:51 +0000206 * (See for example Algorithm 10.3.4. in [9])
207 *
208 * This function is used only as a subrutine of
209 * ecp_mul_comb().
Janos Follathaab9efb2016-12-02 13:49:21 +0000210 *
211 * Warning: fails (returning an error) if one of the points is
212 * zero!
213 * This should never happen, see choice of w in ecp_mul_comb().
214 *
215 * \param grp Pointer to the group representing the curve.
216 *
217 * \param T Array of pointers to the points to normalise.
218 *
219 * \param t_len Number of elements in the array.
220 *
221 * \return 0 if successful,
222 * an error if one of the points is zero.
223 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224# if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
225int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
226 mbedtls_ecp_point *T[],
227 size_t t_len);
228# endif
Janos Follathb0697532016-08-18 12:38:46 +0100229
Janos Follathaab9efb2016-12-02 13:49:21 +0000230/**
231 * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
232 *
Janos Follath5634b862016-12-08 16:15:51 +0000233 * Cost in field operations if done by [5] 3.2.1:
Janos Follathaab9efb2016-12-02 13:49:21 +0000234 * 1N := 1I + 3M + 1S
235 *
236 * \param grp Pointer to the group representing the curve.
237 *
238 * \param pt pointer to the point to be normalised. This is an
239 * input/output parameter.
240 *
241 * \return 0 if successful.
242 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243# if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
244int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group *grp,
245 mbedtls_ecp_point *pt);
246# endif
Janos Follathb0697532016-08-18 12:38:46 +0100247
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200248# endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Janos Follathaab9efb2016-12-02 13:49:21 +0000249
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250# if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathaab9efb2016-12-02 13:49:21 +0000251
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252# if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
253int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group *grp,
254 mbedtls_ecp_point *R,
255 mbedtls_ecp_point *S,
256 const mbedtls_ecp_point *P,
257 const mbedtls_ecp_point *Q,
258 const mbedtls_mpi *d);
259# endif
Janos Follathb0697532016-08-18 12:38:46 +0100260
Janos Follathaab9efb2016-12-02 13:49:21 +0000261/**
262 * \brief Randomize projective x/z coordinates:
263 * (X, Z) -> (l X, l Z) for random l
Janos Follathaab9efb2016-12-02 13:49:21 +0000264 *
265 * \param grp pointer to the group representing the curve
266 *
267 * \param P the point on the curve to be randomised given with
268 * projective coordinates. This is an input/output parameter.
269 *
270 * \param f_rng a function pointer to the random number generator
271 *
272 * \param p_rng a pointer to the random number generator state
273 *
274 * \return 0 if successful
275 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276# if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
277int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group *grp,
278 mbedtls_ecp_point *P,
279 int (*f_rng)(void *,
280 unsigned char *,
281 size_t),
282 void *p_rng);
283# endif
Janos Follathb0697532016-08-18 12:38:46 +0100284
Janos Follathaab9efb2016-12-02 13:49:21 +0000285/**
286 * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
287 *
288 * \param grp pointer to the group representing the curve
289 *
290 * \param P pointer to the point to be normalised. This is an
291 * input/output parameter.
292 *
293 * \return 0 if successful
294 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200295# if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
296int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group *grp,
297 mbedtls_ecp_point *P);
298# endif
Janos Follathb0697532016-08-18 12:38:46 +0100299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300# endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Janos Follathaab9efb2016-12-02 13:49:21 +0000301
Janos Follathc44ab972016-11-18 16:38:23 +0000302#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Janos Follathb0697532016-08-18 12:38:46 +0100303
Gilles Peskine6a2fb612021-05-24 22:25:04 +0200304#endif /* ecp_internal_alt.h */