Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1 | /** |
Janos Follath | 58cf392 | 2016-11-01 13:22:05 +0000 | [diff] [blame] | 2 | * \file ecp_internal.h |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 3 | * |
Janos Follath | 61ea6ec | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 4 | * \brief Function declarations for alternative implementation of elliptic curve |
| 5 | * point arithmetic. |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 6 | * |
Janos Follath | 61ea6ec | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 7 | * Copyright (C) 2016, ARM Limited, All Rights Reserved |
Janos Follath | 4ced7c2 | 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 | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * References: |
| 27 | * |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 28 | * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records. |
| 29 | * <http://cr.yp.to/ecdh/curve25519-20060209.pdf> |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 30 | * |
| 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 Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 40 | * |
| 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 Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 60 | #ifndef MBEDTLS_ECP_INTERNAL_H |
| 61 | #define MBEDTLS_ECP_INTERNAL_H |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 62 | |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 63 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 64 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 65 | /** |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 66 | * \brief Indicate if the Elliptic Curve Point module extension can |
| 67 | * handle the group. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 68 | * |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 69 | * \param grp The pointer to the elliptic curve group that will be the |
| 70 | * basis of the cryptographic computations. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 71 | * |
| 72 | * \return Non-zero if successful. |
| 73 | */ |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 74 | unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 75 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 76 | /** |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 77 | * \brief Initialise the Elliptic Curve Point module extension. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 78 | * |
| 79 | * If mbedtls_internal_ecp_grp_capable returns true for a |
| 80 | * group, this function has to be able to initialise the |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 81 | * module for it. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 82 | * |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 83 | * 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 Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 87 | * initialised for. |
| 88 | * |
| 89 | * \return 0 if successful. |
| 90 | */ |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 91 | int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 92 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 93 | /** |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 94 | * \brief Frees and deallocates the Elliptic Curve Point module |
| 95 | * extension. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 96 | * |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 97 | * \param grp The pointer to the group the module was initialised for. |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 98 | */ |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 99 | void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 100 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 101 | #if defined(ECP_SHORTWEIERSTRASS) |
| 102 | |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 103 | #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 104 | /** |
| 105 | * \brief Randomize jacobian coordinates: |
| 106 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l. |
| 107 | * |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 108 | * \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 Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 119 | int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 120 | mbedtls_ecp_point *pt, int (*f_rng)(void *, unsigned char *, size_t), |
| 121 | void *p_rng ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | #if defined(MBEDTLS_ECP_ADD_MIXED_ALT) |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 125 | /** |
| 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 Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 131 | * This function is used only as a subrutine of |
| 132 | * ecp_mul_comb(). |
| 133 | * |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 134 | * 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 Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 149 | * Cost in field operations if done by [5] 3.22: |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 150 | * 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 Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 164 | int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 165 | mbedtls_ecp_point *R, const mbedtls_ecp_point *P, |
| 166 | const mbedtls_ecp_point *Q ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 167 | #endif |
| 168 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 169 | /** |
| 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 Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 175 | * 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 Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 178 | * |
| 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 Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 189 | int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 190 | mbedtls_ecp_point *R, const mbedtls_ecp_point *P ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 191 | #endif |
| 192 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 193 | /** |
| 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 Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 200 | * (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 Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 204 | * |
| 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 Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 218 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 219 | int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 220 | mbedtls_ecp_point *T[], size_t t_len ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 221 | #endif |
| 222 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 223 | /** |
| 224 | * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1. |
| 225 | * |
Janos Follath | 6669918 | 2016-12-08 16:15:51 +0000 | [diff] [blame^] | 226 | * Cost in field operations if done by [5] 3.2.1: |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 227 | * 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 Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 236 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 237 | int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 238 | mbedtls_ecp_point *pt ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 239 | #endif |
| 240 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 241 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 242 | |
| 243 | #if defined(ECP_MONTGOMERY) |
| 244 | |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 245 | #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 246 | int mbedtls_internal_ecp_double_add_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 247 | mbedtls_ecp_point *R, mbedtls_ecp_point *S, const mbedtls_ecp_point *P, |
| 248 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 249 | #endif |
| 250 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 251 | /** |
| 252 | * \brief Randomize projective x/z coordinates: |
| 253 | * (X, Z) -> (l X, l Z) for random l |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 254 | * |
| 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 Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 266 | #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 267 | int mbedtls_internal_ecp_randomize_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 268 | mbedtls_ecp_point *P, int (*f_rng)(void *, unsigned char *, size_t), |
| 269 | void *p_rng ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 270 | #endif |
| 271 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 272 | /** |
| 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 Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 282 | #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 283 | int mbedtls_internal_ecp_normalize_mxz( const mbedtls_ecp_group *grp, |
Janos Follath | 466d207 | 2016-11-15 13:45:01 +0000 | [diff] [blame] | 284 | mbedtls_ecp_point *P ); |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 285 | #endif |
| 286 | |
Janos Follath | 1a552ec | 2016-12-02 13:49:21 +0000 | [diff] [blame] | 287 | #endif /* ECP_MONTGOMERY */ |
| 288 | |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 289 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 290 | |
Janos Follath | d26f07e | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 291 | #endif /* ecp_internal.h */ |
Janos Follath | 4ced7c2 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 292 | |