Crypto++ 8.2
Free C&
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_ARMV8 || \
22 CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
23# ifndef CRYPTOPP_DISABLE_SPECK_SIMD
24# define CRYPTOPP_SPECK64_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_SPECK_SIMD
32# define CRYPTOPP_SPECK128_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_SPECK64_ADVANCED_PROCESS_BLOCKS
40# undef CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
41#endif
42
43NAMESPACE_BEGIN(CryptoPP)
44
45/// \brief SPECK 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 SPECK_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 "SPECK-" + IntToString(L*8);
62 }
63};
64
65/// \brief SPECK block cipher base class
66/// \tparam W the word type
67/// \details User code should use SPECK64 or SPECK128
68/// \sa SPECK64, SPECK128, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a>
69/// \since Crypto++ 6.0
70template <class W>
72{
73 virtual ~SPECK_Base() {}
74 SPECK_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 SPECK 64-bit block cipher
84/// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
85/// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
86/// \details SPECK64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
87/// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
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/SPECK">SPECK</a> on the
90/// Crypto++ wiki
91/// \since Crypto++ 6.0
92class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
93{
94public:
95 /// \brief SPECK 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 SPECK_Base<word32>, public BlockCipherImpl<SPECK_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_SPECK64_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_SPECK64_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 SPECK 128-bit block cipher
147/// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
148/// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
149/// \details SPECK128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
150/// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
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/SPECK">SPECK</a> on the
153/// Crypto++ wiki
154/// \since Crypto++ 6.0
155class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
156{
157public:
158 /// \brief SPECK 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 SPECK_Base<word64>, public BlockCipherImpl<SPECK_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_SPECK128_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_SPECK128_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_SPECK_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
SPECK block cipher transformation functions.
Definition: speck.h:162
std::string AlgorithmName() const
The algorithm name.
Definition: speck.h:168
Encryption transformation.
Definition: speck.h:197
Encryption transformation.
Definition: speck.h:184
SPECK 128-bit block cipher.
Definition: speck.h:156
SPECK block cipher transformation functions.
Definition: speck.h:99
std::string AlgorithmName() const
The algorithm name.
Definition: speck.h:105
Encryption transformation.
Definition: speck.h:134
Encryption transformation.
Definition: speck.h:121
SPECK 64-bit block cipher.
Definition: speck.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
SPECK block cipher base class.
Definition: speck.h:72
SPECK block cipher information.
Definition: speck.h:53
static const std::string StaticAlgorithmName()
The algorithm name.
Definition: speck.h:58