blob: ff7d1cb60bb4874b4c05e5ddef6f0bb06bf6a6bb [file] [log] [blame]
Janos Follath4ced7c22016-08-18 12:38:46 +01001/**
Janos Follath58cf3922016-11-01 13:22:05 +00002 * \file ecp_internal.h
Janos Follath4ced7c22016-08-18 12:38:46 +01003 *
Janos Follath61ea6ec2016-10-28 16:53:11 +01004 * \brief Function declarations for alternative implementation of elliptic curve
5 * point arithmetic.
Janos Follath4ced7c22016-08-18 12:38:46 +01006 *
Janos Follath61ea6ec2016-10-28 16:53:11 +01007 * Copyright (C) 2016, ARM Limited, All Rights Reserved
Janos Follath4ced7c22016-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 Follath1a552ec2016-12-02 13:49:21 +000024
25/*
26 * References:
27 *
28 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
29 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
30 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
31 * RFC 4492 for the related TLS structures and constants
32 *
33 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
34 *
35 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
36 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
37 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
38 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
39 *
40 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
41 * render ECC resistant against Side Channel Attacks. IACR Cryptology
42 * ePrint Archive, 2004, vol. 2004, p. 342.
43 * <http://eprint.iacr.org/2004/342.pdf>
44 */
45
Janos Follathd26f07e2016-11-18 16:38:23 +000046#ifndef MBEDTLS_ECP_INTERNAL_H
47#define MBEDTLS_ECP_INTERNAL_H
Janos Follath4ced7c22016-08-18 12:38:46 +010048
Janos Follathd26f07e2016-11-18 16:38:23 +000049#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Janos Follath4ced7c22016-08-18 12:38:46 +010050
Janos Follath1a552ec2016-12-02 13:49:21 +000051/**
52 * \brief Tell if the cryptographic hardware can handle the group.
53 *
54 * \param grp The pointer to the group.
55 *
56 * \return Non-zero if successful.
57 */
Janos Follathd26f07e2016-11-18 16:38:23 +000058unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp );
Janos Follath4ced7c22016-08-18 12:38:46 +010059
Janos Follath1a552ec2016-12-02 13:49:21 +000060/**
61 * \brief Initialise the crypto hardware accelerator.
62 *
63 * If mbedtls_internal_ecp_grp_capable returns true for a
64 * group, this function has to be able to initialise the
65 * hardware for it.
66 *
67 * \param grp The pointer to the group the hardware needs to be
68 * initialised for.
69 *
70 * \return 0 if successful.
71 */
Janos Follathd26f07e2016-11-18 16:38:23 +000072int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp );
Janos Follath4ced7c22016-08-18 12:38:46 +010073
Janos Follath1a552ec2016-12-02 13:49:21 +000074/**
75 * \brief Reset the crypto hardware accelerator to an uninitialised
76 * state.
77 *
78 * \param grp The pointer to the group the hardware was initialised for.
79 */
Janos Follathd26f07e2016-11-18 16:38:23 +000080void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp );
Janos Follath4ced7c22016-08-18 12:38:46 +010081
Janos Follath1a552ec2016-12-02 13:49:21 +000082#if defined(ECP_SHORTWEIERSTRASS)
83
Janos Follath4ced7c22016-08-18 12:38:46 +010084#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Janos Follath1a552ec2016-12-02 13:49:21 +000085/**
86 * \brief Randomize jacobian coordinates:
87 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
88 *
89 * This is sort of the reverse operation of
90 * ecp_normalize_jac().
91 *
92 * \param grp Pointer to the group representing the curve.
93 *
94 * \param pt The point on the curve to be randomised, given with Jacobian
95 * coordinates.
96 *
97 * \param f_rng A function pointer to the random number generator.
98 *
99 * \param p_rng A pointer to the random number generator state.
100 *
101 * \return 0 if successful.
102 */
Janos Follathd26f07e2016-11-18 16:38:23 +0000103int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000104 mbedtls_ecp_point *pt, int (*f_rng)(void *, unsigned char *, size_t),
105 void *p_rng );
Janos Follath4ced7c22016-08-18 12:38:46 +0100106#endif
107
108#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Janos Follath1a552ec2016-12-02 13:49:21 +0000109/**
110 * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
111 *
112 * The coordinates of Q must be normalized (= affine),
113 * but those of P don't need to. R is not normalized.
114 *
115 * Special cases: (1) P or Q is zero, (2) R is zero,
116 * (3) P == Q.
117 * None of these cases can happen as intermediate step in
118 * ecp_mul_comb():
119 * - at each step, P, Q and R are multiples of the base
120 * point, the factor being less than its order, so none of
121 * them is zero;
122 * - Q is an odd multiple of the base point, P an even
123 * multiple, due to the choice of precomputed points in the
124 * modified comb method.
125 * So branches for these cases do not leak secret information.
126 *
127 * We accept Q->Z being unset (saving memory in tables) as
128 * meaning 1.
129 *
130 * Cost in field operations if done by GECC 3.22:
131 * 1A := 8M + 3S
132 *
133 * \param grp Pointer to the group representing the curve.
134 *
135 * \param R Pointer to a point structure to hold the result.
136 *
137 * \param P Pointer to the first summand, given with Jacobian
138 * coordinates
139 *
140 * \param Q Pointer to the second summand, given with affine
141 * coordinates.
142 *
143 * \return 0 if successful.
144 */
Janos Follathd26f07e2016-11-18 16:38:23 +0000145int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000146 mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
147 const mbedtls_ecp_point *Q );
Janos Follath4ced7c22016-08-18 12:38:46 +0100148#endif
149
Janos Follath1a552ec2016-12-02 13:49:21 +0000150/**
151 * \brief Point doubling R = 2 P, Jacobian coordinates.
152 *
153 * Cost: 1D := 3M + 4S (A == 0)
154 * 4M + 4S (A == -3)
155 * 3M + 6S + 1a otherwise
156 * when the implementation is based on
157 * http://www.hyperelliptic.org/EFD/g1p/
158 * auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2
159 * and standard optimizations are applied when curve parameter
160 * A is one of { 0, -3 }.
161 *
162 * \param grp Pointer to the group representing the curve.
163 *
164 * \param R Pointer to a point structure to hold the result.
165 *
166 * \param P Pointer to the point that has to be doubled, given with
167 * Jacobian coordinates.
168 *
169 * \return 0 if successful.
170 */
Janos Follath4ced7c22016-08-18 12:38:46 +0100171#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000172int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000173 mbedtls_ecp_point *R, const mbedtls_ecp_point *P );
Janos Follath4ced7c22016-08-18 12:38:46 +0100174#endif
175
Janos Follath1a552ec2016-12-02 13:49:21 +0000176/**
177 * \brief Normalize jacobian coordinates of an array of (pointers to)
178 * points.
179 *
180 * Using Montgomery's trick to perform only one inversion mod P
181 * the cost is:
182 * 1N(t) := 1I + (6t - 3)M + 1S
183 * (See for example Cohen's "A Course in Computational
184 * Algebraic Number Theory", Algorithm 10.3.4.)
185 *
186 * Warning: fails (returning an error) if one of the points is
187 * zero!
188 * This should never happen, see choice of w in ecp_mul_comb().
189 *
190 * \param grp Pointer to the group representing the curve.
191 *
192 * \param T Array of pointers to the points to normalise.
193 *
194 * \param t_len Number of elements in the array.
195 *
196 * \return 0 if successful,
197 * an error if one of the points is zero.
198 */
Janos Follath4ced7c22016-08-18 12:38:46 +0100199#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000200int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000201 mbedtls_ecp_point *T[], size_t t_len );
Janos Follath4ced7c22016-08-18 12:38:46 +0100202#endif
203
Janos Follath1a552ec2016-12-02 13:49:21 +0000204/**
205 * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
206 *
207 * Cost in field operations if done by GECC 3.2.1:
208 * 1N := 1I + 3M + 1S
209 *
210 * \param grp Pointer to the group representing the curve.
211 *
212 * \param pt pointer to the point to be normalised. This is an
213 * input/output parameter.
214 *
215 * \return 0 if successful.
216 */
Janos Follath4ced7c22016-08-18 12:38:46 +0100217#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000218int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000219 mbedtls_ecp_point *pt );
Janos Follath4ced7c22016-08-18 12:38:46 +0100220#endif
221
Janos Follath1a552ec2016-12-02 13:49:21 +0000222#endif /* ECP_SHORTWEIERSTRASS */
223
224#if defined(ECP_MONTGOMERY)
225
Janos Follath4ced7c22016-08-18 12:38:46 +0100226#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000227int mbedtls_internal_ecp_double_add_mxz( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000228 mbedtls_ecp_point *R, mbedtls_ecp_point *S, const mbedtls_ecp_point *P,
229 const mbedtls_ecp_point *Q, const mbedtls_mpi *d );
Janos Follath4ced7c22016-08-18 12:38:46 +0100230#endif
231
Janos Follath1a552ec2016-12-02 13:49:21 +0000232/**
233 * \brief Randomize projective x/z coordinates:
234 * (X, Z) -> (l X, l Z) for random l
235 * This is sort of the reverse operation of ecp_normalize_mxz().
236 *
237 * \param grp pointer to the group representing the curve
238 *
239 * \param P the point on the curve to be randomised given with
240 * projective coordinates. This is an input/output parameter.
241 *
242 * \param f_rng a function pointer to the random number generator
243 *
244 * \param p_rng a pointer to the random number generator state
245 *
246 * \return 0 if successful
247 */
Janos Follath4ced7c22016-08-18 12:38:46 +0100248#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000249int mbedtls_internal_ecp_randomize_mxz( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000250 mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, size_t),
251 void *p_rng );
Janos Follath4ced7c22016-08-18 12:38:46 +0100252#endif
253
Janos Follath1a552ec2016-12-02 13:49:21 +0000254/**
255 * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
256 *
257 * \param grp pointer to the group representing the curve
258 *
259 * \param P pointer to the point to be normalised. This is an
260 * input/output parameter.
261 *
262 * \return 0 if successful
263 */
Janos Follath4ced7c22016-08-18 12:38:46 +0100264#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Janos Follathd26f07e2016-11-18 16:38:23 +0000265int mbedtls_internal_ecp_normalize_mxz( const mbedtls_ecp_group *grp,
Janos Follath466d2072016-11-15 13:45:01 +0000266 mbedtls_ecp_point *P );
Janos Follath4ced7c22016-08-18 12:38:46 +0100267#endif
268
Janos Follath1a552ec2016-12-02 13:49:21 +0000269#endif /* ECP_MONTGOMERY */
270
Janos Follathd26f07e2016-11-18 16:38:23 +0000271#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Janos Follath4ced7c22016-08-18 12:38:46 +0100272
Janos Follathd26f07e2016-11-18 16:38:23 +0000273#endif /* ecp_internal.h */
Janos Follath4ced7c22016-08-18 12:38:46 +0100274