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.
 
 
 
 
 

925 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 inline const Wrapped &ZERO() NOEXCEPT;
  211. static inline const Wrapped &ONE() NOEXCEPT;
  212. static inline void add3(Wrapped&, const Wrapped&, const Wrapped&) NOEXCEPT;
  213. static inline void setu(Wrapped&, decaf_word_t) NOEXCEPT;
  214. static inline void sub3(Wrapped&, const Wrapped&, const Wrapped&) NOEXCEPT;
  215. static inline void mul3(Wrapped&, const Wrapped&, const Wrapped&) NOEXCEPT;
  216. static inline void dl3(Wrapped&, const unsigned char *buffer, size_t size) NOEXCEPT;
  217. static inline decaf_word_t eq2(const Wrapped&, const Wrapped&) NOEXCEPT;
  218. static inline void assign2(Wrapped&, const Wrapped&) NOEXCEPT;
  219. static inline void inv2(Wrapped&, const Wrapped&) NOEXCEPT;
  220. /** @endcond */
  221. public:
  222. /** @brief Size of a serialized element */
  223. static const size_t SER_BYTES = WrappedTypes<group>::SCALAR_SER_BYTES;
  224. /** @brief access to the Wrapped scalar object */
  225. Wrapped s;
  226. /** @brief Don't initialize. */
  227. inline Scalar(const NOINIT &) {}
  228. /** @brief Set to an unsigned word */
  229. inline Scalar(const decaf_word_t w = 0) NOEXCEPT { *this = w; }
  230. /** @brief Set to a signed word */
  231. inline Scalar(const int w) NOEXCEPT { *this = w; }
  232. /** @brief Construct from RNG */
  233. inline explicit Scalar(SpongeRng &rng) NOEXCEPT;
  234. /** @brief Construct from decaf_scalar_t object. */
  235. inline Scalar(const Wrapped &x) NOEXCEPT { *this = x; }
  236. /** @brief Copy constructor. */
  237. inline Scalar(const Scalar &x) NOEXCEPT { *this = x; }
  238. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  239. inline Scalar(const Block &buffer) NOEXCEPT { *this = buffer; }
  240. /** Destructor securely erases the scalar. */
  241. inline ~Scalar() NOEXCEPT;
  242. /** @brief Assign from arbitrary-length little-endian byte sequence in a Block. */
  243. inline Scalar &operator=(const Block &bl) NOEXCEPT { dl3(s, bl, bl.size()); return *this; }
  244. /** @brief Assignment. */
  245. inline Scalar &operator=(const Scalar &t) NOEXCEPT { assign2(s, t.s); return *this; }
  246. /** @brief Assignment. */
  247. inline Scalar &operator=(decaf_word_t w) NOEXCEPT { setu(s, w); return *this; }
  248. /** @brief Assignment. */
  249. inline Scalar &operator=(int w) NOEXCEPT {
  250. Scalar t(-(decaf_word_t)INT_MIN);
  251. setu(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  252. *this -= t;
  253. return *this;
  254. }
  255. /**
  256. * @brief Decode from correct-length little-endian byte sequence.
  257. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  258. */
  259. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  260. Scalar &sc, const unsigned char buffer[SER_BYTES]
  261. ) NOEXCEPT;
  262. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  263. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  264. Scalar &sc, const Block &buffer
  265. ) NOEXCEPT {
  266. if (buffer.size() != SER_BYTES) return DECAF_FAILURE;
  267. return decode(sc.s,(const unsigned char *)buffer);
  268. }
  269. /** @brief Encode to fixed-length buffer */
  270. inline void encode(unsigned char buffer[SER_BYTES]) const NOEXCEPT;
  271. /** @brief Encode to fixed-length string */
  272. inline EXPLICIT_CON operator SecureBuffer() const NOEXCEPT {
  273. SecureBuffer buf(SER_BYTES); encode((unsigned char *)buf,s); return buf;
  274. }
  275. public:
  276. /** Add. */
  277. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); add3(r.s,s,q.s); return r; }
  278. /** Add to this. */
  279. inline Scalar &operator+=(const Scalar &q) NOEXCEPT { add3(s,s,q.s); return *this; }
  280. /** Subtract. */
  281. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); sub3(r.s,s,q.s); return r; }
  282. /** Subtract from this. */
  283. inline Scalar &operator-=(const Scalar &q) NOEXCEPT { sub3(s,s,q.s); return *this; }
  284. /** Multiply */
  285. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r((NOINIT())); mul3(r.s,s,q.s); return r; }
  286. /** Multiply into this. */
  287. inline Scalar &operator*=(const Scalar &q) NOEXCEPT { mul3(s,s,q.s); return *this; }
  288. /** Negate */
  289. inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); sub3(r.s,ZERO(),s); return r; }
  290. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  291. inline Scalar inverse() const NOEXCEPT { Scalar q((NOINIT())); inv2(q.s,s); return q; }
  292. /** @brief Divide by inverting q. If q == 0, return 0. */
  293. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { return *this * q.inverse(); }
  294. /** @brief Divide by inverting q. If q == 0, return 0. */
  295. inline Scalar &operator/=(const Scalar &q) NOEXCEPT { return *this *= q.inverse(); }
  296. /** @brief Compare in constant time */
  297. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!eq2(s,q.s); }
  298. /** @brief Compare in constant time */
  299. inline bool operator!=(const Scalar &q) const NOEXCEPT { return !(*this == q); }
  300. /** @brief Scalarmul with scalar on left. */
  301. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  302. /** @brief Scalarmul-precomputed with scalar on left. */
  303. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  304. /** @brief Direct scalar multiplication. */
  305. inline SecureBuffer direct_scalarmul(
  306. const Block &in,
  307. decaf_bool_t allow_identity=DECAF_FALSE,
  308. decaf_bool_t short_circuit=DECAF_TRUE
  309. ) const throw(CryptoException);
  310. };
  311. /**
  312. * @brief Element of prime-order group.
  313. */
  314. class Point {
  315. private:
  316. /** @cond internal */
  317. typedef typename WrappedTypes<group>::Point Wrapped;
  318. friend class Scalar;
  319. friend class Precomputed;
  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 = WrappedTypes<group>::POINT_SER_BYTES;
  341. /** @brief Bytes required for hash */
  342. static const size_t HASH_BYTES = WrappedTypes<group>::POINT_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_s &q) { *this = q; }
  349. /** @brief Copy constructor. */
  350. inline Point(const Point &q = identity()) { *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 Point base() NOEXCEPT;
  484. /** @brief Return the identity point */
  485. static inline Point identity() NOEXCEPT;
  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 inline size_t sizeof_this() NOEXCEPT;
  497. static inline size_t alignof_this() NOEXCEPT;
  498. typedef typename WrappedTypes<group>::Precomputed Wrapped;
  499. static inline const Wrapped *GENERATOR() NOEXCEPT;
  500. static inline void destroy(Wrapped*) NOEXCEPT;
  501. static inline void precompute(Wrapped*, const typename Point::Wrapped&) NOEXCEPT;
  502. static inline void psmul3(typename Point::Wrapped&, const Wrapped*, const typename Scalar::Wrapped&) NOEXCEPT;
  503. union {
  504. Wrapped *mine;
  505. const Wrapped *yours;
  506. } ours;
  507. bool isMine;
  508. inline void clear() NOEXCEPT {
  509. if (isMine) {
  510. destroy(ours.mine);
  511. free(ours.mine);
  512. ours.yours = GENERATOR();
  513. isMine = false;
  514. }
  515. }
  516. inline void alloc() throw(std::bad_alloc) {
  517. if (isMine) return;
  518. int ret = posix_memalign((void**)&ours.mine, alignof_this(),sizeof_this());
  519. if (ret || !ours.mine) {
  520. isMine = false;
  521. throw std::bad_alloc();
  522. }
  523. isMine = true;
  524. }
  525. inline const Wrapped *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  526. /** @endcond */
  527. public:
  528. /** Destructor securely erases the memory. */
  529. inline ~Precomputed() NOEXCEPT { clear(); }
  530. /**
  531. * @brief Initialize from Wrapped type, declared as a reference to prevent
  532. * it from being called with 0, thereby breaking override.
  533. *
  534. * The Wrapped object must remain valid throughout the lifetime of this one.
  535. *
  536. * By default, initializes to the table for the base point.
  537. *
  538. * @warning The empty initializer makes this equal to base, unlike the empty
  539. * initializer for points which makes this equal to the identity.
  540. */
  541. inline Precomputed(
  542. const Wrapped &yours = *GENERATOR()
  543. ) NOEXCEPT {
  544. ours.yours = &yours;
  545. isMine = false;
  546. }
  547. /**
  548. * @brief Assign. This may require an allocation and memcpy.
  549. */
  550. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  551. if (this == &it) return *this;
  552. if (it.isMine) {
  553. alloc();
  554. memcpy(ours.mine,it.ours.mine,sizeof_this());
  555. } else {
  556. clear();
  557. ours.yours = it.ours.yours;
  558. }
  559. isMine = it.isMine;
  560. return *this;
  561. }
  562. /**
  563. * @brief Initilaize from point. Must allocate memory, and may throw.
  564. */
  565. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  566. alloc(); precompute(ours.mine,it.p); return *this;
  567. }
  568. /**
  569. * @brief Copy constructor.
  570. */
  571. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  572. /**
  573. * @brief Constructor which initializes from point.
  574. */
  575. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  576. #if __cplusplus >= 201103L
  577. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  578. if (this == &it) return *this;
  579. clear();
  580. ours = it.ours;
  581. isMine = it.isMine;
  582. it.isMine = false;
  583. it.ours.yours = base;
  584. return *this;
  585. }
  586. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  587. #endif
  588. /** @brief Fixed base scalarmul. */
  589. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; psmul3(r.p,get(),s.s); return r; }
  590. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  591. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  592. /** @brief Return the table for the base point. */
  593. static inline const Precomputed base() NOEXCEPT { return Precomputed(*GENERATOR()); }
  594. };
  595. };
  596. /***************************************************************/
  597. /* Instantiation */
  598. /***************************************************************/
  599. /** @cond internal */
  600. template<> struct WrappedTypes<Ed448Goldilocks> {
  601. typedef decaf_448_point_s Point;
  602. typedef decaf_448_scalar_s Scalar;
  603. typedef decaf_448_precomputed_s Precomputed;
  604. static const size_t SCALAR_SER_BYTES = 56;
  605. static const size_t POINT_SER_BYTES = 56;
  606. static const size_t POINT_HASH_BYTES = 56;
  607. };
  608. /* Scalar instantiation */
  609. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::add3(
  610. Wrapped& a, const Wrapped& b, const Wrapped& c
  611. ) NOEXCEPT { decaf_448_scalar_add(&a,&b,&c); }
  612. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::sub3(
  613. Wrapped& a, const Wrapped& b, const Wrapped& c
  614. ) NOEXCEPT { decaf_448_scalar_sub(&a,&b,&c); }
  615. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::mul3(
  616. Wrapped& a, const Wrapped& b, const Wrapped& c
  617. ) NOEXCEPT { decaf_448_scalar_mul(&a,&b,&c); }
  618. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::dl3(
  619. Wrapped& a, const unsigned char *b, size_t c
  620. ) NOEXCEPT { decaf_448_scalar_decode_long(&a,b,c); }
  621. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::assign2(
  622. Wrapped& a, const Wrapped& b
  623. ) NOEXCEPT { decaf_448_scalar_copy(&a,&b); }
  624. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::setu(
  625. Wrapped& a, decaf_word_t w
  626. ) NOEXCEPT { decaf_448_scalar_set(&a,w); }
  627. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::inv2(
  628. Wrapped& a, const Wrapped& b
  629. ) NOEXCEPT { decaf_448_scalar_invert(&a,&b); }
  630. template<> inline decaf_word_t EcGroup<Ed448Goldilocks>::Scalar::eq2(
  631. const Wrapped& a, const Wrapped& b
  632. ) NOEXCEPT { return decaf_448_scalar_eq(&a,&b); }
  633. /* CLASSY */
  634. template<> inline SecureBuffer EcGroup<Ed448Goldilocks>::Scalar::direct_scalarmul(
  635. const Block &in, decaf_bool_t allow_identity, decaf_bool_t short_circuit
  636. ) const throw(CryptoException) {
  637. SecureBuffer out(SER_BYTES);
  638. if (!decaf_448_direct_scalarmul(out, in.data(), &s, allow_identity, short_circuit))
  639. throw CryptoException();
  640. return out;
  641. }
  642. template<> inline void EcGroup<Ed448Goldilocks>::Scalar::encode(
  643. unsigned char buffer[SER_BYTES]
  644. ) const NOEXCEPT {
  645. decaf_448_scalar_encode(buffer,&s);
  646. }
  647. template<> inline decaf_bool_t __attribute__((warn_unused_result))
  648. EcGroup<Ed448Goldilocks>::Scalar::decode (
  649. Scalar &s, const unsigned char buffer[SER_BYTES]
  650. ) NOEXCEPT {
  651. return decaf_448_scalar_decode(&s.s,buffer);
  652. }
  653. /* CLASSY */
  654. template<> inline EcGroup<Ed448Goldilocks>::Scalar::~Scalar() NOEXCEPT { decaf_448_scalar_destroy(&s); }
  655. template<> inline const EcGroup<Ed448Goldilocks>::Scalar::Wrapped&
  656. EcGroup<Ed448Goldilocks>::Scalar::ZERO() NOEXCEPT { return decaf_448_scalar_zero[0]; }
  657. template<> inline const EcGroup<Ed448Goldilocks>::Scalar::Wrapped&
  658. EcGroup<Ed448Goldilocks>::Scalar::ONE() NOEXCEPT { return decaf_448_scalar_one[0]; }
  659. /* Point instantiation */
  660. /* CLASSY */
  661. template<> inline EcGroup<Ed448Goldilocks>::Point::~Point() NOEXCEPT { decaf_448_point_destroy(&p); }
  662. template<> inline void EcGroup<Ed448Goldilocks>::Point::add3(
  663. Wrapped& a, const Wrapped& b, const Wrapped& c
  664. ) NOEXCEPT { decaf_448_point_add(&a,&b,&c); }
  665. template<> inline void EcGroup<Ed448Goldilocks>::Point::sub3(
  666. Wrapped& a, const Wrapped& b, const Wrapped& c
  667. ) NOEXCEPT { decaf_448_point_sub(&a,&b,&c); }
  668. template<> inline void EcGroup<Ed448Goldilocks>::Point::assign2(
  669. Wrapped& a, const Wrapped& b
  670. ) NOEXCEPT { decaf_448_point_copy(&a,&b); }
  671. template<> inline void EcGroup<Ed448Goldilocks>::Point::dbl2(
  672. Wrapped& a, const Wrapped& b
  673. ) NOEXCEPT { decaf_448_point_double(&a,&b); }
  674. template<> inline decaf_word_t EcGroup<Ed448Goldilocks>::Point::eq2(
  675. const Wrapped& a, const Wrapped& b
  676. ) NOEXCEPT { return decaf_448_point_eq(&a,&b); }
  677. /* CLASSY */
  678. template<> inline bool EcGroup<Ed448Goldilocks>::Point::validate() const NOEXCEPT { return !!decaf_448_point_valid(&p); }
  679. template<> inline void EcGroup<Ed448Goldilocks>::Point::sm3(
  680. Wrapped& a, const Wrapped& b, const Scalar::Wrapped &c
  681. ) NOEXCEPT { decaf_448_point_scalarmul(&a,&b,&c); }
  682. template<> inline void EcGroup<Ed448Goldilocks>::Point::dsm5(
  683. Wrapped& a, const Wrapped& b, const Scalar::Wrapped &c, const Wrapped& d, const Scalar::Wrapped &e
  684. ) NOEXCEPT { decaf_448_point_double_scalarmul(&a,&b,&c,&d,&e); }
  685. template<> inline void EcGroup<Ed448Goldilocks>::Point::dsmns(
  686. Wrapped& a, const Scalar::Wrapped &b, const Wrapped& c, const Scalar::Wrapped &d
  687. ) NOEXCEPT { decaf_448_base_double_scalarmul_non_secret(&a,&b,&c,&d); }
  688. /* CLASSY */
  689. template<> inline decaf_bool_t __attribute__((warn_unused_result))
  690. EcGroup<Ed448Goldilocks>::Point::decode (
  691. Point &p, const unsigned char buffer[SER_BYTES], decaf_bool_t allow_identity
  692. ) NOEXCEPT {
  693. return decaf_448_point_decode(&p.p,buffer,allow_identity);
  694. }
  695. /* CLASSY */
  696. template<> inline void EcGroup<Ed448Goldilocks>::Point::set_to_hash( const Block &s ) NOEXCEPT {
  697. if (s.size() < HASH_BYTES) {
  698. SecureBuffer b(HASH_BYTES);
  699. memcpy(b.data(), s.data(), s.size());
  700. decaf_448_point_from_hash_nonuniform(&p,b);
  701. } else if (s.size() == HASH_BYTES) {
  702. decaf_448_point_from_hash_nonuniform(&p,s);
  703. } else if (s.size() < 2*HASH_BYTES) {
  704. SecureBuffer b(2*HASH_BYTES);
  705. memcpy(b.data(), s.data(), s.size());
  706. decaf_448_point_from_hash_uniform(&p,b);
  707. } else {
  708. decaf_448_point_from_hash_uniform(&p,s);
  709. }
  710. }
  711. /* CLASSY */
  712. template<> inline void EcGroup<Ed448Goldilocks>::Point::encode(
  713. unsigned char buffer[SER_BYTES]
  714. ) const NOEXCEPT {
  715. decaf_448_point_encode(buffer,&p);
  716. }
  717. template<> inline EcGroup<Ed448Goldilocks>::Point
  718. EcGroup<Ed448Goldilocks>::Point::identity() NOEXCEPT { return decaf_448_point_identity[0]; }
  719. template<> inline EcGroup<Ed448Goldilocks>::Point
  720. EcGroup<Ed448Goldilocks>::Point::base() NOEXCEPT { return decaf_448_point_base[0]; }
  721. /* Precomputed instantiation */
  722. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::destroy(
  723. Wrapped *doomed
  724. ) NOEXCEPT {
  725. decaf_448_precomputed_destroy(doomed);
  726. }
  727. /* Precomputed instantiation */
  728. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::precompute(
  729. Wrapped *pre, const Point::Wrapped &point
  730. ) NOEXCEPT {
  731. decaf_448_precompute(pre,&point);
  732. }
  733. template<> inline void EcGroup<Ed448Goldilocks>::Precomputed::psmul3(
  734. Point::Wrapped &out, const Wrapped *pre, const Scalar::Wrapped &sc
  735. ) NOEXCEPT {
  736. decaf_448_precomputed_scalarmul(&out,pre,&sc);
  737. }
  738. template<> inline size_t EcGroup<Ed448Goldilocks>::Precomputed:: sizeof_this() NOEXCEPT
  739. { return sizeof_decaf_448_precomputed_s; }
  740. template<> inline size_t EcGroup<Ed448Goldilocks>::Precomputed::alignof_this() NOEXCEPT
  741. { return alignof_decaf_448_precomputed_s; }
  742. template<> inline const EcGroup<Ed448Goldilocks>::Precomputed::Wrapped*
  743. EcGroup<Ed448Goldilocks>::Precomputed::GENERATOR() NOEXCEPT { return decaf_448_precomputed_base; }
  744. /** @endcond */
  745. #undef NOEXCEPT
  746. #undef EXPLICIT_CON
  747. #undef GET_DATA
  748. } /* namespace decaf */
  749. #endif /* __DECAF_448_HXX__ */