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 API_VIS, alignof_decaf_448_precomputed_s API_VIS;
  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. /* PERF: make this memcpy */
  198. void decaf_448_scalar_copy (
  199. decaf_448_scalar_t out,
  200. const decaf_448_scalar_t a
  201. ) API_VIS NONNULL2;
  202. /**
  203. * @brief Set a scalar to an integer.
  204. * @param [in] a An integer.
  205. * @param [out] out Will become equal to a.
  206. */
  207. void decaf_448_scalar_set (
  208. decaf_448_scalar_t out,
  209. decaf_word_t w
  210. ) API_VIS NONNULL1;
  211. /**
  212. * @brief Encode a point as a sequence of bytes.
  213. *
  214. * @param [out] ser The byte representation of the point.
  215. * @param [in] pt The point to encode.
  216. */
  217. void decaf_448_point_encode (
  218. uint8_t ser[DECAF_448_SER_BYTES],
  219. const decaf_448_point_t pt
  220. ) API_VIS NONNULL2 NOINLINE;
  221. /**
  222. * @brief Decode a point from a sequence of bytes.
  223. *
  224. * Every point has a unique encoding, so not every
  225. * sequence of bytes is a valid encoding. If an invalid
  226. * encoding is given, the output is undefined.
  227. *
  228. * @param [out] pt The decoded point.
  229. * @param [in] ser The serialized version of the point.
  230. * @retval DECAF_SUCCESS The decoding succeeded.
  231. * @retval DECAF_FAILURE The decoding didn't succeed, because
  232. * ser does not represent a point.
  233. */
  234. decaf_bool_t decaf_448_point_decode (
  235. decaf_448_point_t pt,
  236. const uint8_t ser[DECAF_448_SER_BYTES],
  237. decaf_bool_t allow_identity
  238. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  239. /**
  240. * @brief Copy a point. The input and output may alias,
  241. * in which case this function does nothing.
  242. *
  243. * @param [out] a A copy of the point.
  244. * @param [in] b Any point.
  245. */
  246. void decaf_448_point_copy (
  247. decaf_448_point_t a,
  248. const decaf_448_point_t b
  249. ) API_VIS NONNULL2;
  250. /**
  251. * @brief Test whether two points are equal. If yes, return
  252. * DECAF_TRUE, else return DECAF_FALSE.
  253. *
  254. * @param [in] a A point.
  255. * @param [in] b Another point.
  256. * @retval DECAF_TRUE The points are equal.
  257. * @retval DECAF_FALSE The points are not equal.
  258. */
  259. decaf_bool_t decaf_448_point_eq (
  260. const decaf_448_point_t a,
  261. const decaf_448_point_t b
  262. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  263. /**
  264. * @brief Add two points to produce a third point. The
  265. * input points and output point can be pointers to the same
  266. * memory.
  267. *
  268. * @param [out] sum The sum a+b.
  269. * @param [in] a An addend.
  270. * @param [in] b An addend.
  271. */
  272. void decaf_448_point_add (
  273. decaf_448_point_t sum,
  274. const decaf_448_point_t a,
  275. const decaf_448_point_t b
  276. ) API_VIS NONNULL3; // TODO: NOINLINE?
  277. /**
  278. * @brief Double a point. Equivalent to
  279. * decaf_448_point_add(two_a,a,a), but potentially faster.
  280. *
  281. * @param [out] sum The sum a+a.
  282. * @param [in] a A point.
  283. */
  284. void decaf_448_point_double (
  285. decaf_448_point_t two_a,
  286. const decaf_448_point_t a
  287. ) API_VIS NONNULL2; // TODO: NOINLINE?
  288. /**
  289. * @brief Subtract two points to produce a third point. The
  290. * input points and output point can be pointers to the same
  291. * memory.
  292. *
  293. * @param [out] diff The difference a-b.
  294. * @param [in] a The minuend.
  295. * @param [in] b The subtrahend.
  296. */
  297. void decaf_448_point_sub (
  298. decaf_448_point_t diff,
  299. const decaf_448_point_t a,
  300. const decaf_448_point_t b
  301. ) API_VIS NONNULL3;
  302. /**
  303. * @brief Negate a point to produce another point. The input
  304. * and output points can use the same memory.
  305. *
  306. * @param [out] nega The negated input point
  307. * @param [in] a The input point.
  308. */
  309. void decaf_448_point_negate (
  310. decaf_448_point_t nega,
  311. const decaf_448_point_t a
  312. ) API_VIS NONNULL2;
  313. /**
  314. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  315. *
  316. * @param [out] scaled The scaled point base*scalar
  317. * @param [in] base The point to be scaled.
  318. * @param [in] scalar The scalar to multiply by.
  319. */
  320. void decaf_448_point_scalarmul (
  321. decaf_448_point_t scaled,
  322. const decaf_448_point_t base,
  323. const decaf_448_scalar_t scalar
  324. ) API_VIS NONNULL3 NOINLINE;
  325. /**
  326. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  327. * This function operates directly on serialized forms.
  328. *
  329. * @warning This function is experimental. It may not be supported
  330. * long-term.
  331. *
  332. * @param [out] scaled The scaled point base*scalar
  333. * @param [in] base The point to be scaled.
  334. * @param [in] scalar The scalar to multiply by.
  335. * @param [in] allow_identity Allow the input to be the identity.
  336. * @param [in] short_circuit Allow a fast return if the input is illegal.
  337. *
  338. * @retval DECAF_SUCCESS The scalarmul succeeded.
  339. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  340. * base does not represent a point.
  341. */
  342. decaf_bool_t decaf_448_direct_scalarmul (
  343. uint8_t scaled[DECAF_448_SER_BYTES],
  344. const uint8_t base[DECAF_448_SER_BYTES],
  345. const decaf_448_scalar_t scalar,
  346. decaf_bool_t allow_identity,
  347. decaf_bool_t short_circuit
  348. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  349. /**
  350. * @brief Precompute a table for fast scalar multiplication.
  351. * Some implementations do not include precomputed points; for
  352. * those implementations, this implementation simply copies the
  353. * point.
  354. *
  355. * @param [out] a A precomputed table of multiples of the point.
  356. * @param [in] b Any point.
  357. */
  358. void decaf_448_precompute (
  359. decaf_448_precomputed_s *a,
  360. const decaf_448_point_t b
  361. ) API_VIS NONNULL2 NOINLINE;
  362. /**
  363. * @brief Multiply a precomputed base point by a scalar:
  364. * scaled = scalar*base.
  365. * Some implementations do not include precomputed points; for
  366. * those implementations, this function is the same as
  367. * decaf_448_point_scalarmul
  368. *
  369. * @param [out] scaled The scaled point base*scalar
  370. * @param [in] base The point to be scaled.
  371. * @param [in] scalar The scalar to multiply by.
  372. *
  373. * @TODO: precomputed dsmul? const or variable time?
  374. */
  375. void decaf_448_precomputed_scalarmul (
  376. decaf_448_point_t scaled,
  377. const decaf_448_precomputed_s *base,
  378. const decaf_448_scalar_t scalar
  379. ) API_VIS NONNULL3 NOINLINE;
  380. /**
  381. * @brief Multiply two base points by two scalars:
  382. * scaled = scalar1*base1 + scalar2*base2.
  383. *
  384. * Equivalent to two calls to decaf_448_point_scalarmul, but may be
  385. * faster.
  386. *
  387. * @param [out] scaled The scaled point base*scalar
  388. * @param [in] base1 A first point to be scaled.
  389. * @param [in] scalar1 A first scalar to multiply by.
  390. * @param [in] base2 A second point to be scaled.
  391. * @param [in] scalar2 A second scalar to multiply by.
  392. * @fixme This function isn't tested!
  393. */
  394. void decaf_448_point_double_scalarmul (
  395. decaf_448_point_t combo,
  396. const decaf_448_point_t base1,
  397. const decaf_448_scalar_t scalar1,
  398. const decaf_448_point_t base2,
  399. const decaf_448_scalar_t scalar2
  400. ) API_VIS NONNULL5 NOINLINE;
  401. /**
  402. * @brief Multiply two base points by two scalars:
  403. * scaled = scalar1*decaf_448_point_base + scalar2*base2.
  404. *
  405. * Otherwise equivalent to decaf_448_point_double_scalarmul, but may be
  406. * faster.
  407. *
  408. * @param [out] scaled The scaled point base*scalar
  409. * @param [in] scalar1 A first scalar to multiply by.
  410. * @param [in] base2 A second point to be scaled.
  411. * @param [in] scalar2 A second scalar to multiply by.
  412. *
  413. * @warning: This function takes variable time, and may leak the scalars
  414. * used. It is designed for signature verification.
  415. */
  416. void decaf_448_base_double_scalarmul_non_secret (
  417. decaf_448_point_t combo,
  418. const decaf_448_scalar_t scalar1,
  419. const decaf_448_point_t base2,
  420. const decaf_448_scalar_t scalar2
  421. ) API_VIS NONNULL4 NOINLINE;
  422. /**
  423. * @brief Test that a point is valid, for debugging purposes.
  424. *
  425. * @param [in] point The number to test.
  426. * @retval DECAF_TRUE The point is valid.
  427. * @retval DECAF_FALSE The point is invalid.
  428. */
  429. decaf_bool_t decaf_448_point_valid (
  430. const decaf_448_point_t toTest
  431. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  432. /**
  433. * @brief Almost-Elligator-like hash to curve.
  434. *
  435. * Call this function with the output of a hash to make a hash to the curve.
  436. *
  437. * This function runs Elligator2 on the decaf_448 Jacobi quartic model. It then
  438. * uses the isogeny to put the result in twisted Edwards form. As a result,
  439. * it is safe (cannot produce points of order 4), and would be compatible with
  440. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  441. * Edwards model.
  442. *
  443. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  444. * A factor of 2 due to the isogeny.
  445. * A factor of 2 because we quotient out the 2-torsion.
  446. *
  447. * Negating the input (mod q) results in the same point. Inverting the input
  448. * (mod q) results in the negative point. This is the same as Elligator.
  449. *
  450. * This function isn't quite indifferentiable from a random oracle.
  451. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  452. * Furthermore, calling it twice with independent seeds and adding the results
  453. * is indifferentiable from a random oracle.
  454. *
  455. * @param [in] hashed_data Output of some hash function.
  456. * @param [out] pt The data hashed to the curve.
  457. */
  458. void decaf_448_point_from_hash_nonuniform (
  459. decaf_448_point_t pt,
  460. const unsigned char hashed_data[DECAF_448_SER_BYTES]
  461. ) API_VIS NONNULL2 NOINLINE;
  462. /**
  463. * @brief Indifferentiable hash function encoding to curve.
  464. *
  465. * Equivalent to calling decaf_448_point_from_hash_nonuniform twice and adding.
  466. *
  467. * @param [in] hashed_data Output of some hash function.
  468. * @param [out] pt The data hashed to the curve.
  469. */
  470. void decaf_448_point_from_hash_uniform (
  471. decaf_448_point_t pt,
  472. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  473. ) API_VIS NONNULL2 NOINLINE;
  474. /**
  475. * @brief Overwrite data with zeros. Uses memset_s if available.
  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__ */