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.
 
 
 
 
 

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