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.
 
 
 
 
 

608 lines
18 KiB

  1. /**
  2. * @file decaf_255.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 Ed255-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_255_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_255_base_double_scalarmul_non_secret.
  21. *
  22. * This library may support multiple curves eventually. The Ed255-Goldilocks
  23. * specific identifiers are prefixed with DECAF_255 or decaf_255.
  24. */
  25. #ifndef __DECAF_255_H__
  26. #define __DECAF_255_H__ 1
  27. #ifndef __DECAF_H__
  28. #error "include <decaf.h>, not <decaf_255.h>."
  29. #endif
  30. #define DECAF_255_LIMBS (320/DECAF_WORD_BITS)
  31. #define DECAF_255_SCALAR_BITS 254 // Curve25519: 253
  32. #define DECAF_255_SCALAR_LIMBS (256/DECAF_WORD_BITS)
  33. /** Galois field element internal structure */
  34. typedef struct gf_255_s {
  35. decaf_word_t limb[DECAF_255_LIMBS];
  36. } gf_255_s, gf_255_t[1];
  37. /** @endcond */
  38. /** Number of bytes in a serialized point. */
  39. #define DECAF_255_SER_BYTES 32
  40. /** Number of bytes in a serialized scalar. */
  41. #define DECAF_255_SCALAR_BYTES 32
  42. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  43. typedef struct decaf_255_point_s { /**@cond internal*/gf_255_t x,y,z,t;/**@endcond*/ } decaf_255_point_t[1];
  44. /** Precomputed table based on a point. Can be trivial implementation. */
  45. struct decaf_255_precomputed_s;
  46. /** Precomputed table based on a point. Can be trivial implementation. */
  47. typedef struct decaf_255_precomputed_s decaf_255_precomputed_s;
  48. /** Size and alignment of precomputed point tables. */
  49. extern const size_t sizeof_decaf_255_precomputed_s API_VIS, alignof_decaf_255_precomputed_s API_VIS;
  50. /** Scalar is stored packed, because we don't need the speed. */
  51. typedef struct decaf_255_scalar_s {
  52. /** @cond internal */
  53. decaf_word_t limb[DECAF_255_SCALAR_LIMBS];
  54. /** @endcond */
  55. } decaf_255_scalar_t[1];
  56. /** A scalar equal to 1. */
  57. extern const decaf_255_scalar_t decaf_255_scalar_one API_VIS;
  58. /** A scalar equal to 0. */
  59. extern const decaf_255_scalar_t decaf_255_scalar_zero API_VIS;
  60. /** The identity point on the curve. */
  61. extern const decaf_255_point_t decaf_255_point_identity API_VIS;
  62. /**
  63. * An arbitrarily chosen base point on the curve.
  64. * Equal to Ed255-Goldilocks base point defined by DJB, except of course that
  65. * it's on the twist in this case. TODO: choose a base point with nice encoding?
  66. */
  67. extern const decaf_255_point_t decaf_255_point_base API_VIS;
  68. /** Precomputed table for the base point on the curve. */
  69. extern const struct decaf_255_precomputed_s *decaf_255_precomputed_base API_VIS;
  70. /**
  71. * @brief Read a scalar from wire format or from bytes.
  72. *
  73. * @param [in] ser Serialized form of a scalar.
  74. * @param [out] out Deserialized form.
  75. *
  76. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  77. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  78. * and has been reduced modulo that modulus.
  79. */
  80. decaf_bool_t decaf_255_scalar_decode (
  81. decaf_255_scalar_t out,
  82. const unsigned char ser[DECAF_255_SCALAR_BYTES]
  83. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  84. /**
  85. * @brief Read a scalar from wire format or from bytes. Reduces mod
  86. * scalar prime.
  87. *
  88. * @param [in] ser Serialized form of a scalar.
  89. * @param [in] ser_len Length of serialized form.
  90. * @param [out] out Deserialized form.
  91. */
  92. void decaf_255_scalar_decode_long (
  93. decaf_255_scalar_t out,
  94. const unsigned char *ser,
  95. size_t ser_len
  96. ) API_VIS NONNULL2 NOINLINE;
  97. /**
  98. * @brief Serialize a scalar to wire format.
  99. *
  100. * @param [out] ser Serialized form of a scalar.
  101. * @param [in] s Deserialized scalar.
  102. */
  103. void decaf_255_scalar_encode (
  104. unsigned char ser[DECAF_255_SCALAR_BYTES],
  105. const decaf_255_scalar_t s
  106. ) API_VIS NONNULL2 NOINLINE NOINLINE;
  107. /**
  108. * @brief Add two scalars. The scalars may use the same memory.
  109. * @param [in] a One scalar.
  110. * @param [in] b Another scalar.
  111. * @param [out] out a+b.
  112. */
  113. void decaf_255_scalar_add (
  114. decaf_255_scalar_t out,
  115. const decaf_255_scalar_t a,
  116. const decaf_255_scalar_t b
  117. ) API_VIS NONNULL3 NOINLINE;
  118. /**
  119. * @brief Compare two scalars.
  120. * @param [in] a One scalar.
  121. * @param [in] b Another scalar.
  122. * @retval DECAF_TRUE The scalars are equal.
  123. * @retval DECAF_FALSE The scalars are not equal.
  124. */
  125. decaf_bool_t decaf_255_scalar_eq (
  126. const decaf_255_scalar_t a,
  127. const decaf_255_scalar_t b
  128. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  129. /**
  130. * @brief Subtract two scalars. The scalars may use the same memory.
  131. * @param [in] a One scalar.
  132. * @param [in] b Another scalar.
  133. * @param [out] out a-b.
  134. */
  135. void decaf_255_scalar_sub (
  136. decaf_255_scalar_t out,
  137. const decaf_255_scalar_t a,
  138. const decaf_255_scalar_t b
  139. ) API_VIS NONNULL3 NOINLINE;
  140. /**
  141. * @brief Multiply 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_255_scalar_mul (
  147. decaf_255_scalar_t out,
  148. const decaf_255_scalar_t a,
  149. const decaf_255_scalar_t b
  150. ) API_VIS NONNULL3 NOINLINE;
  151. /**
  152. * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
  153. * @param [in] a A scalar.
  154. * @param [out] out 1/a.
  155. * @return DECAF_TRUE The input is nonzero.
  156. */
  157. decaf_bool_t decaf_255_scalar_invert (
  158. decaf_255_scalar_t out,
  159. const decaf_255_scalar_t a
  160. ) API_VIS NONNULL2 NOINLINE;
  161. /**
  162. * @brief Copy a scalar. The scalars may use the same memory, in which
  163. * case this function does nothing.
  164. * @param [in] a A scalar.
  165. * @param [out] out Will become a copy of a.
  166. */
  167. static inline void NONNULL2 decaf_255_scalar_copy (
  168. decaf_255_scalar_t out,
  169. const decaf_255_scalar_t a
  170. ) {
  171. *out = *a;
  172. }
  173. /**
  174. * @brief Set a scalar to an integer.
  175. * @param [in] a An integer.
  176. * @param [out] out Will become equal to a.
  177. * @todo Make inline?
  178. */
  179. void decaf_255_scalar_set(
  180. decaf_255_scalar_t out,
  181. decaf_word_t a
  182. ) API_VIS NONNULL1;
  183. /**
  184. * @brief Encode a point as a sequence of bytes.
  185. *
  186. * @param [out] ser The byte representation of the point.
  187. * @param [in] pt The point to encode.
  188. */
  189. void decaf_255_point_encode (
  190. uint8_t ser[DECAF_255_SER_BYTES],
  191. const decaf_255_point_t pt
  192. ) API_VIS NONNULL2 NOINLINE;
  193. /**
  194. * @brief Decode a point from a sequence of bytes.
  195. *
  196. * Every point has a unique encoding, so not every
  197. * sequence of bytes is a valid encoding. If an invalid
  198. * encoding is given, the output is undefined.
  199. *
  200. * @param [out] pt The decoded point.
  201. * @param [in] ser The serialized version of the point.
  202. * @param [in] allow_identity DECAF_TRUE if the identity is a legal input.
  203. * @retval DECAF_SUCCESS The decoding succeeded.
  204. * @retval DECAF_FAILURE The decoding didn't succeed, because
  205. * ser does not represent a point.
  206. */
  207. decaf_bool_t decaf_255_point_decode (
  208. decaf_255_point_t pt,
  209. const uint8_t ser[DECAF_255_SER_BYTES],
  210. decaf_bool_t allow_identity
  211. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  212. /**
  213. * @brief Copy a point. The input and output may alias,
  214. * in which case this function does nothing.
  215. *
  216. * @param [out] a A copy of the point.
  217. * @param [in] b Any point.
  218. */
  219. static inline void NONNULL2 decaf_255_point_copy (
  220. decaf_255_point_t a,
  221. const decaf_255_point_t b
  222. ) {
  223. *a=*b;
  224. }
  225. /**
  226. * @brief Test whether two points are equal. If yes, return
  227. * DECAF_TRUE, else return DECAF_FALSE.
  228. *
  229. * @param [in] a A point.
  230. * @param [in] b Another point.
  231. * @retval DECAF_TRUE The points are equal.
  232. * @retval DECAF_FALSE The points are not equal.
  233. */
  234. decaf_bool_t decaf_255_point_eq (
  235. const decaf_255_point_t a,
  236. const decaf_255_point_t b
  237. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  238. /**
  239. * @brief Add two points to produce a third point. The
  240. * input points and output point can be pointers to the same
  241. * memory.
  242. *
  243. * @param [out] sum The sum a+b.
  244. * @param [in] a An addend.
  245. * @param [in] b An addend.
  246. */
  247. void decaf_255_point_add (
  248. decaf_255_point_t sum,
  249. const decaf_255_point_t a,
  250. const decaf_255_point_t b
  251. ) API_VIS NONNULL3;
  252. /**
  253. * @brief Double a point. Equivalent to
  254. * decaf_255_point_add(two_a,a,a), but potentially faster.
  255. *
  256. * @param [out] two_a The sum a+a.
  257. * @param [in] a A point.
  258. */
  259. void decaf_255_point_double (
  260. decaf_255_point_t two_a,
  261. const decaf_255_point_t a
  262. ) API_VIS NONNULL2;
  263. /**
  264. * @brief Subtract 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] diff The difference a-b.
  269. * @param [in] a The minuend.
  270. * @param [in] b The subtrahend.
  271. */
  272. void decaf_255_point_sub (
  273. decaf_255_point_t diff,
  274. const decaf_255_point_t a,
  275. const decaf_255_point_t b
  276. ) API_VIS NONNULL3;
  277. /**
  278. * @brief Negate a point to produce another point. The input
  279. * and output points can use the same memory.
  280. *
  281. * @param [out] nega The negated input point
  282. * @param [in] a The input point.
  283. */
  284. void decaf_255_point_negate (
  285. decaf_255_point_t nega,
  286. const decaf_255_point_t a
  287. ) API_VIS NONNULL2;
  288. /**
  289. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  290. *
  291. * @param [out] scaled The scaled point base*scalar
  292. * @param [in] base The point to be scaled.
  293. * @param [in] scalar The scalar to multiply by.
  294. */
  295. void decaf_255_point_scalarmul (
  296. decaf_255_point_t scaled,
  297. const decaf_255_point_t base,
  298. const decaf_255_scalar_t scalar
  299. ) API_VIS NONNULL3 NOINLINE;
  300. /**
  301. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  302. * This function operates directly on serialized forms.
  303. *
  304. * @warning This function is experimental. It may not be supported
  305. * long-term.
  306. *
  307. * @param [out] scaled The scaled point base*scalar
  308. * @param [in] base The point to be scaled.
  309. * @param [in] scalar The scalar to multiply by.
  310. * @param [in] allow_identity Allow the input to be the identity.
  311. * @param [in] short_circuit Allow a fast return if the input is illegal.
  312. *
  313. * @retval DECAF_SUCCESS The scalarmul succeeded.
  314. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  315. * base does not represent a point.
  316. */
  317. decaf_bool_t decaf_255_direct_scalarmul (
  318. uint8_t scaled[DECAF_255_SER_BYTES],
  319. const uint8_t base[DECAF_255_SER_BYTES],
  320. const decaf_255_scalar_t scalar,
  321. decaf_bool_t allow_identity,
  322. decaf_bool_t short_circuit
  323. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  324. /**
  325. * @brief Precompute a table for fast scalar multiplication.
  326. * Some implementations do not include precomputed points; for
  327. * those implementations, this implementation simply copies the
  328. * point.
  329. *
  330. * @param [out] a A precomputed table of multiples of the point.
  331. * @param [in] b Any point.
  332. */
  333. void decaf_255_precompute (
  334. decaf_255_precomputed_s *a,
  335. const decaf_255_point_t b
  336. ) API_VIS NONNULL2 NOINLINE;
  337. /**
  338. * @brief Multiply a precomputed base point by a scalar:
  339. * scaled = scalar*base.
  340. * Some implementations do not include precomputed points; for
  341. * those implementations, this function is the same as
  342. * decaf_255_point_scalarmul
  343. *
  344. * @param [out] scaled The scaled point base*scalar
  345. * @param [in] base The point to be scaled.
  346. * @param [in] scalar The scalar to multiply by.
  347. *
  348. * @todo precomputed dsmul? const or variable time?
  349. */
  350. void decaf_255_precomputed_scalarmul (
  351. decaf_255_point_t scaled,
  352. const decaf_255_precomputed_s *base,
  353. const decaf_255_scalar_t scalar
  354. ) API_VIS NONNULL3 NOINLINE;
  355. /**
  356. * @brief Multiply two base points by two scalars:
  357. * scaled = scalar1*base1 + scalar2*base2.
  358. *
  359. * Equivalent to two calls to decaf_255_point_scalarmul, but may be
  360. * faster.
  361. *
  362. * @param [out] combo The linear combination scalar1*base1 + scalar2*base2.
  363. * @param [in] base1 A first point to be scaled.
  364. * @param [in] scalar1 A first scalar to multiply by.
  365. * @param [in] base2 A second point to be scaled.
  366. * @param [in] scalar2 A second scalar to multiply by.
  367. */
  368. void decaf_255_point_double_scalarmul (
  369. decaf_255_point_t combo,
  370. const decaf_255_point_t base1,
  371. const decaf_255_scalar_t scalar1,
  372. const decaf_255_point_t base2,
  373. const decaf_255_scalar_t scalar2
  374. ) API_VIS NONNULL5 NOINLINE;
  375. /**
  376. * @brief Multiply two base points by two scalars:
  377. * scaled = scalar1*decaf_255_point_base + scalar2*base2.
  378. *
  379. * Otherwise equivalent to decaf_255_point_double_scalarmul, but may be
  380. * faster at the expense of being variable time.
  381. *
  382. * @param [out] combo The linear combination scalar1*base + scalar2*base2.
  383. * @param [in] scalar1 A first scalar to multiply by.
  384. * @param [in] base2 A second point to be scaled.
  385. * @param [in] scalar2 A second scalar to multiply by.
  386. *
  387. * @warning: This function takes variable time, and may leak the scalars
  388. * used. It is designed for signature verification.
  389. */
  390. void decaf_255_base_double_scalarmul_non_secret (
  391. decaf_255_point_t combo,
  392. const decaf_255_scalar_t scalar1,
  393. const decaf_255_point_t base2,
  394. const decaf_255_scalar_t scalar2
  395. ) API_VIS NONNULL4 NOINLINE;
  396. /**
  397. * @brief Test that a point is valid, for debugging purposes.
  398. *
  399. * @param [in] toTest The point to test.
  400. * @retval DECAF_TRUE The point is valid.
  401. * @retval DECAF_FALSE The point is invalid.
  402. */
  403. decaf_bool_t decaf_255_point_valid (
  404. const decaf_255_point_t toTest
  405. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  406. /**
  407. * @brief Torque a point, for debugging purposes. The output
  408. * will be equal to the input.
  409. *
  410. * @param [out] q The point to torque.
  411. * @param [in] p The point to torque.
  412. */
  413. void decaf_255_point_debugging_torque (
  414. decaf_255_point_t q,
  415. const decaf_255_point_t p
  416. ) API_VIS NONNULL2 NOINLINE;
  417. /**
  418. * @brief Projectively scale a point, for debugging purposes.
  419. * The output will be equal to the input, and will be valid
  420. * even if the factor is zero.
  421. *
  422. * @param [out] q The point to scale.
  423. * @param [in] p The point to scale.
  424. * @param [in] factor Serialized GF factor to scale.
  425. */
  426. void decaf_255_point_debugging_pscale (
  427. decaf_255_point_t q,
  428. const decaf_255_point_t p,
  429. const unsigned char factor[DECAF_255_SER_BYTES]
  430. ) API_VIS NONNULL2 NOINLINE;
  431. /**
  432. * @brief Almost-Elligator-like hash to curve.
  433. *
  434. * Call this function with the output of a hash to make a hash to the curve.
  435. *
  436. * This function runs Elligator2 on the decaf_255 Jacobi quartic model. It then
  437. * uses the isogeny to put the result in twisted Edwards form. As a result,
  438. * it is safe (cannot produce points of order 4), and would be compatible with
  439. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  440. * Edwards model.
  441. *
  442. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  443. * A factor of 2 due to the isogeny.
  444. * A factor of 2 because we quotient out the 2-torsion.
  445. *
  446. * This makes it about 8:1 overall.
  447. *
  448. * Negating the input (mod q) results in the same point. Inverting the input
  449. * (mod q) results in the negative point. This is the same as Elligator.
  450. *
  451. * This function isn't quite indifferentiable from a random oracle.
  452. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  453. * Furthermore, calling it twice with independent seeds and adding the results
  454. * is indifferentiable from a random oracle.
  455. *
  456. * @param [in] hashed_data Output of some hash function.
  457. * @param [out] pt The data hashed to the curve.
  458. */
  459. void
  460. decaf_255_point_from_hash_nonuniform (
  461. decaf_255_point_t pt,
  462. const unsigned char hashed_data[DECAF_255_SER_BYTES]
  463. ) API_VIS NONNULL2 NOINLINE;
  464. /**
  465. * @brief Inverse of elligator-like hash to curve.
  466. *
  467. * This function writes to the buffer, to make it so that
  468. * decaf_255_point_from_hash_nonuniform(buffer) = pt if possible.
  469. *
  470. * @param [out] recovered_hash Encoded data.
  471. * @param [in] pt The point to encode.
  472. * @param [in] which A "hint" that indicates which inverse to return.
  473. *
  474. * @retval DECAF_SUCCESS The inverse succeeded.
  475. * @retval DECAF_FAILURE The pt isn't the image of
  476. * decaf_255_point_from_hash_nonuniform with the given hint.
  477. *
  478. * @warning The hinting system is subject to change, especially in corner cases.
  479. * @warning FIXME The hinting system doesn't work for certain inputs which have many 0xFF.
  480. */
  481. decaf_bool_t
  482. decaf_255_invert_elligator_nonuniform (
  483. unsigned char recovered_hash[DECAF_255_SER_BYTES],
  484. const decaf_255_point_t pt,
  485. uint16_t which
  486. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  487. /**
  488. * @brief Inverse of elligator-like hash to curve, uniform.
  489. *
  490. * This function modifies the first DECAF_255_SER_BYTES of the
  491. * buffer, to make it so that
  492. * decaf_255_point_from_hash_uniform(buffer) = pt if possible.
  493. *
  494. * @param [out] recovered_hash Encoded data.
  495. * @param [in] pt The point to encode.
  496. * @param [in] which A "hint" that indicates which inverse to return.
  497. *
  498. * @retval DECAF_SUCCESS The inverse succeeded.
  499. * @retval DECAF_FAILURE The pt isn't the image of
  500. * decaf_255_point_from_hash_uniform with the given hint.
  501. */
  502. decaf_bool_t
  503. decaf_255_invert_elligator_uniform (
  504. unsigned char recovered_hash[2*DECAF_255_SER_BYTES],
  505. const decaf_255_point_t pt,
  506. uint16_t which
  507. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  508. /**
  509. * @brief Indifferentiable hash function encoding to curve.
  510. *
  511. * Equivalent to calling decaf_255_point_from_hash_nonuniform twice and adding.
  512. *
  513. * @param [in] hashed_data Output of some hash function.
  514. * @param [out] pt The data hashed to the curve.
  515. */
  516. void decaf_255_point_from_hash_uniform (
  517. decaf_255_point_t pt,
  518. const unsigned char hashed_data[2*DECAF_255_SER_BYTES]
  519. ) API_VIS NONNULL2 NOINLINE;
  520. /**
  521. * @brief Overwrite data with zeros. Uses memset_s if available.
  522. */
  523. void decaf_bzero (
  524. void *data,
  525. size_t size
  526. ) NONNULL1 API_VIS NOINLINE;
  527. /**
  528. * @brief Compare two buffers, returning DECAF_TRUE if they are equal.
  529. */
  530. decaf_bool_t decaf_memeq (
  531. const void *data1,
  532. const void *data2,
  533. size_t size
  534. ) NONNULL2 WARN_UNUSED API_VIS NOINLINE;
  535. /**
  536. * @brief Overwrite scalar with zeros.
  537. */
  538. void decaf_255_scalar_destroy (
  539. decaf_255_scalar_t scalar
  540. ) NONNULL1 API_VIS;
  541. /**
  542. * @brief Overwrite point with zeros.
  543. * @todo Use this internally.
  544. */
  545. void decaf_255_point_destroy (
  546. decaf_255_point_t point
  547. ) NONNULL1 API_VIS;
  548. /**
  549. * @brief Overwrite point with zeros.
  550. * @todo Use this internally.
  551. */
  552. void decaf_255_precomputed_destroy (
  553. decaf_255_precomputed_s *pre
  554. ) NONNULL1 API_VIS;
  555. #endif /* __DECAF_255_H__ */