Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:10

0001 // threefish.h - written and placed in the public domain by Jeffrey Walton

0002 //               Based on public domain code by Keru Kuro. Kuro's code is

0003 //               available at http://cppcrypto.sourceforge.net/.

0004 
0005 /// \file Threefish.h

0006 /// \brief Classes for the Threefish block cipher

0007 /// \since Crypto++ 6.0

0008 
0009 #ifndef CRYPTOPP_THREEFISH_H
0010 #define CRYPTOPP_THREEFISH_H
0011 
0012 #include "config.h"
0013 #include "seckey.h"
0014 #include "secblock.h"
0015 #include "algparam.h"
0016 #include "argnames.h"
0017 #include "stdcpp.h"
0018 
0019 NAMESPACE_BEGIN(CryptoPP)
0020 
0021 /// \brief Threefish block cipher information

0022 /// \tparam BS block size of the cipher, in bytes

0023 /// \since Crypto++ 6.0

0024 template <unsigned int BS>
0025 struct Threefish_Info : public FixedBlockSize<BS>, FixedKeyLength<BS>
0026 {
0027     static const std::string StaticAlgorithmName()
0028     {
0029         // Format is Cipher-Blocksize(Keylength)

0030         return "Threefish-" + IntToString(BS*8) + "(" + IntToString(BS*8) + ")";
0031     }
0032 };
0033 
0034 /// \brief Threefish block cipher base class

0035 /// \tparam BS block size of the cipher, in bytes

0036 /// \details User code should use Threefish256, Threefish512, Threefish1024

0037 /// \sa Threefish256, Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>

0038 /// \since Crypto++ 6.0

0039 template <unsigned int BS>
0040 struct CRYPTOPP_NO_VTABLE Threefish_Base
0041 {
0042     virtual ~Threefish_Base() {}
0043 
0044     void SetTweak(const NameValuePairs &params)
0045     {
0046         m_tweak.New(3);
0047         ConstByteArrayParameter t;
0048         if (params.GetValue(Name::Tweak(), t))
0049         {
0050             // Tweak size is fixed at 16 for Threefish

0051             CRYPTOPP_ASSERT(t.size() == 16);
0052             GetUserKey(LITTLE_ENDIAN_ORDER, m_tweak.begin(), 2, t.begin(), 16);
0053             m_tweak[2] = m_tweak[0] ^ m_tweak[1];
0054         }
0055         else
0056         {
0057             std::memset(m_tweak.begin(), 0x00, 24);
0058         }
0059     }
0060 
0061     typedef SecBlock<word64, AllocatorWithCleanup<word64, true> > AlignedSecBlock64;
0062     mutable AlignedSecBlock64 m_wspace;   // workspace

0063     AlignedSecBlock64         m_rkey;     // keys

0064     AlignedSecBlock64         m_tweak;
0065 };
0066 
0067 /// \brief Threefish 256-bit block cipher

0068 /// \details Threefish256 provides 256-bit block size. The valid key size is 256-bit.

0069 /// \note Crypto++ provides a byte oriented implementation

0070 /// \sa Threefish512, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>

0071 /// \since Crypto++ 6.0

0072 class CRYPTOPP_NO_VTABLE Threefish256 : public Threefish_Info<32>, public BlockCipherDocumentation
0073 {
0074 public:
0075     /// \brief Threefish block cipher transformation functions

0076     /// \details Provides implementation common to encryption and decryption

0077     /// \since Crypto++ 6.0

0078     class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<32>, public BlockCipherImpl<Threefish_Info<32> >
0079     {
0080     protected:
0081         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0082     };
0083 
0084     /// \brief Encryption transformation

0085     /// \details Enc provides implementation for encryption transformation. All key and block

0086     ///   sizes are supported.

0087     /// \since Crypto++ 6.0

0088     class CRYPTOPP_NO_VTABLE Enc : public Base
0089     {
0090     protected:
0091         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0092     };
0093 
0094     /// \brief Decryption transformation

0095     /// \details Dec provides implementation for decryption transformation. All key and block

0096     ///   sizes are supported.

0097     /// \since Crypto++ 6.0

0098     class CRYPTOPP_NO_VTABLE Dec : public Base
0099     {
0100     protected:
0101         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0102     };
0103 
0104     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0105     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0106 };
0107 
0108 typedef Threefish256::Encryption Threefish256Encryption;
0109 typedef Threefish256::Decryption Threefish256Decryption;
0110 
0111 /// \brief Threefish 512-bit block cipher

0112 /// \details Threefish512 provides 512-bit block size. The valid key size is 512-bit.

0113 /// \note Crypto++ provides a byte oriented implementation

0114 /// \sa Threefish256, Threefish1024, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>

0115 /// \since Crypto++ 6.0

0116 class CRYPTOPP_NO_VTABLE Threefish512 : public Threefish_Info<64>, public BlockCipherDocumentation
0117 {
0118 public:
0119     /// \brief Threefish block cipher transformation functions

0120     /// \details Provides implementation common to encryption and decryption

0121     /// \since Crypto++ 6.0

0122     class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<64>, public BlockCipherImpl<Threefish_Info<64> >
0123     {
0124     protected:
0125         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0126     };
0127 
0128     /// \brief Encryption transformation

0129     /// \details Enc provides implementation for encryption transformation. All key and block

0130     ///   sizes are supported.

0131     /// \since Crypto++ 6.0

0132     class CRYPTOPP_NO_VTABLE Enc : public Base
0133     {
0134     protected:
0135         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0136     };
0137 
0138     /// \brief Decryption transformation

0139     /// \details Dec provides implementation for decryption transformation. All key and block

0140     ///   sizes are supported.

0141     /// \since Crypto++ 6.0

0142     class CRYPTOPP_NO_VTABLE Dec : public Base
0143     {
0144     protected:
0145         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0146     };
0147 
0148     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0149     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0150 };
0151 
0152 typedef Threefish512::Encryption Threefish512Encryption;
0153 typedef Threefish512::Decryption Threefish512Decryption;
0154 
0155 /// \brief Threefish 1024-bit block cipher

0156 /// \details Threefish1024 provides 1024-bit block size. The valid key size is 1024-bit.

0157 /// \note Crypto++ provides a byte oriented implementation

0158 /// \sa Threefish256, Threefish512, <a href="http://www.cryptopp.com/wiki/Threefish">Threefish</a>

0159 /// \since Crypto++ 6.0

0160 class CRYPTOPP_NO_VTABLE Threefish1024 : public Threefish_Info<128>, public BlockCipherDocumentation
0161 {
0162 public:
0163     /// \brief Threefish block cipher transformation functions

0164     /// \details Provides implementation common to encryption and decryption

0165     /// \since Crypto++ 6.0

0166     class CRYPTOPP_NO_VTABLE Base : public Threefish_Base<128>, public BlockCipherImpl<Threefish_Info<128> >
0167     {
0168     protected:
0169         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0170     };
0171 
0172     /// \brief Encryption transformation

0173     /// \details Enc provides implementation for encryption transformation. All key and block

0174     ///   sizes are supported.

0175     /// \since Crypto++ 6.0

0176     class CRYPTOPP_NO_VTABLE Enc : public Base
0177     {
0178     protected:
0179         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0180     };
0181 
0182     /// \brief Encryption transformation

0183     /// \details Dec provides implementation for decryption transformation. All key and block

0184     ///   sizes are supported.

0185     /// \since Crypto++ 6.0

0186     class CRYPTOPP_NO_VTABLE Dec : public Base
0187     {
0188     protected:
0189         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0190     };
0191 
0192     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0193     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0194 };
0195 
0196 typedef Threefish1024::Encryption Threefish1024Encryption;
0197 typedef Threefish1024::Decryption Threefish1024Decryption;
0198 
0199 NAMESPACE_END
0200 
0201 #endif  // CRYPTOPP_THREEFISH_H