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.
 
 
 
 
 

751 lines
27 KiB

  1. /**
  2. * @file decaf_255.hxx
  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, 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 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. #ifndef __DECAF_255_HXX__
  23. #define __DECAF_255_HXX__ 1
  24. /** This code uses posix_memalign. */
  25. #define _XOPEN_SOURCE 600
  26. #include <stdlib.h>
  27. #include <string.h> /* for memcpy */
  28. #include "decaf.h"
  29. #include <string>
  30. #include <sys/types.h>
  31. #include <limits.h>
  32. /* TODO: This is incomplete */
  33. /* TODO: attribute nonnull */
  34. /** @cond internal */
  35. #if __cplusplus >= 201103L
  36. #define NOEXCEPT noexcept
  37. #define EXPLICIT_CON explicit
  38. #define GET_DATA(str) ((const unsigned char *)&(str)[0])
  39. #else
  40. #define NOEXCEPT throw()
  41. #define EXPLICIT_CON
  42. #define GET_DATA(str) ((const unsigned char *)((str).data()))
  43. #endif
  44. /** @endcond */
  45. namespace decaf {
  46. /** @brief An exception for when crypto (ie point decode) has failed. */
  47. class CryptoException : public std::exception {
  48. public:
  49. /** @return "CryptoException" */
  50. virtual const char * what() const NOEXCEPT { return "CryptoException"; }
  51. };
  52. /** @brief An exception for when crypto (ie point decode) has failed. */
  53. class LengthException : public std::exception {
  54. public:
  55. /** @return "CryptoException" */
  56. virtual const char * what() const NOEXCEPT { return "LengthException"; }
  57. };
  58. /**
  59. * Securely erase contents of memory.
  60. */
  61. static inline void really_bzero(void *data, size_t size) { decaf_bzero(data,size); }
  62. /** Block object */
  63. class Block {
  64. protected:
  65. unsigned char *data_;
  66. size_t size_;
  67. public:
  68. /** Empty init */
  69. inline Block() NOEXCEPT : data_(NULL), size_(0) {}
  70. /** Init from C string */
  71. inline Block(const char *data) NOEXCEPT : data_((unsigned char *)data), size_(strlen(data)) {}
  72. /** Unowned init */
  73. inline Block(const unsigned char *data, size_t size) NOEXCEPT : data_((unsigned char *)data), size_(size) {}
  74. /** Block from std::string */
  75. inline Block(const std::string &s) : data_((unsigned char *)GET_DATA(s)), size_(s.size()) {}
  76. /** Get const data */
  77. inline const unsigned char *data() const NOEXCEPT { return data_; }
  78. /** Get the size */
  79. inline size_t size() const NOEXCEPT { return size_; }
  80. /** Autocast to const unsigned char * */
  81. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  82. /** Convert to C++ string */
  83. inline std::string get_string() const {
  84. return std::string((const char *)data_,size_);
  85. }
  86. /** Slice the buffer*/
  87. inline Block slice(size_t off, size_t length) const throw(LengthException) {
  88. if (off > size() || length > size() - off)
  89. throw LengthException();
  90. return Block(data()+off, length);
  91. }
  92. /* Content-wise comparison; constant-time if they are the same length.
  93. * FIXME: is it wise to have a content-wise compare on objects that may be mutable?
  94. */
  95. inline decaf_bool_t operator==(const Block &b) const NOEXCEPT {
  96. return ~(*this != b);
  97. }
  98. inline decaf_bool_t operator!=(const Block &b) const NOEXCEPT {
  99. if (b.size() != size()) return true;
  100. return ~decaf_memeq(b,*this,size());
  101. }
  102. /** Virtual destructor for SecureBlock. TODO: probably means vtable? Make bool? */
  103. inline virtual ~Block() {};
  104. };
  105. class TmpBuffer;
  106. class Buffer : public Block {
  107. public:
  108. /** Null init */
  109. inline Buffer() NOEXCEPT : Block() {}
  110. /** Unowned init */
  111. inline Buffer(unsigned char *data, size_t size) NOEXCEPT : Block(data,size) {}
  112. /** Get unconst data */
  113. inline unsigned char *data() NOEXCEPT { return data_; }
  114. /** Get const data */
  115. inline const unsigned char *data() const NOEXCEPT { return data_; }
  116. /** Autocast to const unsigned char * */
  117. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  118. /** Autocast to unsigned char */
  119. inline operator unsigned char*() NOEXCEPT { return data_; }
  120. /** Slice the buffer*/
  121. inline TmpBuffer slice(size_t off, size_t length) throw(LengthException);
  122. };
  123. class TmpBuffer : public Buffer {
  124. public:
  125. /** Unowned init */
  126. inline TmpBuffer(unsigned char *data, size_t size) NOEXCEPT : Buffer(data,size) {}
  127. };
  128. TmpBuffer Buffer::slice(size_t off, size_t length) throw(LengthException) {
  129. if (off > size() || length > size() - off) throw LengthException();
  130. return TmpBuffer(data()+off, length);
  131. }
  132. /** A self-erasing block of data */
  133. class SecureBuffer : public Buffer {
  134. public:
  135. /** Null secure block */
  136. inline SecureBuffer() NOEXCEPT : Buffer() {}
  137. /** Construct empty from size */
  138. inline SecureBuffer(size_t size) {
  139. data_ = new unsigned char[size_ = size];
  140. memset(data_,0,size);
  141. }
  142. /** Construct from data */
  143. inline SecureBuffer(const unsigned char *data, size_t size){
  144. data_ = new unsigned char[size_ = size];
  145. memcpy(data_, data, size);
  146. }
  147. /** Copy constructor */
  148. inline SecureBuffer(const Block &copy) : Buffer() { *this = copy; }
  149. /** Copy-assign constructor */
  150. inline SecureBuffer& operator=(const Block &copy) throw(std::bad_alloc) {
  151. if (&copy == this) return *this;
  152. clear();
  153. data_ = new unsigned char[size_ = copy.size()];
  154. memcpy(data_,copy.data(),size_);
  155. return *this;
  156. }
  157. /** Copy-assign constructor */
  158. inline SecureBuffer& operator=(const SecureBuffer &copy) throw(std::bad_alloc) {
  159. if (&copy == this) return *this;
  160. clear();
  161. data_ = new unsigned char[size_ = copy.size()];
  162. memcpy(data_,copy.data(),size_);
  163. return *this;
  164. }
  165. /** Destructor erases data */
  166. ~SecureBuffer() NOEXCEPT { clear(); }
  167. /** Clear data */
  168. inline void clear() NOEXCEPT {
  169. if (data_ == NULL) return;
  170. really_bzero(data_,size_);
  171. delete[] data_;
  172. data_ = NULL;
  173. size_ = 0;
  174. }
  175. #if __cplusplus >= 201103L
  176. /** Move constructor */
  177. inline SecureBuffer(SecureBuffer &&move) { *this = move; }
  178. /** Move non-constructor */
  179. inline SecureBuffer(Block &&move) { *this = (Block &)move; }
  180. /** Move-assign constructor. TODO: check that this actually gets used.*/
  181. inline SecureBuffer& operator=(SecureBuffer &&move) {
  182. clear();
  183. data_ = move.data_; move.data_ = NULL;
  184. size_ = move.size_; move.size_ = 0;
  185. return *this;
  186. }
  187. /** C++11-only explicit cast */
  188. inline explicit operator std::string() const { return get_string(); }
  189. #endif
  190. };
  191. /** @brief Passed to constructors to avoid (conservative) initialization */
  192. struct NOINIT {};
  193. /**@cond internal*/
  194. /** Forward-declare sponge RNG object */
  195. class SpongeRng;
  196. /**@endcond*/
  197. /**
  198. * @brief Ed255-Goldilocks/Decaf instantiation of group.
  199. */
  200. struct Ed255 {
  201. /** @cond internal */
  202. class Point;
  203. class Precomputed;
  204. /** @endcond */
  205. /**
  206. * @brief A scalar modulo the curve order.
  207. * Supports the usual arithmetic operations, all in constant time.
  208. */
  209. class Scalar {
  210. public:
  211. /** @brief Size of a serialized element */
  212. static const size_t SER_BYTES = DECAF_255_SCALAR_BYTES;
  213. /** @brief access to the underlying scalar object */
  214. decaf_255_scalar_t s;
  215. /** @brief Don't initialize. */
  216. inline Scalar(const NOINIT &) NOEXCEPT {}
  217. /** @brief Set to an unsigned word */
  218. inline Scalar(const decaf_word_t w) NOEXCEPT { *this = w; }
  219. /** @brief Set to a signed word */
  220. inline Scalar(const int w) NOEXCEPT { *this = w; }
  221. /** @brief Construct from RNG */
  222. inline explicit Scalar(SpongeRng &rng) NOEXCEPT;
  223. /** @brief Construct from decaf_scalar_t object. */
  224. inline Scalar(const decaf_255_scalar_t &t = decaf_255_scalar_zero) NOEXCEPT { decaf_255_scalar_copy(s,t); }
  225. /** @brief Copy constructor. */
  226. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  227. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  228. inline Scalar(const Block &buffer) NOEXCEPT { *this = buffer; }
  229. /** @brief Assignment. */
  230. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_255_scalar_copy(s,x.s); return *this; }
  231. /** @brief Assign from unsigned word. */
  232. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_255_scalar_set(s,w); return *this; }
  233. /** @brief Assign from signed int. */
  234. inline Scalar& operator=(int w) NOEXCEPT {
  235. Scalar t(-(decaf_word_t)INT_MIN);
  236. decaf_255_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  237. *this -= t;
  238. return *this;
  239. }
  240. /** Destructor securely erases the scalar. */
  241. inline ~Scalar() NOEXCEPT { decaf_255_scalar_destroy(s); }
  242. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  243. inline Scalar &operator=(const Block &bl) NOEXCEPT {
  244. decaf_255_scalar_decode_long(s,bl.data(),bl.size()); return *this;
  245. }
  246. /**
  247. * @brief Decode from correct-length little-endian byte sequence.
  248. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  249. */
  250. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  251. Scalar &sc, const unsigned char buffer[SER_BYTES]
  252. ) NOEXCEPT {
  253. return decaf_255_scalar_decode(sc.s,buffer);
  254. }
  255. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  256. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  257. Scalar &sc, const Block &buffer
  258. ) NOEXCEPT {
  259. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  260. return decaf_255_scalar_decode(sc.s,buffer);
  261. }
  262. /** @brief Encode to fixed-length string */
  263. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  264. SecureBuffer buf(SER_BYTES); decaf_255_scalar_encode(buf,s); return buf;
  265. }
  266. /** @brief Encode to fixed-length buffer */
  267. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  268. decaf_255_scalar_encode(buffer, s);
  269. }
  270. /** Add. */
  271. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_add(r.s,s,q.s); return r; }
  272. /** Add to this. */
  273. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { decaf_255_scalar_add(s,s,q.s); return *this; }
  274. /** Subtract. */
  275. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,s,q.s); return r; }
  276. /** Subtract from this. */
  277. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { decaf_255_scalar_sub(s,s,q.s); return *this; }
  278. /** Multiply */
  279. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_mul(r.s,s,q.s); return r; }
  280. /** Multiply into this. */
  281. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { decaf_255_scalar_mul(s,s,q.s); return *this; }
  282. /** Negate */
  283. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); decaf_255_scalar_sub(r.s,decaf_255_scalar_zero,s); return r; }
  284. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  285. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_255_scalar_invert(r.s,s); return r; }
  286. /** @brief Divide by inverting q. If q == 0, return 0. */
  287. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  288. /** @brief Divide by inverting q. If q == 0, return 0. */
  289. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  290. /** @brief Compare in constant time */
  291. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  292. /** @brief Compare in constant time */
  293. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_255_scalar_eq(s,q.s); }
  294. /** @brief Scalarmul with scalar on left. */
  295. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  296. /** @brief Scalarmul-precomputed with scalar on left. */
  297. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  298. /** @brief Direct scalar multiplication. */
  299. inline SecureBuffer direct_scalarmul(
  300. const Block &in,
  301. decaf_bool_t allow_identity=DECAF_FALSE,
  302. decaf_bool_t short_circuit=DECAF_TRUE
  303. ) const throw(CryptoException) {
  304. SecureBuffer out(/*FIXME Point::*/SER_BYTES);
  305. if (!decaf_255_direct_scalarmul(out, in.data(), s, allow_identity, short_circuit))
  306. throw CryptoException();
  307. return out;
  308. }
  309. };
  310. /**
  311. * @brief Element of prime-order group.
  312. */
  313. class Point {
  314. public:
  315. /** @brief Size of a serialized element */
  316. static const size_t SER_BYTES = DECAF_255_SER_BYTES;
  317. /** @brief Size of a stegged element */
  318. static const size_t STEG_BYTES = DECAF_255_SER_BYTES + 8;
  319. /** @brief Bytes required for hash */
  320. static const size_t HASH_BYTES = DECAF_255_SER_BYTES;
  321. /** The c-level object. */
  322. decaf_255_point_t p;
  323. /** @brief Don't initialize. */
  324. inline Point(const NOINIT &) NOEXCEPT {}
  325. /** @brief Constructor sets to identity by default. */
  326. inline Point(const decaf_255_point_t &q = decaf_255_point_identity) NOEXCEPT { decaf_255_point_copy(p,q); }
  327. /** @brief Copy constructor. */
  328. inline Point(const Point &q) NOEXCEPT { decaf_255_point_copy(p,q.p); }
  329. /** @brief Assignment. */
  330. inline Point& operator=(const Point &q) NOEXCEPT { decaf_255_point_copy(p,q.p); return *this; }
  331. /** @brief Destructor securely erases the point. */
  332. inline ~Point() NOEXCEPT { decaf_255_point_destroy(p); }
  333. /** @brief Construct from RNG */
  334. inline explicit Point(SpongeRng &rng, bool uniform = true) NOEXCEPT;
  335. /**
  336. * @brief Initialize from C++ fixed-length byte string.
  337. * The all-zero string maps to the identity.
  338. *
  339. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  340. * or was the identity and allow_identity was DECAF_FALSE.
  341. */
  342. inline explicit Point(const Block &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  343. if (!decode(*this,s,allow_identity)) throw CryptoException();
  344. }
  345. /**
  346. * @brief Initialize from C fixed-length byte string.
  347. * The all-zero string maps to the identity.
  348. *
  349. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  350. * or was the identity and allow_identity was DECAF_FALSE.
  351. */
  352. inline explicit Point(const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  353. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  354. /**
  355. * @brief Initialize from C fixed-length byte string.
  356. * The all-zero string maps to the identity.
  357. *
  358. * @retval DECAF_SUCCESS the string was successfully decoded.
  359. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  360. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  361. */
  362. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  363. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  364. ) NOEXCEPT {
  365. return decaf_255_point_decode(p.p,buffer,allow_identity);
  366. }
  367. /**
  368. * @brief Initialize from C++ fixed-length byte string.
  369. * The all-zero string maps to the identity.
  370. *
  371. * @retval DECAF_SUCCESS the string was successfully decoded.
  372. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  373. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  374. */
  375. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  376. Point &p, const Block &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  377. ) NOEXCEPT {
  378. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  379. return decaf_255_point_decode(p.p,buffer.data(),allow_identity);
  380. }
  381. /**
  382. * @brief Map uniformly to the curve from a hash buffer.
  383. * The empty or all-zero string maps to the identity, as does the string "\x01".
  384. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  385. * but the buffer will be zero-padded on the right.
  386. */
  387. static inline Point from_hash ( const Block &s ) NOEXCEPT {
  388. Point p((NOINIT())); p.set_to_hash(s); return p;
  389. }
  390. /**
  391. * @brief Map to the curve from a hash buffer.
  392. * The empty or all-zero string maps to the identity, as does the string "\x01".
  393. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  394. * but the buffer will be zero-padded on the right.
  395. */
  396. inline unsigned char set_to_hash( const Block &s ) NOEXCEPT {
  397. if (s.size() < HASH_BYTES) {
  398. SecureBuffer b(HASH_BYTES);
  399. memcpy(b.data(), s.data(), s.size());
  400. return decaf_255_point_from_hash_nonuniform(p,b);
  401. } else if (s.size() == HASH_BYTES) {
  402. return decaf_255_point_from_hash_nonuniform(p,s);
  403. } else if (s.size() < 2*HASH_BYTES) {
  404. SecureBuffer b(2*HASH_BYTES);
  405. memcpy(b.data(), s.data(), s.size());
  406. return decaf_255_point_from_hash_uniform(p,b);
  407. } else {
  408. return decaf_255_point_from_hash_uniform(p,s);
  409. }
  410. }
  411. /**
  412. * @brief Encode to string. The identity encodes to the all-zero string.
  413. */
  414. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  415. SecureBuffer buffer(SER_BYTES);
  416. decaf_255_point_encode(buffer, p);
  417. return buffer;
  418. }
  419. /**
  420. * @brief Encode to a C buffer. The identity encodes to all zeros.
  421. */
  422. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT{
  423. decaf_255_point_encode(buffer, p);
  424. }
  425. /** @brief Point add. */
  426. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_add(r.p,p,q.p); return r; }
  427. /** @brief Point add. */
  428. inline Point &operator+=(const Point &q) NOEXCEPT { decaf_255_point_add(p,p,q.p); return *this; }
  429. /** @brief Point subtract. */
  430. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_sub(r.p,p,q.p); return r; }
  431. /** @brief Point subtract. */
  432. inline Point &operator-=(const Point &q) NOEXCEPT { decaf_255_point_sub(p,p,q.p); return *this; }
  433. /** @brief Point negate. */
  434. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); decaf_255_point_negate(r.p,p); return r; }
  435. /** @brief Double the point out of place. */
  436. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); decaf_255_point_double(r.p,p); return r; }
  437. /** @brief Double the point in place. */
  438. inline Point &double_in_place() NOEXCEPT { decaf_255_point_double(p,p); return *this; }
  439. /** @brief Constant-time compare. */
  440. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_255_point_eq(p,q.p); }
  441. /** @brief Constant-time compare. */
  442. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_255_point_eq(p,q.p); }
  443. /** @brief Scalar multiply. */
  444. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); decaf_255_point_scalarmul(r.p,p,s.s); return r; }
  445. /** @brief Scalar multiply in place. */
  446. inline Point &operator*=(const Scalar &s) NOEXCEPT { decaf_255_point_scalarmul(p,p,s.s); return *this; }
  447. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  448. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  449. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  450. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  451. /** @brief Validate / sanity check */
  452. inline bool validate() const NOEXCEPT { return !!decaf_255_point_valid(p); }
  453. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  454. static inline Point double_scalarmul (
  455. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  456. ) NOEXCEPT {
  457. Point p((NOINIT())); decaf_255_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  458. }
  459. /**
  460. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  461. * For those who like their scalars before the point.
  462. */
  463. static inline Point double_scalarmul (
  464. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  465. ) NOEXCEPT {
  466. Point p((NOINIT())); decaf_255_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  467. }
  468. /**
  469. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  470. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  471. * it doesn't).
  472. */
  473. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) NOEXCEPT {
  474. Point r((NOINIT())); decaf_255_base_double_scalarmul_non_secret(r.p,s_base.s,p,s.s); return r;
  475. }
  476. inline Point& debugging_torque_in_place() {
  477. decaf_255_point_debugging_torque(p,p);
  478. return *this;
  479. }
  480. inline bool invert_elligator (
  481. Buffer &buf, uint16_t hint
  482. ) const NOEXCEPT {
  483. unsigned char buf2[2*HASH_BYTES];
  484. memset(buf2,0,sizeof(buf2));
  485. memcpy(buf2,buf,(buf.size() > 2*HASH_BYTES) ? 2*HASH_BYTES : buf.size());
  486. decaf_bool_t ret;
  487. if (buf.size() > HASH_BYTES) {
  488. ret = decaf_255_invert_elligator_uniform(buf2, p, hint);
  489. } else {
  490. ret = decaf_255_invert_elligator_nonuniform(buf2, p, hint);
  491. }
  492. if (buf.size() < HASH_BYTES) {
  493. ret &= decaf_memeq(&buf2[buf.size()], &buf2[HASH_BYTES], HASH_BYTES - buf.size());
  494. }
  495. memcpy(buf,buf2,(buf.size() < HASH_BYTES) ? buf.size() : HASH_BYTES);
  496. decaf_bzero(buf2,sizeof(buf2));
  497. return !!ret;
  498. }
  499. /** @brief Steganographically encode this */
  500. inline SecureBuffer steg_encode(SpongeRng &rng) const NOEXCEPT;
  501. /** @brief Return the base point */
  502. static inline const Point base() NOEXCEPT { return Point(decaf_255_point_base); }
  503. /** @brief Return the identity point */
  504. static inline const Point identity() NOEXCEPT { return Point(decaf_255_point_identity); }
  505. };
  506. /**
  507. * @brief Precomputed table of points.
  508. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  509. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  510. * stack-allocate a 15kiB object anyway.
  511. */
  512. class Precomputed {
  513. private:
  514. /** @cond internal */
  515. union {
  516. decaf_255_precomputed_s *mine;
  517. const decaf_255_precomputed_s *yours;
  518. } ours;
  519. bool isMine;
  520. inline void clear() NOEXCEPT {
  521. if (isMine) {
  522. decaf_255_precomputed_destroy(ours.mine);
  523. free(ours.mine);
  524. ours.yours = decaf_255_precomputed_base;
  525. isMine = false;
  526. }
  527. }
  528. inline void alloc() throw(std::bad_alloc) {
  529. if (isMine) return;
  530. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_255_precomputed_s,sizeof_decaf_255_precomputed_s);
  531. if (ret || !ours.mine) {
  532. isMine = false;
  533. throw std::bad_alloc();
  534. }
  535. isMine = true;
  536. }
  537. inline const decaf_255_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  538. /** @endcond */
  539. public:
  540. /** Destructor securely erases the memory. */
  541. inline ~Precomputed() NOEXCEPT { clear(); }
  542. /**
  543. * @brief Initialize from underlying type, declared as a reference to prevent
  544. * it from being called with 0, thereby breaking override.
  545. *
  546. * The underlying object must remain valid throughout the lifetime of this one.
  547. *
  548. * By default, initializes to the table for the base point.
  549. *
  550. * @warning The empty initializer makes this equal to base, unlike the empty
  551. * initializer for points which makes this equal to the identity.
  552. */
  553. inline Precomputed(
  554. const decaf_255_precomputed_s &yours = *decaf_255_precomputed_base
  555. ) NOEXCEPT {
  556. ours.yours = &yours;
  557. isMine = false;
  558. }
  559. /**
  560. * @brief Assign. This may require an allocation and memcpy.
  561. */
  562. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  563. if (this == &it) return *this;
  564. if (it.isMine) {
  565. alloc();
  566. memcpy(ours.mine,it.ours.mine,sizeof_decaf_255_precomputed_s);
  567. } else {
  568. clear();
  569. ours.yours = it.ours.yours;
  570. }
  571. isMine = it.isMine;
  572. return *this;
  573. }
  574. /**
  575. * @brief Initilaize from point. Must allocate memory, and may throw.
  576. */
  577. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  578. alloc();
  579. decaf_255_precompute(ours.mine,it.p);
  580. return *this;
  581. }
  582. /**
  583. * @brief Copy constructor.
  584. */
  585. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  586. /**
  587. * @brief Constructor which initializes from point.
  588. */
  589. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  590. #if __cplusplus >= 201103L
  591. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  592. if (this == &it) return *this;
  593. clear();
  594. ours = it.ours;
  595. isMine = it.isMine;
  596. it.isMine = false;
  597. it.ours.yours = decaf_255_precomputed_base;
  598. return *this;
  599. }
  600. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  601. #endif
  602. /** @brief Fixed base scalarmul. */
  603. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_255_precomputed_scalarmul(r.p,get(),s.s); return r; }
  604. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  605. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  606. /** @brief Return the table for the base point. */
  607. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_255_precomputed_base); }
  608. };
  609. }; /* struct Decaf255 */
  610. #undef NOEXCEPT
  611. #undef EXPLICIT_CON
  612. #undef GET_DATA
  613. } /* namespace decaf */
  614. #endif /* __DECAF_255_HXX__ */