blob: 2991e26dd9b587fc73519e0dca77684351d00a08 [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.
Janos Follathb0697532016-08-18 12:38:46 +01006 *
Janos Follath372697b2016-10-28 16:53:11 +01007 * Copyright (C) 2016, ARM Limited, All Rights Reserved
Janos Follathb0697532016-08-18 12:38:46 +01008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
Janos Follathaab9efb2016-12-02 13:49:21 +000024
25/*
26 * References:
27 *
Janos Follath5634b862016-12-08 16:15:51 +000028 * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.
29 * <http://cr.yp.to/ecdh/curve25519-20060209.pdf>
Janos Follathaab9efb2016-12-02 13:49:21 +000030 *
31 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
32 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
33 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
34 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
35 *
36 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
37 * render ECC resistant against Side Channel Attacks. IACR Cryptology
38 * ePrint Archive, 2004, vol. 2004, p. 342.
39 * <http://eprint.iacr.org/2004/342.pdf>
Janos Follath5634b862016-12-08 16:15:51 +000040 *
41 * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.
42 * <http://www.secg.org/sec2-v2.pdf>
43 *
44 * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic
45 * Curve Cryptography.
46 *
47 * [6] Digital Signature Standard (DSS), FIPS 186-4.
48 * <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
49 *
50 * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer
51 * Security (TLS), RFC 4492.
52 * <https://tools.ietf.org/search/rfc4492>
53 *
54 * [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>
55 *
56 * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.
57 * Springer Science & Business Media, 1 Aug 2000
Janos Follathaab9efb2016-12-02 13:49:21 +000058 */
59
Janos Follathc44ab972016-11-18 16:38:23 +000060#ifndef MBEDTLS_ECP_INTERNAL_H
61#define MBEDTLS_ECP_INTERNAL_H
Janos Follathb0697532016-08-18 12:38:46 +010062
Janos Follathc44ab972016-11-18 16:38:23 +000063#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Janos Follathb0697532016-08-18 12:38:46 +010064
Janos Follathaab9efb2016-12-02 13:49:21 +000065/**
Janos Follath5634b862016-12-08 16:15:51 +000066 * \brief Indicate if the Elliptic Curve Point module extension can
67 * handle the group.
Janos Follathaab9efb2016-12-02 13:49:21 +000068 *
Janos Follath5634b862016-12-08 16:15:51 +000069 * \param grp The pointer to the elliptic curve group that will be the
70 * basis of the cryptographic computations.
Janos Follathaab9efb2016-12-02 13:49:21 +000071 *
72 * \return Non-zero if successful.
73 */
Janos Follathc44ab972016-11-18 16:38:23 +000074unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +010075
Janos Follathaab9efb2016-12-02 13:49:21 +000076/**
Janos Follath5634b862016-12-08 16:15:51 +000077 * \brief Initialise the Elliptic Curve Point module extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000078 *
79 * If mbedtls_internal_ecp_grp_capable returns true for a
80 * group, this function has to be able to initialise the
Janos Follath5634b862016-12-08 16:15:51 +000081 * module for it.
Janos Follathaab9efb2016-12-02 13:49:21 +000082 *
Janos Follath5634b862016-12-08 16:15:51 +000083 * This module can be a driver to a crypto hardware
84 * accelerator, for which this could be an initialise function.
85 *
86 * \param grp The pointer to the group the module needs to be
Janos Follathaab9efb2016-12-02 13:49:21 +000087 * initialised for.
88 *
89 * \return 0 if successful.
90 */
Janos Follathc44ab972016-11-18 16:38:23 +000091int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +010092
Janos Follathaab9efb2016-12-02 13:49:21 +000093/**
Janos Follath5634b862016-12-08 16:15:51 +000094 * \brief Frees and deallocates the Elliptic Curve Point module
95 * extension.
Janos Follathaab9efb2016-12-02 13:49:21 +000096 *
Janos Follath5634b862016-12-08 16:15:51 +000097 * \param grp The pointer to the group the module was initialised for.
Janos Follathaab9efb2016-12-02 13:49:21 +000098 */
Janos Follathc44ab972016-11-18 16:38:23 +000099void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp );
Janos Follathb0697532016-08-18 12:38:46 +0100100
Janos Follathaab9efb2016-12-02 13:49:21 +0000101#if defined(ECP_SHORTWEIERSTRASS)
102
Janos Follathb0697532016-08-18 12:38:46 +0100103#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000104/**
105 * \brief Randomize jacobian coordinates:
106 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
107 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000108 * \param grp Pointer to the group representing the curve.
109 *
110 * \param pt The point on the curve to be randomised, given with Jacobian
111 * coordinates.
112 *
113 * \param f_rng A function pointer to the random number generator.
114 *
115 * \param p_rng A pointer to the random number generator state.
116 *
117 * \return 0 if successful.
118 */
Janos Follathc44ab972016-11-18 16:38:23 +0000119int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000120 mbedtls_ecp_point *pt, int (*f_rng)(void *, unsigned char *, size_t),
121 void *p_rng );
Janos Follathb0697532016-08-18 12:38:46 +0100122#endif
123
124#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Janos Follathaab9efb2016-12-02 13:49:21 +0000125/**
126 * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
127 *
128 * The coordinates of Q must be normalized (= affine),
129 * but those of P don't need to. R is not normalized.
130 *
Janos Follath5634b862016-12-08 16:15:51 +0000131 * This function is used only as a subrutine of
132 * ecp_mul_comb().
133 *
Janos Follathaab9efb2016-12-02 13:49:21 +0000134 * Special cases: (1) P or Q is zero, (2) R is zero,
135 * (3) P == Q.
136 * None of these cases can happen as intermediate step in
137 * ecp_mul_comb():
138 * - at each step, P, Q and R are multiples of the base
139 * point, the factor being less than its order, so none of
140 * them is zero;
141 * - Q is an odd multiple of the base point, P an even
142 * multiple, due to the choice of precomputed points in the
143 * modified comb method.
144 * So branches for these cases do not leak secret information.
145 *
146 * We accept Q->Z being unset (saving memory in tables) as
147 * meaning 1.
148 *
Janos Follath5634b862016-12-08 16:15:51 +0000149 * Cost in field operations if done by [5] 3.22:
Janos Follathaab9efb2016-12-02 13:49:21 +0000150 * 1A := 8M + 3S
151 *
152 * \param grp Pointer to the group representing the curve.
153 *
154 * \param R Pointer to a point structure to hold the result.
155 *
156 * \param P Pointer to the first summand, given with Jacobian
157 * coordinates
158 *
159 * \param Q Pointer to the second summand, given with affine
160 * coordinates.
161 *
162 * \return 0 if successful.
163 */
Janos Follathc44ab972016-11-18 16:38:23 +0000164int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000165 mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
166 const mbedtls_ecp_point *Q );
Janos Follathb0697532016-08-18 12:38:46 +0100167#endif
168
Janos Follathaab9efb2016-12-02 13:49:21 +0000169/**
170 * \brief Point doubling R = 2 P, Jacobian coordinates.
171 *
172 * Cost: 1D := 3M + 4S (A == 0)
173 * 4M + 4S (A == -3)
174 * 3M + 6S + 1a otherwise
Janos Follath5634b862016-12-08 16:15:51 +0000175 * when the implementation is based on the "dbl-1998-cmo-2"
176 * doubling formulas in [8] and standard optimizations are
177 * applied when curve parameter A is one of { 0, -3 }.
Janos Follathaab9efb2016-12-02 13:49:21 +0000178 *
179 * \param grp Pointer to the group representing the curve.
180 *
181 * \param R Pointer to a point structure to hold the result.
182 *
183 * \param P Pointer to the point that has to be doubled, given with
184 * Jacobian coordinates.
185 *
186 * \return 0 if successful.
187 */
Janos Follathb0697532016-08-18 12:38:46 +0100188#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000189int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000190 mbedtls_ecp_point *R, const mbedtls_ecp_point *P );
Janos Follathb0697532016-08-18 12:38:46 +0100191#endif
192
Janos Follathaab9efb2016-12-02 13:49:21 +0000193/**
194 * \brief Normalize jacobian coordinates of an array of (pointers to)
195 * points.
196 *
197 * Using Montgomery's trick to perform only one inversion mod P
198 * the cost is:
199 * 1N(t) := 1I + (6t - 3)M + 1S
Janos Follath5634b862016-12-08 16:15:51 +0000200 * (See for example Algorithm 10.3.4. in [9])
201 *
202 * This function is used only as a subrutine of
203 * ecp_mul_comb().
Janos Follathaab9efb2016-12-02 13:49:21 +0000204 *
205 * Warning: fails (returning an error) if one of the points is
206 * zero!
207 * This should never happen, see choice of w in ecp_mul_comb().
208 *
209 * \param grp Pointer to the group representing the curve.
210 *
211 * \param T Array of pointers to the points to normalise.
212 *
213 * \param t_len Number of elements in the array.
214 *
215 * \return 0 if successful,
216 * an error if one of the points is zero.
217 */
Janos Follathb0697532016-08-18 12:38:46 +0100218#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000219int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000220 mbedtls_ecp_point *T[], size_t t_len );
Janos Follathb0697532016-08-18 12:38:46 +0100221#endif
222
Janos Follathaab9efb2016-12-02 13:49:21 +0000223/**
224 * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
225 *
Janos Follath5634b862016-12-08 16:15:51 +0000226 * Cost in field operations if done by [5] 3.2.1:
Janos Follathaab9efb2016-12-02 13:49:21 +0000227 * 1N := 1I + 3M + 1S
228 *
229 * \param grp Pointer to the group representing the curve.
230 *
231 * \param pt pointer to the point to be normalised. This is an
232 * input/output parameter.
233 *
234 * \return 0 if successful.
235 */
Janos Follathb0697532016-08-18 12:38:46 +0100236#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000237int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000238 mbedtls_ecp_point *pt );
Janos Follathb0697532016-08-18 12:38:46 +0100239#endif
240
Janos Follathaab9efb2016-12-02 13:49:21 +0000241#endif /* ECP_SHORTWEIERSTRASS */
242
243#if defined(ECP_MONTGOMERY)
244
Janos Follathb0697532016-08-18 12:38:46 +0100245#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000246int mbedtls_internal_ecp_double_add_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000247 mbedtls_ecp_point *R, mbedtls_ecp_point *S, const mbedtls_ecp_point *P,
248 const mbedtls_ecp_point *Q, const mbedtls_mpi *d );
Janos Follathb0697532016-08-18 12:38:46 +0100249#endif
250
Janos Follathaab9efb2016-12-02 13:49:21 +0000251/**
252 * \brief Randomize projective x/z coordinates:
253 * (X, Z) -> (l X, l Z) for random l
Janos Follathaab9efb2016-12-02 13:49:21 +0000254 *
255 * \param grp pointer to the group representing the curve
256 *
257 * \param P the point on the curve to be randomised given with
258 * projective coordinates. This is an input/output parameter.
259 *
260 * \param f_rng a function pointer to the random number generator
261 *
262 * \param p_rng a pointer to the random number generator state
263 *
264 * \return 0 if successful
265 */
Janos Follathb0697532016-08-18 12:38:46 +0100266#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Janos Follathc44ab972016-11-18 16:38:23 +0000267int mbedtls_internal_ecp_randomize_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000268 mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, 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)
Janos Follathc44ab972016-11-18 16:38:23 +0000283int mbedtls_internal_ecp_normalize_mxz( const mbedtls_ecp_group *grp,
Janos Follathb8a90fb2016-11-15 13:45:01 +0000284 mbedtls_ecp_point *P );
Janos Follathb0697532016-08-18 12:38:46 +0100285#endif
286
Janos Follathaab9efb2016-12-02 13:49:21 +0000287#endif /* ECP_MONTGOMERY */
288
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 */
Janos Follathb0697532016-08-18 12:38:46 +0100292