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.
 
 
 
 
 

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