Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // tea.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file tea.h

0004 /// \brief Classes for the TEA, BTEA and XTEA block ciphers

0005 
0006 #ifndef CRYPTOPP_TEA_H
0007 #define CRYPTOPP_TEA_H
0008 
0009 #include "seckey.h"
0010 #include "secblock.h"
0011 #include "misc.h"
0012 
0013 NAMESPACE_BEGIN(CryptoPP)
0014 
0015 /// \brief TEA block cipher information

0016 struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
0017 {
0018     /// \brief The algorithm name

0019     /// \return the algorithm name

0020     /// \details StaticAlgorithmName returns the algorithm's name as a static

0021     ///   member function.

0022     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "TEA";}
0023 };
0024 
0025 /// \brief TEA block cipher

0026 /// \sa <a href="http://www.cryptopp.com/wiki/TEA">TEA</a>

0027 class TEA : public TEA_Info, public BlockCipherDocumentation
0028 {
0029     /// \brief TEA block cipher default operation

0030     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<TEA_Info>
0031     {
0032     public:
0033         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0034 
0035     protected:
0036         FixedSizeSecBlock<word32, 4> m_k;
0037         word32 m_limit;
0038     };
0039 
0040     /// \brief TEA block cipher encryption operation

0041     class CRYPTOPP_NO_VTABLE Enc : public Base
0042     {
0043     public:
0044         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0045     };
0046 
0047     /// \brief TEA block cipher decryption operation

0048     class CRYPTOPP_NO_VTABLE Dec : public Base
0049     {
0050     public:
0051         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0052     };
0053 
0054 public:
0055     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0056     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0057 };
0058 
0059 typedef TEA::Encryption TEAEncryption;
0060 typedef TEA::Decryption TEADecryption;
0061 
0062 /// \brief XTEA block cipher information

0063 struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
0064 {
0065     /// \brief The algorithm name

0066     /// \return the algorithm name

0067     /// \details StaticAlgorithmName returns the algorithm's name as a static

0068     ///   member function.

0069     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "XTEA";}
0070 };
0071 
0072 /// \brief XTEA block cipher

0073 /// \sa <a href="http://www.cryptopp.com/wiki/TEA">XTEA</a>

0074 class XTEA : public XTEA_Info, public BlockCipherDocumentation
0075 {
0076     /// \brief XTEA block cipher default operation

0077     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<XTEA_Info>
0078     {
0079     public:
0080         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0081 
0082     protected:
0083         FixedSizeSecBlock<word32, 4> m_k;
0084         word32 m_limit;
0085     };
0086 
0087     /// \brief XTEA block cipher encryption operation

0088     class CRYPTOPP_NO_VTABLE Enc : public Base
0089     {
0090     public:
0091         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0092     };
0093 
0094     /// \brief XTEA block cipher decryption operation

0095     class CRYPTOPP_NO_VTABLE Dec : public Base
0096     {
0097     public:
0098         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0099     };
0100 
0101 public:
0102     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0103     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0104 };
0105 
0106 /// \brief BTEA block cipher information

0107 struct BTEA_Info : public FixedKeyLength<16>
0108 {
0109     /// \brief The algorithm name

0110     /// \return the algorithm name

0111     /// \details StaticAlgorithmName returns the algorithm's name as a static

0112     ///   member function.

0113     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BTEA";}
0114 };
0115 
0116 /// \brief BTEA block cipher

0117 /// \details Corrected Block TEA as described in "xxtea". This class hasn't been tested yet.

0118 /// \sa <A HREF="http://www.movable-type.co.uk/scripts/xxtea.pdf">Correction to xtea</A> and

0119 ///   <a href="http://www.cryptopp.com/wiki/TEA">Corrected Block TEA</a>.

0120 class BTEA : public BTEA_Info, public BlockCipherDocumentation
0121 {
0122     /// \brief BTEA block cipher default operation

0123     class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BlockCipher, BTEA_Info>, BTEA_Info>
0124     {
0125     public:
0126         void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
0127         {
0128             CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params);
0129             m_blockSize = params.GetIntValueWithDefault("BlockSize", 60*4);
0130             GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, key, KEYLENGTH);
0131         }
0132 
0133         unsigned int BlockSize() const {return m_blockSize;}
0134 
0135     protected:
0136         FixedSizeSecBlock<word32, 4> m_k;
0137         unsigned int m_blockSize;
0138     };
0139 
0140     /// \brief BTEA block cipher encryption operation

0141     class CRYPTOPP_NO_VTABLE Enc : public Base
0142     {
0143     public:
0144         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0145     };
0146 
0147     /// \brief BTEA block cipher decryption operation

0148     class CRYPTOPP_NO_VTABLE Dec : public Base
0149     {
0150     public:
0151         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0152     };
0153 
0154 public:
0155     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0156     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0157 };
0158 
0159 NAMESPACE_END
0160 
0161 #endif