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_ARMV8 || \
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_ARMV8 || \
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 /// \brief The algorithm name
51 /// \returns the algorithm name
52 /// \details StaticAlgorithmName returns the algorithm's name as a static
53 /// member function.
54 static const std::string StaticAlgorithmName()
55 {
56 // Format is Cipher-Blocksize(Keylength)
57 return "SPECK-" + IntToString(L*8);
58 }
59 };
60
61 /// \brief SPECK block cipher base class
62 /// \tparam W the word type
63 /// \details User code should use SPECK64 or SPECK128
64 /// \sa SPECK64, SPECK128, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a>
65 /// \since Crypto++ 6.0
66 template <class W>
67 struct SPECK_Base
68 {
69 virtual ~SPECK_Base() {}
70 SPECK_Base() : m_kwords(0), m_rounds(0) {}
71
72 typedef SecBlock<W, AllocatorWithCleanup<W, true> > AlignedSecBlock;
73 mutable AlignedSecBlock m_wspace; // workspace
74 AlignedSecBlock m_rkeys; // round keys
75 unsigned int m_kwords; // number of key words
76 unsigned int m_rounds; // number of rounds
77 };
78
79 /// \brief SPECK 64-bit block cipher
80 /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
81 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
82 /// \details SPECK64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
83 /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
84 /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
85 /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
86 /// Crypto++ wiki
87 /// \since Crypto++ 6.0
88 class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
89 {
90 public:
91 /// \brief SPECK block cipher transformation functions
92 /// \details Provides implementation common to encryption and decryption
93 /// \since Crypto++ 6.0
94 class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word32>, public BlockCipherImpl<SPECK_Info<8, 12, 12, 16> >
95 {
96 public:
97 /// \brief The algorithm name
98 /// \returns the algorithm name
99 /// \details AlgorithmName returns the algorithm's name as a
100 /// member function.
101 std::string AlgorithmName() const {
102 return StaticAlgorithmName() + (m_kwords == 0 ? "" :
103 "(" + IntToString(m_kwords*sizeof(word32)*8) + ")");
104 }
105
106 std::string AlgorithmProvider() const;
107
108 protected:
109 void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
110 };
111
112 /// \brief Encryption transformation
113 /// \details Enc provides implementation for encryption transformation. All key
114 /// sizes are supported.
115 /// \since Crypto++ 6.0
116 class CRYPTOPP_NO_VTABLE Enc : public Base
117 {
118 public:
119 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
120 #if CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS
121 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
122 #endif
123 };
124
125 /// \brief Encryption transformation
126 /// \details Dec provides implementation for decryption transformation. All key
127 /// sizes are supported.
128 /// \since Crypto++ 6.0
129 class CRYPTOPP_NO_VTABLE Dec : public Base
130 {
131 public:
132 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
133 #if CRYPTOPP_SPECK64_ADVANCED_PROCESS_BLOCKS
134 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
135 #endif
136 };
137
138 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
139 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
140 };
141
142 /// \brief SPECK 128-bit block cipher
143 /// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
144 /// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
145 /// \details SPECK128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
146 /// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
147 /// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
148 /// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
149 /// Crypto++ wiki
150 /// \since Crypto++ 6.0
151 class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
152 {
153 public:
154 /// \brief SPECK block cipher transformation functions
155 /// \details Provides implementation common to encryption and decryption
156 /// \since Crypto++ 6.0
157 class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word64>, public BlockCipherImpl<SPECK_Info<16, 16, 16, 32> >
158 {
159 public:
160 /// \brief The algorithm name
161 /// \returns the algorithm name
162 /// \details AlgorithmName returns the algorithm's name as a
163 /// member function.
164 std::string AlgorithmName() const {
165 return StaticAlgorithmName() + (m_kwords == 0 ? "" :
166 "(" + IntToString(m_kwords*sizeof(word64)*8) + ")");
167 }
168
169 std::string AlgorithmProvider() const;
170
171 protected:
172 void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
173 };
174
175 /// \brief Encryption transformation
176 /// \details Enc provides implementation for encryption transformation. All key
177 /// sizes are supported.
178 /// \since Crypto++ 6.0
179 class CRYPTOPP_NO_VTABLE Enc : public Base
180 {
181 public:
182 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
183 #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
184 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
185 #endif
186 };
187
188 /// \brief Encryption transformation
189 /// \details Dec provides implementation for decryption transformation. All key
190 /// sizes are supported.
191 /// \since Crypto++ 6.0
192 class CRYPTOPP_NO_VTABLE Dec : public Base
193 {
194 public:
195 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
196 #if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
197 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
198 #endif
199 };
200
201 typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
202 typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
203 };
204
205 NAMESPACE_END
206
207 #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:101
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:192
SecBlock
Secure memory block with allocator and cleanup.
Definition: secblock.h:682
config.h
Library configuration file.
SPECK64::Base
SPECK block cipher transformation functions.
Definition: speck.h:94
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1248
SPECK_Base
SPECK block cipher base class.
Definition: speck.h:67
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:164
SPECK128::Base
SPECK block cipher transformation functions.
Definition: speck.h:157
seckey.h
Classes and functions for implementing secret key algorithms.
BlockCipherFinal< ENCRYPTION, Enc >
SPECK64::Dec
Encryption transformation.
Definition: speck.h:129
SPECK128
SPECK 128-bit block cipher.
Definition: speck.h:151
SPECK64::Enc
Encryption transformation.
Definition: speck.h:116
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:632
SPECK64
SPECK 64-bit block cipher.
Definition: speck.h:88
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:179
SPECK_Info::StaticAlgorithmName
static const std::string StaticAlgorithmName()
The algorithm name.
Definition: speck.h:54
SPECK_Info
SPECK block cipher information.
Definition: speck.h:48
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293