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.
 
 
 
 
 

921 lines
34 KiB

  1. /**
  2. * @file decaf.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 Ed448-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_448_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_448_base_double_scalarmul_non_secret.
  21. */
  22. #ifndef __DECAF_448_HXX__
  23. #define __DECAF_448_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. typedef uint32_t GroupId;
  47. static const GroupId Ed448Goldilocks = 448;
  48. /** @brief An exception for when crypto (ie point decode) has failed. */
  49. class CryptoException : public std::exception {
  50. public:
  51. /** @return "CryptoException" */
  52. virtual const char * what() const NOEXCEPT { return "CryptoException"; }
  53. };
  54. /** @brief An exception for when crypto (ie point decode) has failed. */
  55. class LengthException : public std::exception {
  56. public:
  57. /** @return "CryptoException" */
  58. virtual const char * what() const NOEXCEPT { return "LengthException"; }
  59. };
  60. /**
  61. * Securely erase contents of memory.
  62. */
  63. static inline void really_bzero(void *data, size_t size) { decaf_bzero(data,size); }
  64. /** Block object */
  65. class Block {
  66. protected:
  67. unsigned char *data_;
  68. size_t size_;
  69. public:
  70. /** Empty init */
  71. inline Block() NOEXCEPT : data_(NULL), size_(0) {}
  72. /** Init from C string */
  73. inline Block(const char *data) NOEXCEPT : data_((unsigned char *)data), size_(strlen(data)) {}
  74. /** Unowned init */
  75. inline Block(const unsigned char *data, size_t size) NOEXCEPT : data_((unsigned char *)data), size_(size) {}
  76. /** Block from std::string */
  77. inline Block(const std::string &s) : data_((unsigned char *)GET_DATA(s)), size_(s.size()) {}
  78. /** Get const data */
  79. inline const unsigned char *data() const NOEXCEPT { return data_; }
  80. /** Get the size */
  81. inline size_t size() const NOEXCEPT { return size_; }
  82. /** Autocast to const unsigned char * */
  83. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  84. /** Convert to C++ string */
  85. inline std::string get_string() const {
  86. return std::string((const char *)data_,size_);
  87. }
  88. /** Slice the buffer*/
  89. inline Block slice(size_t off, size_t length) const throw(LengthException) {
  90. if (off > size() || length > size() - off)
  91. throw LengthException();
  92. return Block(data()+off, length);
  93. }
  94. /** Virtual destructor for SecureBlock. TODO: probably means vtable? Make bool? */
  95. inline virtual ~Block() {};
  96. };
  97. class TmpBuffer;
  98. class Buffer : public Block {
  99. public:
  100. /** Null init */
  101. inline Buffer() NOEXCEPT : Block() {}
  102. /** Unowned init */
  103. inline Buffer(unsigned char *data, size_t size) NOEXCEPT : Block(data,size) {}
  104. /** Get unconst data */
  105. inline unsigned char *data() NOEXCEPT { return data_; }
  106. /** Get const data */
  107. inline const unsigned char *data() const NOEXCEPT { return data_; }
  108. /** Autocast to const unsigned char * */
  109. inline operator const unsigned char*() const NOEXCEPT { return data_; }
  110. /** Autocast to unsigned char */
  111. inline operator unsigned char*() NOEXCEPT { return data_; }
  112. /** Slice the buffer*/
  113. inline TmpBuffer slice(size_t off, size_t length) throw(LengthException);
  114. };
  115. class TmpBuffer : public Buffer {
  116. public:
  117. /** Unowned init */
  118. inline TmpBuffer(unsigned char *data, size_t size) NOEXCEPT : Buffer(data,size) {}
  119. };
  120. TmpBuffer Buffer::slice(size_t off, size_t length) throw(LengthException) {
  121. if (off > size() || length > size() - off) throw LengthException();
  122. return TmpBuffer(data()+off, length);
  123. }
  124. /** A self-erasing block of data */
  125. class SecureBuffer : public Buffer {
  126. public:
  127. /** Null secure block */
  128. inline SecureBuffer() NOEXCEPT : Buffer() {}
  129. /** Construct empty from size */
  130. inline SecureBuffer(size_t size) {
  131. data_ = new unsigned char[size_ = size];
  132. memset(data_,0,size);
  133. }
  134. /** Construct from data */
  135. inline SecureBuffer(const unsigned char *data, size_t size){
  136. data_ = new unsigned char[size_ = size];
  137. memcpy(data_, data, size);
  138. }
  139. /** Copy constructor */
  140. inline SecureBuffer(const Block &copy) : Buffer() { *this = copy; }
  141. /** Copy-assign constructor */
  142. inline SecureBuffer& operator=(const Block &copy) throw(std::bad_alloc) {
  143. if (&copy == this) return *this;
  144. clear();
  145. data_ = new unsigned char[size_ = copy.size()];
  146. memcpy(data_,copy.data(),size_);
  147. return *this;
  148. }
  149. /** Copy-assign constructor */
  150. inline SecureBuffer& operator=(const SecureBuffer &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. /** Destructor erases data */
  158. ~SecureBuffer() NOEXCEPT { clear(); }
  159. /** Clear data */
  160. inline void clear() NOEXCEPT {
  161. if (data_ == NULL) return;
  162. really_bzero(data_,size_);
  163. delete[] data_;
  164. data_ = NULL;
  165. size_ = 0;
  166. }
  167. #if __cplusplus >= 201103L
  168. /** Move constructor */
  169. inline SecureBuffer(SecureBuffer &&move) { *this = move; }
  170. /** Move non-constructor */
  171. inline SecureBuffer(Block &&move) { *this = (Block &)move; }
  172. /** Move-assign constructor. TODO: check that this actually gets used.*/
  173. inline SecureBuffer& operator=(SecureBuffer &&move) {
  174. clear();
  175. data_ = move.data_; move.data_ = NULL;
  176. size_ = move.size_; move.size_ = 0;
  177. return *this;
  178. }
  179. /** C++11-only explicit cast */
  180. inline explicit operator std::string() const { return get_string(); }
  181. #endif
  182. };
  183. /** @brief Passed to constructors to avoid (conservative) initialization */
  184. struct NOINIT {};
  185. /**@cond internal*/
  186. /** Forward-declare sponge RNG object */
  187. class SpongeRng;
  188. /**@endcond*/
  189. template<GroupId group> struct WrappedTypes;
  190. /**
  191. * @brief Group with prime order.
  192. * @todo Move declarations of functions up here?
  193. */
  194. template<GroupId group = Ed448Goldilocks> struct EcGroup {
  195. /** @cond internal */
  196. class Point;
  197. class Precomputed;
  198. /** @endcond */
  199. /**
  200. * @brief A scalar modulo the curve order.
  201. * Supports the usual arithmetic operations, all in constant time.
  202. */
  203. class Scalar {
  204. private:
  205. /** @cond internal */
  206. /** @brief Wrapped C object */
  207. friend class Point;
  208. friend class Precomputed;
  209. typedef typename WrappedTypes<group>::Scalar Wrapped;
  210. static const Wrapped &ZERO, &ONE;
  211. static inline void add3(Wrapped, const Wrapped, const Wrapped) NOEXCEPT;
  212. static inline void setu(Wrapped, decaf_word_t) NOEXCEPT;
  213. static inline void sub3(Wrapped, const Wrapped, const Wrapped) NOEXCEPT;
  214. static inline void mul3(Wrapped, const Wrapped, const Wrapped) NOEXCEPT;
  215. static inline void dl3(Wrapped, const unsigned char *buffer, size_t size) NOEXCEPT;
  216. static inline decaf_word_t eq2(const Wrapped, const Wrapped) NOEXCEPT;
  217. static inline void assign2(Wrapped, const Wrapped) NOEXCEPT;
  218. static inline void inv2(Wrapped, const Wrapped) NOEXCEPT;
  219. /** @endcond */
  220. public:
  221. /** @brief Size of a serialized element */
  222. static const size_t SER_BYTES;
  223. /** @brief access to the Wrapped scalar object */
  224. Wrapped s;
  225. /** @brief Don't initialize. */
  226. inline Scalar(const NOINIT &) {}
  227. /** @brief Set to an unsigned word */
  228. inline Scalar(const decaf_word_t w = 0) NOEXCEPT { *this = w; }
  229. /** @brief Set to a signed word */
  230. inline Scalar(const int w) NOEXCEPT { *this = w; }
  231. /** @brief Construct from RNG */
  232. inline explicit Scalar(SpongeRng &rng) NOEXCEPT;
  233. /** @brief Construct from decaf_scalar_t object. */
  234. inline Scalar(const Wrapped &x) NOEXCEPT { *this = x; }
  235. /** @brief Copy constructor. */
  236. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  237. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  238. inline Scalar(const Block &buffer) NOEXCEPT { *this = buffer; }
  239. /** Destructor securely erases the scalar. */
  240. inline ~Scalar() NOEXCEPT;
  241. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  242. inline Scalar &operator=(const Block &bl) NOEXCEPT { dl3(s, bl, bl.size()); return *this; }
  243. /** @brief Assignment. */
  244. inline Scalar &operator=(const Scalar &t) NOEXCEPT { assign2(s, t.s); return *this; }
  245. /** @brief Assignment. */
  246. inline Scalar &operator=(decaf_word_t w) NOEXCEPT { setu(s, w); return *this; }
  247. /** @brief Assignment. */
  248. inline Scalar &operator=(int w) NOEXCEPT {
  249. Scalar t(-(decaf_word_t)INT_MIN);
  250. setu(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  251. *this -= t;
  252. return *this;
  253. }
  254. /**
  255. * @brief Decode from correct-length little-endian byte sequence.
  256. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  257. */
  258. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  259. Scalar &sc, const unsigned char buffer[/*SER_BYTES*/] // TODO
  260. ) NOEXCEPT;
  261. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  262. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  263. Scalar &sc, const Block &buffer
  264. ) NOEXCEPT {
  265. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  266. return decode(sc.s,(const unsigned char *)buffer);
  267. }
  268. /** @brief Encode to fixed-length buffer */
  269. inline void encode(unsigned char buffer[/*SER_BYTES*/]) const NOEXCEPT;
  270. /** @brief Encode to fixed-length string */
  271. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  272. SecureBuffer buf(SER_BYTES); encode((unsigned char *)buf,s); return buf;
  273. }
  274. public:
  275. /** Add. */
  276. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); add3(r.s,s,q.s); return r; }
  277. /** Add to this. */
  278. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { add3(s,s,q.s); return *this; }
  279. /** Subtract. */
  280. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); sub3(r.s,s,q.s); return r; }
  281. /** Subtract from this. */
  282. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { sub3(s,s,q.s); return *this; }
  283. /** Multiply */
  284. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); mul3(r.s,s,q.s); return r; }
  285. /** Multiply into this. */
  286. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { mul3(s,s,q.s); return *this; }
  287. /** Negate */
  288. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); sub3(r.s,ZERO,s); return r; }
  289. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  290. inline Scalar inverse() const NOEXCEPT { Scalar q((NOINIT())); inv2(q.s,s); return q; }
  291. /** @brief Divide by inverting q. If q == 0, return 0. */
  292. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  293. /** @brief Divide by inverting q. If q == 0, return 0. */
  294. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  295. /** @brief Compare in constant time */
  296. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!eq2(s,q.s); }
  297. /** @brief Compare in constant time */
  298. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  299. /** @brief Scalarmul with scalar on left. */
  300. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  301. /** @brief Scalarmul-precomputed with scalar on left. */
  302. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  303. /** @brief Direct scalar multiplication. */
  304. inline SecureBuffer direct_scalarmul(
  305. const Block &in,
  306. decaf_bool_t allow_identity=DECAF_FALSE,
  307. decaf_bool_t short_circuit=DECAF_TRUE
  308. ) const throw(CryptoException);
  309. };
  310. /**
  311. * @brief Element of prime-order group.
  312. */
  313. class Point {
  314. private:
  315. /** @cond internal */
  316. typedef typename WrappedTypes<group>::Point Wrapped;
  317. friend class Scalar;
  318. friend class Precomputed;
  319. static const Wrapped &IDENTITY, &GENERATOR;
  320. static inline void add3(Wrapped, const Wrapped, const Wrapped) NOEXCEPT;
  321. static inline void sub3(Wrapped, const Wrapped, const Wrapped) NOEXCEPT;
  322. static inline void dbl2(Wrapped, const Wrapped) NOEXCEPT;
  323. static inline void neg2(Wrapped, const Wrapped) NOEXCEPT;
  324. static inline decaf_word_t eq2(const Wrapped, const Wrapped) NOEXCEPT;
  325. static inline void assign2(Wrapped, const Wrapped) NOEXCEPT;
  326. static inline void sm3(Wrapped, const Wrapped, const typename Scalar::Wrapped) NOEXCEPT;
  327. static inline void dsm5(
  328. Wrapped,
  329. const Wrapped, const typename Scalar::Wrapped,
  330. const Wrapped, const typename Scalar::Wrapped
  331. ) NOEXCEPT;
  332. static inline void dsmns(
  333. Wrapped,
  334. const typename Scalar::Wrapped,
  335. const Wrapped, const typename Scalar::Wrapped
  336. ) NOEXCEPT;
  337. /** @endcond */
  338. public:
  339. /** @brief Size of a serialized element */
  340. static const size_t SER_BYTES;
  341. /** @brief Bytes required for hash */
  342. static const size_t HASH_BYTES;
  343. /** The c-level object. */
  344. Wrapped p;
  345. /** @brief Don't initialize. */
  346. inline Point(const NOINIT &) {}
  347. /** @brief Constructor sets to identity by default. */
  348. inline Point(const decaf_448_point_t &q = IDENTITY) { *this = q; }
  349. /** @brief Copy constructor. */
  350. inline Point(const Point &q) { *this = q; }
  351. /** @brief Assignment. */
  352. inline Point& operator=(const Point &q) NOEXCEPT { assign2(p,q.p); return *this; }
  353. /** @brief Assignment from wrapped. */
  354. inline Point& operator=(const Wrapped &q) NOEXCEPT { assign2(p,q); return *this; }
  355. /** @brief Destructor securely erases the point. */
  356. inline ~Point() NOEXCEPT;
  357. /** @brief Construct from RNG */
  358. inline explicit Point(SpongeRng &rng, bool uniform = true) NOEXCEPT;
  359. /**
  360. * @brief Initialize from C++ fixed-length byte string.
  361. * The all-zero string maps to the identity.
  362. *
  363. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  364. * or was the identity and allow_identity was DECAF_FALSE.
  365. */
  366. inline explicit Point(const Block &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  367. if (!decode(*this,s,allow_identity)) throw CryptoException();
  368. }
  369. /**
  370. * @brief Initialize from C fixed-length byte string.
  371. * The all-zero string maps to the identity.
  372. *
  373. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  374. * or was the identity and allow_identity was DECAF_FALSE.
  375. */
  376. inline explicit Point(const unsigned char buffer[/*SER_BYTES*/], decaf_bool_t allow_identity=DECAF_TRUE)
  377. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  378. /**
  379. * @brief Initialize from C fixed-length byte string.
  380. * The all-zero string maps to the identity.
  381. *
  382. * @retval DECAF_SUCCESS the string was successfully decoded.
  383. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  384. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  385. */
  386. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  387. Point &p, const unsigned char buffer[/*SER_BYTES*/], decaf_bool_t allow_identity=DECAF_TRUE
  388. ) NOEXCEPT;
  389. /**
  390. * @brief Initialize from C++ fixed-length byte string.
  391. * The all-zero string maps to the identity.
  392. *
  393. * @retval DECAF_SUCCESS the string was successfully decoded.
  394. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  395. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  396. */
  397. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  398. Point &p, const Block &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  399. ) NOEXCEPT {
  400. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  401. return decode(p,buffer.data(),allow_identity);
  402. }
  403. /**
  404. * @brief Map uniformly to the curve from a hash buffer.
  405. * The empty or all-zero string maps to the identity, as does the string "\x01".
  406. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  407. * but the buffer will be zero-padded on the right.
  408. */
  409. static inline Point from_hash ( const Block &s ) NOEXCEPT {
  410. Point p((NOINIT())); p.set_to_hash(s); return p;
  411. }
  412. /**
  413. * @brief Map to the curve from a hash buffer.
  414. * The empty or all-zero string maps to the identity, as does the string "\x01".
  415. * If the buffer is shorter than 2*HASH_BYTES, well, it won't be as uniform,
  416. * but the buffer will be zero-padded on the right.
  417. */
  418. inline void set_to_hash( const Block &s ) NOEXCEPT;
  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_448_point_encode(buffer, p);
  424. }
  425. /**
  426. * @brief Encode to string. The identity encodes to the all-zero string.
  427. */
  428. inline operator SecureBuffer() const NOEXCEPT {
  429. SecureBuffer buffer(SER_BYTES); encode(buffer.data()); return buffer;
  430. }
  431. /** @brief Point add. */
  432. inline Point operator+ (const Point &q) const NOEXCEPT { Point r((NOINIT())); add3(r.p,p,q.p); return r; }
  433. /** @brief Point add. */
  434. inline Point &operator+=(const Point &q) NOEXCEPT { add3(p,p,q.p); return *this; }
  435. /** @brief Point subtract. */
  436. inline Point operator- (const Point &q) const NOEXCEPT { Point r((NOINIT())); sub3(r.p,p,q.p); return r; }
  437. /** @brief Point subtract. */
  438. inline Point &operator-=(const Point &q) NOEXCEPT { sub3(p,p,q.p); return *this; }
  439. /** @brief Point negate. */
  440. inline Point operator- () const NOEXCEPT { Point r((NOINIT())); neg2(r.p,p); return r; }
  441. /** @brief Double the point out of place. */
  442. inline Point times_two () const NOEXCEPT { Point r((NOINIT())); dbl2(r.p,p); return r; }
  443. /** @brief Double the point in place. */
  444. inline Point &double_in_place() NOEXCEPT { dbl2(p,p); return *this; }
  445. /** @brief Constant-time compare. */
  446. inline bool operator!=(const Point &q) const NOEXCEPT { return !eq2(p,q.p); }
  447. /** @brief Constant-time compare. */
  448. inline bool operator==(const Point &q) const NOEXCEPT { return !!eq2(p,q.p); }
  449. /** @brief Scalar multiply. */
  450. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r((NOINIT())); sm3(r.p,p,s.s); return r; }
  451. /** @brief Scalar multiply in place. */
  452. inline Point &operator*=(const Scalar &s) NOEXCEPT { sm3(p,p,s.s); return *this; }
  453. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  454. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  455. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  456. inline Point &operator/=(const Scalar &s) NOEXCEPT { return (*this) *= s.inverse(); }
  457. /** @brief Validate / sanity check */
  458. inline bool validate() const NOEXCEPT;
  459. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  460. static inline Point double_scalarmul (
  461. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  462. ) NOEXCEPT {
  463. Point p((NOINIT())); dsm5(p.p,q.p,qs.s,r.p,rs.s); return p;
  464. }
  465. /**
  466. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  467. * For those who like their scalars before the point.
  468. */
  469. static inline Point double_scalarmul (
  470. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  471. ) NOEXCEPT {
  472. Point p((NOINIT())); dsm5(p.p,q.p,qs.s,r.p,rs.s); return p;
  473. }
  474. /**
  475. * @brief Double-scalar multiply: this point by the first scalar and base by the second scalar.
  476. * @warning This function takes variable time, and may leak the scalars (or points, but currently
  477. * it doesn't).
  478. */
  479. inline Point non_secret_combo_with_base(const Scalar &s, const Scalar &s_base) {
  480. Point r((NOINIT())); dsmns(r.p,s_base.s,p,s.s); return r;
  481. }
  482. /** @brief Return the base point */
  483. static inline const Point base() NOEXCEPT { return GENERATOR; }
  484. /** @brief Return the identity point */
  485. static inline const Point identity() NOEXCEPT { return IDENTITY; }
  486. };
  487. /**
  488. * @brief Precomputed table of points.
  489. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  490. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  491. * stack-allocate a 15kiB object anyway.
  492. */
  493. class Precomputed {
  494. private:
  495. /** @cond internal */
  496. static const size_t sizeof_this, alignof_this;
  497. typedef typename WrappedTypes<group>::Precomputed Wrapped;
  498. static const Wrapped *GENERATOR;
  499. static inline void destroy(Wrapped*) NOEXCEPT;
  500. static inline void precompute(Wrapped*, const typename Point::Wrapped) NOEXCEPT;
  501. static inline void psmul3(typename Point::Wrapped, const Wrapped*, const typename Scalar::Wrapped) NOEXCEPT;
  502. union {
  503. Wrapped *mine;
  504. const Wrapped *yours;
  505. } ours;
  506. bool isMine;
  507. inline void clear() NOEXCEPT {
  508. if (isMine) {
  509. destroy(ours.mine);
  510. free(ours.mine);
  511. ours.yours = GENERATOR;
  512. isMine = false;
  513. }
  514. }
  515. inline void alloc() throw(std::bad_alloc) {
  516. if (isMine) return;
  517. int ret = posix_memalign((void**)&ours.mine, alignof_this,sizeof_this);
  518. if (ret || !ours.mine) {
  519. isMine = false;
  520. throw std::bad_alloc();
  521. }
  522. isMine = true;
  523. }
  524. inline const Wrapped *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  525. /** @endcond */
  526. public:
  527. /** Destructor securely erases the memory. */
  528. inline ~Precomputed() NOEXCEPT { clear(); }
  529. /**
  530. * @brief Initialize from Wrapped type, declared as a reference to prevent
  531. * it from being called with 0, thereby breaking override.
  532. *
  533. * The Wrapped object must remain valid throughout the lifetime of this one.
  534. *
  535. * By default, initializes to the table for the base point.
  536. *
  537. * @warning The empty initializer makes this equal to base, unlike the empty
  538. * initializer for points which makes this equal to the identity.
  539. */
  540. inline Precomputed(
  541. const Wrapped &yours = *GENERATOR
  542. ) NOEXCEPT {
  543. ours.yours = &yours;
  544. isMine = false;
  545. }
  546. /**
  547. * @brief Assign. This may require an allocation and memcpy.
  548. */
  549. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  550. if (this == &it) return *this;
  551. if (it.isMine) {
  552. alloc();
  553. memcpy(ours.mine,it.ours.mine,sizeof_this);
  554. } else {
  555. clear();
  556. ours.yours = it.ours.yours;
  557. }
  558. isMine = it.isMine;
  559. return *this;
  560. }
  561. /**
  562. * @brief Initilaize from point. Must allocate memory, and may throw.
  563. */
  564. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  565. alloc(); precompute(ours.mine,it.p); return *this;
  566. }
  567. /**
  568. * @brief Copy constructor.
  569. */
  570. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  571. /**
  572. * @brief Constructor which initializes from point.
  573. */
  574. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  575. #if __cplusplus >= 201103L
  576. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  577. if (this == &it) return *this;
  578. clear();
  579. ours = it.ours;
  580. isMine = it.isMine;
  581. it.isMine = false;
  582. it.ours.yours = base;
  583. return *this;
  584. }
  585. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  586. #endif
  587. /** @brief Fixed base scalarmul. */
  588. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; psmul3(r.p,get(),s.s); return r; }
  589. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  590. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  591. /** @brief Return the table for the base point. */
  592. static inline const Precomputed base() NOEXCEPT { return Precomputed(*GENERATOR); }
  593. };
  594. };
  595. /***************************************************************/
  596. /* Instantiation */
  597. /***************************************************************/
  598. /** @cond internal */
  599. template<> struct WrappedTypes<Ed448Goldilocks> {
  600. typedef decaf_448_point_t Point;
  601. typedef decaf_448_scalar_t Scalar;
  602. typedef decaf_448_precomputed_s Precomputed;
  603. };
  604. /* Scalar instantiation */
  605. template<> const size_t EcGroup<Ed448Goldilocks>::Scalar::SER_BYTES = 56;
  606. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::add3(
  607. Wrapped a, const Wrapped b, const Wrapped c
  608. ) NOEXCEPT { decaf_448_scalar_add(a,b,c); }
  609. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::sub3(
  610. Wrapped a, const Wrapped b, const Wrapped c
  611. ) NOEXCEPT { decaf_448_scalar_sub(a,b,c); }
  612. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::mul3(
  613. Wrapped a, const Wrapped b, const Wrapped c
  614. ) NOEXCEPT { decaf_448_scalar_mul(a,b,c); }
  615. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::dl3(
  616. Wrapped a, const unsigned char *b, size_t c
  617. ) NOEXCEPT { decaf_448_scalar_decode_long(a,b,c); }
  618. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::assign2(
  619. Wrapped a, const Wrapped b
  620. ) NOEXCEPT { decaf_448_scalar_copy(a,b); }
  621. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::setu(
  622. Wrapped a, decaf_word_t w
  623. ) NOEXCEPT { decaf_448_scalar_set(a,w); }
  624. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::inv2(
  625. Wrapped a, const Wrapped b
  626. ) NOEXCEPT { decaf_448_scalar_invert(a,b); }
  627. template<> inline decaf_word_t EcGroup<Ed448Goldilocks>::Scalar::eq2(
  628. const Wrapped a, const Wrapped b
  629. ) NOEXCEPT { return decaf_448_scalar_eq(a,b); }
  630. /* CLASSY */
  631. template<> inline SecureBuffer EcGroup<Ed448Goldilocks>::Scalar::direct_scalarmul(
  632. const Block &in, decaf_bool_t allow_identity, decaf_bool_t short_circuit
  633. ) const throw(CryptoException) {
  634. SecureBuffer out(SER_BYTES);
  635. if (!decaf_448_direct_scalarmul(out, in.data(), s, allow_identity, short_circuit))
  636. throw CryptoException();
  637. return out;
  638. }
  639. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::encode(
  640. unsigned char buffer[SER_BYTES]
  641. ) const NOEXCEPT {
  642. decaf_448_scalar_encode(buffer,s);
  643. }
  644. template<> inline decaf_bool_t __attribute__((warn_unused_result))
  645. EcGroup<Ed448Goldilocks>::Scalar::decode (
  646. Scalar &s, const unsigned char buffer[SER_BYTES]
  647. ) NOEXCEPT {
  648. return decaf_448_scalar_decode(s.s,buffer);
  649. }
  650. /* CLASSY */
  651. template<> inline EcGroup<Ed448Goldilocks>::Scalar::~Scalar() NOEXCEPT { decaf_448_scalar_destroy(s); }
  652. template<> const EcGroup<Ed448Goldilocks>::Scalar::Wrapped&
  653. EcGroup<Ed448Goldilocks>::Scalar::ZERO = decaf_448_scalar_zero;
  654. template<> const EcGroup<Ed448Goldilocks>::Scalar::Wrapped&
  655. EcGroup<Ed448Goldilocks>::Scalar::ONE = decaf_448_scalar_one;
  656. /* Point instantiation */
  657. template<> const size_t EcGroup<Ed448Goldilocks>::Point::SER_BYTES = 56;
  658. template<> const size_t EcGroup<Ed448Goldilocks>::Point::HASH_BYTES = 56;
  659. /* CLASSY */
  660. template<> inline EcGroup<Ed448Goldilocks>::Point::~Point() NOEXCEPT { decaf_448_point_destroy(p); }
  661. template<> inline void EcGroup<Ed448Goldilocks>::Point::add3(
  662. Wrapped a, const Wrapped b, const Wrapped c
  663. ) NOEXCEPT { decaf_448_point_add(a,b,c); }
  664. template<> inline void EcGroup<Ed448Goldilocks>::Point::sub3(
  665. Wrapped a, const Wrapped b, const Wrapped c
  666. ) NOEXCEPT { decaf_448_point_sub(a,b,c); }
  667. template<> inline void EcGroup<Ed448Goldilocks>::Point::assign2(
  668. Wrapped a, const Wrapped b
  669. ) NOEXCEPT { decaf_448_point_copy(a,b); }
  670. template<> inline void EcGroup<Ed448Goldilocks>::Point::dbl2(
  671. Wrapped a, const Wrapped b
  672. ) NOEXCEPT { decaf_448_point_double(a,b); }
  673. template<> inline decaf_word_t EcGroup<Ed448Goldilocks>::Point::eq2(
  674. const Wrapped a, const Wrapped b
  675. ) NOEXCEPT { return decaf_448_point_eq(a,b); }
  676. /* CLASSY */
  677. template<> inline bool EcGroup<Ed448Goldilocks>::Point::validate() const NOEXCEPT { return !!decaf_448_point_valid(p); }
  678. template<> inline void EcGroup<Ed448Goldilocks>::Point::sm3(
  679. Wrapped a, const Wrapped b, const decaf_448_scalar_t c
  680. ) NOEXCEPT { decaf_448_point_scalarmul(a,b,c); }
  681. template<> inline void EcGroup<Ed448Goldilocks>::Point::dsm5(
  682. Wrapped a, const Wrapped b, const decaf_448_scalar_t c, const Wrapped d, const decaf_448_scalar_t e
  683. ) NOEXCEPT { decaf_448_point_double_scalarmul(a,b,c,d,e); }
  684. template<> inline void EcGroup<Ed448Goldilocks>::Point::dsmns(
  685. Wrapped a, const decaf_448_scalar_t b, const Wrapped c, const decaf_448_scalar_t d
  686. ) NOEXCEPT { decaf_448_base_double_scalarmul_non_secret(a,b,c,d); }
  687. /* CLASSY */
  688. template<> inline decaf_bool_t __attribute__((warn_unused_result))
  689. EcGroup<Ed448Goldilocks>::Point::decode (
  690. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity
  691. ) NOEXCEPT {
  692. return decaf_448_point_decode(p.p,buffer,allow_identity);
  693. }
  694. /* CLASSY */
  695. template<> inline void EcGroup<Ed448Goldilocks>::Point::set_to_hash( const Block &s ) NOEXCEPT {
  696. if (s.size() < HASH_BYTES) {
  697. SecureBuffer b(HASH_BYTES);
  698. memcpy(b.data(), s.data(), s.size());
  699. decaf_448_point_from_hash_nonuniform(p,b);
  700. } else if (s.size() == HASH_BYTES) {
  701. decaf_448_point_from_hash_nonuniform(p,s);
  702. } else if (s.size() < 2*HASH_BYTES) {
  703. SecureBuffer b(2*HASH_BYTES);
  704. memcpy(b.data(), s.data(), s.size());
  705. decaf_448_point_from_hash_uniform(p,b);
  706. } else {
  707. decaf_448_point_from_hash_uniform(p,s);
  708. }
  709. }
  710. /* CLASSY */
  711. template<> inline void EcGroup<Ed448Goldilocks>::Point::encode(
  712. unsigned char buffer[SER_BYTES]
  713. ) const NOEXCEPT {
  714. decaf_448_point_encode(buffer,p);
  715. }
  716. template<> const EcGroup<Ed448Goldilocks>::Point::Wrapped&
  717. EcGroup<Ed448Goldilocks>::Point::IDENTITY = decaf_448_point_identity;
  718. template<> const EcGroup<Ed448Goldilocks>::Point::Wrapped&
  719. EcGroup<Ed448Goldilocks>::Point::GENERATOR = decaf_448_point_base;
  720. /* Precomputed instantiation */
  721. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::destroy(
  722. Wrapped *doomed
  723. ) NOEXCEPT {
  724. decaf_448_precomputed_destroy(doomed);
  725. }
  726. /* Precomputed instantiation */
  727. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::precompute(
  728. Wrapped *pre, const decaf_448_point_t point
  729. ) NOEXCEPT {
  730. decaf_448_precompute(pre,point);
  731. }
  732. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::psmul3(
  733. decaf_448_point_t out, const Wrapped *pre, const decaf_448_scalar_t sc
  734. ) NOEXCEPT {
  735. decaf_448_precomputed_scalarmul(out,pre,sc);
  736. }
  737. template<> const size_t EcGroup<Ed448Goldilocks>::Precomputed:: sizeof_this = sizeof_decaf_448_precomputed_s;
  738. template<> const size_t EcGroup<Ed448Goldilocks>::Precomputed::alignof_this = alignof_decaf_448_precomputed_s;
  739. template<> const EcGroup<Ed448Goldilocks>::Precomputed::Wrapped*
  740. EcGroup<Ed448Goldilocks>::Precomputed::GENERATOR = decaf_448_precomputed_base;
  741. /** @endcond */
  742. #undef NOEXCEPT
  743. #undef EXPLICIT_CON
  744. #undef GET_DATA
  745. } /* namespace decaf */
  746. #endif /* __DECAF_448_HXX__ */