Crypto++ 8.9 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>
25 struct Threefish_Info : public FixedBlockSize<BS>, FixedKeyLength<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);
47 ConstByteArrayParameter t;
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
61 typedef SecBlock<word64, AllocatorWithCleanup<word64, true> > AlignedSecBlock64;
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
104 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
105 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
106 };
107
108 typedef Threefish256::Encryption Threefish256Encryption;
109 typedef Threefish256::Decryption Threefish256Decryption;
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
148 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
149 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
150 };
151
152 typedef Threefish512::Encryption Threefish512Encryption;
153 typedef Threefish512::Decryption Threefish512Decryption;
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
192 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
193 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
194 };
195
196 typedef Threefish1024::Encryption Threefish1024Encryption;
197 typedef Threefish1024::Decryption Threefish1024Decryption;
198
199 NAMESPACE_END
200
201 #endif // CRYPTOPP_THREEFISH_H
algparam.h
Classes for working with NameValuePairs.
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
BlockCipherFinal< ENCRYPTION, Enc >
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1288
BlockCipherImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:306
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:25
ConstByteArrayParameter::size
size_t size() const
Length of the memory block.
Definition: algparam.h:88
ConstByteArrayParameter::begin
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:84
FixedBlockSize
Inherited by algorithms with fixed block size.
Definition: seckey.h:41
FixedKeyLength
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:125
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
NameValuePairs::GetValue
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:384
SecBlock
Secure memory block with allocator and cleanup.
Definition: secblock.h:731
Threefish1024::Base
Threefish block cipher transformation functions.
Definition: threefish.h:167
Threefish1024::Dec
Encryption transformation.
Definition: threefish.h:187
Threefish1024::Enc
Encryption transformation.
Definition: threefish.h:177
Threefish1024
Threefish 1024-bit block cipher.
Definition: threefish.h:161
Threefish256::Base
Threefish block cipher transformation functions.
Definition: threefish.h:79
Threefish256::Dec
Decryption transformation.
Definition: threefish.h:99
Threefish256::Enc
Encryption transformation.
Definition: threefish.h:89
Threefish256
Threefish 256-bit block cipher.
Definition: threefish.h:73
Threefish512::Base
Threefish block cipher transformation functions.
Definition: threefish.h:123
Threefish512::Dec
Decryption transformation.
Definition: threefish.h:143
Threefish512::Enc
Encryption transformation.
Definition: threefish.h:133
Threefish512
Threefish 512-bit block cipher.
Definition: threefish.h:117
config.h
Library configuration file.
LITTLE_ENDIAN_ORDER
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
Definition: cryptlib.h:150
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:929
GetUserKey
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
CryptoPP
Crypto++ library namespace.
Name::Tweak
const char * Tweak()
ConstByteArrayParameter.
Definition: argnames.h:88
secblock.h
Classes and functions for secure memory allocations.
seckey.h
Classes and functions for implementing secret key algorithms.
stdcpp.h
Common C++ header files.
BlockCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher.
Definition: seckey.h:399
Threefish_Base
Threefish block cipher base class.
Definition: threefish.h:41
Threefish_Info
Threefish block cipher information.
Definition: threefish.h:26
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68