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