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.
 
 
 
 
 

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