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.
 
 
 
 
 

757 lines
28 KiB

  1. /**
  2. * @file decaf/point_255.hxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * A group of prime order p, C++ wrapper.
  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 Curve25519) 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. * @warning This file was automatically generated in Python.
  23. * Please do not edit it.
  24. */
  25. #ifndef __DECAF_POINT_255_HXX__
  26. #define __DECAF_POINT_255_HXX__ 1
  27. /** This code uses posix_memalign. */
  28. #ifndef _XOPEN_SOURCE
  29. #define _XOPEN_SOURCE 600
  30. #endif
  31. #include <stdlib.h>
  32. #include <string.h> /* for memcpy */
  33. #include <decaf/point_255.h>
  34. #include <decaf/ed255.h>
  35. #include <decaf/secure_buffer.hxx>
  36. #include <string>
  37. #include <sys/types.h>
  38. #include <limits.h>
  39. /** @cond internal */
  40. #if __cplusplus >= 201103L
  41. #define DECAF_NOEXCEPT noexcept
  42. #else
  43. #define DECAF_NOEXCEPT throw()
  44. #endif
  45. /** @endcond */
  46. namespace decaf {
  47. /**
  48. * Curve25519/Decaf instantiation of group.
  49. */
  50. struct IsoEd25519 {
  51. /** The name of the curve */
  52. static inline const char *name() { return "Iso-Ed25519"; }
  53. /** The curve's cofactor (removed, but useful for testing) */
  54. static const int REMOVED_COFACTOR = 8;
  55. /** Residue class of field modulus: p == this mod 2*(this-1) */
  56. static const int FIELD_MODULUS_TYPE = 5;
  57. /** @cond internal */
  58. class Point;
  59. class Precomputed;
  60. /** @endcond */
  61. /**
  62. * A scalar modulo the curve order.
  63. * Supports the usual arithmetic operations, all in constant time.
  64. */
  65. class Scalar : public Serializable<Scalar> {
  66. public:
  67. /** wrapped C type */
  68. typedef decaf_255_scalar_t Wrapped;
  69. /** Size of a serialized element */
  70. static const size_t SER_BYTES = DECAF_255_SCALAR_BYTES;
  71. /** access to the underlying scalar object */
  72. Wrapped s;
  73. /** @cond internal */
  74. /** Don't initialize. */
  75. inline Scalar(const NOINIT &) DECAF_NOEXCEPT {}
  76. /** @endcond */
  77. /** Set to an unsigned word */
  78. inline Scalar(uint64_t w) DECAF_NOEXCEPT { *this = w; }
  79. /** Set to a signed word */
  80. inline Scalar(int64_t w) DECAF_NOEXCEPT { *this = w; }
  81. /** Set to an unsigned word */
  82. inline Scalar(unsigned int w) DECAF_NOEXCEPT { *this = w; }
  83. /** Set to a signed word */
  84. inline Scalar(int w) DECAF_NOEXCEPT { *this = w; }
  85. /** Construct from RNG */
  86. inline explicit Scalar(Rng &rng) DECAF_NOEXCEPT {
  87. FixedArrayBuffer<SER_BYTES + 16> sb(rng);
  88. *this = sb;
  89. }
  90. /** Construct from decaf_scalar_t object. */
  91. inline Scalar(const Wrapped &t = decaf_255_scalar_zero) DECAF_NOEXCEPT { decaf_255_scalar_copy(s,t); }
  92. /** Copy constructor. */
  93. inline Scalar(const Scalar &x) DECAF_NOEXCEPT { *this = x; }
  94. /** Construct from arbitrary-length little-endian byte sequence. */
  95. inline Scalar(const Block &buffer) DECAF_NOEXCEPT { *this = buffer; }
  96. /** Serializable instance */
  97. inline size_t ser_size() const DECAF_NOEXCEPT { return SER_BYTES; }
  98. /** Serializable instance */
  99. inline void serialize_into(unsigned char *buffer) const DECAF_NOEXCEPT {
  100. decaf_255_scalar_encode(buffer, s);
  101. }
  102. /** Assignment. */
  103. inline Scalar& operator=(const Scalar &x) DECAF_NOEXCEPT { decaf_255_scalar_copy(s,x.s); return *this; }
  104. /** Assign from unsigned 64-bit integer. */
  105. inline Scalar& operator=(uint64_t w) DECAF_NOEXCEPT { decaf_255_scalar_set_unsigned(s,w); return *this; }
  106. /** Assign from signed int. */
  107. inline Scalar& operator=(int64_t w) DECAF_NOEXCEPT {
  108. Scalar t(-(uint64_t)INT_MIN);
  109. decaf_255_scalar_set_unsigned(s,(uint64_t)w - (uint64_t)INT_MIN);
  110. *this -= t;
  111. return *this;
  112. }
  113. /** Assign from unsigned int. */
  114. inline Scalar& operator=(unsigned int w) DECAF_NOEXCEPT { return *this = (uint64_t)w; }
  115. /** Assign from signed int. */
  116. inline Scalar& operator=(int w) DECAF_NOEXCEPT { return *this = (int64_t)w; }
  117. /** Destructor securely zeorizes the scalar. */
  118. inline ~Scalar() DECAF_NOEXCEPT { decaf_255_scalar_destroy(s); }
  119. /** Assign from arbitrary-length little-endian byte sequence in a Block. */
  120. inline Scalar &operator=(const Block &bl) DECAF_NOEXCEPT {
  121. decaf_255_scalar_decode_long(s,bl.data(),bl.size()); return *this;
  122. }
  123. /**
  124. * Decode from correct-length little-endian byte sequence.
  125. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  126. */
  127. static inline decaf_error_t DECAF_WARN_UNUSED decode (
  128. Scalar &sc, const FixedBlock<SER_BYTES> buffer
  129. ) DECAF_NOEXCEPT {
  130. return decaf_255_scalar_decode(sc.s,buffer.data());
  131. }
  132. /** Add. */
  133. inline Scalar operator+ (const Scalar &q) const DECAF_NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_add(r.s,s,q.s); return r; }
  134. /** Add to this. */
  135. inline Scalar &operator+=(const Scalar &q) DECAF_NOEXCEPT { decaf_255_scalar_add(s,s,q.s); return *this; }
  136. /** Subtract. */
  137. inline Scalar operator- (const Scalar &q) const DECAF_NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,s,q.s); return r; }
  138. /** Subtract from this. */
  139. inline Scalar &operator-=(const Scalar &q) DECAF_NOEXCEPT { decaf_255_scalar_sub(s,s,q.s); return *this; }
  140. /** Multiply */
  141. inline Scalar operator* (const Scalar &q) const DECAF_NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_mul(r.s,s,q.s); return r; }
  142. /** Multiply into this. */
  143. inline Scalar &operator*=(const Scalar &q) DECAF_NOEXCEPT { decaf_255_scalar_mul(s,s,q.s); return *this; }
  144. /** Negate */
  145. inline Scalar operator- () const DECAF_NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,decaf_255_scalar_zero,s); return r; }
  146. /** Invert with Fermat's Little Theorem (slow!). If *this == 0,
  147. * throw CryptoException. */
  148. inline Scalar inverse() const /*throw(CryptoException)*/ {
  149. Scalar r;
  150. if (DECAF_SUCCESS != decaf_255_scalar_invert(r.s,s)) {
  151. throw CryptoException();
  152. }
  153. return r;
  154. }
  155. /** Invert with Fermat's Little Theorem (slow!). If *this == 0, set r=0
  156. * and return DECAF_FAILURE. */
  157. inline decaf_error_t DECAF_WARN_UNUSED
  158. inverse_noexcept(Scalar &r) const DECAF_NOEXCEPT {
  159. return decaf_255_scalar_invert(r.s,s);
  160. }
  161. /** Divide by inverting q. If q == 0, return 0. */
  162. inline Scalar operator/ (const Scalar &q) const /*throw(CryptoException)*/ { return *this * q.inverse(); }
  163. /** Divide by inverting q. If q == 0, return 0. */
  164. inline Scalar &operator/=(const Scalar &q) /*throw(CryptoException)*/ { return *this *= q.inverse(); }
  165. /** Return half this scalar. Much faster than /2. */
  166. inline Scalar half() const { Scalar out; decaf_255_scalar_halve(out.s,s); return out; }
  167. /** Compare in constant time */
  168. inline bool operator!=(const Scalar &q) const DECAF_NOEXCEPT { return !(*this == q); }
  169. /** Compare in constant time */
  170. inline bool operator==(const Scalar &q) const DECAF_NOEXCEPT { return !!decaf_255_scalar_eq(s,q.s); }
  171. /** Scalarmul with scalar on left. */
  172. inline Point operator* (const Point &q) const DECAF_NOEXCEPT { return q * (*this); }
  173. /** Scalarmul-precomputed with scalar on left. */
  174. inline Point operator* (const Precomputed &q) const DECAF_NOEXCEPT { return q * (*this); }
  175. /** Direct scalar multiplication. */
  176. inline SecureBuffer direct_scalarmul (
  177. const FixedBlock<SER_BYTES> &in,
  178. decaf_bool_t allow_identity=DECAF_FALSE,
  179. decaf_bool_t short_circuit=DECAF_TRUE
  180. ) const /*throw(CryptoException)*/;
  181. /** Direct scalar multiplication. */
  182. inline decaf_error_t DECAF_WARN_UNUSED direct_scalarmul_noexcept(
  183. FixedBuffer<SER_BYTES> &out,
  184. const FixedBlock<SER_BYTES> &in,
  185. decaf_bool_t allow_identity=DECAF_FALSE,
  186. decaf_bool_t short_circuit=DECAF_TRUE
  187. ) const DECAF_NOEXCEPT;
  188. };
  189. /**
  190. * Element of prime-order group.
  191. */
  192. class Point : public Serializable<Point> {
  193. public:
  194. /** wrapped C type */
  195. typedef decaf_255_point_t Wrapped;
  196. /** Size of a serialized element */
  197. static const size_t SER_BYTES = DECAF_255_SER_BYTES;
  198. /** Bytes required for hash */
  199. static const size_t HASH_BYTES = DECAF_255_HASH_BYTES;
  200. /**
  201. * Size of a stegged element.
  202. *
  203. * FUTURE: You can use HASH_BYTES * 3/2 (or more likely much less, eg HASH_BYTES + 8)
  204. * with a random oracle hash function, by hash-expanding everything past the first
  205. * HASH_BYTES of the element. However, since the internal C invert_elligator is not
  206. * tied to a hash function, I didn't want to tie the C++ wrapper to a hash function
  207. * either. But it might be a good idea to do this in the future, either with STROBE
  208. * or something else.
  209. *
  210. * Then again, calling invert_elligator at all is super niche, so maybe who cares?
  211. */
  212. static const size_t STEG_BYTES = HASH_BYTES * 2;
  213. /** Number of bits in invert_elligator which are actually used. */
  214. static const unsigned int INVERT_ELLIGATOR_WHICH_BITS = DECAF_255_INVERT_ELLIGATOR_WHICH_BITS;
  215. /** The c-level object. */
  216. Wrapped p;
  217. /** @cond internal */
  218. /** Don't initialize. */
  219. inline Point(const NOINIT &) DECAF_NOEXCEPT {}
  220. /** @endcond */
  221. /** Constructor sets to identity by default. */
  222. inline Point(const Wrapped &q = decaf_255_point_identity) DECAF_NOEXCEPT { decaf_255_point_copy(p,q); }
  223. /** Copy constructor. */
  224. inline Point(const Point &q) DECAF_NOEXCEPT { *this = q; }
  225. /** Assignment. */
  226. inline Point& operator=(const Point &q) DECAF_NOEXCEPT { decaf_255_point_copy(p,q.p); return *this; }
  227. /** Destructor securely zeorizes the point. */
  228. inline ~Point() DECAF_NOEXCEPT { decaf_255_point_destroy(p); }
  229. /** Construct from RNG */
  230. inline explicit Point(Rng &rng, bool uniform = true) DECAF_NOEXCEPT {
  231. if (uniform) {
  232. FixedArrayBuffer<2*HASH_BYTES> b(rng);
  233. set_to_hash(b);
  234. } else {
  235. FixedArrayBuffer<HASH_BYTES> b(rng);
  236. set_to_hash(b);
  237. }
  238. }
  239. /**
  240. * Initialize from a fixed-length byte string.
  241. * The all-zero string maps to the identity.
  242. *
  243. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  244. * or was the identity and allow_identity was DECAF_FALSE.
  245. */
  246. inline explicit Point(const FixedBlock<SER_BYTES> &buffer, decaf_bool_t allow_identity=DECAF_TRUE)
  247. /*throw(CryptoException)*/ {
  248. if (DECAF_SUCCESS != decode(buffer,allow_identity)) {
  249. throw CryptoException();
  250. }
  251. }
  252. /**
  253. * Initialize from C++ fixed-length byte string.
  254. * The all-zero string maps to the identity.
  255. *
  256. * @retval DECAF_SUCCESS the string was successfully decoded.
  257. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  258. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  259. */
  260. inline decaf_error_t DECAF_WARN_UNUSED decode (
  261. const FixedBlock<SER_BYTES> &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  262. ) DECAF_NOEXCEPT {
  263. return decaf_255_point_decode(p,buffer.data(),allow_identity);
  264. }
  265. /**
  266. * Initialize from C++ fixed-length byte string, like EdDSA.
  267. * The all-zero string maps to the identity.
  268. *
  269. * @retval DECAF_SUCCESS the string was successfully decoded.
  270. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point.
  271. * Contents of the point are undefined.
  272. */
  273. inline decaf_error_t DECAF_WARN_UNUSED decode_like_eddsa_and_ignore_cofactor_noexcept (
  274. const FixedBlock<DECAF_EDDSA_25519_PUBLIC_BYTES> &buffer
  275. ) DECAF_NOEXCEPT {
  276. return decaf_255_point_decode_like_eddsa_and_ignore_cofactor(p,buffer.data());
  277. }
  278. inline void decode_like_eddsa_and_ignore_cofactor (
  279. const FixedBlock<DECAF_EDDSA_25519_PUBLIC_BYTES> &buffer
  280. ) /*throw(CryptoException)*/ {
  281. if (DECAF_SUCCESS != decode_like_eddsa_and_ignore_cofactor_noexcept(buffer)) throw(CryptoException());
  282. }
  283. /** Multiply out cofactor and encode like EdDSA. */
  284. inline SecureBuffer mul_by_cofactor_and_encode_like_eddsa() const {
  285. SecureBuffer ret(DECAF_EDDSA_25519_PUBLIC_BYTES);
  286. decaf_255_point_mul_by_cofactor_and_encode_like_eddsa(ret.data(),p);
  287. return ret;
  288. }
  289. /**
  290. * Map uniformly to the curve from a hash buffer.
  291. * The empty or all-zero string maps to the identity, as does the string "\\x01".
  292. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  293. * but the buffer will be zero-padded on the right.
  294. */
  295. static inline Point from_hash ( const Block &s ) DECAF_NOEXCEPT {
  296. Point p((NOINIT())); p.set_to_hash(s); return p;
  297. }
  298. /**
  299. * Map to the curve from a hash buffer.
  300. * The empty or all-zero string maps to the identity, as does the string "\\x01".
  301. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  302. * but the buffer will be zero-padded on the right.
  303. */
  304. inline void set_to_hash( const Block &s ) DECAF_NOEXCEPT {
  305. if (s.size() < HASH_BYTES) {
  306. SecureBuffer b(HASH_BYTES);
  307. memcpy(b.data(), s.data(), s.size());
  308. decaf_255_point_from_hash_nonuniform(p,b.data());
  309. } else if (s.size() == HASH_BYTES) {
  310. decaf_255_point_from_hash_nonuniform(p,s.data());
  311. } else if (s.size() < 2*HASH_BYTES) {
  312. SecureBuffer b(2*HASH_BYTES);
  313. memcpy(b.data(), s.data(), s.size());
  314. decaf_255_point_from_hash_uniform(p,b.data());
  315. } else {
  316. decaf_255_point_from_hash_uniform(p,s.data());
  317. }
  318. }
  319. /**
  320. * Encode to string. The identity encodes to the all-zero string.
  321. */
  322. inline operator SecureBuffer() const {
  323. SecureBuffer buffer(SER_BYTES);
  324. decaf_255_point_encode(buffer.data(), p);
  325. return buffer;
  326. }
  327. /** Serializable instance */
  328. inline size_t ser_size() const DECAF_NOEXCEPT { return SER_BYTES; }
  329. /** Serializable instance */
  330. inline void serialize_into(unsigned char *buffer) const DECAF_NOEXCEPT {
  331. decaf_255_point_encode(buffer, p);
  332. }
  333. /** Point add. */
  334. inline Point operator+ (const Point &q) const DECAF_NOEXCEPT { Point r((NOINIT())); decaf_255_point_add(r.p,p,q.p); return r; }
  335. /** Point add. */
  336. inline Point &operator+=(const Point &q) DECAF_NOEXCEPT { decaf_255_point_add(p,p,q.p); return *this; }
  337. /** Point subtract. */
  338. inline Point operator- (const Point &q) const DECAF_NOEXCEPT { Point r((NOINIT())); decaf_255_point_sub(r.p,p,q.p); return r; }
  339. /** Point subtract. */
  340. inline Point &operator-=(const Point &q) DECAF_NOEXCEPT { decaf_255_point_sub(p,p,q.p); return *this; }
  341. /** Point negate. */
  342. inline Point operator- () const DECAF_NOEXCEPT { Point r((NOINIT())); decaf_255_point_negate(r.p,p); return r; }
  343. /** Double the point out of place. */
  344. inline Point times_two () const DECAF_NOEXCEPT { Point r((NOINIT())); decaf_255_point_double(r.p,p); return r; }
  345. /** Double the point in place. */
  346. inline Point &double_in_place() DECAF_NOEXCEPT { decaf_255_point_double(p,p); return *this; }
  347. /** Constant-time compare. */
  348. inline bool operator!=(const Point &q) const DECAF_NOEXCEPT { return ! decaf_255_point_eq(p,q.p); }
  349. /** Constant-time compare. */
  350. inline bool operator==(const Point &q) const DECAF_NOEXCEPT { return !!decaf_255_point_eq(p,q.p); }
  351. /** Scalar multiply. */
  352. inline Point operator* (const Scalar &s) const DECAF_NOEXCEPT { Point r((NOINIT())); decaf_255_point_scalarmul(r.p,p,s.s); return r; }
  353. /** Scalar multiply in place. */
  354. inline Point &operator*=(const Scalar &s) DECAF_NOEXCEPT { decaf_255_point_scalarmul(p,p,s.s); return *this; }
  355. /** Multiply by s.inverse(). If s=0, maps to the identity. */
  356. inline Point operator/ (const Scalar &s) const /*throw(CryptoException)*/ { return (*this) * s.inverse(); }
  357. /** Multiply by s.inverse(). If s=0, maps to the identity. */
  358. inline Point &operator/=(const Scalar &s) /*throw(CryptoException)*/ { return (*this) *= s.inverse(); }
  359. /** Validate / sanity check */
  360. inline bool validate() const DECAF_NOEXCEPT { return decaf_255_point_valid(p); }
  361. /** Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  362. static inline Point double_scalarmul (
  363. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  364. ) DECAF_NOEXCEPT {
  365. Point p((NOINIT())); decaf_255_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  366. }
  367. /** Dual-scalar multiply, equivalent to this*r1, this*r2 but faster. */
  368. inline void dual_scalarmul (
  369. Point &q1, Point &q2, const Scalar &r1, const Scalar &r2
  370. ) const DECAF_NOEXCEPT {
  371. decaf_255_point_dual_scalarmul(q1.p,q2.p,p,r1.s,r2.s);
  372. }
  373. /**
  374. * Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  375. * For those who like their scalars before the point.
  376. */
  377. static inline Point double_scalarmul (
  378. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  379. ) DECAF_NOEXCEPT {
  380. return double_scalarmul(q,qs,r,rs);
  381. }
  382. /**
  383. * Double-scalar multiply: this point by the first scalar and base by the second scalar.
  384. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  385. * it doesn't).
  386. */
  387. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) DECAF_NOEXCEPT {
  388. Point r((NOINIT())); decaf_255_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  389. }
  390. /** Return a point equal to *this, whose internal data is rotated by a torsion element. */
  391. inline Point debugging_torque() const DECAF_NOEXCEPT {
  392. Point q;
  393. decaf_255_point_debugging_torque(q.p,p);
  394. return q;
  395. }
  396. /** Return a point equal to *this, whose internal data has a modified representation. */
  397. inline Point debugging_pscale(const FixedBlock<SER_BYTES> factor) const DECAF_NOEXCEPT {
  398. Point q;
  399. decaf_255_point_debugging_pscale(q.p,p,factor.data());
  400. return q;
  401. }
  402. /** Return a point equal to *this, whose internal data has a randomized representation. */
  403. inline Point debugging_pscale(Rng &r) const DECAF_NOEXCEPT {
  404. FixedArrayBuffer<SER_BYTES> sb(r);
  405. return debugging_pscale(sb);
  406. }
  407. /**
  408. * Modify buffer so that Point::from_hash(Buffer) == *this, and return DECAF_SUCCESS;
  409. * or leave buf unmodified and return DECAF_FAILURE.
  410. */
  411. inline decaf_error_t invert_elligator (
  412. Buffer buf, uint32_t hint
  413. ) const DECAF_NOEXCEPT {
  414. unsigned char buf2[2*HASH_BYTES];
  415. memset(buf2,0,sizeof(buf2));
  416. memcpy(buf2,buf.data(),(buf.size() > 2*HASH_BYTES) ? 2*HASH_BYTES : buf.size());
  417. decaf_bool_t ret;
  418. if (buf.size() > HASH_BYTES) {
  419. ret = decaf_successful(decaf_255_invert_elligator_uniform(buf2, p, hint));
  420. } else {
  421. ret = decaf_successful(decaf_255_invert_elligator_nonuniform(buf2, p, hint));
  422. }
  423. if (buf.size() < HASH_BYTES) {
  424. ret &= decaf_memeq(&buf2[buf.size()], &buf2[HASH_BYTES], HASH_BYTES - buf.size());
  425. }
  426. for (size_t i=0; i<buf.size() && i<HASH_BYTES; i++) {
  427. buf[i] = (buf[i] & ~ret) | (buf2[i] &ret);
  428. }
  429. decaf_bzero(buf2,sizeof(buf2));
  430. return decaf_succeed_if(ret);
  431. }
  432. /** Steganographically encode this */
  433. inline SecureBuffer steg_encode(Rng &rng, size_t size=STEG_BYTES) const /*throw(std::bad_alloc, LengthException)*/ {
  434. if (size <= HASH_BYTES + 4 || size > 2*HASH_BYTES) throw LengthException();
  435. SecureBuffer out(STEG_BYTES);
  436. decaf_error_t done;
  437. do {
  438. rng.read(Buffer(out).slice(HASH_BYTES-4,STEG_BYTES-HASH_BYTES+1));
  439. uint32_t hint = 0;
  440. for (int i=0; i<4; i++) { hint |= uint32_t(out[HASH_BYTES-4+i])<<(8*i); }
  441. done = invert_elligator(out, hint);
  442. } while (!decaf_successful(done));
  443. return out;
  444. }
  445. /** Return the base point */
  446. static inline const Point base() DECAF_NOEXCEPT { return Point(decaf_255_point_base); }
  447. /** Return the identity point */
  448. static inline const Point identity() DECAF_NOEXCEPT { return Point(decaf_255_point_identity); }
  449. };
  450. /**
  451. * Precomputed table of points.
  452. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  453. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  454. * stack-allocate a 15kiB object anyway.
  455. */
  456. /** @cond internal */
  457. typedef decaf_255_precomputed_s Precomputed_U;
  458. /** @endcond */
  459. class Precomputed
  460. /** @cond internal */
  461. : protected OwnedOrUnowned<Precomputed,Precomputed_U>
  462. /** @endcond */
  463. {
  464. public:
  465. /** Destructor securely zeorizes the memory. */
  466. inline ~Precomputed() DECAF_NOEXCEPT { clear(); }
  467. /**
  468. * Initialize from underlying type, declared as a reference to prevent
  469. * it from being called with 0, thereby breaking override.
  470. *
  471. * The underlying object must remain valid throughout the lifetime of this one.
  472. *
  473. * By default, initializes to the table for the base point.
  474. *
  475. * @warning The empty initializer makes this equal to base, unlike the empty
  476. * initializer for points which makes this equal to the identity.
  477. */
  478. inline Precomputed (
  479. const Precomputed_U &yours = *decaf_255_precomputed_base
  480. ) DECAF_NOEXCEPT : OwnedOrUnowned<Precomputed,Precomputed_U>(yours) {}
  481. #if __cplusplus >= 201103L
  482. /** Move-assign operator */
  483. inline Precomputed &operator=(Precomputed &&it) DECAF_NOEXCEPT {
  484. OwnedOrUnowned<Precomputed,Precomputed_U>::operator= (it);
  485. return *this;
  486. }
  487. /** Move constructor */
  488. inline Precomputed(Precomputed &&it) DECAF_NOEXCEPT : OwnedOrUnowned<Precomputed,Precomputed_U>() {
  489. *this = it;
  490. }
  491. /** Undelete copy operator */
  492. inline Precomputed &operator=(const Precomputed &it) DECAF_NOEXCEPT {
  493. OwnedOrUnowned<Precomputed,Precomputed_U>::operator= (it);
  494. return *this;
  495. }
  496. #endif
  497. /**
  498. * Initilaize from point. Must allocate memory, and may throw.
  499. */
  500. inline Precomputed &operator=(const Point &it) /*throw(std::bad_alloc)*/ {
  501. alloc();
  502. decaf_255_precompute(ours.mine,it.p);
  503. return *this;
  504. }
  505. /**
  506. * Copy constructor.
  507. */
  508. inline Precomputed(const Precomputed &it) /*throw(std::bad_alloc)*/
  509. : OwnedOrUnowned<Precomputed,Precomputed_U>() { *this = it; }
  510. /**
  511. * Constructor which initializes from point.
  512. */
  513. inline explicit Precomputed(const Point &it) /*throw(std::bad_alloc)*/
  514. : OwnedOrUnowned<Precomputed,Precomputed_U>() { *this = it; }
  515. /** Fixed base scalarmul. */
  516. inline Point operator* (const Scalar &s) const DECAF_NOEXCEPT { Point r; decaf_255_precomputed_scalarmul(r.p,get(),s.s); return r; }
  517. /** Multiply by s.inverse(). If s=0, maps to the identity. */
  518. inline Point operator/ (const Scalar &s) const /*throw(CryptoException)*/ { return (*this) * s.inverse(); }
  519. /** Return the table for the base point. */
  520. static inline const Precomputed base() DECAF_NOEXCEPT { return Precomputed(); }
  521. public:
  522. /** @cond internal */
  523. friend class OwnedOrUnowned<Precomputed,Precomputed_U>;
  524. static inline size_t size() DECAF_NOEXCEPT { return decaf_255_sizeof_precomputed_s; }
  525. static inline size_t alignment() DECAF_NOEXCEPT { return decaf_255_alignof_precomputed_s; }
  526. static inline const Precomputed_U * default_value() DECAF_NOEXCEPT { return decaf_255_precomputed_base; }
  527. /** @endcond */
  528. };
  529. struct DhLadder {
  530. public:
  531. /** Bytes in an X25519 public key. */
  532. static const size_t PUBLIC_BYTES = DECAF_X25519_PUBLIC_BYTES;
  533. /** Bytes in an X25519 private key. */
  534. static const size_t PRIVATE_BYTES = DECAF_X25519_PRIVATE_BYTES;
  535. /** Base point for a scalar multiplication. */
  536. static const FixedBlock<PUBLIC_BYTES> base_point() DECAF_NOEXCEPT {
  537. return FixedBlock<PUBLIC_BYTES>(decaf_x25519_base_point);
  538. }
  539. /** Calculate and return a shared secret with public key. */
  540. static inline SecureBuffer shared_secret(
  541. const FixedBlock<PUBLIC_BYTES> &pk,
  542. const FixedBlock<PRIVATE_BYTES> &scalar
  543. ) /*throw(std::bad_alloc,CryptoException)*/ {
  544. SecureBuffer out(PUBLIC_BYTES);
  545. if (DECAF_SUCCESS != decaf_x25519(out.data(), pk.data(), scalar.data())) {
  546. throw CryptoException();
  547. }
  548. return out;
  549. }
  550. /** Calculate and write into out a shared secret with public key, noexcept version. */
  551. static inline decaf_error_t DECAF_WARN_UNUSED
  552. shared_secret_noexcept (
  553. FixedBuffer<PUBLIC_BYTES> &out,
  554. const FixedBlock<PUBLIC_BYTES> &pk,
  555. const FixedBlock<PRIVATE_BYTES> &scalar
  556. ) DECAF_NOEXCEPT {
  557. return decaf_x25519(out.data(), pk.data(), scalar.data());
  558. }
  559. /** Calculate and return a public key; equivalent to shared_secret(base_point(),scalar)
  560. * but possibly faster.
  561. * @deprecated Renamed to derive_public_key.
  562. */
  563. static inline SecureBuffer DECAF_DEPRECATED("Renamed to derive_public_key")
  564. generate_key(
  565. const FixedBlock<PRIVATE_BYTES> &scalar
  566. ) /*throw(std::bad_alloc)*/ {
  567. SecureBuffer out(PUBLIC_BYTES);
  568. decaf_x25519_derive_public_key(out.data(), scalar.data());
  569. return out;
  570. }
  571. /** Calculate and return a public key; equivalent to shared_secret(base_point(),scalar)
  572. * but possibly faster.
  573. */
  574. static inline SecureBuffer derive_public_key(
  575. const FixedBlock<PRIVATE_BYTES> &scalar
  576. ) /*throw(std::bad_alloc)*/ {
  577. SecureBuffer out(PUBLIC_BYTES);
  578. decaf_x25519_derive_public_key(out.data(), scalar.data());
  579. return out;
  580. }
  581. /** Calculate and return a public key into a fixed buffer;
  582. * equivalent to shared_secret(base_point(),scalar) but possibly faster.
  583. */
  584. static inline void
  585. derive_public_key_noexcept (
  586. FixedBuffer<PUBLIC_BYTES> &out,
  587. const FixedBlock<PRIVATE_BYTES> &scalar
  588. ) DECAF_NOEXCEPT {
  589. decaf_x25519_derive_public_key(out.data(), scalar.data());
  590. }
  591. /** Calculate and return a public key into a fixed buffer;
  592. * equivalent to shared_secret(base_point(),scalar) but possibly faster.
  593. * @deprecated Renamed to derive_public_key_noexcept.
  594. */
  595. static inline void DECAF_DEPRECATED("Renamed to derive_public_key_noexcept")
  596. generate_key_noexcept (
  597. FixedBuffer<PUBLIC_BYTES> &out,
  598. const FixedBlock<PRIVATE_BYTES> &scalar
  599. ) DECAF_NOEXCEPT {
  600. decaf_x25519_derive_public_key(out.data(), scalar.data());
  601. }
  602. };
  603. }; /* struct IsoEd25519 */
  604. /** @cond internal */
  605. inline SecureBuffer IsoEd25519::Scalar::direct_scalarmul (
  606. const FixedBlock<IsoEd25519::Point::SER_BYTES> &in,
  607. decaf_bool_t allow_identity,
  608. decaf_bool_t short_circuit
  609. ) const /*throw(CryptoException)*/ {
  610. SecureBuffer out(IsoEd25519::Point::SER_BYTES);
  611. if (DECAF_SUCCESS !=
  612. decaf_255_direct_scalarmul(out.data(), in.data(), s, allow_identity, short_circuit)
  613. ) {
  614. throw CryptoException();
  615. }
  616. return out;
  617. }
  618. inline decaf_error_t IsoEd25519::Scalar::direct_scalarmul_noexcept (
  619. FixedBuffer<IsoEd25519::Point::SER_BYTES> &out,
  620. const FixedBlock<IsoEd25519::Point::SER_BYTES> &in,
  621. decaf_bool_t allow_identity,
  622. decaf_bool_t short_circuit
  623. ) const DECAF_NOEXCEPT {
  624. return decaf_255_direct_scalarmul(out.data(), in.data(), s, allow_identity, short_circuit);
  625. }
  626. /** @endcond */
  627. #undef DECAF_NOEXCEPT
  628. } /* namespace decaf */
  629. #endif /* __DECAF_POINT_255_HXX__ */