ecp_internal_alt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * \file ecp_internal_alt.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. * 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 analysis
  18. * for elliptic curve cryptosystems. In : Cryptographic Hardware and
  19. * 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 to
  23. * render ECC resistant against Side Channel Attacks. IACR Cryptology
  24. * 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 Elliptic
  31. * 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 Layer
  37. * 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 2000
  44. */
  45. #ifndef MBEDTLS_ECP_INTERNAL_H
  46. #define MBEDTLS_ECP_INTERNAL_H
  47. #include "mbedtls/build_info.h"
  48. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  49. /**
  50. * \brief Indicate if the Elliptic Curve Point module extension can
  51. * handle the group.
  52. *
  53. * \param grp The pointer to the elliptic curve group that will be the
  54. * basis of the cryptographic computations.
  55. *
  56. * \return Non-zero if successful.
  57. */
  58. unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group *grp);
  59. /**
  60. * \brief Initialise the Elliptic Curve Point module extension.
  61. *
  62. * If mbedtls_internal_ecp_grp_capable returns true for a
  63. * group, this function has to be able to initialise the
  64. * module for it.
  65. *
  66. * This module can be a driver to a crypto hardware
  67. * accelerator, for which this could be an initialise function.
  68. *
  69. * \param grp The pointer to the group the module needs to be
  70. * initialised for.
  71. *
  72. * \return 0 if successful.
  73. */
  74. int mbedtls_internal_ecp_init(const mbedtls_ecp_group *grp);
  75. /**
  76. * \brief Frees and deallocates the Elliptic Curve Point module
  77. * extension.
  78. *
  79. * \param grp The pointer to the group the module was initialised for.
  80. */
  81. void mbedtls_internal_ecp_free(const mbedtls_ecp_group *grp);
  82. #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
  83. #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
  84. /**
  85. * \brief Randomize jacobian coordinates:
  86. * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l.
  87. *
  88. * \param grp Pointer to the group representing the curve.
  89. *
  90. * \param pt The point on the curve to be randomised, given with Jacobian
  91. * coordinates.
  92. *
  93. * \param f_rng A function pointer to the random number generator.
  94. *
  95. * \param p_rng A pointer to the random number generator state.
  96. *
  97. * \return 0 if successful.
  98. */
  99. int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group *grp,
  100. mbedtls_ecp_point *pt, int (*f_rng)(void *,
  101. unsigned char *,
  102. size_t),
  103. void *p_rng);
  104. #endif
  105. #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
  106. /**
  107. * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates.
  108. *
  109. * The coordinates of Q must be normalized (= affine),
  110. * but those of P don't need to. R is not normalized.
  111. *
  112. * This function is used only as a subrutine of
  113. * ecp_mul_comb().
  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 [5] 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. */
  145. int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group *grp,
  146. mbedtls_ecp_point *R, const mbedtls_ecp_point *P,
  147. const mbedtls_ecp_point *Q);
  148. #endif
  149. /**
  150. * \brief Point doubling R = 2 P, Jacobian coordinates.
  151. *
  152. * Cost: 1D := 3M + 4S (A == 0)
  153. * 4M + 4S (A == -3)
  154. * 3M + 6S + 1a otherwise
  155. * when the implementation is based on the "dbl-1998-cmo-2"
  156. * doubling formulas in [8] and standard optimizations are
  157. * applied when curve parameter A is one of { 0, -3 }.
  158. *
  159. * \param grp Pointer to the group representing the curve.
  160. *
  161. * \param R Pointer to a point structure to hold the result.
  162. *
  163. * \param P Pointer to the point that has to be doubled, given with
  164. * Jacobian coordinates.
  165. *
  166. * \return 0 if successful.
  167. */
  168. #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
  169. int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group *grp,
  170. mbedtls_ecp_point *R, const mbedtls_ecp_point *P);
  171. #endif
  172. /**
  173. * \brief Normalize jacobian coordinates of an array of (pointers to)
  174. * points.
  175. *
  176. * Using Montgomery's trick to perform only one inversion mod P
  177. * the cost is:
  178. * 1N(t) := 1I + (6t - 3)M + 1S
  179. * (See for example Algorithm 10.3.4. in [9])
  180. *
  181. * This function is used only as a subrutine of
  182. * ecp_mul_comb().
  183. *
  184. * Warning: fails (returning an error) if one of the points is
  185. * zero!
  186. * This should never happen, see choice of w in ecp_mul_comb().
  187. *
  188. * \param grp Pointer to the group representing the curve.
  189. *
  190. * \param T Array of pointers to the points to normalise.
  191. *
  192. * \param t_len Number of elements in the array.
  193. *
  194. * \return 0 if successful,
  195. * an error if one of the points is zero.
  196. */
  197. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
  198. int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
  199. mbedtls_ecp_point *T[], size_t t_len);
  200. #endif
  201. /**
  202. * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1.
  203. *
  204. * Cost in field operations if done by [5] 3.2.1:
  205. * 1N := 1I + 3M + 1S
  206. *
  207. * \param grp Pointer to the group representing the curve.
  208. *
  209. * \param pt pointer to the point to be normalised. This is an
  210. * input/output parameter.
  211. *
  212. * \return 0 if successful.
  213. */
  214. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
  215. int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group *grp,
  216. mbedtls_ecp_point *pt);
  217. #endif
  218. #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
  219. #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
  220. #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
  221. int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group *grp,
  222. mbedtls_ecp_point *R,
  223. mbedtls_ecp_point *S,
  224. const mbedtls_ecp_point *P,
  225. const mbedtls_ecp_point *Q,
  226. const mbedtls_mpi *d);
  227. #endif
  228. /**
  229. * \brief Randomize projective x/z coordinates:
  230. * (X, Z) -> (l X, l Z) for random l
  231. *
  232. * \param grp pointer to the group representing the curve
  233. *
  234. * \param P the point on the curve to be randomised given with
  235. * projective coordinates. This is an input/output parameter.
  236. *
  237. * \param f_rng a function pointer to the random number generator
  238. *
  239. * \param p_rng a pointer to the random number generator state
  240. *
  241. * \return 0 if successful
  242. */
  243. #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
  244. int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group *grp,
  245. mbedtls_ecp_point *P, int (*f_rng)(void *,
  246. unsigned char *,
  247. size_t),
  248. void *p_rng);
  249. #endif
  250. /**
  251. * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1.
  252. *
  253. * \param grp pointer to the group representing the curve
  254. *
  255. * \param P pointer to the point to be normalised. This is an
  256. * input/output parameter.
  257. *
  258. * \return 0 if successful
  259. */
  260. #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
  261. int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group *grp,
  262. mbedtls_ecp_point *P);
  263. #endif
  264. #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
  265. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  266. #endif /* ecp_internal_alt.h */