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.
 
 
 
 
 

202 lines
6.1 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. #ifndef __SHAKE_HXX__
  12. #define __SHAKE_HXX__
  13. #include "shake.h"
  14. #include <string>
  15. #include <sys/types.h>
  16. /** @cond internal */
  17. #if __cplusplus >= 201103L
  18. #define DELETE = delete
  19. #define NOEXCEPT noexcept
  20. #define EXPLICIT_CON explicit
  21. #define GET_DATA(str) ((const unsigned char *)&(str)[0])
  22. #else
  23. #define DELETE
  24. #define NOEXCEPT throw()
  25. #define EXPLICIT_CON
  26. #define GET_DATA(str) ((const unsigned char *)((str).data()))
  27. #endif
  28. /** @endcond */
  29. namespace decaf {
  30. /** A Keccak sponge internal class */
  31. class KeccakSponge {
  32. protected:
  33. /** The C-wrapper sponge state */
  34. keccak_sponge_t sp;
  35. /** Initialize from parameters */
  36. inline KeccakSponge(const struct kparams_s *params) NOEXCEPT { sponge_init(sp, params); }
  37. /** No initialization */
  38. inline KeccakSponge(const NOINIT &) NOEXCEPT { }
  39. public:
  40. /** Destructor zeroizes state */
  41. inline ~KeccakSponge() NOEXCEPT { sponge_destroy(sp); }
  42. };
  43. /**
  44. * Hash function derived from Keccak
  45. * @todo throw exceptions when hash is misused.
  46. */
  47. class KeccakHash : public KeccakSponge {
  48. protected:
  49. /** Initialize from parameters */
  50. inline KeccakHash(const kparams_s *params) NOEXCEPT : KeccakSponge(params) {}
  51. public:
  52. /** Add more data to running hash */
  53. inline void update(const uint8_t *__restrict__ in, size_t len) { sha3_update(sp,in,len); }
  54. /** Add more data to running hash, C++ version. */
  55. inline void update(const Block &s) { sha3_update(sp,s.data(),s.size()); }
  56. /** Add more data, stream version. */
  57. inline KeccakHash &operator<<(const Block &s) { update(s); return *this; }
  58. /** Same as <<. */
  59. inline KeccakHash &operator+=(const Block &s) { return *this << s; }
  60. /**
  61. * @brief Output bytes from the sponge.
  62. * @todo make this throw exceptions.
  63. */
  64. inline void output(unsigned char *c, size_t len) {
  65. sha3_output(sp,c,len);
  66. }
  67. /** @brief Output bytes from the sponge. */
  68. inline SecureBuffer output(size_t len) {
  69. SecureBuffer buffer(len);
  70. sha3_output(sp,buffer,len);
  71. return buffer;
  72. }
  73. /** @brief Return the sponge's default output size. */
  74. inline size_t default_output_size() const NOEXCEPT {
  75. return sponge_default_output_bytes(sp);
  76. }
  77. /** Output the default number of bytes. */
  78. inline SecureBuffer output() {
  79. return output(default_output_size());
  80. }
  81. };
  82. /** Fixed-output-length SHA3 */
  83. template<int bits> class SHA3 : public KeccakSponge {
  84. private:
  85. /** Get the parameter template block for this hash */
  86. const struct kparams_s *get_params();
  87. public:
  88. /** Initializer */
  89. inline SHA3() NOEXCEPT : KeccakHash(get_params()) {}
  90. };
  91. /** Variable-output-length SHAKE */
  92. template<int bits>
  93. class SHAKE : public KeccakSponge {
  94. private:
  95. /** Get the parameter template block for this hash */
  96. const struct kparams_s *get_params();
  97. public:
  98. /** Initializer */
  99. inline SHAKE() NOEXCEPT : KeccakHash(get_params()) {}
  100. };
  101. /** @cond internal */
  102. template<> const struct kparams_s *SHAKE<128>::get_params() { return &SHAKE128_params_s; }
  103. template<> const struct kparams_s *SHAKE<256>::get_params() { return &SHAKE256_params_s; }
  104. template<> const struct kparams_s *SHA3<224>::get_params() { return &SHA3_224_params_s; }
  105. template<> const struct kparams_s *SHA3<256>::get_params() { return &SHA3_256_params_s; }
  106. template<> const struct kparams_s *SHA3<384>::get_params() { return &SHA3_384_params_s; }
  107. template<> const struct kparams_s *SHA3<512>::get_params() { return &SHA3_512_params_s; }
  108. /** @endcond */
  109. /** Sponge-based random-number generator */
  110. class SpongeRng : private KeccakSponge {
  111. public:
  112. class RngException : public std::exception {
  113. private:
  114. const char *const what_;
  115. public:
  116. const int err_code;
  117. const char *what() const NOEXCEPT { return what_; }
  118. RngException(int err_code, const char *what_) NOEXCEPT : what_(what_), err_code(err_code) {}
  119. };
  120. /** Initialize, deterministically by default, from block */
  121. inline SpongeRng( const Block &in, bool deterministic = true )
  122. : KeccakSponge((NOINIT())) {
  123. spongerng_init_from_buffer(sp,in.data(),in.size(),deterministic);
  124. }
  125. /** Initialize, non-deterministically by default, from C/C++ filename */
  126. inline SpongeRng( const std::string &in = "/dev/urandom", size_t len = 32, bool deterministic = false )
  127. throw(RngException)
  128. : KeccakSponge((NOINIT())) {
  129. int ret = spongerng_init_from_file(sp,in.c_str(),len,deterministic);
  130. if (ret) {
  131. throw RngException(ret, "Couldn't load from file");
  132. }
  133. }
  134. /** Read data to a buffer. */
  135. inline void read(Buffer &buffer) { spongerng_next(sp,buffer.data(),buffer.size()); }
  136. /** Read data to a buffer. */
  137. inline void read(TmpBuffer buffer) { read((Buffer &)buffer); }
  138. /** Read data to a C++ string
  139. * @warning TODO Future versions of this function may throw RngException if a
  140. * nondeterministic RNG fails a reseed.
  141. */
  142. inline SecureBuffer read(size_t length) throw(std::bad_alloc) {
  143. SecureBuffer out(length); read(out); return out;
  144. }
  145. private:
  146. SpongeRng(const SpongeRng &) DELETE;
  147. SpongeRng &operator=(const SpongeRng &) DELETE;
  148. };
  149. /**@cond internal*/
  150. /* FIXME: multiple sizes */
  151. decaf<448>::Scalar::Scalar(SpongeRng &rng) {
  152. *this = rng.read(SER_BYTES);
  153. }
  154. decaf<448>::Point::Point(SpongeRng &rng, bool uniform) {
  155. SecureBuffer buffer((uniform ? 2 : 1) * HASH_BYTES);
  156. rng.read(buffer);
  157. if (uniform) {
  158. decaf_448_point_from_hash_uniform(p,buffer);
  159. } else {
  160. decaf_448_point_from_hash_nonuniform(p,buffer);
  161. }
  162. }
  163. /**@endcond*/
  164. } /* namespace decaf */
  165. #undef NOEXCEPT
  166. #undef EXPLICIT_CON
  167. #undef GET_DATA
  168. #undef DELETE
  169. #endif /* __SHAKE_HXX__ */