Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1 | /** |
Janos Follath | 47d28f0 | 2016-11-01 13:22:05 +0000 | [diff] [blame] | 2 | * \file ecp_internal.h |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 3 | * |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 4 | * \brief Function declarations for alternative implementation of elliptic curve |
| 5 | * point arithmetic. |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 6 | * |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 7 | * Copyright (C) 2016, ARM Limited, All Rights Reserved |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 8 | * 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 Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 24 | |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 46 | #ifndef MBEDTLS_ECP_INTERNAL_H |
| 47 | #define MBEDTLS_ECP_INTERNAL_H |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 48 | |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 49 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 50 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 51 | /** |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 58 | unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 59 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 60 | /** |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 72 | int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 73 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 74 | /** |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 80 | void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 81 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 82 | #if defined(ECP_SHORTWEIERSTRASS) |
| 83 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 85 | /** |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 103 | int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 104 | mbedtls_ecp_point *pt, int (*f_rng)(void *, unsigned char *, size_t), |
| 105 | void *p_rng ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 106 | #endif |
| 107 | |
| 108 | #if defined(MBEDTLS_ECP_ADD_MIXED_ALT) |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 109 | /** |
| 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 Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 145 | int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 146 | mbedtls_ecp_point *R, const mbedtls_ecp_point *P, |
| 147 | const mbedtls_ecp_point *Q ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 148 | #endif |
| 149 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 150 | /** |
| 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 Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 171 | #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 172 | int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 173 | mbedtls_ecp_point *R, const mbedtls_ecp_point *P ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 174 | #endif |
| 175 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 176 | /** |
| 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 Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 199 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 200 | int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 201 | mbedtls_ecp_point *T[], size_t t_len ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 202 | #endif |
| 203 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 204 | /** |
| 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 Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 217 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 218 | int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 219 | mbedtls_ecp_point *pt ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 220 | #endif |
| 221 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 222 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 223 | |
| 224 | #if defined(ECP_MONTGOMERY) |
| 225 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 226 | #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 227 | int mbedtls_internal_ecp_double_add_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 228 | mbedtls_ecp_point *R, mbedtls_ecp_point *S, const mbedtls_ecp_point *P, |
| 229 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 230 | #endif |
| 231 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 232 | /** |
| 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 Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 248 | #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 249 | int mbedtls_internal_ecp_randomize_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 250 | mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, size_t), |
| 251 | void *p_rng ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 252 | #endif |
| 253 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 254 | /** |
| 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 Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 264 | #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 265 | int mbedtls_internal_ecp_normalize_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | b8a90fb | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 266 | mbedtls_ecp_point *P ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 267 | #endif |
| 268 | |
Janos Follath | aab9efb | 2016-12-02 13:49:21 +0000 | [diff] [blame^] | 269 | #endif /* ECP_MONTGOMERY */ |
| 270 | |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 271 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 272 | |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 273 | #endif /* ecp_internal.h */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 274 | |