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