Path: blob/master/thirdparty/mbedtls/library/ecp_internal_alt.h
9898 views
/**1* \file ecp_internal_alt.h2*3* \brief Function declarations for alternative implementation of elliptic curve4* point arithmetic.5*/6/*7* Copyright The Mbed TLS Contributors8* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later9*/1011/*12* References:13*14* [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records.15* <http://cr.yp.to/ecdh/curve25519-20060209.pdf>16*17* [2] CORON, Jean-S'ebastien. Resistance against differential power analysis18* for elliptic curve cryptosystems. In : Cryptographic Hardware and19* Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.20* <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>21*22* [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to23* render ECC resistant against Side Channel Attacks. IACR Cryptology24* ePrint Archive, 2004, vol. 2004, p. 342.25* <http://eprint.iacr.org/2004/342.pdf>26*27* [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters.28* <http://www.secg.org/sec2-v2.pdf>29*30* [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic31* Curve Cryptography.32*33* [6] Digital Signature Standard (DSS), FIPS 186-4.34* <http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf>35*36* [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer37* Security (TLS), RFC 4492.38* <https://tools.ietf.org/search/rfc4492>39*40* [8] <http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html>41*42* [9] COHEN, Henri. A Course in Computational Algebraic Number Theory.43* Springer Science & Business Media, 1 Aug 200044*/4546#ifndef MBEDTLS_ECP_INTERNAL_H47#define MBEDTLS_ECP_INTERNAL_H4849#include "mbedtls/build_info.h"5051#if defined(MBEDTLS_ECP_INTERNAL_ALT)5253/**54* \brief Indicate if the Elliptic Curve Point module extension can55* handle the group.56*57* \param grp The pointer to the elliptic curve group that will be the58* basis of the cryptographic computations.59*60* \return Non-zero if successful.61*/62unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group *grp);6364/**65* \brief Initialise the Elliptic Curve Point module extension.66*67* If mbedtls_internal_ecp_grp_capable returns true for a68* group, this function has to be able to initialise the69* module for it.70*71* This module can be a driver to a crypto hardware72* accelerator, for which this could be an initialise function.73*74* \param grp The pointer to the group the module needs to be75* initialised for.76*77* \return 0 if successful.78*/79int mbedtls_internal_ecp_init(const mbedtls_ecp_group *grp);8081/**82* \brief Frees and deallocates the Elliptic Curve Point module83* extension.84*85* \param grp The pointer to the group the module was initialised for.86*/87void mbedtls_internal_ecp_free(const mbedtls_ecp_group *grp);8889#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)9091#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)92/**93* \brief Randomize jacobian coordinates:94* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.95*96* \param grp Pointer to the group representing the curve.97*98* \param pt The point on the curve to be randomised, given with Jacobian99* coordinates.100*101* \param f_rng A function pointer to the random number generator.102*103* \param p_rng A pointer to the random number generator state.104*105* \return 0 if successful.106*/107int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group *grp,108mbedtls_ecp_point *pt, int (*f_rng)(void *,109unsigned char *,110size_t),111void *p_rng);112#endif113114#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)115/**116* \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.117*118* The coordinates of Q must be normalized (= affine),119* but those of P don't need to. R is not normalized.120*121* This function is used only as a subrutine of122* ecp_mul_comb().123*124* Special cases: (1) P or Q is zero, (2) R is zero,125* (3) P == Q.126* None of these cases can happen as intermediate step in127* ecp_mul_comb():128* - at each step, P, Q and R are multiples of the base129* point, the factor being less than its order, so none of130* them is zero;131* - Q is an odd multiple of the base point, P an even132* multiple, due to the choice of precomputed points in the133* modified comb method.134* So branches for these cases do not leak secret information.135*136* We accept Q->Z being unset (saving memory in tables) as137* meaning 1.138*139* Cost in field operations if done by [5] 3.22:140* 1A := 8M + 3S141*142* \param grp Pointer to the group representing the curve.143*144* \param R Pointer to a point structure to hold the result.145*146* \param P Pointer to the first summand, given with Jacobian147* coordinates148*149* \param Q Pointer to the second summand, given with affine150* coordinates.151*152* \return 0 if successful.153*/154int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group *grp,155mbedtls_ecp_point *R, const mbedtls_ecp_point *P,156const mbedtls_ecp_point *Q);157#endif158159/**160* \brief Point doubling R = 2 P, Jacobian coordinates.161*162* Cost: 1D := 3M + 4S (A == 0)163* 4M + 4S (A == -3)164* 3M + 6S + 1a otherwise165* when the implementation is based on the "dbl-1998-cmo-2"166* doubling formulas in [8] and standard optimizations are167* applied when curve parameter A is one of { 0, -3 }.168*169* \param grp Pointer to the group representing the curve.170*171* \param R Pointer to a point structure to hold the result.172*173* \param P Pointer to the point that has to be doubled, given with174* Jacobian coordinates.175*176* \return 0 if successful.177*/178#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)179int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group *grp,180mbedtls_ecp_point *R, const mbedtls_ecp_point *P);181#endif182183/**184* \brief Normalize jacobian coordinates of an array of (pointers to)185* points.186*187* Using Montgomery's trick to perform only one inversion mod P188* the cost is:189* 1N(t) := 1I + (6t - 3)M + 1S190* (See for example Algorithm 10.3.4. in [9])191*192* This function is used only as a subrutine of193* ecp_mul_comb().194*195* Warning: fails (returning an error) if one of the points is196* zero!197* This should never happen, see choice of w in ecp_mul_comb().198*199* \param grp Pointer to the group representing the curve.200*201* \param T Array of pointers to the points to normalise.202*203* \param t_len Number of elements in the array.204*205* \return 0 if successful,206* an error if one of the points is zero.207*/208#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)209int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group *grp,210mbedtls_ecp_point *T[], size_t t_len);211#endif212213/**214* \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.215*216* Cost in field operations if done by [5] 3.2.1:217* 1N := 1I + 3M + 1S218*219* \param grp Pointer to the group representing the curve.220*221* \param pt pointer to the point to be normalised. This is an222* input/output parameter.223*224* \return 0 if successful.225*/226#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)227int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group *grp,228mbedtls_ecp_point *pt);229#endif230231#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */232233#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)234235#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)236int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group *grp,237mbedtls_ecp_point *R,238mbedtls_ecp_point *S,239const mbedtls_ecp_point *P,240const mbedtls_ecp_point *Q,241const mbedtls_mpi *d);242#endif243244/**245* \brief Randomize projective x/z coordinates:246* (X, Z) -> (l X, l Z) for random l247*248* \param grp pointer to the group representing the curve249*250* \param P the point on the curve to be randomised given with251* projective coordinates. This is an input/output parameter.252*253* \param f_rng a function pointer to the random number generator254*255* \param p_rng a pointer to the random number generator state256*257* \return 0 if successful258*/259#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)260int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group *grp,261mbedtls_ecp_point *P, int (*f_rng)(void *,262unsigned char *,263size_t),264void *p_rng);265#endif266267/**268* \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.269*270* \param grp pointer to the group representing the curve271*272* \param P pointer to the point to be normalised. This is an273* input/output parameter.274*275* \return 0 if successful276*/277#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)278int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group *grp,279mbedtls_ecp_point *P);280#endif281282#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */283284#endif /* MBEDTLS_ECP_INTERNAL_ALT */285286#endif /* ecp_internal_alt.h */287288289