Crypto++  7.0
Free C++ class library of cryptographic schemes
simon.h
Go to the documentation of this file.
1 // simon.h - written and placed in the public domain by Jeffrey Walton
2 
3 /// \file simon.h
4 /// \brief Classes for the Simon block cipher
5 /// \details Simon 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/SIMON">
10 /// SIMON</A> on the Crypto++ wiki.
11 /// \since Crypto++ 6.0
12 
13 #ifndef CRYPTOPP_SIMON_H
14 #define CRYPTOPP_SIMON_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_SIMON64_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_SIMON128_ADVANCED_PROCESS_BLOCKS 1
26 #endif
27 
28 NAMESPACE_BEGIN(CryptoPP)
29 
30 /// \brief SIMON 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 SIMON_Info : public FixedBlockSize<L>, VariableKeyLength<D, N, M>
38 {
39  static const std::string StaticAlgorithmName()
40  {
41  // Format is Cipher-Blocksize(Keylength)
42  return "SIMON-" + IntToString(L*8);
43  }
44 };
45 
46 /// \brief SIMON block cipher base class
47 /// \tparam W the word type
48 /// \details User code should use SIMON64 or SIMON128
49 /// \sa SIMON64, SIMON128, <a href="http://www.cryptopp.com/wiki/SIMON">SIMON</a> on the Crypto++ wiki
50 /// \since Crypto++ 6.0
51 template <class W>
52 struct SIMON_Base
53 {
54  virtual ~SIMON_Base() {}
55 SIMON_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 SIMON 64-bit block cipher
65 /// \details Simon is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
66 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
67 /// \details SIMON64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
68 /// \sa SIMON64, SIMON128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SIMON
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/SIMON">SIMON</a> on the
71 /// Crypto++ wiki
72 /// \since Crypto++ 6.0
73 class CRYPTOPP_NO_VTABLE SIMON64 : public SIMON_Info<8, 12, 12, 16>, public BlockCipherDocumentation
74 {
75 public:
76  /// \brief SIMON 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 SIMON_Base<word32>, public BlockCipherImpl<SIMON_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  protected:
88  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
89  };
90 
91  /// \brief Provides implementation for encryption transformation
92  /// \details Enc provides implementation for encryption transformation. All key
93  /// sizes are supported.
94  /// \since Crypto++ 6.0
95  class CRYPTOPP_NO_VTABLE Enc : public Base
96  {
97  protected:
98  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
99 #if CRYPTOPP_SIMON64_ADVANCED_PROCESS_BLOCKS
100  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
101 #endif
102  };
103 
104  /// \brief Provides implementation for encryption transformation
105  /// \details Dec provides implementation for decryption transformation. All key
106  /// sizes are supported.
107  /// \since Crypto++ 6.0
108  class CRYPTOPP_NO_VTABLE Dec : public Base
109  {
110  protected:
111  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
112 #if CRYPTOPP_SIMON64_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 
119 };
120 
121 /// \brief SIMON 128-bit block cipher
122 /// \details Simon is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
123 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
124 /// \details SIMON128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
125 /// \sa SIMON64, SIMON128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SIMON
126 /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
127 /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SIMON">SIMON</a> on the
128 /// Crypto++ wiki
129 /// \since Crypto++ 6.0
130 class CRYPTOPP_NO_VTABLE SIMON128 : public SIMON_Info<16, 16, 16, 32>, public BlockCipherDocumentation
131 {
132 public:
133  /// \brief SIMON block cipher transformation functions
134  /// \details Provides implementation common to encryption and decryption
135  /// \since Crypto++ 6.0
136  class CRYPTOPP_NO_VTABLE Base : protected SIMON_Base<word64>, public BlockCipherImpl<SIMON_Info<16, 16, 16, 32> >
137  {
138  public:
139  std::string AlgorithmName() const {
140  return StaticAlgorithmName() + (m_kwords == 0 ? "" :
141  "(" + IntToString(m_kwords*sizeof(word64)*8) + ")");
142  }
143 
144  protected:
145  void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
146  };
147 
148  /// \brief Provides implementation for encryption transformation
149  /// \details Enc provides implementation for encryption transformation. All key
150  /// sizes are supported.
151  /// \since Crypto++ 6.0
152  class CRYPTOPP_NO_VTABLE Enc : public Base
153  {
154  protected:
155  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
156 #if CRYPTOPP_SIMON128_ADVANCED_PROCESS_BLOCKS
157  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
158 #endif
159  };
160 
161  /// \brief Provides implementation for encryption transformation
162  /// \details Dec provides implementation for decryption transformation. All key
163  /// sizes are supported.
164  /// \since Crypto++ 6.0
165  class CRYPTOPP_NO_VTABLE Dec : public Base
166  {
167  protected:
168  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
169 #if CRYPTOPP_SIMON128_ADVANCED_PROCESS_BLOCKS
170  size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
171 #endif
172  };
173 
176 };
177 
178 NAMESPACE_END
179 
180 #endif // CRYPTOPP_SIMON_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:420
Provides implementation for encryption transformation.
Definition: simon.h:165
std::string AlgorithmName() const
The algorithm name.
Definition: simple.h:48
Secure memory block with allocator and cleanup.
Definition: secblock.h:454
Provides implementation for encryption transformation.
Definition: simon.h:152
Library configuration file.
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1229
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
SIMON 64-bit block cipher.
Definition: simon.h:73
Provides implementation for encryption transformation.
Definition: simon.h:95
SIMON block cipher transformation functions.
Definition: simon.h:79
Classes and functions for implementing secret key algorithms.
SIMON 128-bit block cipher.
Definition: simon.h:130
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:188
Provides implementation for encryption transformation.
Definition: simon.h:108
SIMON block cipher transformation functions.
Definition: simon.h:136
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:576
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:327
Crypto++ library namespace.
SIMON block cipher information.
Definition: simon.h:37
SIMON block cipher base class.
Definition: simon.h:52
Interface for retrieving values given their names.
Definition: cryptlib.h:290