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.
 
 
 
 
 

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