You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

569 lines
17 KiB

  1. /**
  2. * @file decaf.h
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief A group of prime order p.
  10. *
  11. * The Decaf library implements cryptographic operations on a an elliptic curve
  12. * group of prime order p. It accomplishes this by using a twisted Edwards
  13. * curve (isogenous to Ed448-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_448_decode can fail because not every sequence of bytes is a valid group
  17. * element.
  18. *
  19. * The formulas contain no data-dependent branches, timing or memory accesses,
  20. * except for decaf_448_base_double_scalarmul_non_secret.
  21. *
  22. * This library may support multiple curves eventually. The Ed448-Goldilocks
  23. * specific identifiers are prefixed with DECAF_448 or decaf_448.
  24. */
  25. #ifndef __DECAF_448_H__
  26. #define __DECAF_448_H__ 1
  27. #include <stdint.h>
  28. #include <sys/types.h>
  29. /* Goldilocks' build flags default to hidden and stripping executables. */
  30. /** @cond internal */
  31. #if defined(DOXYGEN) && !defined(__attribute__)
  32. #define __attribute__((x))
  33. #endif
  34. #define API_VIS __attribute__((visibility("default")))
  35. #define NOINLINE __attribute__((noinline))
  36. #define WARN_UNUSED __attribute__((warn_unused_result))
  37. #define NONNULL1 __attribute__((nonnull(1)))
  38. #define NONNULL2 __attribute__((nonnull(1,2)))
  39. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  40. #define NONNULL4 __attribute__((nonnull(1,2,3,4)))
  41. #define NONNULL5 __attribute__((nonnull(1,2,3,4,5)))
  42. /* Internal word types */
  43. #if (defined(__ILP64__) || defined(__amd64__) || defined(__x86_64__) || (((__UINT_FAST32_MAX__)>>30)>>30)) \
  44. && !defined(DECAF_FORCE_32_BIT)
  45. #define DECAF_WORD_BITS 64
  46. typedef uint64_t decaf_word_t, decaf_bool_t;
  47. #else
  48. #define DECAF_WORD_BITS 32
  49. typedef uint32_t decaf_word_t, decaf_bool_t;
  50. #endif
  51. /** @endcond */
  52. #define DECAF_448_LIMBS (512/DECAF_WORD_BITS)
  53. #define DECAF_448_SCALAR_BITS 446
  54. #define DECAF_448_SCALAR_LIMBS (448/DECAF_WORD_BITS)
  55. /** Number of bytes in a serialized point. */
  56. #define DECAF_448_SER_BYTES 56
  57. /** Number of bytes in a serialized scalar. */
  58. #define DECAF_448_SCALAR_BYTES 56
  59. /** Galois field element internal structure */
  60. typedef struct gf_s {
  61. decaf_word_t limb[DECAF_448_LIMBS];
  62. } __attribute__((aligned(32))) gf_s, gf[1];
  63. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  64. typedef struct decaf_448_point_s { gf x,y,z,t; } decaf_448_point_t[1];
  65. /** Precomputed table based on a point. Can be trivial implementation. */
  66. struct decaf_448_precomputed_s;
  67. typedef struct decaf_448_precomputed_s decaf_448_precomputed_s;
  68. /** Size and alignment of precomputed point tables. */
  69. extern const size_t sizeof_decaf_448_precomputed_s, alignof_decaf_448_precomputed_s;
  70. /** Scalar is stored packed, because we don't need the speed. */
  71. typedef struct decaf_448_scalar_s {
  72. decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
  73. } decaf_448_scalar_t[1];
  74. /** DECAF_TRUE = -1 so that DECAF_TRUE & x = x */
  75. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  76. /** NB Success is -1, failure is 0. TODO: see if people would rather the reverse. */
  77. static const decaf_bool_t DECAF_SUCCESS = -(decaf_bool_t)1 /*DECAF_TRUE*/,
  78. DECAF_FAILURE = 0 /*DECAF_FALSE*/;
  79. /** The prime p, for debugging purposes.
  80. * TODO: prevent this scalar from actually being used for non-debugging purposes?
  81. */
  82. extern const decaf_448_scalar_t decaf_448_scalar_p API_VIS;
  83. /** A scalar equal to 1. */
  84. extern const decaf_448_scalar_t decaf_448_scalar_one API_VIS;
  85. /** A scalar equal to 0. */
  86. extern const decaf_448_scalar_t decaf_448_scalar_zero API_VIS;
  87. /** The identity point on the curve. */
  88. extern const decaf_448_point_t decaf_448_point_identity API_VIS;
  89. /**
  90. * An arbitrarily chosen base point on the curve.
  91. * Equal to Ed448-Goldilocks base point defined by DJB, except of course that
  92. * it's on the twist in this case. TODO: choose a base point with nice encoding?
  93. */
  94. extern const decaf_448_point_t decaf_448_point_base API_VIS;
  95. /** Precomputed table for the base point on the curve. */
  96. extern const struct decaf_448_precomputed_s *decaf_448_precomputed_base API_VIS;
  97. #ifdef __cplusplus
  98. extern "C" {
  99. #endif
  100. /**
  101. * @brief Read a scalar from wire format or from bytes.
  102. *
  103. * @param [in] ser Serialized form of a scalar.
  104. * @param [out] out Deserialized form.
  105. *
  106. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  107. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  108. * and has been reduced modulo that modulus.
  109. */
  110. decaf_bool_t decaf_448_scalar_decode (
  111. decaf_448_scalar_t s,
  112. const unsigned char ser[DECAF_448_SCALAR_BYTES]
  113. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  114. /**
  115. * @brief Read a scalar from wire format or from bytes. Reduces mod
  116. * scalar prime.
  117. *
  118. * @param [in] ser Serialized form of a scalar.
  119. * @param [in] ser_len Length of serialized form.
  120. * @param [out] out Deserialized form.
  121. */
  122. void decaf_448_scalar_decode_long (
  123. decaf_448_scalar_t s,
  124. const unsigned char *ser,
  125. size_t ser_len
  126. ) API_VIS NONNULL2 NOINLINE;
  127. /**
  128. * @brief Serialize a scalar to wire format.
  129. *
  130. * @param [out] ser Serialized form of a scalar.
  131. * @param [in] s Deserialized scalar.
  132. */
  133. void decaf_448_scalar_encode (
  134. unsigned char ser[DECAF_448_SCALAR_BYTES],
  135. const decaf_448_scalar_t s
  136. ) API_VIS NONNULL2 NOINLINE NOINLINE;
  137. /**
  138. * @brief Add two scalars. The scalars may use the same memory.
  139. * @param [in] a One scalar.
  140. * @param [in] b Another scalar.
  141. * @param [out] out a+b.
  142. */
  143. void decaf_448_scalar_add (
  144. decaf_448_scalar_t out,
  145. const decaf_448_scalar_t a,
  146. const decaf_448_scalar_t b
  147. ) API_VIS NONNULL3 NOINLINE;
  148. /**
  149. * @brief Compare two scalars.
  150. * @param [in] a One scalar.
  151. * @param [in] b Another scalar.
  152. * @retval DECAF_TRUE The scalars are equal.
  153. * @retval DECAF_FALSE The scalars are not equal.
  154. */
  155. decaf_bool_t decaf_448_scalar_eq (
  156. const decaf_448_scalar_t a,
  157. const decaf_448_scalar_t b
  158. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  159. /**
  160. * @brief Subtract two scalars. The scalars may use the same memory.
  161. * @param [in] a One scalar.
  162. * @param [in] b Another scalar.
  163. * @param [out] out a-b.
  164. */
  165. void decaf_448_scalar_sub (
  166. decaf_448_scalar_t out,
  167. const decaf_448_scalar_t a,
  168. const decaf_448_scalar_t b
  169. ) API_VIS NONNULL3 NOINLINE;
  170. /**
  171. * @brief Multiply two scalars. The scalars may use the same memory.
  172. * @param [in] a One scalar.
  173. * @param [in] b Another scalar.
  174. * @param [out] out a*b.
  175. */
  176. void decaf_448_scalar_mul (
  177. decaf_448_scalar_t out,
  178. const decaf_448_scalar_t a,
  179. const decaf_448_scalar_t b
  180. ) API_VIS NONNULL3 NOINLINE;
  181. /**
  182. * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
  183. * @param [in] a A scalar.
  184. * @param [out] out 1/a.
  185. * @return DECAF_TRUE The input is nonzero.
  186. */
  187. decaf_bool_t decaf_448_scalar_invert (
  188. decaf_448_scalar_t out,
  189. const decaf_448_scalar_t a
  190. ) API_VIS NONNULL2 NOINLINE;
  191. /**
  192. * @brief Copy a scalar. The scalars may use the same memory, in which
  193. * case this function does nothing.
  194. * @param [in] a A scalar.
  195. * @param [out] out Will become a copy of a.
  196. */
  197. void decaf_448_scalar_copy (
  198. decaf_448_scalar_t out,
  199. const decaf_448_scalar_t a
  200. ) API_VIS NONNULL2;
  201. /**
  202. * @brief Set a scalar to an integer.
  203. * @param [in] a An integer.
  204. * @param [out] out Will become equal to a.
  205. */
  206. void decaf_448_scalar_set (
  207. decaf_448_scalar_t out,
  208. decaf_word_t w
  209. ) API_VIS NONNULL1;
  210. /**
  211. * @brief Encode a point as a sequence of bytes.
  212. *
  213. * @param [out] ser The byte representation of the point.
  214. * @param [in] pt The point to encode.
  215. */
  216. void decaf_448_point_encode (
  217. uint8_t ser[DECAF_448_SER_BYTES],
  218. const decaf_448_point_t pt
  219. ) API_VIS NONNULL2 NOINLINE;
  220. /**
  221. * @brief Decode a point from a sequence of bytes.
  222. *
  223. * Every point has a unique encoding, so not every
  224. * sequence of bytes is a valid encoding. If an invalid
  225. * encoding is given, the output is undefined.
  226. *
  227. * @param [out] pt The decoded point.
  228. * @param [in] ser The serialized version of the point.
  229. * @retval DECAF_SUCCESS The decoding succeeded.
  230. * @retval DECAF_FAILURE The decoding didn't succeed, because
  231. * ser does not represent a point.
  232. */
  233. decaf_bool_t decaf_448_point_decode (
  234. decaf_448_point_t pt,
  235. const uint8_t ser[DECAF_448_SER_BYTES],
  236. decaf_bool_t allow_identity
  237. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  238. /**
  239. * @brief Copy a point. The input and output may alias,
  240. * in which case this function does nothing.
  241. *
  242. * @param [out] a A copy of the point.
  243. * @param [in] b Any point.
  244. */
  245. void decaf_448_point_copy (
  246. decaf_448_point_t a,
  247. const decaf_448_point_t b
  248. ) API_VIS NONNULL2;
  249. /**
  250. * @brief Test whether two points are equal. If yes, return
  251. * DECAF_TRUE, else return DECAF_FALSE.
  252. *
  253. * @param [in] a A point.
  254. * @param [in] b Another point.
  255. * @retval DECAF_TRUE The points are equal.
  256. * @retval DECAF_FALSE The points are not equal.
  257. */
  258. decaf_bool_t decaf_448_point_eq (
  259. const decaf_448_point_t a,
  260. const decaf_448_point_t b
  261. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  262. /**
  263. * @brief Add two points to produce a third point. The
  264. * input points and output point can be pointers to the same
  265. * memory.
  266. *
  267. * @param [out] sum The sum a+b.
  268. * @param [in] a An addend.
  269. * @param [in] b An addend.
  270. */
  271. void decaf_448_point_add (
  272. decaf_448_point_t sum,
  273. const decaf_448_point_t a,
  274. const decaf_448_point_t b
  275. ) API_VIS NONNULL3; // TODO: NOINLINE?
  276. /**
  277. * @brief Double a point. Equivalent to
  278. * decaf_448_point_add(two_a,a,a), but potentially faster.
  279. *
  280. * @param [out] sum The sum a+a.
  281. * @param [in] a A point.
  282. */
  283. void decaf_448_point_double (
  284. decaf_448_point_t two_a,
  285. const decaf_448_point_t a
  286. ) API_VIS NONNULL2; // TODO: NOINLINE?
  287. /**
  288. * @brief Subtract two points to produce a third point. The
  289. * input points and output point can be pointers to the same
  290. * memory.
  291. *
  292. * @param [out] diff The difference a-b.
  293. * @param [in] a The minuend.
  294. * @param [in] b The subtrahend.
  295. */
  296. void decaf_448_point_sub (
  297. decaf_448_point_t diff,
  298. const decaf_448_point_t a,
  299. const decaf_448_point_t b
  300. ) API_VIS NONNULL3;
  301. /**
  302. * @brief Negate a point to produce another point. The input
  303. * and output points can use the same memory.
  304. *
  305. * @param [out] nega The negated input point
  306. * @param [in] a The input point.
  307. */
  308. void decaf_448_point_negate (
  309. decaf_448_point_t nega,
  310. const decaf_448_point_t a
  311. ) API_VIS NONNULL2;
  312. /**
  313. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  314. *
  315. * @param [out] scaled The scaled point base*scalar
  316. * @param [in] base The point to be scaled.
  317. * @param [in] scalar The scalar to multiply by.
  318. */
  319. void decaf_448_point_scalarmul (
  320. decaf_448_point_t scaled,
  321. const decaf_448_point_t base,
  322. const decaf_448_scalar_t scalar
  323. ) API_VIS NONNULL3 NOINLINE;
  324. /**
  325. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  326. * This function operates directly on serialized forms.
  327. *
  328. * @warning This function is experimental. It may not be supported
  329. * long-term.
  330. *
  331. * @param [out] scaled The scaled point base*scalar
  332. * @param [in] base The point to be scaled.
  333. * @param [in] scalar The scalar to multiply by.
  334. * @param [in] allow_identity Allow the input to be the identity.
  335. * @param [in] short_circuit Allow a fast return if the input is illegal.
  336. *
  337. * @retval DECAF_SUCCESS The scalarmul succeeded.
  338. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  339. * base does not represent a point.
  340. */
  341. decaf_bool_t decaf_448_direct_scalarmul (
  342. uint8_t scaled[DECAF_448_SER_BYTES],
  343. const uint8_t base[DECAF_448_SER_BYTES],
  344. const decaf_448_scalar_t scalar,
  345. decaf_bool_t allow_identity,
  346. decaf_bool_t short_circuit
  347. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  348. /**
  349. * @brief Precompute a table for fast scalar multiplication.
  350. * Some implementations do not include precomputed points; for
  351. * those implementations, this implementation simply copies the
  352. * point.
  353. *
  354. * @param [out] a A precomputed table of multiples of the point.
  355. * @param [in] b Any point.
  356. */
  357. void decaf_448_precompute (
  358. decaf_448_precomputed_s *a,
  359. const decaf_448_point_t b
  360. ) API_VIS NONNULL2 NOINLINE;
  361. /**
  362. * @brief Multiply a precomputed base point by a scalar:
  363. * scaled = scalar*base.
  364. * Some implementations do not include precomputed points; for
  365. * those implementations, this function is the same as
  366. * decaf_448_point_scalarmul
  367. *
  368. * @param [out] scaled The scaled point base*scalar
  369. * @param [in] base The point to be scaled.
  370. * @param [in] scalar The scalar to multiply by.
  371. *
  372. * @TODO: precomputed dsmul? const or variable time?
  373. */
  374. void decaf_448_precomputed_scalarmul (
  375. decaf_448_point_t scaled,
  376. const decaf_448_precomputed_s *base,
  377. const decaf_448_scalar_t scalar
  378. ) API_VIS NONNULL3 NOINLINE;
  379. /**
  380. * @brief Multiply two base points by two scalars:
  381. * scaled = scalar1*base1 + scalar2*base2.
  382. *
  383. * Equivalent to two calls to decaf_448_point_scalarmul, but may be
  384. * faster.
  385. *
  386. * @param [out] scaled The scaled point base*scalar
  387. * @param [in] base1 A first point to be scaled.
  388. * @param [in] scalar1 A first scalar to multiply by.
  389. * @param [in] base2 A second point to be scaled.
  390. * @param [in] scalar2 A second scalar to multiply by.
  391. * @fixme This function isn't tested!
  392. */
  393. void decaf_448_point_double_scalarmul (
  394. decaf_448_point_t combo,
  395. const decaf_448_point_t base1,
  396. const decaf_448_scalar_t scalar1,
  397. const decaf_448_point_t base2,
  398. const decaf_448_scalar_t scalar2
  399. ) API_VIS NONNULL5 NOINLINE;
  400. /**
  401. * @brief Multiply two base points by two scalars:
  402. * scaled = scalar1*decaf_448_point_base + scalar2*base2.
  403. *
  404. * Otherwise equivalent to decaf_448_point_double_scalarmul, but may be
  405. * faster.
  406. *
  407. * @param [out] scaled The scaled point base*scalar
  408. * @param [in] scalar1 A first scalar to multiply by.
  409. * @param [in] base2 A second point to be scaled.
  410. * @param [in] scalar2 A second scalar to multiply by.
  411. *
  412. * @warning: This function takes variable time, and may leak the scalars
  413. * used. It is designed for signature verification.
  414. */
  415. void decaf_448_base_double_scalarmul_non_secret (
  416. decaf_448_point_t combo,
  417. const decaf_448_scalar_t scalar1,
  418. const decaf_448_point_t base2,
  419. const decaf_448_scalar_t scalar2
  420. ) API_VIS NONNULL4 NOINLINE;
  421. /**
  422. * @brief Test that a point is valid, for debugging purposes.
  423. *
  424. * @param [in] point The number to test.
  425. * @retval DECAF_TRUE The point is valid.
  426. * @retval DECAF_FALSE The point is invalid.
  427. */
  428. decaf_bool_t decaf_448_point_valid (
  429. const decaf_448_point_t toTest
  430. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  431. /**
  432. * @brief Almost-Elligator-like hash to curve.
  433. *
  434. * Call this function with the output of a hash to make a hash to the curve.
  435. *
  436. * This function runs Elligator2 on the decaf_448 Jacobi quartic model. It then
  437. * uses the isogeny to put the result in twisted Edwards form. As a result,
  438. * it is safe (cannot produce points of order 4), and would be compatible with
  439. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  440. * Edwards model.
  441. *
  442. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  443. * A factor of 2 due to the isogeny.
  444. * A factor of 2 because we quotient out the 2-torsion.
  445. *
  446. * Negating the input (mod q) results in the same point. Inverting the input
  447. * (mod q) results in the negative point. This is the same as Elligator.
  448. *
  449. * This function isn't quite indifferentiable from a random oracle.
  450. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  451. * Furthermore, calling it twice with independent seeds and adding the results
  452. * is indifferentiable from a random oracle.
  453. *
  454. * @param [in] hashed_data Output of some hash function.
  455. * @param [out] pt The data hashed to the curve.
  456. */
  457. void decaf_448_point_from_hash_nonuniform (
  458. decaf_448_point_t pt,
  459. const unsigned char hashed_data[DECAF_448_SER_BYTES]
  460. ) API_VIS NONNULL2 NOINLINE;
  461. /**
  462. * @brief Indifferentiable hash function encoding to curve.
  463. *
  464. * Equivalent to calling decaf_448_point_from_hash_nonuniform twice and adding.
  465. *
  466. * @param [in] hashed_data Output of some hash function.
  467. * @param [out] pt The data hashed to the curve.
  468. */
  469. void decaf_448_point_from_hash_uniform (
  470. decaf_448_point_t pt,
  471. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  472. ) API_VIS NONNULL2 NOINLINE;
  473. /**
  474. * @brief Overwrite data with zeros. Uses memset_s if available.
  475. * If data is NULL, this function has no effect.
  476. */
  477. void decaf_bzero (
  478. void *data,
  479. size_t size
  480. ) NONNULL1 API_VIS NOINLINE;
  481. /**
  482. * @brief Overwrite scalar with zeros.
  483. */
  484. void decaf_448_scalar_destroy (
  485. decaf_448_scalar_t scalar
  486. ) NONNULL1 API_VIS;
  487. /**
  488. * @brief Overwrite point with zeros.
  489. * @todo Use this internally.
  490. */
  491. void decaf_448_point_destroy (
  492. decaf_448_point_t point
  493. ) NONNULL1 API_VIS;
  494. /**
  495. * @brief Overwrite point with zeros.
  496. * @todo Use this internally.
  497. */
  498. void decaf_448_precomputed_destroy (
  499. decaf_448_precomputed_s *pre
  500. ) NONNULL1 API_VIS;
  501. /* TODO: functions to invert point_from_hash?? */
  502. #undef API_VIS
  503. #undef WARN_UNUSED
  504. #undef NOINLINE
  505. #undef NONNULL1
  506. #undef NONNULL2
  507. #undef NONNULL3
  508. #undef NONNULL4
  509. #undef NONNULL5
  510. #ifdef __cplusplus
  511. }; /* extern "C" */
  512. #endif
  513. #endif /* __DECAF_448_H__ */