blob: f6af5cbca62393915c2797e976f01b52368a11f6 [file] [log] [blame]
Janos Follathb0697532016-08-18 12:38:46 +01001/**
Janos Follath47d28f02016-11-01 13:22:05 +00002 * \file ecp_internal.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
Dave Rodgman7ff79652023-11-03 12:04:52 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Janos Follathb0697532016-08-18 12:38:46 +010010 */
Janos Follathaab9efb2016-12-02 13:49:21 +000011
12/*
13 * References:
14 *
Janos Follath5634b862016-12-08 16:15:51 +000015 * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.
16 * <http://cr.yp.to/ecdh/curve25519-20060209.pdf>
Janos Follathaab9efb2016-12-02 13:49:21 +000017 *
18 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
19 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
20 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
21 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
22 *
23 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
24 * render ECC resistant against Side Channel Attacks. IACR Cryptology
25 * ePrint Archive, 2004, vol. 2004, p. 342.
26 * <http://eprint.iacr.org/2004/342.pdf>
Janos Follath5634b862016-12-08 16:15:51 +000027 *
28 * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.
29 * <http://www.secg.org/sec2-v2.pdf>
30 *
31 * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic
32 * Curve Cryptography.
33 *
34 * [6] Digital Signature Standard (DSS), FIPS 186-4.
35 * <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
36 *
Darryl Green11999bb2018-03-13 15:22:58 +000037 * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer
Janos Follath5634b862016-12-08 16:15:51 +000038 * Security (TLS), RFC 4492.
39 * <https://tools.ietf.org/search/rfc4492>
40 *
41 * [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>
42 *
43 * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.
44 * Springer Science & Business Media, 1 Aug 2000
Janos Follathaab9efb2016-12-02 13:49:21 +000045 */
46
Janos Follathc44ab972016-11-18 16:38:23 +000047#ifndef MBEDTLS_ECP_INTERNAL_H
48#define MBEDTLS_ECP_INTERNAL_H
Janos Follathb0697532016-08-18 12:38:46 +010049
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050050#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010051#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052#else
53#include MBEDTLS_CONFIG_FILE
54#endif
55
Janos Follathc44ab972016-11-18 16:38:23 +000056#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Janos Follathb0697532016-08-18 12:38:46 +010057
Janos Follathaab9efb2016-12-02 13:49:21 +000058/**
Janos Follath5634b862016-12-08 16:15:51 +000059 * \brief Indicate if the Elliptic Curve Point module extension can
60 * handle the group.
Janos Follathaab9efb2016-12-02 13:49:21 +000061 *
Janos Follath5634b862016-12-08 16:15:51 +000062 * \param grp The pointer to the elliptic curve group that will be the
63 * basis of the cryptographic computations.
Janos Follathaab9efb2016-12-02 13:49:21 +000064 *
65 * \return Non-zero if successful.
66 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +010068
Janos Follathaab9efb2016-12-02 13:49:21 +000069/**
Janos Follath5634b862016-12-08 16:15:51 +000070 * \brief Initialise the Elliptic Curve Point module extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000071 *
72 * If mbedtls_internal_ecp_grp_capable returns true for a
73 * group, this function has to be able to initialise the
Janos Follath5634b862016-12-08 16:15:51 +000074 * module for it.
Janos Follathaab9efb2016-12-02 13:49:21 +000075 *
Janos Follath5634b862016-12-08 16:15:51 +000076 * This module can be a driver to a crypto hardware
77 * accelerator, for which this could be an initialise function.
78 *
79 * \param grp The pointer to the group the module needs to be
Janos Follathaab9efb2016-12-02 13:49:21 +000080 * initialised for.
81 *
82 * \return 0 if successful.
83 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084int mbedtls_internal_ecp_init(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +010085
Janos Follathaab9efb2016-12-02 13:49:21 +000086/**
Janos Follath5634b862016-12-08 16:15:51 +000087 * \brief Frees and deallocates the Elliptic Curve Point module
88 * extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000089 *
Janos Follath5634b862016-12-08 16:15:51 +000090 * \param grp The pointer to the group the module was initialised for.
Janos Follathaab9efb2016-12-02 13:49:21 +000091 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092void mbedtls_internal_ecp_free(const mbedtls_ecp_group *grp);
Janos Follathb0697532016-08-18 12:38:46 +010093
Gilles Peskinee8c04fe2018-09-14 17:44:21 +020094#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Janos Follathaab9efb2016-12-02 13:49:21 +000095
Janos Follathb0697532016-08-18 12:38:46 +010096#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +000097/**
98 * \brief Randomize jacobian coordinates:
99 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
100 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000101 * \param grp Pointer to the group representing the curve.
102 *
103 * \param pt The point on the curve to be randomised, given with Jacobian
104 * coordinates.
105 *
106 * \param f_rng A function pointer to the random number generator.
107 *
108 * \param p_rng A pointer to the random number generator state.
109 *
110 * \return 0 if successful.
111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group *grp,
113 mbedtls_ecp_point *pt, int (*f_rng)(void *,
114 unsigned char *,
115 size_t),
116 void *p_rng);
Janos Follathb0697532016-08-18 12:38:46 +0100117#endif
118
119#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000120/**
121 * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
122 *
123 * The coordinates of Q must be normalized (= affine),
124 * but those of P don't need to. R is not normalized.
125 *
Janos Follath5634b862016-12-08 16:15:51 +0000126 * This function is used only as a subrutine of
127 * ecp_mul_comb().
128 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000129 * Special cases: (1) P or Q is zero, (2) R is zero,
130 * (3) P == Q.
131 * None of these cases can happen as intermediate step in
132 * ecp_mul_comb():
133 * - at each step, P, Q and R are multiples of the base
134 * point, the factor being less than its order, so none of
135 * them is zero;
136 * - Q is an odd multiple of the base point, P an even
137 * multiple, due to the choice of precomputed points in the
138 * modified comb method.
139 * So branches for these cases do not leak secret information.
140 *
141 * We accept Q->Z being unset (saving memory in tables) as
142 * meaning 1.
143 *
Janos Follath5634b862016-12-08 16:15:51 +0000144 * Cost in field operations if done by [5] 3.22:
Janos Follathaab9efb2016-12-02 13:49:21 +0000145 * 1A := 8M + 3S
146 *
147 * \param grp Pointer to the group representing the curve.
148 *
149 * \param R Pointer to a point structure to hold the result.
150 *
151 * \param P Pointer to the first summand, given with Jacobian
152 * coordinates
153 *
154 * \param Q Pointer to the second summand, given with affine
155 * coordinates.
156 *
157 * \return 0 if successful.
158 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group *grp,
160 mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
161 const mbedtls_ecp_point *Q);
Janos Follathb0697532016-08-18 12:38:46 +0100162#endif
163
Janos Follathaab9efb2016-12-02 13:49:21 +0000164/**
165 * \brief Point doubling R = 2 P, Jacobian coordinates.
166 *
167 * Cost: 1D := 3M + 4S (A == 0)
168 * 4M + 4S (A == -3)
169 * 3M + 6S + 1a otherwise
Janos Follath5634b862016-12-08 16:15:51 +0000170 * when the implementation is based on the "dbl-1998-cmo-2"
171 * doubling formulas in [8] and standard optimizations are
172 * applied when curve parameter A is one of { 0, -3 }.
Janos Follathaab9efb2016-12-02 13:49:21 +0000173 *
174 * \param grp Pointer to the group representing the curve.
175 *
176 * \param R Pointer to a point structure to hold the result.
177 *
178 * \param P Pointer to the point that has to be doubled, given with
179 * Jacobian coordinates.
180 *
181 * \return 0 if successful.
182 */
Janos Follathb0697532016-08-18 12:38:46 +0100183#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group *grp,
185 mbedtls_ecp_point *R, const mbedtls_ecp_point *P);
Janos Follathb0697532016-08-18 12:38:46 +0100186#endif
187
Janos Follathaab9efb2016-12-02 13:49:21 +0000188/**
189 * \brief Normalize jacobian coordinates of an array of (pointers to)
190 * points.
191 *
192 * Using Montgomery's trick to perform only one inversion mod P
193 * the cost is:
194 * 1N(t) := 1I + (6t - 3)M + 1S
Janos Follath5634b862016-12-08 16:15:51 +0000195 * (See for example Algorithm 10.3.4. in [9])
196 *
197 * This function is used only as a subrutine of
198 * ecp_mul_comb().
Janos Follathaab9efb2016-12-02 13:49:21 +0000199 *
200 * Warning: fails (returning an error) if one of the points is
201 * zero!
202 * This should never happen, see choice of w in ecp_mul_comb().
203 *
204 * \param grp Pointer to the group representing the curve.
205 *
206 * \param T Array of pointers to the points to normalise.
207 *
208 * \param t_len Number of elements in the array.
209 *
210 * \return 0 if successful,
211 * an error if one of the points is zero.
212 */
Janos Follathb0697532016-08-18 12:38:46 +0100213#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
215 mbedtls_ecp_point *T[], size_t t_len);
Janos Follathb0697532016-08-18 12:38:46 +0100216#endif
217
Janos Follathaab9efb2016-12-02 13:49:21 +0000218/**
219 * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
220 *
Janos Follath5634b862016-12-08 16:15:51 +0000221 * Cost in field operations if done by [5] 3.2.1:
Janos Follathaab9efb2016-12-02 13:49:21 +0000222 * 1N := 1I + 3M + 1S
223 *
224 * \param grp Pointer to the group representing the curve.
225 *
226 * \param pt pointer to the point to be normalised. This is an
227 * input/output parameter.
228 *
229 * \return 0 if successful.
230 */
Janos Follathb0697532016-08-18 12:38:46 +0100231#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group *grp,
233 mbedtls_ecp_point *pt);
Janos Follathb0697532016-08-18 12:38:46 +0100234#endif
235
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200236#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
Janos Follathaab9efb2016-12-02 13:49:21 +0000237
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200238#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
Janos Follathaab9efb2016-12-02 13:49:21 +0000239
Janos Follathb0697532016-08-18 12:38:46 +0100240#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group *grp,
242 mbedtls_ecp_point *R,
243 mbedtls_ecp_point *S,
244 const mbedtls_ecp_point *P,
245 const mbedtls_ecp_point *Q,
246 const mbedtls_mpi *d);
Janos Follathb0697532016-08-18 12:38:46 +0100247#endif
248
Janos Follathaab9efb2016-12-02 13:49:21 +0000249/**
250 * \brief Randomize projective x/z coordinates:
251 * (X, Z) -> (l X, l Z) for random l
Janos Follathaab9efb2016-12-02 13:49:21 +0000252 *
253 * \param grp pointer to the group representing the curve
254 *
255 * \param P the point on the curve to be randomised given with
256 * projective coordinates. This is an input/output parameter.
257 *
258 * \param f_rng a function pointer to the random number generator
259 *
260 * \param p_rng a pointer to the random number generator state
261 *
262 * \return 0 if successful
263 */
Janos Follathb0697532016-08-18 12:38:46 +0100264#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group *grp,
266 mbedtls_ecp_point *P, int (*f_rng)(void *,
267 unsigned char *,
268 size_t),
269 void *p_rng);
Janos Follathb0697532016-08-18 12:38:46 +0100270#endif
271
Janos Follathaab9efb2016-12-02 13:49:21 +0000272/**
273 * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
274 *
275 * \param grp pointer to the group representing the curve
276 *
277 * \param P pointer to the point to be normalised. This is an
278 * input/output parameter.
279 *
280 * \return 0 if successful
281 */
Janos Follathb0697532016-08-18 12:38:46 +0100282#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group *grp,
284 mbedtls_ecp_point *P);
Janos Follathb0697532016-08-18 12:38:46 +0100285#endif
286
Gilles Peskinee8c04fe2018-09-14 17:44:21 +0200287#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
Janos Follathaab9efb2016-12-02 13:49:21 +0000288
Janos Follathc44ab972016-11-18 16:38:23 +0000289#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Janos Follathb0697532016-08-18 12:38:46 +0100290
Janos Follathc44ab972016-11-18 16:38:23 +0000291#endif /* ecp_internal.h */