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.
 
 
 
 
 

516 lines
20 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. /**
  47. * Securely erase contents of memory.
  48. */
  49. void really_bzero(void *data, size_t size);
  50. /**
  51. * @brief Group with prime order.
  52. * @todo Move declarations of functions up here?
  53. */
  54. template<unsigned int bits = 448> struct decaf;
  55. /**
  56. * @brief Ed448-Goldilocks/Decaf instantiation of group.
  57. */
  58. template<> struct decaf<448> {
  59. /** @brief An exception for when crypto (i.e. point decode) has failed. */
  60. class CryptoException : public std::exception {
  61. public:
  62. /** @return "CryptoException" */
  63. virtual const char * what() const NOEXCEPT { return "CryptoException"; }
  64. };
  65. /** @cond internal */
  66. class Point;
  67. class Precomputed;
  68. /** @endcond */
  69. /**
  70. * @brief A scalar modulo the curve order.
  71. * Supports the usual arithmetic operations, all in constant time.
  72. */
  73. class Scalar {
  74. public:
  75. /** @brief access to the underlying scalar object */
  76. decaf_448_scalar_t s;
  77. /** @brief Set to an unsigned word */
  78. inline Scalar(const decaf_word_t w=0) NOEXCEPT { *this = w; }
  79. /** @brief Set to a signed word */
  80. inline Scalar(const int w) NOEXCEPT { *this = w; }
  81. /** @brief Construct from decaf_scalar_t object. */
  82. inline Scalar(const decaf_448_scalar_t &t) NOEXCEPT { decaf_448_scalar_copy(s,t); }
  83. /** @brief Copy constructor. */
  84. inline Scalar(const Scalar &x) NOEXCEPT { decaf_448_scalar_copy(s,x.s); }
  85. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  86. inline explicit Scalar(const std::string &str) NOEXCEPT { *this = str; }
  87. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  88. inline Scalar(const unsigned char *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,buffer,n); }
  89. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  90. inline Scalar(const char *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,(const unsigned char *)buffer,n); }
  91. /** @brief Construct from arbitrary-length little-endian byte sequence. */
  92. inline Scalar(const void *buffer, size_t n) NOEXCEPT { decaf_448_scalar_decode_long(s,(const unsigned char *)buffer,n); }
  93. /** @brief Assignment. */
  94. inline Scalar& operator=(const Scalar &x) NOEXCEPT { decaf_448_scalar_copy(s,x.s); return *this; }
  95. /** @brief Assign from unsigned word. */
  96. inline Scalar& operator=(decaf_word_t w) NOEXCEPT { decaf_448_scalar_set(s,w); return *this; }
  97. /** @brief Assign from signed int. */
  98. inline Scalar& operator=(int w) {
  99. Scalar t(-(decaf_word_t)INT_MIN);
  100. decaf_448_scalar_set(s,(decaf_word_t)w - (decaf_word_t)INT_MIN);
  101. *this -= t;
  102. return *this;
  103. }
  104. /** Destructor securely erases the scalar. */
  105. inline ~Scalar() NOEXCEPT { decaf_448_scalar_destroy(s); }
  106. /** @brief Assign from arbitrary-length little-endian byte sequence in C++ string. */
  107. inline Scalar &operator=(const std::string &str) NOEXCEPT {
  108. decaf_448_scalar_decode_long(s,GET_DATA(str),str.length()); return *this;
  109. }
  110. /**
  111. * @brief Decode from correct-length little-endian byte sequence.
  112. * @return DECAF_FAILURE if the scalar is greater than or equal to the group order q.
  113. */
  114. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  115. Scalar &sc, const unsigned char buffer[DECAF_448_SCALAR_BYTES]
  116. ) NOEXCEPT {
  117. return decaf_448_scalar_decode(sc.s,buffer);
  118. }
  119. /** @brief Decode from correct-length little-endian byte sequence in C++ string. */
  120. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  121. Scalar &sc, const std::string buffer
  122. ) NOEXCEPT {
  123. if (buffer.size() != DECAF_448_SCALAR_BYTES) return DECAF_FAILURE;
  124. return decaf_448_scalar_decode(sc.s,GET_DATA(buffer));
  125. }
  126. /** @brief Encode to fixed-length string */
  127. inline EXPLICIT_CON operator std::string() const NOEXCEPT {
  128. unsigned char buffer[DECAF_448_SCALAR_BYTES];
  129. decaf_448_scalar_encode(buffer, s);
  130. return std::string((char*)buffer,sizeof(buffer));
  131. }
  132. /** @brief Encode to fixed-length buffer */
  133. inline void encode(unsigned char buffer[DECAF_448_SCALAR_BYTES]) const NOEXCEPT{
  134. decaf_448_scalar_encode(buffer, s);
  135. }
  136. /** Add. */
  137. inline Scalar operator+ (const Scalar &q) const NOEXCEPT { Scalar r; decaf_448_scalar_add(r.s,s,q.s); return r; }
  138. /** Add to this. */
  139. inline Scalar operator+=(const Scalar &q) NOEXCEPT { decaf_448_scalar_add(s,s,q.s); return *this; }
  140. /** Subtract. */
  141. inline Scalar operator- (const Scalar &q) const NOEXCEPT { Scalar r; decaf_448_scalar_sub(r.s,s,q.s); return r; }
  142. /** Subtract from this. */
  143. inline Scalar operator-=(const Scalar &q) NOEXCEPT { decaf_448_scalar_sub(s,s,q.s); return *this; }
  144. /** Multiply */
  145. inline Scalar operator* (const Scalar &q) const NOEXCEPT { Scalar r; decaf_448_scalar_mul(r.s,s,q.s); return r; }
  146. /** Multiply into this. */
  147. inline Scalar operator*=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.s); return *this; }
  148. /** Negate */
  149. inline Scalar operator- () const NOEXCEPT { Scalar r; decaf_448_scalar_sub(r.s,decaf_448_scalar_zero,s); return r; }
  150. /** @brief Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */
  151. inline Scalar inverse() const NOEXCEPT { Scalar r; decaf_448_scalar_invert(r.s,s); return r; }
  152. /** @brief Divide by inverting q. If q == 0, return 0. */
  153. inline Scalar operator/ (const Scalar &q) const NOEXCEPT { Scalar r; decaf_448_scalar_mul(r.s,s,q.inverse().s); return r; }
  154. /** @brief Divide by inverting q. If q == 0, return 0. */
  155. inline Scalar operator/=(const Scalar &q) NOEXCEPT { decaf_448_scalar_mul(s,s,q.inverse().s); return *this; }
  156. /** @brief Compare in constant time */
  157. inline bool operator!=(const Scalar &q) const NOEXCEPT { return ! decaf_448_scalar_eq(s,q.s); }
  158. /** @brief Compare in constant time */
  159. inline bool operator==(const Scalar &q) const NOEXCEPT { return !!decaf_448_scalar_eq(s,q.s); }
  160. /** @brief Scalarmul with scalar on left. */
  161. inline Point operator* (const Point &q) const NOEXCEPT { return q * (*this); }
  162. /** @brief Scalarmul-precomputed with scalar on left. */
  163. inline Point operator* (const Precomputed &q) const NOEXCEPT { return q * (*this); }
  164. };
  165. /**
  166. * @brief Element of prime-order group.
  167. */
  168. class Point {
  169. public:
  170. /** The c-level object. */
  171. decaf_448_point_t p;
  172. /** @brief Constructor sets to identity by default. */
  173. inline Point(const decaf_448_point_t &q = decaf_448_point_identity) { decaf_448_point_copy(p,q); }
  174. /** @brief Copy constructor. */
  175. inline Point(const Point &q) { decaf_448_point_copy(p,q.p); }
  176. /** @brief Assignment. */
  177. inline Point& operator=(const Point &q) { decaf_448_point_copy(p,q.p); return *this; }
  178. /** @brief Destructor securely erases the point. */
  179. inline ~Point() { decaf_448_point_destroy(p); }
  180. /**
  181. * @brief Initialize from C++ fixed-length byte string.
  182. * The all-zero string maps to the identity.
  183. *
  184. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  185. * or was the identity and allow_identity was DECAF_FALSE.
  186. */
  187. inline explicit Point(const std::string &s, decaf_bool_t allow_identity=DECAF_TRUE) throw(CryptoException) {
  188. if (!decode(*this,s,allow_identity)) throw CryptoException();
  189. }
  190. /**
  191. * @brief Initialize from C fixed-length byte string.
  192. * The all-zero string maps to the identity.
  193. *
  194. * @throw CryptoException the string was the wrong length, or wasn't the encoding of a point,
  195. * or was the identity and allow_identity was DECAF_FALSE.
  196. */
  197. inline explicit Point(const unsigned char buffer[DECAF_448_SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE)
  198. throw(CryptoException) { if (!decode(*this,buffer,allow_identity)) throw CryptoException(); }
  199. /**
  200. * @brief Initialize from C fixed-length byte string.
  201. * The all-zero string maps to the identity.
  202. *
  203. * @retval DECAF_SUCCESS the string was successfully decoded.
  204. * @return DECAF_FAILURE the string wasn't the encoding of a point, or was the identity
  205. * and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  206. */
  207. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  208. Point &p, const unsigned char buffer[DECAF_448_SER_BYTES], decaf_bool_t allow_identity=DECAF_TRUE
  209. ) NOEXCEPT {
  210. return decaf_448_point_decode(p.p,buffer,allow_identity);
  211. }
  212. /**
  213. * @brief Initialize from C++ fixed-length byte string.
  214. * The all-zero string maps to the identity.
  215. *
  216. * @retval DECAF_SUCCESS the string was successfully decoded.
  217. * @return DECAF_FAILURE the string was the wrong length, or wasn't the encoding of a point,
  218. * or was the identity and allow_identity was DECAF_FALSE. Contents of the buffer are undefined.
  219. */
  220. static inline decaf_bool_t __attribute__((warn_unused_result)) decode (
  221. Point &p, const std::string &buffer, decaf_bool_t allow_identity=DECAF_TRUE
  222. ) NOEXCEPT {
  223. if (buffer.size() != DECAF_448_SER_BYTES) return DECAF_FAILURE;
  224. return decaf_448_point_decode(p.p,GET_DATA(buffer),allow_identity);
  225. }
  226. /**
  227. * @brief Map to the curve from a C buffer.
  228. * The all-zero buffer maps to the identity, as does the buffer {1,0...}
  229. */
  230. static inline Point from_hash_nonuniform ( const unsigned char buffer[DECAF_448_SER_BYTES] ) NOEXCEPT {
  231. Point p; decaf_448_point_from_hash_nonuniform(p.p,buffer); return p;
  232. }
  233. /**
  234. * @brief Map to the curve from a C++ string buffer.
  235. * The empty or all-zero string maps to the identity, as does the string "\x01".
  236. * If the buffer is shorter than (TODO) DECAF_448_SER_BYTES, it will be zero-padded on the right.
  237. */
  238. static inline Point from_hash_nonuniform ( const std::string &s ) NOEXCEPT {
  239. std::string t = s;
  240. if (t.size() < DECAF_448_SER_BYTES) t.insert(t.size(),DECAF_448_SER_BYTES-t.size(),0);
  241. Point p; decaf_448_point_from_hash_nonuniform(p.p,GET_DATA(t)); return p;
  242. }
  243. /**
  244. * @brief Map uniformly to the curve from a C buffer.
  245. * The all-zero buffer maps to the identity, as does the buffer {1,0...}.
  246. */
  247. static inline Point from_hash ( const unsigned char buffer[2*DECAF_448_SER_BYTES] ) NOEXCEPT {
  248. Point p; decaf_448_point_from_hash_uniform(p.p,buffer); return p;
  249. }
  250. /**
  251. * @brief Map uniformly to the curve from a C++ buffer.
  252. * The empty or all-zero string maps to the identity, as does the string "\x01".
  253. * If the buffer is shorter than (TODO) 2*DECAF_448_SER_BYTES, well, it won't be as uniform,
  254. * but the buffer will be zero-padded on the right.
  255. */
  256. static inline Point from_hash ( const std::string &s ) NOEXCEPT {
  257. std::string t = s;
  258. if (t.size() < DECAF_448_SER_BYTES) return from_hash_nonuniform(s);
  259. if (t.size() < 2*DECAF_448_SER_BYTES) t.insert(t.size(),2*DECAF_448_SER_BYTES-t.size(),0);
  260. Point p; decaf_448_point_from_hash_uniform(p.p,GET_DATA(t)); return p;
  261. }
  262. /**
  263. * @brief Encode to string. The identity encodes to the all-zero string.
  264. */
  265. inline EXPLICIT_CON operator std::string() const NOEXCEPT {
  266. unsigned char buffer[DECAF_448_SER_BYTES];
  267. decaf_448_point_encode(buffer, p);
  268. return std::string((char*)buffer,sizeof(buffer));
  269. }
  270. /**
  271. * @brief Encode to a C buffer. The identity encodes to all zeros.
  272. */
  273. inline void encode(unsigned char buffer[DECAF_448_SER_BYTES]) const NOEXCEPT{
  274. decaf_448_point_encode(buffer, p);
  275. }
  276. /** @brief Point add. */
  277. inline Point operator+ (const Point &q) const NOEXCEPT { Point r; decaf_448_point_add(r.p,p,q.p); return r; }
  278. /** @brief Point add. */
  279. inline Point operator+=(const Point &q) NOEXCEPT { decaf_448_point_add(p,p,q.p); return *this; }
  280. /** @brief Point subtract. */
  281. inline Point operator- (const Point &q) const NOEXCEPT { Point r; decaf_448_point_sub(r.p,p,q.p); return r; }
  282. /** @brief Point subtract. */
  283. inline Point operator-=(const Point &q) NOEXCEPT { decaf_448_point_sub(p,p,q.p); return *this; }
  284. /** @brief Point negate. */
  285. inline Point operator- () const NOEXCEPT { Point r; decaf_448_point_negate(r.p,p); return r; }
  286. /** @brief Double the point out of place. */
  287. inline Point times_two () const NOEXCEPT { Point r; decaf_448_point_double(r.p,p); return r; }
  288. /** @brief Double the point in place. */
  289. inline Point &double_in_place() NOEXCEPT { decaf_448_point_double(p,p); return *this; }
  290. /** @brief Constant-time compare. */
  291. inline bool operator!=(const Point &q) const NOEXCEPT { return ! decaf_448_point_eq(p,q.p); }
  292. /** @brief Constant-time compare. */
  293. inline bool operator==(const Point &q) const NOEXCEPT { return !!decaf_448_point_eq(p,q.p); }
  294. /** @brief Scalar multiply. */
  295. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_448_point_scalarmul(r.p,p,s.s); return r; }
  296. /** @brief Scalar multiply in place. */
  297. inline Point operator*=(const Scalar &s) NOEXCEPT { decaf_448_point_scalarmul(p,p,s.s); return *this; }
  298. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  299. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  300. /** @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster. */
  301. static inline Point double_scalarmul (
  302. const Point &q, const Scalar &qs, const Point &r, const Scalar &rs
  303. ) NOEXCEPT {
  304. Point p; decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  305. }
  306. /**
  307. * @brief Double-scalar multiply, equivalent to q*qs + r*rs but faster.
  308. * For those who like their scalars before the point.
  309. */
  310. static inline Point double_scalarmul (
  311. const Scalar &qs, const Point &q, const Scalar &rs, const Point &r
  312. ) NOEXCEPT {
  313. Point p; decaf_448_point_double_scalarmul(p.p,q.p,qs.s,r.p,rs.s); return p;
  314. }
  315. /** @brief Return the base point */
  316. static inline const Point base() NOEXCEPT { return Point(decaf_448_point_base); }
  317. /** @brief Return the identity point */
  318. static inline const Point identity() NOEXCEPT { return Point(decaf_448_point_identity); }
  319. };
  320. /**
  321. * @brief Precomputed table of points.
  322. * Minor difficulties arise here because the decaf API doesn't expose, as a constant, how big such an object is.
  323. * Therefore we have to call malloc() or friends, but that's probably for the best, because you don't want to
  324. * stack-allocate a 15kiB object anyway.
  325. */
  326. class Precomputed {
  327. private:
  328. /** @cond internal */
  329. union {
  330. decaf_448_precomputed_s *mine;
  331. const decaf_448_precomputed_s *yours;
  332. } ours;
  333. bool isMine;
  334. inline void clear() NOEXCEPT {
  335. if (isMine) {
  336. decaf_448_precomputed_destroy(ours.mine);
  337. free(ours.mine);
  338. ours.yours = decaf_448_precomputed_base;
  339. isMine = false;
  340. }
  341. }
  342. inline void alloc() throw(std::bad_alloc) {
  343. if (isMine) return;
  344. int ret = posix_memalign((void**)&ours.mine, alignof_decaf_448_precomputed_s,sizeof_decaf_448_precomputed_s);
  345. if (ret || !ours.mine) {
  346. isMine = false;
  347. throw std::bad_alloc();
  348. }
  349. isMine = true;
  350. }
  351. inline const decaf_448_precomputed_s *get() const NOEXCEPT { return isMine ? ours.mine : ours.yours; }
  352. /** @endcond */
  353. public:
  354. /** Destructor securely erases the memory. */
  355. inline ~Precomputed() NOEXCEPT { clear(); }
  356. /**
  357. * @brief Initialize from underlying type, declared as a reference to prevent
  358. * it from being called with 0, thereby breaking override.
  359. *
  360. * The underlying object must remain valid throughout the lifetime of this one.
  361. *
  362. * By default, initializes to the table for the base point.
  363. *
  364. * @todo: FIXME Harmonize with Point(), which initializes to the identity.
  365. */
  366. inline Precomputed(
  367. const decaf_448_precomputed_s &yours = *decaf_448_precomputed_base
  368. ) NOEXCEPT {
  369. ours.yours = &yours;
  370. isMine = false;
  371. }
  372. /**
  373. * @brief Assign. This may require an allocation and memcpy.
  374. */
  375. inline Precomputed &operator=(const Precomputed &it) throw(std::bad_alloc) {
  376. if (this == &it) return *this;
  377. if (it.isMine) {
  378. alloc();
  379. memcpy(ours.mine,it.ours.mine,sizeof_decaf_448_precomputed_s);
  380. } else {
  381. clear();
  382. ours.yours = it.ours.yours;
  383. }
  384. isMine = it.isMine;
  385. return *this;
  386. }
  387. /**
  388. * @brief Initilaize from point. Must allocate memory, and may throw.
  389. */
  390. inline Precomputed &operator=(const Point &it) throw(std::bad_alloc) {
  391. alloc();
  392. decaf_448_precompute(ours.mine,it.p);
  393. return *this;
  394. }
  395. /**
  396. * @brief Copy constructor.
  397. */
  398. inline Precomputed(const Precomputed &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  399. /**
  400. * @brief Constructor which initializes from point.
  401. */
  402. inline explicit Precomputed(const Point &it) throw(std::bad_alloc) : isMine(false) { *this = it; }
  403. #if __cplusplus >= 201103L
  404. inline Precomputed &operator=(Precomputed &&it) NOEXCEPT {
  405. if (this == &it) return *this;
  406. clear();
  407. ours = it.ours;
  408. isMine = it.isMine;
  409. it.isMine = false;
  410. it.ours.yours = decaf_448_precomputed_base;
  411. return *this;
  412. }
  413. inline Precomputed(Precomputed &&it) NOEXCEPT : isMine(false) { *this = it; }
  414. #endif
  415. /** @brief Fixed base scalarmul. */
  416. inline Point operator* (const Scalar &s) const NOEXCEPT { Point r; decaf_448_precomputed_scalarmul(r.p,get(),s.s); return r; }
  417. /** @brief Multiply by s.inverse(). If s=0, maps to the identity. */
  418. inline Point operator/ (const Scalar &s) const NOEXCEPT { return (*this) * s.inverse(); }
  419. /** @brief Return the table for the base point. */
  420. static inline const Precomputed base() NOEXCEPT { return Precomputed(*decaf_448_precomputed_base); }
  421. };
  422. }; /* struct decaf<448> */
  423. #undef NOEXCEPT
  424. #undef EXPLICIT_CON
  425. #undef GET_DATA
  426. } /* namespace decaf */
  427. #endif /* __DECAF_448_HXX__ */