Crypto++ 8.1 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_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
43 NAMESPACE_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
51 template <unsigned int L, unsigned int D, unsigned int N, unsigned int M>
52 struct 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
70 template <class W>
71 struct SPECK_Base
72 {
73 virtual ~SPECK_Base() {}
74 SPECK_Base() : m_kwords(0), m_rounds(0) {}
75
76 typedef SecBlock<W, AllocatorWithCleanup<W, true> > AlignedSecBlock;
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
92 class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
93 {
94 public:
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
142 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
143 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
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
155 class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
156 {
157 public:
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
205 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
206 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
207 };
208
209 NAMESPACE_END
210
211 #endif // CRYPTOPP_SPECK_H
AlgorithmImpl< SimpleKeyingInterfaceImpl< TwoBases< BASE, INFO > > >::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm name.
Definition: simple.h:43
SPECK64::Base::AlgorithmName
std::string AlgorithmName() const
The algorithm name.
Definition: speck.h:105
BlockCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:398
SPECK128::Dec
Encryption transformation.
Definition: speck.h:196
SecBlock
Secure memory block with allocator and cleanup.
Definition: secblock.h:688
config.h
Library configuration file.
SPECK64::Base
SPECK block cipher transformation functions.
Definition: speck.h:98
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1250
SPECK_Base
SPECK block cipher base class.
Definition: speck.h:71
secblock.h
Classes and functions for secure memory allocations.
FixedBlockSize
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
SPECK128::Base::AlgorithmName
std::string AlgorithmName() const
The algorithm name.
Definition: speck.h:168
SPECK128::Base
SPECK block cipher transformation functions.
Definition: speck.h:161
seckey.h
Classes and functions for implementing secret key algorithms.
BlockCipherFinal< ENCRYPTION, Enc >
SPECK64::Dec
Encryption transformation.
Definition: speck.h:133
SPECK128
SPECK 128-bit block cipher.
Definition: speck.h:155
SPECK64::Enc
Encryption transformation.
Definition: speck.h:120
VariableKeyLength
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:165
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:604
SPECK64
SPECK 64-bit block cipher.
Definition: speck.h:92
BlockCipherImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:305
CryptoPP
Crypto++ library namespace.
SPECK128::Enc
Encryption transformation.
Definition: speck.h:183
SPECK_Info::StaticAlgorithmName
static const std::string StaticAlgorithmName()
The algorithm name.
Definition: speck.h:58
SPECK_Info
SPECK block cipher information.
Definition: speck.h:52
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293