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.
 
 
 
 
 

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