Crypto++  8.2
Free C++ class library of cryptographic schemes
tea.h
Go to the documentation of this file.
1 // tea.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file tea.h
4 /// \brief Classes for the TEA, BTEA and XTEA block ciphers
5 
6 #ifndef CRYPTOPP_TEA_H
7 #define CRYPTOPP_TEA_H
8 
9 #include "seckey.h"
10 #include "secblock.h"
11 #include "misc.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 /// \brief TEA block cipher information
16 struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
17 {
18  /// \brief The algorithm name
19  /// \returns the algorithm name
20  /// \details StaticAlgorithmName returns the algorithm's name as a static
21  /// member function.
22  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "TEA";}
23 };
24 
25 /// \brief TEA block cipher
26 /// \sa <a href="http://www.cryptopp.com/wiki/TEA">TEA</a>
27 class TEA : public TEA_Info, public BlockCipherDocumentation
28 {
29  /// \brief TEA block cipher default operation
30  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<TEA_Info>
31  {
32  public:
33  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
34 
35  protected:
37  word32 m_limit;
38  };
39 
40  /// \brief TEA block cipher encryption operation
41  class CRYPTOPP_NO_VTABLE Enc : public Base
42  {
43  public:
44  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
45  };
46 
47  /// \brief TEA block cipher decryption operation
48  class CRYPTOPP_NO_VTABLE Dec : public Base
49  {
50  public:
51  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
52  };
53 
54 public:
57 };
58 
61 
62 /// \brief XTEA block cipher information
63 struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32>
64 {
65  /// \brief The algorithm name
66  /// \returns the algorithm name
67  /// \details StaticAlgorithmName returns the algorithm's name as a static
68  /// member function.
69  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "XTEA";}
70 };
71 
72 /// \brief XTEA block cipher
73 /// \sa <a href="http://www.cryptopp.com/wiki/TEA">XTEA</a>
74 class XTEA : public XTEA_Info, public BlockCipherDocumentation
75 {
76  /// \brief XTEA block cipher default operation
77  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<XTEA_Info>
78  {
79  public:
80  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
81 
82  protected:
84  word32 m_limit;
85  };
86 
87  /// \brief XTEA block cipher encryption operation
88  class CRYPTOPP_NO_VTABLE Enc : public Base
89  {
90  public:
91  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
92  };
93 
94  /// \brief XTEA block cipher decryption operation
95  class CRYPTOPP_NO_VTABLE Dec : public Base
96  {
97  public:
98  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
99  };
100 
101 public:
104 };
105 
106 /// \brief BTEA block cipher information
107 struct BTEA_Info : public FixedKeyLength<16>
108 {
109  /// \brief The algorithm name
110  /// \returns the algorithm name
111  /// \details StaticAlgorithmName returns the algorithm's name as a static
112  /// member function.
113  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "BTEA";}
114 };
115 
116 /// \brief BTEA block cipher
117 /// \details Corrected Block TEA as described in "xxtea". This class hasn't been tested yet.
118 /// \sa <A HREF="http://www.movable-type.co.uk/scripts/xxtea.pdf">Correction to xtea</A> and
119 /// <a href="http://www.cryptopp.com/wiki/TEA">Corrected Block TEA</a>.
121 {
122  /// \brief BTEA block cipher default operation
123  class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BlockCipher, BTEA_Info>, BTEA_Info>
124  {
125  public:
126  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
127  {
128  CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params);
129  m_blockSize = params.GetIntValueWithDefault("BlockSize", 60*4);
130  GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, key, KEYLENGTH);
131  }
132 
133  unsigned int BlockSize() const {return m_blockSize;}
134 
135  protected:
137  unsigned int m_blockSize;
138  };
139 
140  /// \brief BTEA block cipher encryption operation
141  class CRYPTOPP_NO_VTABLE Enc : public Base
142  {
143  public:
144  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
145  };
146 
147  /// \brief BTEA block cipher decryption operation
148  class CRYPTOPP_NO_VTABLE Dec : public Base
149  {
150  public:
151  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
152  };
153 
154 public:
157 };
158 
159 NAMESPACE_END
160 
161 #endif
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:395
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:124
Utility functions for the Crypto++ library.
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:398
static const int KEYLENGTH
The default key length used by the algorithm provided as a constant.
Definition: seckey.h:129
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1250
Classes and functions for secure memory allocations.
BTEA block cipher.
Definition: tea.h:120
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
Inherited by algorithms with variable number of rounds.
Definition: seckey.h:64
static const char * StaticAlgorithmName()
The algorithm name.
Definition: tea.h:113
Classes and functions for implementing secret key algorithms.
XTEA block cipher information.
Definition: tea.h:63
XTEA block cipher.
Definition: tea.h:74
TEA block cipher information.
Definition: tea.h:16
TEA block cipher.
Definition: tea.h:27
byte order is big-endian
Definition: cryptlib.h:147
const char * BlockSize()
int, in bytes
Definition: argnames.h:27
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:305
BTEA block cipher information.
Definition: tea.h:107
Crypto++ library namespace.
static const char * StaticAlgorithmName()
The algorithm name.
Definition: tea.h:69
Interface for retrieving values given their names.
Definition: cryptlib.h:293
Base class information.
Definition: simple.h:36