point_448.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #ifndef OSSL_CRYPTO_EC_CURVE448_POINT_448_H
  13. # define OSSL_CRYPTO_EC_CURVE448_POINT_448_H
  14. # include "curve448utils.h"
  15. # include "field.h"
  16. /* Comb config: number of combs, n, t, s. */
  17. #define COMBS_N 5
  18. #define COMBS_T 5
  19. #define COMBS_S 18
  20. /* Projective Niels coordinates */
  21. typedef struct {
  22. gf a, b, c;
  23. } niels_s, niels_t[1];
  24. typedef struct {
  25. niels_t n;
  26. gf z;
  27. } pniels_t[1];
  28. /* Precomputed base */
  29. struct curve448_precomputed_s {
  30. niels_t table[COMBS_N << (COMBS_T - 1)];
  31. };
  32. # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
  33. /* The number of bits in a scalar */
  34. # define C448_SCALAR_BITS 446
  35. /* Number of bytes in a serialized scalar. */
  36. # define C448_SCALAR_BYTES 56
  37. /* X448 encoding ratio. */
  38. # define X448_ENCODE_RATIO 2
  39. /* Number of bytes in an x448 public key */
  40. # define X448_PUBLIC_BYTES 56
  41. /* Number of bytes in an x448 private key */
  42. # define X448_PRIVATE_BYTES 56
  43. /* Twisted Edwards extended homogeneous coordinates */
  44. typedef struct curve448_point_s {
  45. gf x, y, z, t;
  46. } curve448_point_t[1];
  47. /* Precomputed table based on a point. Can be trivial implementation. */
  48. struct curve448_precomputed_s;
  49. /* Precomputed table based on a point. Can be trivial implementation. */
  50. typedef struct curve448_precomputed_s curve448_precomputed_s;
  51. /* Scalar is stored packed, because we don't need the speed. */
  52. typedef struct curve448_scalar_s {
  53. c448_word_t limb[C448_SCALAR_LIMBS];
  54. } curve448_scalar_t[1];
  55. /* A scalar equal to 1. */
  56. extern const curve448_scalar_t ossl_curve448_scalar_one;
  57. /* A scalar equal to 0. */
  58. extern const curve448_scalar_t ossl_curve448_scalar_zero;
  59. /* The identity point on the curve. */
  60. extern const curve448_point_t ossl_curve448_point_identity;
  61. /* Precomputed table for the base point on the curve. */
  62. extern const struct curve448_precomputed_s *ossl_curve448_precomputed_base;
  63. extern const niels_t *ossl_curve448_wnaf_base;
  64. /*
  65. * Read a scalar from wire format or from bytes.
  66. *
  67. * ser (in): Serialized form of a scalar.
  68. * out (out): Deserialized form.
  69. *
  70. * Returns:
  71. * C448_SUCCESS: The scalar was correctly encoded.
  72. * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
  73. * modulo that modulus.
  74. */
  75. c448_error_t
  76. ossl_curve448_scalar_decode(curve448_scalar_t out,
  77. const unsigned char ser[C448_SCALAR_BYTES]);
  78. /*
  79. * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
  80. *
  81. * ser (in): Serialized form of a scalar.
  82. * ser_len (in): Length of serialized form.
  83. * out (out): Deserialized form.
  84. */
  85. void
  86. ossl_curve448_scalar_decode_long(curve448_scalar_t out,
  87. const unsigned char *ser, size_t ser_len);
  88. /*
  89. * Serialize a scalar to wire format.
  90. *
  91. * ser (out): Serialized form of a scalar.
  92. * s (in): Deserialized scalar.
  93. */
  94. void
  95. ossl_curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
  96. const curve448_scalar_t s);
  97. /*
  98. * Add two scalars. |a|, |b| and |out| may alias each other.
  99. *
  100. * a (in): One scalar.
  101. * b (in): Another scalar.
  102. * out (out): a+b.
  103. */
  104. void
  105. ossl_curve448_scalar_add(curve448_scalar_t out,
  106. const curve448_scalar_t a, const curve448_scalar_t b);
  107. /*
  108. * Subtract two scalars. |a|, |b| and |out| may alias each other.
  109. * a (in): One scalar.
  110. * b (in): Another scalar.
  111. * out (out): a-b.
  112. */
  113. void
  114. ossl_curve448_scalar_sub(curve448_scalar_t out,
  115. const curve448_scalar_t a, const curve448_scalar_t b);
  116. /*
  117. * Multiply two scalars. |a|, |b| and |out| may alias each other.
  118. *
  119. * a (in): One scalar.
  120. * b (in): Another scalar.
  121. * out (out): a*b.
  122. */
  123. void
  124. ossl_curve448_scalar_mul(curve448_scalar_t out,
  125. const curve448_scalar_t a, const curve448_scalar_t b);
  126. /*
  127. * Halve a scalar. |a| and |out| may alias each other.
  128. *
  129. * a (in): A scalar.
  130. * out (out): a/2.
  131. */
  132. void
  133. ossl_curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
  134. /*
  135. * Copy a scalar. The scalars may alias each other, in which case this
  136. * function does nothing.
  137. *
  138. * a (in): A scalar.
  139. * out (out): Will become a copy of a.
  140. */
  141. static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
  142. const curve448_scalar_t a)
  143. {
  144. *out = *a;
  145. }
  146. /*
  147. * Copy a point. The input and output may alias, in which case this function
  148. * does nothing.
  149. *
  150. * a (out): A copy of the point.
  151. * b (in): Any point.
  152. */
  153. static ossl_inline void curve448_point_copy(curve448_point_t a,
  154. const curve448_point_t b)
  155. {
  156. *a = *b;
  157. }
  158. /*
  159. * Test whether two points are equal. If yes, return C448_TRUE, else return
  160. * C448_FALSE.
  161. *
  162. * a (in): A point.
  163. * b (in): Another point.
  164. *
  165. * Returns:
  166. * C448_TRUE: The points are equal.
  167. * C448_FALSE: The points are not equal.
  168. */
  169. __owur c448_bool_t
  170. ossl_curve448_point_eq(const curve448_point_t a,
  171. const curve448_point_t b);
  172. /*
  173. * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
  174. * faster.
  175. *
  176. * two_a (out): The sum a+a.
  177. * a (in): A point.
  178. */
  179. void
  180. ossl_curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
  181. /*
  182. * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
  183. * (non-Decaf) encoding.
  184. *
  185. * out (out): The scaled point base*scalar
  186. * base (in): The point to be scaled.
  187. * scalar (in): The scalar to multiply by.
  188. *
  189. * Returns:
  190. * C448_SUCCESS: The scalarmul succeeded.
  191. * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
  192. * small subgroup.
  193. */
  194. __owur c448_error_t
  195. ossl_x448_int(uint8_t out[X448_PUBLIC_BYTES],
  196. const uint8_t base[X448_PUBLIC_BYTES],
  197. const uint8_t scalar[X448_PRIVATE_BYTES]);
  198. /*
  199. * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
  200. *
  201. * This function is mainly used internally, but is exported in case
  202. * it will be useful.
  203. *
  204. * The ratio is necessary because the internal representation doesn't
  205. * track the cofactor information, so on output we must clear the cofactor.
  206. * This would multiply by the cofactor, but in fact internally points are always
  207. * even, so it multiplies by half the cofactor instead.
  208. *
  209. * As it happens, this aligns with the base point definitions; that is,
  210. * if you pass the Decaf/Ristretto base point to this function, the result
  211. * will be X448_ENCODE_RATIO times the X448
  212. * base point.
  213. *
  214. * out (out): The scaled and encoded point.
  215. * p (in): The point to be scaled and encoded.
  216. */
  217. void
  218. ossl_curve448_point_mul_by_ratio_and_encode_like_x448(
  219. uint8_t out[X448_PUBLIC_BYTES],
  220. const curve448_point_t p);
  221. /*
  222. * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
  223. * (non-Decaf) encoding.
  224. *
  225. * out (out): The scaled point base*scalar
  226. * scalar (in): The scalar to multiply by.
  227. */
  228. void
  229. ossl_x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
  230. const uint8_t scalar[X448_PRIVATE_BYTES]);
  231. /*
  232. * Multiply a precomputed base point by a scalar: out = scalar*base.
  233. *
  234. * scaled (out): The scaled point base*scalar
  235. * base (in): The point to be scaled.
  236. * scalar (in): The scalar to multiply by.
  237. */
  238. void
  239. ossl_curve448_precomputed_scalarmul(curve448_point_t scaled,
  240. const curve448_precomputed_s * base,
  241. const curve448_scalar_t scalar);
  242. /*
  243. * Multiply two base points by two scalars:
  244. * combo = scalar1*curve448_point_base + scalar2*base2.
  245. *
  246. * Otherwise equivalent to curve448_point_double_scalarmul, but may be
  247. * faster at the expense of being variable time.
  248. *
  249. * combo (out): The linear combination scalar1*base + scalar2*base2.
  250. * scalar1 (in): A first scalar to multiply by.
  251. * base2 (in): A second point to be scaled.
  252. * scalar2 (in) A second scalar to multiply by.
  253. *
  254. * Warning: This function takes variable time, and may leak the scalars used.
  255. * It is designed for signature verification.
  256. */
  257. void
  258. ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
  259. const curve448_scalar_t scalar1,
  260. const curve448_point_t base2,
  261. const curve448_scalar_t scalar2);
  262. /*
  263. * Test that a point is valid, for debugging purposes.
  264. *
  265. * to_test (in): The point to test.
  266. *
  267. * Returns:
  268. * C448_TRUE The point is valid.
  269. * C448_FALSE The point is invalid.
  270. */
  271. __owur c448_bool_t
  272. ossl_curve448_point_valid(const curve448_point_t to_test);
  273. /* Overwrite scalar with zeros. */
  274. void ossl_curve448_scalar_destroy(curve448_scalar_t scalar);
  275. /* Overwrite point with zeros. */
  276. void ossl_curve448_point_destroy(curve448_point_t point);
  277. #endif /* OSSL_CRYPTO_EC_CURVE448_POINT_448_H */