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.
 
 
 
 
 

729 lines
26 KiB

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