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.
 
 
 
 
 

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