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