blob: 4e9445ae444de5e7aa303e5dba07cb44dbf5452a [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/*
Janos Follath372697b2016-10-28 16:53:11 +01008 * Copyright (C) 2016, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Janos Follathb0697532016-08-18 12:38:46 +010016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
49 *
Janos Follathb0697532016-08-18 12:38:46 +010050 * This file is part of mbed TLS (https://tls.mbed.org)
51 */
Janos Follathaab9efb2016-12-02 13:49:21 +000052
53/*
54 * References:
55 *
Janos Follath5634b862016-12-08 16:15:51 +000056 * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.
57 * <http://cr.yp.to/ecdh/curve25519-20060209.pdf>
Janos Follathaab9efb2016-12-02 13:49:21 +000058 *
59 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
60 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
61 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
62 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
63 *
64 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
65 * render ECC resistant against Side Channel Attacks. IACR Cryptology
66 * ePrint Archive, 2004, vol. 2004, p. 342.
67 * <http://eprint.iacr.org/2004/342.pdf>
Janos Follath5634b862016-12-08 16:15:51 +000068 *
69 * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.
70 * <http://www.secg.org/sec2-v2.pdf>
71 *
72 * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic
73 * Curve Cryptography.
74 *
75 * [6] Digital Signature Standard (DSS), FIPS 186-4.
76 * <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
77 *
Darryl Green11999bb2018-03-13 15:22:58 +000078 * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer
Janos Follath5634b862016-12-08 16:15:51 +000079 * Security (TLS), RFC 4492.
80 * <https://tools.ietf.org/search/rfc4492>
81 *
82 * [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>
83 *
84 * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.
85 * Springer Science & Business Media, 1 Aug 2000
Janos Follathaab9efb2016-12-02 13:49:21 +000086 */
87
Janos Follathc44ab972016-11-18 16:38:23 +000088#ifndef MBEDTLS_ECP_INTERNAL_H
89#define MBEDTLS_ECP_INTERNAL_H
Janos Follathb0697532016-08-18 12:38:46 +010090
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020091#if !defined(MBEDTLS_CONFIG_FILE)
92#include "config.h"
93#else
94#include MBEDTLS_CONFIG_FILE
95#endif
96
Janos Follathc44ab972016-11-18 16:38:23 +000097#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Janos Follathb0697532016-08-18 12:38:46 +010098
Janos Follathaab9efb2016-12-02 13:49:21 +000099/**
Janos Follath5634b862016-12-08 16:15:51 +0000100 * \brief Indicate if the Elliptic Curve Point module extension can
101 * handle the group.
Janos Follathaab9efb2016-12-02 13:49:21 +0000102 *
Janos Follath5634b862016-12-08 16:15:51 +0000103 * \param grp The pointer to the elliptic curve group that will be the
104 * basis of the cryptographic computations.
Janos Follathaab9efb2016-12-02 13:49:21 +0000105 *
106 * \return Non-zero if successful.
107 */
Janos Follathc44ab972016-11-18 16:38:23 +0000108unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +0100109
Janos Follathaab9efb2016-12-02 13:49:21 +0000110/**
Janos Follath5634b862016-12-08 16:15:51 +0000111 * \brief Initialise the Elliptic Curve Point module extension.
Janos Follathaab9efb2016-12-02 13:49:21 +0000112 *
113 * If mbedtls_internal_ecp_grp_capable returns true for a
114 * group, this function has to be able to initialise the
Janos Follath5634b862016-12-08 16:15:51 +0000115 * module for it.
Janos Follathaab9efb2016-12-02 13:49:21 +0000116 *
Janos Follath5634b862016-12-08 16:15:51 +0000117 * This module can be a driver to a crypto hardware
118 * accelerator, for which this could be an initialise function.
119 *
120 * \param grp The pointer to the group the module needs to be
Janos Follathaab9efb2016-12-02 13:49:21 +0000121 * initialised for.
122 *
123 * \return 0 if successful.
124 */
Janos Follathc44ab972016-11-18 16:38:23 +0000125int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +0100126
Janos Follathaab9efb2016-12-02 13:49:21 +0000127/**
Janos Follath5634b862016-12-08 16:15:51 +0000128 * \brief Frees and deallocates the Elliptic Curve Point module
129 * extension.
Janos Follathaab9efb2016-12-02 13:49:21 +0000130 *
Janos Follath5634b862016-12-08 16:15:51 +0000131 * \param grp The pointer to the group the module was initialised for.
Janos Follathaab9efb2016-12-02 13:49:21 +0000132 */
Janos Follathc44ab972016-11-18 16:38:23 +0000133void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +0100134
Janos Follathaab9efb2016-12-02 13:49:21 +0000135#if defined(ECP_SHORTWEIERSTRASS)
136
Janos Follathb0697532016-08-18 12:38:46 +0100137#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000138/**
139 * \brief Randomize jacobian coordinates:
140 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
141 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000142 * \param grp Pointer to the group representing the curve.
143 *
144 * \param pt The point on the curve to be randomised, given with Jacobian
145 * coordinates.
146 *
147 * \param f_rng A function pointer to the random number generator.
148 *
149 * \param p_rng A pointer to the random number generator state.
150 *
151 * \return 0 if successful.
152 */
Janos Follathc44ab972016-11-18 16:38:23 +0000153int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000154 mbedtls_ecp_point *pt, int (*f_rng)(void *, unsigned char *, size_t),
155 void *p_rng );
Janos Follathb0697532016-08-18 12:38:46 +0100156#endif
157
158#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000159/**
160 * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
161 *
162 * The coordinates of Q must be normalized (= affine),
163 * but those of P don't need to. R is not normalized.
164 *
Janos Follath5634b862016-12-08 16:15:51 +0000165 * This function is used only as a subrutine of
166 * ecp_mul_comb().
167 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000168 * Special cases: (1) P or Q is zero, (2) R is zero,
169 * (3) P == Q.
170 * None of these cases can happen as intermediate step in
171 * ecp_mul_comb():
172 * - at each step, P, Q and R are multiples of the base
173 * point, the factor being less than its order, so none of
174 * them is zero;
175 * - Q is an odd multiple of the base point, P an even
176 * multiple, due to the choice of precomputed points in the
177 * modified comb method.
178 * So branches for these cases do not leak secret information.
179 *
180 * We accept Q->Z being unset (saving memory in tables) as
181 * meaning 1.
182 *
Janos Follath5634b862016-12-08 16:15:51 +0000183 * Cost in field operations if done by [5] 3.22:
Janos Follathaab9efb2016-12-02 13:49:21 +0000184 * 1A := 8M + 3S
185 *
186 * \param grp Pointer to the group representing the curve.
187 *
188 * \param R Pointer to a point structure to hold the result.
189 *
190 * \param P Pointer to the first summand, given with Jacobian
191 * coordinates
192 *
193 * \param Q Pointer to the second summand, given with affine
194 * coordinates.
195 *
196 * \return 0 if successful.
197 */
Janos Follathc44ab972016-11-18 16:38:23 +0000198int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000199 mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
200 const mbedtls_ecp_point *Q );
Janos Follathb0697532016-08-18 12:38:46 +0100201#endif
202
Janos Follathaab9efb2016-12-02 13:49:21 +0000203/**
204 * \brief Point doubling R = 2 P, Jacobian coordinates.
205 *
206 * Cost: 1D := 3M + 4S (A == 0)
207 * 4M + 4S (A == -3)
208 * 3M + 6S + 1a otherwise
Janos Follath5634b862016-12-08 16:15:51 +0000209 * when the implementation is based on the "dbl-1998-cmo-2"
210 * doubling formulas in [8] and standard optimizations are
211 * applied when curve parameter A is one of { 0, -3 }.
Janos Follathaab9efb2016-12-02 13:49:21 +0000212 *
213 * \param grp Pointer to the group representing the curve.
214 *
215 * \param R Pointer to a point structure to hold the result.
216 *
217 * \param P Pointer to the point that has to be doubled, given with
218 * Jacobian coordinates.
219 *
220 * \return 0 if successful.
221 */
Janos Follathb0697532016-08-18 12:38:46 +0100222#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000223int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000224 mbedtls_ecp_point *R, const mbedtls_ecp_point *P );
Janos Follathb0697532016-08-18 12:38:46 +0100225#endif
226
Janos Follathaab9efb2016-12-02 13:49:21 +0000227/**
228 * \brief Normalize jacobian coordinates of an array of (pointers to)
229 * points.
230 *
231 * Using Montgomery's trick to perform only one inversion mod P
232 * the cost is:
233 * 1N(t) := 1I + (6t - 3)M + 1S
Janos Follath5634b862016-12-08 16:15:51 +0000234 * (See for example Algorithm 10.3.4. in [9])
235 *
236 * This function is used only as a subrutine of
237 * ecp_mul_comb().
Janos Follathaab9efb2016-12-02 13:49:21 +0000238 *
239 * Warning: fails (returning an error) if one of the points is
240 * zero!
241 * This should never happen, see choice of w in ecp_mul_comb().
242 *
243 * \param grp Pointer to the group representing the curve.
244 *
245 * \param T Array of pointers to the points to normalise.
246 *
247 * \param t_len Number of elements in the array.
248 *
249 * \return 0 if successful,
250 * an error if one of the points is zero.
251 */
Janos Follathb0697532016-08-18 12:38:46 +0100252#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000253int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000254 mbedtls_ecp_point *T[], size_t t_len );
Janos Follathb0697532016-08-18 12:38:46 +0100255#endif
256
Janos Follathaab9efb2016-12-02 13:49:21 +0000257/**
258 * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
259 *
Janos Follath5634b862016-12-08 16:15:51 +0000260 * Cost in field operations if done by [5] 3.2.1:
Janos Follathaab9efb2016-12-02 13:49:21 +0000261 * 1N := 1I + 3M + 1S
262 *
263 * \param grp Pointer to the group representing the curve.
264 *
265 * \param pt pointer to the point to be normalised. This is an
266 * input/output parameter.
267 *
268 * \return 0 if successful.
269 */
Janos Follathb0697532016-08-18 12:38:46 +0100270#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000271int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000272 mbedtls_ecp_point *pt );
Janos Follathb0697532016-08-18 12:38:46 +0100273#endif
274
Janos Follathaab9efb2016-12-02 13:49:21 +0000275#endif /* ECP_SHORTWEIERSTRASS */
276
277#if defined(ECP_MONTGOMERY)
278
Janos Follathb0697532016-08-18 12:38:46 +0100279#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000280int mbedtls_internal_ecp_double_add_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000281 mbedtls_ecp_point *R, mbedtls_ecp_point *S, const mbedtls_ecp_point *P,
282 const mbedtls_ecp_point *Q, const mbedtls_mpi *d );
Janos Follathb0697532016-08-18 12:38:46 +0100283#endif
284
Janos Follathaab9efb2016-12-02 13:49:21 +0000285/**
286 * \brief Randomize projective x/z coordinates:
287 * (X, Z) -> (l X, l Z) for random l
Janos Follathaab9efb2016-12-02 13:49:21 +0000288 *
289 * \param grp pointer to the group representing the curve
290 *
291 * \param P the point on the curve to be randomised given with
292 * projective coordinates. This is an input/output parameter.
293 *
294 * \param f_rng a function pointer to the random number generator
295 *
296 * \param p_rng a pointer to the random number generator state
297 *
298 * \return 0 if successful
299 */
Janos Follathb0697532016-08-18 12:38:46 +0100300#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000301int mbedtls_internal_ecp_randomize_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000302 mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, size_t),
303 void *p_rng );
Janos Follathb0697532016-08-18 12:38:46 +0100304#endif
305
Janos Follathaab9efb2016-12-02 13:49:21 +0000306/**
307 * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
308 *
309 * \param grp pointer to the group representing the curve
310 *
311 * \param P pointer to the point to be normalised. This is an
312 * input/output parameter.
313 *
314 * \return 0 if successful
315 */
Janos Follathb0697532016-08-18 12:38:46 +0100316#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000317int mbedtls_internal_ecp_normalize_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000318 mbedtls_ecp_point *P );
Janos Follathb0697532016-08-18 12:38:46 +0100319#endif
320
Janos Follathaab9efb2016-12-02 13:49:21 +0000321#endif /* ECP_MONTGOMERY */
322
Janos Follathc44ab972016-11-18 16:38:23 +0000323#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Janos Follathb0697532016-08-18 12:38:46 +0100324
Janos Follathc44ab972016-11-18 16:38:23 +0000325#endif /* ecp_internal.h */
Janos Follathb0697532016-08-18 12:38:46 +0100326