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.
 
 
 
 
 

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