Skein.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. Bruce Schneier's SHA-3 candidate Skein hash function
  27. http://www.skein-hash.info/
  28. */
  29. #ifndef CAT_SKEIN_HPP
  30. #define CAT_SKEIN_HPP
  31. #include <cat/crypt/hash/ICryptHash.hpp>
  32. namespace cat {
  33. // Base class for various versions of Skein
  34. class CAT_EXPORT Skein : public ICryptHash
  35. {
  36. protected:
  37. // Tweak word 1 bit field starting positions
  38. static const int T1_POS_TREE_LVL = 112-64; // bits 112..118 : level in hash tree
  39. static const int T1_POS_BIT_PAD = 119-64; // bit 119 : partial final input byte
  40. static const int T1_POS_BLK_TYPE = 120-64; // bits 120..125 : type field
  41. static const int T1_POS_FIRST = 126-64; // bits 126 : first block flag
  42. static const int T1_POS_FINAL = 127-64; // bit 127 : final block flag
  43. // Tweak word 1 bit field masks
  44. static const u64 T1_MASK_FIRST = (u64)1 << T1_POS_FIRST;
  45. static const u64 T1_MASK_FINAL = (u64)1 << T1_POS_FINAL;
  46. static const u64 T1_MASK_BIT_PAD = (u64)1 << T1_POS_BIT_PAD;
  47. static const u64 T1_MASK_TREE_LVL = (u64)0x7F << T1_POS_TREE_LVL;
  48. static const u64 T1_MASK_BLK_TYPE = (u64)63 << T1_POS_BLK_TYPE;
  49. static const int BLK_TYPE_KEY = 0; // key, for MAC and KDF
  50. static const int BLK_TYPE_CFG = 4; // configuration block
  51. static const int BLK_TYPE_PERS = 8; // personalization string
  52. static const int BLK_TYPE_PK = 12; // public key (for digital signature hashing)
  53. static const int BLK_TYPE_KDF = 16; // key identifier for KDF
  54. static const int BLK_TYPE_NONCE = 20; // nonce for PRNG
  55. static const int BLK_TYPE_MSG = 48; // message processing
  56. static const int BLK_TYPE_OUT = 63; // output stage
  57. static const u32 ID_STRING_LE = 0x33414853;
  58. static const u32 SKEIN_VERSION = 1;
  59. static const u64 SCHEMA_VER = ((u64)SKEIN_VERSION << 32) | ID_STRING_LE;
  60. static const int MAX_BITS = 512;
  61. static const int MAX_WORDS = MAX_BITS / 64;
  62. static const int MAX_BYTES = MAX_BITS / 8;
  63. u64 Tweak[2];
  64. u64 State[MAX_WORDS];
  65. u8 Work[MAX_BYTES];
  66. int used_bytes, digest_words;
  67. u64 output_block_counter;
  68. bool output_prng_mode;
  69. typedef void (Skein::*HashComputation)(const void *message, int blocks, u32 byte_count, u64 *NextState);
  70. void HashComputation256(const void *message, int blocks, u32 byte_count, u64 *NextState);
  71. void HashComputation512(const void *message, int blocks, u32 byte_count, u64 *NextState);
  72. HashComputation hash_func;
  73. void GenerateInitialState(int bits);
  74. public:
  75. ~Skein();
  76. bool BeginKey(int bits);
  77. bool SetKey(ICryptHash *parent);
  78. bool BeginMAC();
  79. bool BeginKDF();
  80. bool BeginPRNG();
  81. void Crunch(const void *message, int bytes);
  82. void End();
  83. void Generate(void *out, int bytes, int strengthening_rounds = 0);
  84. };
  85. } // namespace cat
  86. #endif // CAT_SKEIN_HPP
粤ICP备19079148号