Crypto++  7.0
Free C++ class library of cryptographic schemes
speck.h
Go to the documentation of this file.
1 // speck.h - written and placed in the public domain by Jeffrey Walton
2 
3 /// \file speck.h
4 /// \brief Classes for the Speck block cipher
5 /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
6 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
7 /// \sa <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK Families of
8 /// Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
9 /// The Simon and Speck GitHub</A> and <A HREF="https://www.cryptopp.com/wiki/SPECK">
10 /// SPECK</A> on the Crypto++ wiki.
11 /// \since Crypto++ 6.0
12 
13 #ifndef CRYPTOPP_SPECK_H
14 #define CRYPTOPP_SPECK_H
15 
16 #include "config.h"
17 #include "seckey.h"
18 #include "secblock.h"
19 
20 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64
21 # define CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS 1
22 #endif
23 
24 #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64
25 # define CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS 1
26 #endif
27 
28 NAMESPACE_BEGIN(CryptoPP)
29 
30 /// \brief SPECK block cipher information
31 /// \tparam L block size of the cipher, in bytes
32 /// \tparam D default key length, in bytes
33 /// \tparam N minimum key length, in bytes
34 /// \tparam M maximum key length, in bytes
35 /// \since Crypto++ 6.0
36 template <unsigned int L, unsigned int D, unsigned int N, unsigned int M>
37 struct SPECK_Info : public FixedBlockSize<L>, VariableKeyLength<D, N, M>
38 {
39  static const std::string StaticAlgorithmName()
40  {
41  // Format is Cipher-Blocksize(Keylength)
42  return "SPECK-" + IntToString(L*8);
43  }
44 };
45 
46 /// \brief SPECK block cipher base class
47 /// \tparam W the word type
48 /// \details User code should use SPECK64 or SPECK128
49 /// \sa SPECK64, SPECK128, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a>
50 /// \since Crypto++ 6.0
51 template <class W>
52 struct SPECK_Base
53 {
54  virtual ~SPECK_Base() {}
55  SPECK_Base() : m_kwords(0), m_rounds(0) {}
56 
58  mutable AlignedSecBlock m_wspace; // workspace
59  AlignedSecBlock m_rkeys; // round keys
60  unsigned int m_kwords; // number of key words
61  unsigned int m_rounds; // number of rounds
62 };
63 
64 /// \brief SPECK 64-bit block cipher
65 /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
66 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
67 /// \details SPECK64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
68 /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
69 /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
70 /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
71 /// Crypto++ wiki
72 /// \since Crypto++ 6.0
73 class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
74 {
75 public:
76  /// \brief SPECK block cipher transformation functions
77  /// \details Provides implementation common to encryption and decryption
78  /// \since Crypto++ 6.0
79  class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word32>, public BlockCipherImpl<SPECK_Info<8, 12, 12, 16> >
80  {
81  public:
82  std::string AlgorithmName() const {
83  return StaticAlgorithmName() + (m_kwords == 0 ? "" :
84  "(" + IntToString(m_kwords*sizeof(word32)*8) + ")");
85  }
86 
87  std::string AlgorithmProvider() const;
88 
89  protected:
90  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
91  };
92 
93  /// \brief Encryption transformation
94  /// \details Enc provides implementation for encryption transformation. All key
95  /// sizes are supported.
96  /// \since Crypto++ 6.0
97  class CRYPTOPP_NO_VTABLE Enc : public Base
98  {
99  public:
100  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
101 #if CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS
102  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
103 #endif
104  };
105 
106  /// \brief Encryption transformation
107  /// \details Dec provides implementation for decryption transformation. All key
108  /// sizes are supported.
109  /// \since Crypto++ 6.0
110  class CRYPTOPP_NO_VTABLE Dec : public Base
111  {
112  public:
113  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
114 #if CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS
115  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
116 #endif
117  };
118 
121 };
122 
123 /// \brief SPECK 128-bit block cipher
124 /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
125 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
126 /// \details SPECK128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
127 /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
128 /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
129 /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
130 /// Crypto++ wiki
131 /// \since Crypto++ 6.0
132 class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
133 {
134 public:
135  /// \brief SPECK block cipher transformation functions
136  /// \details Provides implementation common to encryption and decryption
137  /// \since Crypto++ 6.0
138  class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word64>, public BlockCipherImpl<SPECK_Info<16, 16, 16, 32> >
139  {
140  public:
141  std::string AlgorithmName() const {
142  return StaticAlgorithmName() + (m_kwords == 0 ? "" :
143  "(" + IntToString(m_kwords*sizeof(word64)*8) + ")");
144  }
145 
146  std::string AlgorithmProvider() const;
147 
148  protected:
149  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
150  };
151 
152  /// \brief Encryption transformation
153  /// \details Enc provides implementation for encryption transformation. All key
154  /// sizes are supported.
155  /// \since Crypto++ 6.0
156  class CRYPTOPP_NO_VTABLE Enc : public Base
157  {
158  public:
159  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
160 #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
161  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
162 #endif
163  };
164 
165  /// \brief Encryption transformation
166  /// \details Dec provides implementation for decryption transformation. All key
167  /// sizes are supported.
168  /// \since Crypto++ 6.0
169  class CRYPTOPP_NO_VTABLE Dec : public Base
170  {
171  public:
172  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
173 #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
174  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
175 #endif
176  };
177 
180 };
181 
182 NAMESPACE_END
183 
184 #endif // CRYPTOPP_SPECK_H
static std::string StaticAlgorithmName()
The algorithm name.
Definition: simple.h:43
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:421
Encryption transformation.
Definition: speck.h:169
std::string AlgorithmName() const
The algorithm name.
Definition: simple.h:48
Secure memory block with allocator and cleanup.
Definition: secblock.h:454
Library configuration file.
SPECK block cipher transformation functions.
Definition: speck.h:79
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1252
SPECK block cipher base class.
Definition: speck.h:52
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
SPECK block cipher transformation functions.
Definition: speck.h:138
Classes and functions for implementing secret key algorithms.
Encryption transformation.
Definition: speck.h:110
SPECK 128-bit block cipher.
Definition: speck.h:132
Encryption transformation.
Definition: speck.h:97
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:188
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:632
SPECK 64-bit block cipher.
Definition: speck.h:73
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:328
Crypto++ library namespace.
Encryption transformation.
Definition: speck.h:156
SPECK block cipher information.
Definition: speck.h:37
Interface for retrieving values given their names.
Definition: cryptlib.h:291