xref: /optee_os/lib/libmbedtls/mbedtls/library/ecp_internal_alt.h (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
1*32b31808SJens Wiklander /**
2*32b31808SJens Wiklander  * \file ecp_internal_alt.h
3*32b31808SJens Wiklander  *
4*32b31808SJens Wiklander  * \brief Function declarations for alternative implementation of elliptic curve
5*32b31808SJens Wiklander  * point arithmetic.
6*32b31808SJens Wiklander  */
7*32b31808SJens Wiklander /*
8*32b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
9*32b31808SJens Wiklander  *  SPDX-License-Identifier: Apache-2.0
10*32b31808SJens Wiklander  *
11*32b31808SJens Wiklander  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
12*32b31808SJens Wiklander  *  not use this file except in compliance with the License.
13*32b31808SJens Wiklander  *  You may obtain a copy of the License at
14*32b31808SJens Wiklander  *
15*32b31808SJens Wiklander  *  http://www.apache.org/licenses/LICENSE-2.0
16*32b31808SJens Wiklander  *
17*32b31808SJens Wiklander  *  Unless required by applicable law or agreed to in writing, software
18*32b31808SJens Wiklander  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19*32b31808SJens Wiklander  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20*32b31808SJens Wiklander  *  See the License for the specific language governing permissions and
21*32b31808SJens Wiklander  *  limitations under the License.
22*32b31808SJens Wiklander  */
23*32b31808SJens Wiklander 
24*32b31808SJens Wiklander /*
25*32b31808SJens Wiklander  * References:
26*32b31808SJens Wiklander  *
27*32b31808SJens Wiklander  * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.
28*32b31808SJens Wiklander  *     <http://cr.yp.to/ecdh/curve25519-20060209.pdf>
29*32b31808SJens Wiklander  *
30*32b31808SJens Wiklander  * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
31*32b31808SJens Wiklander  *     for elliptic curve cryptosystems. In : Cryptographic Hardware and
32*32b31808SJens Wiklander  *     Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
33*32b31808SJens Wiklander  *     <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
34*32b31808SJens Wiklander  *
35*32b31808SJens Wiklander  * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
36*32b31808SJens Wiklander  *     render ECC resistant against Side Channel Attacks. IACR Cryptology
37*32b31808SJens Wiklander  *     ePrint Archive, 2004, vol. 2004, p. 342.
38*32b31808SJens Wiklander  *     <http://eprint.iacr.org/2004/342.pdf>
39*32b31808SJens Wiklander  *
40*32b31808SJens Wiklander  * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.
41*32b31808SJens Wiklander  *     <http://www.secg.org/sec2-v2.pdf>
42*32b31808SJens Wiklander  *
43*32b31808SJens Wiklander  * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic
44*32b31808SJens Wiklander  *     Curve Cryptography.
45*32b31808SJens Wiklander  *
46*32b31808SJens Wiklander  * [6] Digital Signature Standard (DSS), FIPS 186-4.
47*32b31808SJens Wiklander  *     <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>
48*32b31808SJens Wiklander  *
49*32b31808SJens Wiklander  * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer
50*32b31808SJens Wiklander  *     Security (TLS), RFC 4492.
51*32b31808SJens Wiklander  *     <https://tools.ietf.org/search/rfc4492>
52*32b31808SJens Wiklander  *
53*32b31808SJens Wiklander  * [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>
54*32b31808SJens Wiklander  *
55*32b31808SJens Wiklander  * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.
56*32b31808SJens Wiklander  *     Springer Science & Business Media, 1 Aug 2000
57*32b31808SJens Wiklander  */
58*32b31808SJens Wiklander 
59*32b31808SJens Wiklander #ifndef MBEDTLS_ECP_INTERNAL_H
60*32b31808SJens Wiklander #define MBEDTLS_ECP_INTERNAL_H
61*32b31808SJens Wiklander 
62*32b31808SJens Wiklander #include "mbedtls/build_info.h"
63*32b31808SJens Wiklander 
64*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_INTERNAL_ALT)
65*32b31808SJens Wiklander 
66*32b31808SJens Wiklander /**
67*32b31808SJens Wiklander  * \brief           Indicate if the Elliptic Curve Point module extension can
68*32b31808SJens Wiklander  *                  handle the group.
69*32b31808SJens Wiklander  *
70*32b31808SJens Wiklander  * \param grp       The pointer to the elliptic curve group that will be the
71*32b31808SJens Wiklander  *                  basis of the cryptographic computations.
72*32b31808SJens Wiklander  *
73*32b31808SJens Wiklander  * \return          Non-zero if successful.
74*32b31808SJens Wiklander  */
75*32b31808SJens Wiklander unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group *grp);
76*32b31808SJens Wiklander 
77*32b31808SJens Wiklander /**
78*32b31808SJens Wiklander  * \brief           Initialise the Elliptic Curve Point module extension.
79*32b31808SJens Wiklander  *
80*32b31808SJens Wiklander  *                  If mbedtls_internal_ecp_grp_capable returns true for a
81*32b31808SJens Wiklander  *                  group, this function has to be able to initialise the
82*32b31808SJens Wiklander  *                  module for it.
83*32b31808SJens Wiklander  *
84*32b31808SJens Wiklander  *                  This module can be a driver to a crypto hardware
85*32b31808SJens Wiklander  *                  accelerator, for which this could be an initialise function.
86*32b31808SJens Wiklander  *
87*32b31808SJens Wiklander  * \param grp       The pointer to the group the module needs to be
88*32b31808SJens Wiklander  *                  initialised for.
89*32b31808SJens Wiklander  *
90*32b31808SJens Wiklander  * \return          0 if successful.
91*32b31808SJens Wiklander  */
92*32b31808SJens Wiklander int mbedtls_internal_ecp_init(const mbedtls_ecp_group *grp);
93*32b31808SJens Wiklander 
94*32b31808SJens Wiklander /**
95*32b31808SJens Wiklander  * \brief           Frees and deallocates the Elliptic Curve Point module
96*32b31808SJens Wiklander  *                  extension.
97*32b31808SJens Wiklander  *
98*32b31808SJens Wiklander  * \param grp       The pointer to the group the module was initialised for.
99*32b31808SJens Wiklander  */
100*32b31808SJens Wiklander void mbedtls_internal_ecp_free(const mbedtls_ecp_group *grp);
101*32b31808SJens Wiklander 
102*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
103*32b31808SJens Wiklander 
104*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
105*32b31808SJens Wiklander /**
106*32b31808SJens Wiklander  * \brief           Randomize jacobian coordinates:
107*32b31808SJens Wiklander  *                  (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
108*32b31808SJens Wiklander  *
109*32b31808SJens Wiklander  * \param grp       Pointer to the group representing the curve.
110*32b31808SJens Wiklander  *
111*32b31808SJens Wiklander  * \param pt        The point on the curve to be randomised, given with Jacobian
112*32b31808SJens Wiklander  *                  coordinates.
113*32b31808SJens Wiklander  *
114*32b31808SJens Wiklander  * \param f_rng     A function pointer to the random number generator.
115*32b31808SJens Wiklander  *
116*32b31808SJens Wiklander  * \param p_rng     A pointer to the random number generator state.
117*32b31808SJens Wiklander  *
118*32b31808SJens Wiklander  * \return          0 if successful.
119*32b31808SJens Wiklander  */
120*32b31808SJens Wiklander int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group *grp,
121*32b31808SJens Wiklander                                        mbedtls_ecp_point *pt, int (*f_rng)(void *,
122*32b31808SJens Wiklander                                                                            unsigned char *,
123*32b31808SJens Wiklander                                                                            size_t),
124*32b31808SJens Wiklander                                        void *p_rng);
125*32b31808SJens Wiklander #endif
126*32b31808SJens Wiklander 
127*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
128*32b31808SJens Wiklander /**
129*32b31808SJens Wiklander  * \brief           Addition: R = P + Q, mixed affine-Jacobian coordinates.
130*32b31808SJens Wiklander  *
131*32b31808SJens Wiklander  *                  The coordinates of Q must be normalized (= affine),
132*32b31808SJens Wiklander  *                  but those of P don't need to. R is not normalized.
133*32b31808SJens Wiklander  *
134*32b31808SJens Wiklander  *                  This function is used only as a subrutine of
135*32b31808SJens Wiklander  *                  ecp_mul_comb().
136*32b31808SJens Wiklander  *
137*32b31808SJens Wiklander  *                  Special cases: (1) P or Q is zero, (2) R is zero,
138*32b31808SJens Wiklander  *                      (3) P == Q.
139*32b31808SJens Wiklander  *                  None of these cases can happen as intermediate step in
140*32b31808SJens Wiklander  *                  ecp_mul_comb():
141*32b31808SJens Wiklander  *                      - at each step, P, Q and R are multiples of the base
142*32b31808SJens Wiklander  *                      point, the factor being less than its order, so none of
143*32b31808SJens Wiklander  *                      them is zero;
144*32b31808SJens Wiklander  *                      - Q is an odd multiple of the base point, P an even
145*32b31808SJens Wiklander  *                      multiple, due to the choice of precomputed points in the
146*32b31808SJens Wiklander  *                      modified comb method.
147*32b31808SJens Wiklander  *                  So branches for these cases do not leak secret information.
148*32b31808SJens Wiklander  *
149*32b31808SJens Wiklander  *                  We accept Q->Z being unset (saving memory in tables) as
150*32b31808SJens Wiklander  *                  meaning 1.
151*32b31808SJens Wiklander  *
152*32b31808SJens Wiklander  *                  Cost in field operations if done by [5] 3.22:
153*32b31808SJens Wiklander  *                      1A := 8M + 3S
154*32b31808SJens Wiklander  *
155*32b31808SJens Wiklander  * \param grp       Pointer to the group representing the curve.
156*32b31808SJens Wiklander  *
157*32b31808SJens Wiklander  * \param R         Pointer to a point structure to hold the result.
158*32b31808SJens Wiklander  *
159*32b31808SJens Wiklander  * \param P         Pointer to the first summand, given with Jacobian
160*32b31808SJens Wiklander  *                  coordinates
161*32b31808SJens Wiklander  *
162*32b31808SJens Wiklander  * \param Q         Pointer to the second summand, given with affine
163*32b31808SJens Wiklander  *                  coordinates.
164*32b31808SJens Wiklander  *
165*32b31808SJens Wiklander  * \return          0 if successful.
166*32b31808SJens Wiklander  */
167*32b31808SJens Wiklander int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group *grp,
168*32b31808SJens Wiklander                                    mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
169*32b31808SJens Wiklander                                    const mbedtls_ecp_point *Q);
170*32b31808SJens Wiklander #endif
171*32b31808SJens Wiklander 
172*32b31808SJens Wiklander /**
173*32b31808SJens Wiklander  * \brief           Point doubling R = 2 P, Jacobian coordinates.
174*32b31808SJens Wiklander  *
175*32b31808SJens Wiklander  *                  Cost:   1D := 3M + 4S    (A ==  0)
176*32b31808SJens Wiklander  *                          4M + 4S          (A == -3)
177*32b31808SJens Wiklander  *                          3M + 6S + 1a     otherwise
178*32b31808SJens Wiklander  *                  when the implementation is based on the "dbl-1998-cmo-2"
179*32b31808SJens Wiklander  *                  doubling formulas in [8] and standard optimizations are
180*32b31808SJens Wiklander  *                  applied when curve parameter A is one of { 0, -3 }.
181*32b31808SJens Wiklander  *
182*32b31808SJens Wiklander  * \param grp       Pointer to the group representing the curve.
183*32b31808SJens Wiklander  *
184*32b31808SJens Wiklander  * \param R         Pointer to a point structure to hold the result.
185*32b31808SJens Wiklander  *
186*32b31808SJens Wiklander  * \param P         Pointer to the point that has to be doubled, given with
187*32b31808SJens Wiklander  *                  Jacobian coordinates.
188*32b31808SJens Wiklander  *
189*32b31808SJens Wiklander  * \return          0 if successful.
190*32b31808SJens Wiklander  */
191*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
192*32b31808SJens Wiklander int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group *grp,
193*32b31808SJens Wiklander                                     mbedtls_ecp_point *R, const mbedtls_ecp_point *P);
194*32b31808SJens Wiklander #endif
195*32b31808SJens Wiklander 
196*32b31808SJens Wiklander /**
197*32b31808SJens Wiklander  * \brief           Normalize jacobian coordinates of an array of (pointers to)
198*32b31808SJens Wiklander  *                  points.
199*32b31808SJens Wiklander  *
200*32b31808SJens Wiklander  *                  Using Montgomery's trick to perform only one inversion mod P
201*32b31808SJens Wiklander  *                  the cost is:
202*32b31808SJens Wiklander  *                      1N(t) := 1I + (6t - 3)M + 1S
203*32b31808SJens Wiklander  *                  (See for example Algorithm 10.3.4. in [9])
204*32b31808SJens Wiklander  *
205*32b31808SJens Wiklander  *                  This function is used only as a subrutine of
206*32b31808SJens Wiklander  *                  ecp_mul_comb().
207*32b31808SJens Wiklander  *
208*32b31808SJens Wiklander  *                  Warning: fails (returning an error) if one of the points is
209*32b31808SJens Wiklander  *                  zero!
210*32b31808SJens Wiklander  *                  This should never happen, see choice of w in ecp_mul_comb().
211*32b31808SJens Wiklander  *
212*32b31808SJens Wiklander  * \param grp       Pointer to the group representing the curve.
213*32b31808SJens Wiklander  *
214*32b31808SJens Wiklander  * \param T         Array of pointers to the points to normalise.
215*32b31808SJens Wiklander  *
216*32b31808SJens Wiklander  * \param t_len     Number of elements in the array.
217*32b31808SJens Wiklander  *
218*32b31808SJens Wiklander  * \return          0 if successful,
219*32b31808SJens Wiklander  *                      an error if one of the points is zero.
220*32b31808SJens Wiklander  */
221*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
222*32b31808SJens Wiklander int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
223*32b31808SJens Wiklander                                             mbedtls_ecp_point *T[], size_t t_len);
224*32b31808SJens Wiklander #endif
225*32b31808SJens Wiklander 
226*32b31808SJens Wiklander /**
227*32b31808SJens Wiklander  * \brief           Normalize jacobian coordinates so that Z == 0 || Z == 1.
228*32b31808SJens Wiklander  *
229*32b31808SJens Wiklander  *                  Cost in field operations if done by [5] 3.2.1:
230*32b31808SJens Wiklander  *                      1N := 1I + 3M + 1S
231*32b31808SJens Wiklander  *
232*32b31808SJens Wiklander  * \param grp       Pointer to the group representing the curve.
233*32b31808SJens Wiklander  *
234*32b31808SJens Wiklander  * \param pt        pointer to the point to be normalised. This is an
235*32b31808SJens Wiklander  *                  input/output parameter.
236*32b31808SJens Wiklander  *
237*32b31808SJens Wiklander  * \return          0 if successful.
238*32b31808SJens Wiklander  */
239*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
240*32b31808SJens Wiklander int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group *grp,
241*32b31808SJens Wiklander                                        mbedtls_ecp_point *pt);
242*32b31808SJens Wiklander #endif
243*32b31808SJens Wiklander 
244*32b31808SJens Wiklander #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
245*32b31808SJens Wiklander 
246*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
247*32b31808SJens Wiklander 
248*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
249*32b31808SJens Wiklander int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group *grp,
250*32b31808SJens Wiklander                                         mbedtls_ecp_point *R,
251*32b31808SJens Wiklander                                         mbedtls_ecp_point *S,
252*32b31808SJens Wiklander                                         const mbedtls_ecp_point *P,
253*32b31808SJens Wiklander                                         const mbedtls_ecp_point *Q,
254*32b31808SJens Wiklander                                         const mbedtls_mpi *d);
255*32b31808SJens Wiklander #endif
256*32b31808SJens Wiklander 
257*32b31808SJens Wiklander /**
258*32b31808SJens Wiklander  * \brief           Randomize projective x/z coordinates:
259*32b31808SJens Wiklander  *                      (X, Z) -> (l X, l Z) for random l
260*32b31808SJens Wiklander  *
261*32b31808SJens Wiklander  * \param grp       pointer to the group representing the curve
262*32b31808SJens Wiklander  *
263*32b31808SJens Wiklander  * \param P         the point on the curve to be randomised given with
264*32b31808SJens Wiklander  *                  projective coordinates. This is an input/output parameter.
265*32b31808SJens Wiklander  *
266*32b31808SJens Wiklander  * \param f_rng     a function pointer to the random number generator
267*32b31808SJens Wiklander  *
268*32b31808SJens Wiklander  * \param p_rng     a pointer to the random number generator state
269*32b31808SJens Wiklander  *
270*32b31808SJens Wiklander  * \return          0 if successful
271*32b31808SJens Wiklander  */
272*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
273*32b31808SJens Wiklander int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group *grp,
274*32b31808SJens Wiklander                                        mbedtls_ecp_point *P, int (*f_rng)(void *,
275*32b31808SJens Wiklander                                                                           unsigned char *,
276*32b31808SJens Wiklander                                                                           size_t),
277*32b31808SJens Wiklander                                        void *p_rng);
278*32b31808SJens Wiklander #endif
279*32b31808SJens Wiklander 
280*32b31808SJens Wiklander /**
281*32b31808SJens Wiklander  * \brief           Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
282*32b31808SJens Wiklander  *
283*32b31808SJens Wiklander  * \param grp       pointer to the group representing the curve
284*32b31808SJens Wiklander  *
285*32b31808SJens Wiklander  * \param P         pointer to the point to be normalised. This is an
286*32b31808SJens Wiklander  *                  input/output parameter.
287*32b31808SJens Wiklander  *
288*32b31808SJens Wiklander  * \return          0 if successful
289*32b31808SJens Wiklander  */
290*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
291*32b31808SJens Wiklander int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group *grp,
292*32b31808SJens Wiklander                                        mbedtls_ecp_point *P);
293*32b31808SJens Wiklander #endif
294*32b31808SJens Wiklander 
295*32b31808SJens Wiklander #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
296*32b31808SJens Wiklander 
297*32b31808SJens Wiklander #endif /* MBEDTLS_ECP_INTERNAL_ALT */
298*32b31808SJens Wiklander 
299*32b31808SJens Wiklander #endif /* ecp_internal_alt.h */
300