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.
 
 
 
 
 

225 lines
7.0 KiB

  1. /**
  2. * @file shake.hxx
  3. * @copyright
  4. * Based on CC0 code by David Leon Gil, 2015 \n
  5. * Copyright (c) 2015 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @brief SHA-3-n and SHAKE-n instances, C++ wrapper.
  9. * @warning EXPERIMENTAL! The names, parameter orders etc are likely to change.
  10. */
  11. /** TODO: Crypto++ style secure auto-erasing strings?? */
  12. #ifndef __SHAKE_HXX__
  13. #define __SHAKE_HXX__
  14. #include "shake.h"
  15. #include <string>
  16. #include <sys/types.h>
  17. /** @cond internal */
  18. #if __cplusplus >= 201103L
  19. #define DELETE = delete
  20. #define NOEXCEPT noexcept
  21. #define EXPLICIT_CON explicit
  22. #define GET_DATA(str) ((const unsigned char *)&(str)[0])
  23. #else
  24. #define DELETE
  25. #define NOEXCEPT throw()
  26. #define EXPLICIT_CON
  27. #define GET_DATA(str) ((const unsigned char *)((str).data()))
  28. #endif
  29. /** @endcond */
  30. namespace decaf {
  31. /** A Keccak sponge internal class */
  32. class KeccakSponge {
  33. protected:
  34. /** The C-wrapper sponge state */
  35. keccak_sponge_t sp;
  36. /** Initialize from parameters */
  37. inline KeccakSponge(const struct kparams_s *params) NOEXCEPT { sponge_init(sp, params); }
  38. /** No initialization */
  39. inline KeccakSponge(const NOINIT &) NOEXCEPT { }
  40. public:
  41. /** Destructor zeroizes state */
  42. inline ~KeccakSponge() NOEXCEPT { sponge_destroy(sp); }
  43. };
  44. /**
  45. * Hash function derived from Keccak
  46. * @todo throw exceptions when hash is misused.
  47. */
  48. class KeccakHash : public KeccakSponge {
  49. protected:
  50. /** Initialize from parameters */
  51. inline KeccakHash(const kparams_s *params) NOEXCEPT : KeccakSponge(params) {}
  52. public:
  53. /** Add more data to running hash */
  54. inline void update(const uint8_t *__restrict__ in, size_t len) { sha3_update(sp,in,len); }
  55. /** Add more data to running hash, C++ version. */
  56. inline void update(const std::string &s) { sha3_update(sp,GET_DATA(s),s.size()); }
  57. /** Add more data, stream version. */
  58. inline KeccakHash &operator<<(const std::string &s) { update(s); return *this; }
  59. /** Same as <<. */
  60. inline KeccakHash &operator+=(const std::string &s) { return *this << s; }
  61. /**
  62. * @brief Output bytes from the sponge.
  63. * @todo make this throw exceptions.
  64. */
  65. inline void output(unsigned char *c, size_t len) {
  66. sha3_output(sp,c,len);
  67. }
  68. /** @brief Output bytes from the sponge. */
  69. inline std::string output(size_t len) {
  70. unsigned char *buffer = new unsigned char[len];
  71. sha3_output(sp,buffer,len);
  72. std::string out((char *)buffer, len);
  73. delete[] buffer;
  74. return out;
  75. }
  76. /** @brief Return the sponge's default output size. */
  77. inline size_t default_output_size() const NOEXCEPT {
  78. return sponge_default_output_bytes(sp);
  79. }
  80. /** Output the default number of bytes. */
  81. inline std::string output() {
  82. return output(default_output_size());
  83. }
  84. };
  85. /** Fixed-output-length SHA3 */
  86. template<int bits> class SHA3 : public KeccakSponge {
  87. private:
  88. /** Get the parameter template block for this hash */
  89. const struct kparams_s *get_params();
  90. public:
  91. /** Initializer */
  92. inline SHA3() NOEXCEPT : KeccakHash(get_params()) {}
  93. };
  94. /** Variable-output-length SHAKE */
  95. template<int bits>
  96. class SHAKE : public KeccakSponge {
  97. private:
  98. /** Get the parameter template block for this hash */
  99. const struct kparams_s *get_params();
  100. public:
  101. /** Initializer */
  102. inline SHAKE() NOEXCEPT : KeccakHash(get_params()) {}
  103. };
  104. /** @cond internal */
  105. template<> const struct kparams_s *SHAKE<128>::get_params() { return &SHAKE128_params_s; }
  106. template<> const struct kparams_s *SHAKE<256>::get_params() { return &SHAKE256_params_s; }
  107. template<> const struct kparams_s *SHA3<224>::get_params() { return &SHA3_224_params_s; }
  108. template<> const struct kparams_s *SHA3<256>::get_params() { return &SHA3_256_params_s; }
  109. template<> const struct kparams_s *SHA3<384>::get_params() { return &SHA3_384_params_s; }
  110. template<> const struct kparams_s *SHA3<512>::get_params() { return &SHA3_512_params_s; }
  111. /** @endcond */
  112. /** Sponge-based random-number generator */
  113. class SpongeRng : private KeccakSponge {
  114. public:
  115. class RngException : public std::exception {
  116. private:
  117. const char *const what_;
  118. public:
  119. const int err_code;
  120. const char *what() const NOEXCEPT { return what_; }
  121. RngException(int err_code, const char *what_) NOEXCEPT : what_(what_), err_code(err_code) {}
  122. };
  123. struct FROM_BUFFER {};
  124. struct FROM_FILE {};
  125. /** Initialize, deterministically by default, from C buffer */
  126. inline SpongeRng( const FROM_BUFFER &, const uint8_t *in, size_t len, bool deterministic = true ) NOEXCEPT
  127. : KeccakSponge((NOINIT())) {
  128. spongerng_init_from_buffer(sp,in,len,deterministic);
  129. }
  130. /** Initialize, deterministically by default, from C++ string */
  131. inline SpongeRng( const FROM_BUFFER &, const std::string &in, bool deterministic = true )
  132. : KeccakSponge((NOINIT())) {
  133. spongerng_init_from_buffer(sp,GET_DATA(in),in.size(),deterministic);
  134. }
  135. /** Initialize, non-deterministically by default, from C/C++ filename */
  136. inline SpongeRng( const FROM_FILE &, const std::string &in = "/dev/urandom", size_t len = 32, bool deterministic = false )
  137. throw(RngException)
  138. : KeccakSponge((NOINIT())) {
  139. int ret = spongerng_init_from_file(sp,in.c_str(),len,deterministic);
  140. if (ret) {
  141. throw RngException(ret, "Couldn't load from file");
  142. }
  143. }
  144. /** Read data to a C buffer.
  145. * @warning TODO Future versions of this function may throw RngException if a
  146. * nondeterministic RNG fails a reseed.
  147. */
  148. inline void read(uint8_t *buffer, size_t length) {
  149. spongerng_next(sp,buffer,length);
  150. }
  151. /** Read data to a C++ string
  152. * @warning TODO Future versions of this function may throw RngException if a
  153. * nondeterministic RNG fails a reseed.
  154. */
  155. inline std::string read(size_t length) throw(std::bad_alloc) {
  156. uint8_t *buffer = new uint8_t[length];
  157. spongerng_next(sp,buffer,length);
  158. std::string out((const char *)buffer, length);
  159. delete[] buffer;
  160. return out;
  161. }
  162. private:
  163. SpongeRng(const SpongeRng &) DELETE;
  164. SpongeRng &operator=(const SpongeRng &) DELETE;
  165. };
  166. /**@cond internal*/
  167. /* FIXME: MAGIC; should use buffer or erase temporary string */
  168. /* FIXME: multiple sizes */
  169. decaf<448>::Scalar::Scalar(SpongeRng &rng) {
  170. uint8_t buffer[SER_BYTES];
  171. rng.read(buffer, sizeof(buffer));
  172. decaf_448_scalar_decode_long(s,buffer,sizeof(buffer));
  173. really_bzero(buffer, sizeof(buffer));
  174. }
  175. decaf<448>::Point::Point(SpongeRng &rng, bool uniform) {
  176. uint8_t buffer[2*HASH_BYTES];
  177. rng.read(buffer, (uniform ? 2 : 1) * HASH_BYTES);
  178. if (uniform) {
  179. decaf_448_point_from_hash_uniform(p,buffer);
  180. } else {
  181. decaf_448_point_from_hash_nonuniform(p,buffer);
  182. }
  183. really_bzero(buffer, sizeof(buffer));
  184. }
  185. /**@endcond*/
  186. } /* namespace decaf */
  187. #undef NOEXCEPT
  188. #undef EXPLICIT_CON
  189. #undef GET_DATA
  190. #undef DELETE
  191. #endif /* __SHAKE_HXX__ */