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 {
StaticAlgorithmNameThreefish_Info27     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 {
~Threefish_BaseThreefish_Base42 	virtual ~Threefish_Base() {}
43 
SetTweakThreefish_Base44     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
202