Crypto++  8.8
Free C++ class library of cryptographic schemes
threefish.h
Go to the documentation of this file.
1 // threefish.h - written and placed in the public domain by Jeffrey Walton
2 // Based on public domain code by Keru Kuro. Kuro's code is
3 // available at http://cppcrypto.sourceforge.net/.
4 
5 /// \file Threefish.h
6 /// \brief Classes for the Threefish block cipher
7 /// \since Crypto++ 6.0
8 
9 #ifndef CRYPTOPP_THREEFISH_H
10 #define CRYPTOPP_THREEFISH_H
11 
12 #include "config.h"
13 #include "seckey.h"
14 #include "secblock.h"
15 #include "algparam.h"
16 #include "argnames.h"
17 #include "stdcpp.h"
18 
19 NAMESPACE_BEGIN(CryptoPP)
20 
21 /// \brief Threefish block cipher information
22 /// \tparam BS block size of the cipher, in bytes
23 /// \since Crypto++ 6.0
24 template <unsigned int BS>
26 {
27  static const std::string StaticAlgorithmName()
28  {
29  // Format is Cipher-Blocksize(Keylength)
30  return "Threefish-" + IntToString(BS*8) + "(" + IntToString(BS*8) + ")";
31  }
32 };
33 
34 /// \brief Threefish block cipher base class
35 /// \tparam BS block size of the cipher, in bytes
36 /// \details User code should use Threefish256, Threefish512, Threefish1024
37 /// \sa Threefish256, Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
38 /// \since Crypto++ 6.0
39 template <unsigned int BS>
40 struct CRYPTOPP_NO_VTABLE Threefish_Base
41 {
42  virtual ~Threefish_Base() {}
43 
44  void SetTweak(const NameValuePairs &params)
45  {
46  m_tweak.New(3);
48  if (params.GetValue(Name::Tweak(), t))
49  {
50  // Tweak size is fixed at 16 for Threefish
51  CRYPTOPP_ASSERT(t.size() == 16);
52  GetUserKey(LITTLE_ENDIAN_ORDER, m_tweak.begin(), 2, t.begin(), 16);
53  m_tweak[2] = m_tweak[0] ^ m_tweak[1];
54  }
55  else
56  {
57  std::memset(m_tweak.begin(), 0x00, 24);
58  }
59  }
60 
62  mutable AlignedSecBlock64 m_wspace; // workspace
63  AlignedSecBlock64 m_rkey; // keys
64  AlignedSecBlock64 m_tweak;
65 };
66 
67 /// \brief Threefish 256-bit block cipher
68 /// \details Threefish256 provides 256-bit block size. The valid key size is 256-bit.
69 /// \note Crypto++ provides a byte oriented implementation
70 /// \sa Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
71 /// \since Crypto++ 6.0
72 class CRYPTOPP_NO_VTABLE Threefish256 : public Threefish_Info<32>, public BlockCipherDocumentation
73 {
74 public:
75  /// \brief Threefish block cipher transformation functions
76  /// \details Provides implementation common to encryption and decryption
77  /// \since Crypto++ 6.0
78  class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<32>, public BlockCipherImpl<Threefish_Info<32> >
79  {
80  protected:
81  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
82  };
83 
84  /// \brief Encryption transformation
85  /// \details Enc provides implementation for encryption transformation. All key and block
86  /// sizes are supported.
87  /// \since Crypto++ 6.0
88  class CRYPTOPP_NO_VTABLE Enc : public Base
89  {
90  protected:
91  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
92  };
93 
94  /// \brief Decryption transformation
95  /// \details Dec provides implementation for decryption transformation. All key and block
96  /// sizes are supported.
97  /// \since Crypto++ 6.0
98  class CRYPTOPP_NO_VTABLE Dec : public Base
99  {
100  protected:
101  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
102  };
103 
106 };
107 
110 
111 /// \brief Threefish 512-bit block cipher
112 /// \details Threefish512 provides 512-bit block size. The valid key size is 512-bit.
113 /// \note Crypto++ provides a byte oriented implementation
114 /// \sa Threefish256, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
115 /// \since Crypto++ 6.0
116 class CRYPTOPP_NO_VTABLE Threefish512 : public Threefish_Info<64>, public BlockCipherDocumentation
117 {
118 public:
119  /// \brief Threefish block cipher transformation functions
120  /// \details Provides implementation common to encryption and decryption
121  /// \since Crypto++ 6.0
122  class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<64>, public BlockCipherImpl<Threefish_Info<64> >
123  {
124  protected:
125  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
126  };
127 
128  /// \brief Encryption transformation
129  /// \details Enc provides implementation for encryption transformation. All key and block
130  /// sizes are supported.
131  /// \since Crypto++ 6.0
132  class CRYPTOPP_NO_VTABLE Enc : public Base
133  {
134  protected:
135  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
136  };
137 
138  /// \brief Decryption transformation
139  /// \details Dec provides implementation for decryption transformation. All key and block
140  /// sizes are supported.
141  /// \since Crypto++ 6.0
142  class CRYPTOPP_NO_VTABLE Dec : public Base
143  {
144  protected:
145  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
146  };
147 
150 };
151 
154 
155 /// \brief Threefish 1024-bit block cipher
156 /// \details Threefish1024 provides 1024-bit block size. The valid key size is 1024-bit.
157 /// \note Crypto++ provides a byte oriented implementation
158 /// \sa Threefish256, Threefish512, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>
159 /// \since Crypto++ 6.0
160 class CRYPTOPP_NO_VTABLE Threefish1024 : public Threefish_Info<128>, public BlockCipherDocumentation
161 {
162 public:
163  /// \brief Threefish block cipher transformation functions
164  /// \details Provides implementation common to encryption and decryption
165  /// \since Crypto++ 6.0
166  class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<128>, public BlockCipherImpl<Threefish_Info<128> >
167  {
168  protected:
169  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
170  };
171 
172  /// \brief Encryption transformation
173  /// \details Enc provides implementation for encryption transformation. All key and block
174  /// sizes are supported.
175  /// \since Crypto++ 6.0
176  class CRYPTOPP_NO_VTABLE Enc : public Base
177  {
178  protected:
179  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
180  };
181 
182  /// \brief Encryption transformation
183  /// \details Dec provides implementation for decryption transformation. All key and block
184  /// sizes are supported.
185  /// \since Crypto++ 6.0
186  class CRYPTOPP_NO_VTABLE Dec : public Base
187  {
188  protected:
189  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
190  };
191 
194 };
195 
198 
199 NAMESPACE_END
200 
201 #endif // CRYPTOPP_THREEFISH_H
Classes for working with NameValuePairs.
Standard names for retrieving values by name when working with NameValuePairs.
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1288
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:306
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:25
size_t size() const
Length of the memory block.
Definition: algparam.h:88
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:84
Inherited by algorithms with fixed block size.
Definition: seckey.h:41
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:125
Interface for retrieving values given their names.
Definition: cryptlib.h:327
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:384
Secure memory block with allocator and cleanup.
Definition: secblock.h:731
Threefish block cipher transformation functions.
Definition: threefish.h:167
Encryption transformation.
Definition: threefish.h:187
Encryption transformation.
Definition: threefish.h:177
Threefish 1024-bit block cipher.
Definition: threefish.h:161
Threefish block cipher transformation functions.
Definition: threefish.h:79
Decryption transformation.
Definition: threefish.h:99
Encryption transformation.
Definition: threefish.h:89
Threefish 256-bit block cipher.
Definition: threefish.h:73
Threefish block cipher transformation functions.
Definition: threefish.h:123
Decryption transformation.
Definition: threefish.h:143
Encryption transformation.
Definition: threefish.h:133
Threefish 512-bit block cipher.
Definition: threefish.h:117
Library configuration file.
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
Definition: cryptlib.h:150
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:929
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
Copy bytes in a buffer to an array of elements in big-endian order.
Definition: misc.h:2500
Crypto++ library namespace.
const char * Tweak()
ConstByteArrayParameter.
Definition: argnames.h:88
Classes and functions for secure memory allocations.
Classes and functions for implementing secret key algorithms.
Common C++ header files.
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher.
Definition: seckey.h:399
Threefish block cipher base class.
Definition: threefish.h:41
Threefish block cipher information.
Definition: threefish.h:26
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68