Crypto++ 8.9
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_SPECK128_ADVANCED_PROCESS_BLOCKS 1
25# endif
26#endif
27
28// Yet another SunStudio/SunCC workaround. Failed self tests
29// in SSE code paths on i386 for SunStudio 12.3 and below.
30#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
31# undef CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
32#endif
33
34NAMESPACE_BEGIN(CryptoPP)
35
36/// \brief SPECK block cipher information
37/// \tparam L block size of the cipher, in bytes
38/// \tparam D default key length, in bytes
39/// \tparam N minimum key length, in bytes
40/// \tparam M maximum key length, in bytes
41/// \since Crypto++ 6.0
42template <unsigned int L, unsigned int D, unsigned int N, unsigned int M>
43struct SPECK_Info : public FixedBlockSize<L>, VariableKeyLength<D, N, M>
44{
45 /// \brief The algorithm name
46 /// \return the algorithm name
47 /// \details StaticAlgorithmName returns the algorithm's name as a static
48 /// member function.
49 static const std::string StaticAlgorithmName()
50 {
51 // Format is Cipher-Blocksize(Keylength)
52 return "SPECK-" + IntToString(L*8);
53 }
54};
55
56/// \brief SPECK block cipher base class
57/// \tparam W the word type
58/// \details User code should use SPECK64 or SPECK128
59/// \sa SPECK64, SPECK128, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a>
60/// \since Crypto++ 6.0
61template <class W>
63{
64 virtual ~SPECK_Base() {}
65 SPECK_Base() : m_kwords(0), m_rounds(0) {}
66
68 mutable AlignedSecBlock m_wspace; // workspace
69 AlignedSecBlock m_rkeys; // round keys
70 unsigned int m_kwords; // number of key words
71 unsigned int m_rounds; // number of rounds
72};
73
74/// \brief SPECK 64-bit block cipher
75/// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
76/// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
77/// \details SPECK64 provides 64-bit block size. The valid key sizes are 96-bit and 128-bit.
78/// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
79/// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
80/// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
81/// Crypto++ wiki
82/// \since Crypto++ 6.0
83class CRYPTOPP_NO_VTABLE SPECK64 : public SPECK_Info<8, 12, 12, 16>, public BlockCipherDocumentation
84{
85public:
86 /// \brief SPECK64 block cipher base implementation
87 /// \details Provides implementation common to encryption and decryption
88 /// \since Crypto++ 6.0
89 class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word32>, public BlockCipherImpl<SPECK_Info<8, 12, 12, 16> >
90 {
91 public:
92 /// \brief The algorithm name
93 /// \return the algorithm name
94 /// \details AlgorithmName returns the algorithm's name as a
95 /// member function.
96 std::string AlgorithmName() const {
97 return StaticAlgorithmName() + (m_kwords == 0 ? "" :
98 "(" + IntToString(m_kwords*sizeof(word32)*8) + ")");
99 }
100
101 std::string AlgorithmProvider() const;
102
103 /// \brief Provides input and output data alignment for optimal performance.
104 /// \return the input data alignment that provides optimal performance
105 /// \sa GetAlignment() and OptimalBlockSize()
106 unsigned int OptimalDataAlignment() const;
107
108 protected:
109 void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
110 };
111
112 /// \brief SPECK64 encryption transformation
113 /// \details Enc provides the encryption transformation.
114 /// All key 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 };
121
122 /// \brief SPECK64 decryption transformation
123 /// \details Dec provides the decryption transformation.
124 /// All key sizes are supported.
125 /// \since Crypto++ 6.0
126 class CRYPTOPP_NO_VTABLE Dec : public Base
127 {
128 public:
129 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
130 };
131
134};
135
136/// \brief SPECK 128-bit block cipher
137/// \details Speck is a block cipher designed by Ray Beaulieu, Douglas Shors, Jason Smith,
138/// Stefan Treatman-Clark, Bryan Weeks and Louis Wingers.
139/// \details SPECK128 provides 128-bit block size. The valid key sizes are 128-bit, 192-bit and 256-bit.
140/// \sa SPECK64, SPECK128, <A HREF="http://eprint.iacr.org/2013/404">The SIMON and SPECK
141/// Families of Lightweight Block Ciphers</A>, <A HREF="http://iadgov.github.io/simon-speck/">
142/// The Simon and Speck GitHub</A>, <a href="http://www.cryptopp.com/wiki/SPECK">SPECK</a> on the
143/// Crypto++ wiki
144/// \since Crypto++ 6.0
145class CRYPTOPP_NO_VTABLE SPECK128 : public SPECK_Info<16, 16, 16, 32>, public BlockCipherDocumentation
146{
147public:
148 /// \brief SPECK128 block cipher base implementation
149 /// \details Provides implementation common to encryption and decryption
150 /// \since Crypto++ 6.0
151 class CRYPTOPP_NO_VTABLE Base : protected SPECK_Base<word64>, public BlockCipherImpl<SPECK_Info<16, 16, 16, 32> >
152 {
153 public:
154 /// \brief The algorithm name
155 /// \return the algorithm name
156 /// \details AlgorithmName returns the algorithm's name as a
157 /// member function.
158 std::string AlgorithmName() const {
159 return StaticAlgorithmName() + (m_kwords == 0 ? "" :
160 "(" + IntToString(m_kwords*sizeof(word64)*8) + ")");
161 }
162
163 std::string AlgorithmProvider() const;
164
165 /// \brief Provides input and output data alignment for optimal performance.
166 /// \return the input data alignment that provides optimal performance
167 /// \sa GetAlignment() and OptimalBlockSize()
168 unsigned int OptimalDataAlignment() const;
169
170 protected:
171 void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
172 };
173
174 /// \brief SPECK128 encryption transformation
175 /// \details Enc provides the encryption transformation.
176 /// All key sizes are supported.
177 /// \since Crypto++ 6.0
178 class CRYPTOPP_NO_VTABLE Enc : public Base
179 {
180 public:
181 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
182#if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
183 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
184#endif
185 };
186
187 /// \brief SPECK128 decryption transformation
188 /// \details Dec provides the decryption transformation.
189 /// All key sizes are supported.
190 /// \since Crypto++ 6.0
191 class CRYPTOPP_NO_VTABLE Dec : public Base
192 {
193 public:
194 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
195#if CRYPTOPP_SPECK128_ADVANCED_PROCESS_BLOCKS
196 size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
197#endif
198 };
199
202};
203
204NAMESPACE_END
205
206#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:327
SPECK128 block cipher base implementation.
Definition speck.h:152
std::string AlgorithmName() const
The algorithm name.
Definition speck.h:158
SPECK128 decryption transformation.
Definition speck.h:192
SPECK128 encryption transformation.
Definition speck.h:179
SPECK 128-bit block cipher.
Definition speck.h:146
SPECK64 block cipher base implementation.
Definition speck.h:90
std::string AlgorithmName() const
The algorithm name.
Definition speck.h:96
SPECK64 decryption transformation.
Definition speck.h:127
SPECK64 encryption transformation.
Definition speck.h:117
SPECK 64-bit block cipher.
Definition speck.h:84
Secure memory block with allocator and cleanup.
Definition secblock.h:731
Inherited by keyed algorithms with variable key length.
Definition seckey.h:166
Library configuration file.
unsigned int word32
32-bit unsigned datatype
Definition config_int.h:72
unsigned long long word64
64-bit unsigned datatype
Definition config_int.h:101
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition misc.h:929
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:63
SPECK block cipher information.
Definition speck.h:44
static const std::string StaticAlgorithmName()
The algorithm name.
Definition speck.h:49