16 ANONYMOUS_NAMESPACE_BEGIN
18 using CryptoPP::word32;
19 using CryptoPP::word64;
29 return (rotlConstant<1>(v) & rotlConstant<8>(v)) ^ rotlConstant<2>(v);
39 inline void R2(W& x, W& y,
const W k,
const W l)
51 template <
class W,
unsigned int R>
52 inline void SIMON_Encrypt(W c[2],
const W p[2],
const W k[R])
56 for (
int i = 0; i < static_cast<int>(R-1); i += 2)
57 R2(c[0], c[1], k[i], k[i + 1]);
61 c[1] ^= f(c[0]); c[1] ^= k[R-1];
62 W t = c[0]; c[0] = c[1]; c[1] = t;
72 template <
class W,
unsigned int R>
73 inline void SIMON_Decrypt(W p[2],
const W c[2],
const W k[R])
76 unsigned int rounds = R;
80 const W t = p[1]; p[1] = p[0]; p[0] = t;
81 p[1] ^= k[R - 1]; p[1] ^= f(p[0]);
85 for (
int i = static_cast<int>(rounds - 2); i >= 0; i -= 2)
86 R2(p[1], p[0], k[i + 1], k[i]);
94 inline void SIMON64_ExpandKey_3W(word32 key[42],
const word32 k[3])
96 const word32 c = 0xfffffffc;
97 word64 z = W64LIT(0x7369f885192c0ef5);
99 key[0] = k[2]; key[1] = k[1]; key[2] = k[0];
100 for (
size_t i = 3; i<42; ++i)
102 key[i] =
static_cast<word32
>(c ^ (z & 1) ^ key[i - 3] ^
103 rotrConstant<3>(key[i - 1]) ^ rotrConstant<4>(key[i - 1]));
113 inline void SIMON64_ExpandKey_4W(word32 key[44],
const word32 k[4])
115 const word32 c = 0xfffffffc;
116 word64 z = W64LIT(0xfc2ce51207a635db);
118 key[0] = k[3]; key[1] = k[2]; key[2] = k[1]; key[3] = k[0];
119 for (
size_t i = 4; i<44; ++i)
121 key[i] =
static_cast<word32
>(c ^ (z & 1) ^ key[i - 4] ^
122 rotrConstant<3>(key[i - 1]) ^ key[i - 3] ^ rotrConstant<4>(key[i - 1]) ^
123 rotrConstant<1>(key[i - 3]));
133 inline void SIMON128_ExpandKey_2W(word64 key[68],
const word64 k[2])
135 const word64 c = W64LIT(0xfffffffffffffffc);
136 word64 z = W64LIT(0x7369f885192c0ef5);
138 key[0] = k[1]; key[1] = k[0];
139 for (
size_t i=2; i<66; ++i)
141 key[i] = c ^ (z & 1) ^ key[i - 2] ^ rotrConstant<3>(key[i - 1]) ^ rotrConstant<4>(key[i - 1]);
145 key[66] = c ^ 1 ^ key[64] ^ rotrConstant<3>(key[65]) ^ rotrConstant<4>(key[65]);
146 key[67] = c^key[65] ^ rotrConstant<3>(key[66]) ^ rotrConstant<4>(key[66]);
154 inline void SIMON128_ExpandKey_3W(word64 key[69],
const word64 k[3])
156 const word64 c = W64LIT(0xfffffffffffffffc);
157 word64 z = W64LIT(0xfc2ce51207a635db);
159 key[0]=k[2]; key[1]=k[1]; key[2]=k[0];
160 for (
size_t i=3; i<67; ++i)
162 key[i] = c ^ (z & 1) ^ key[i - 3] ^ rotrConstant<3>(key[i - 1]) ^ rotrConstant<4>(key[i - 1]);
166 key[67] = c^key[64] ^ rotrConstant<3>(key[66]) ^ rotrConstant<4>(key[66]);
167 key[68] = c ^ 1 ^ key[65] ^ rotrConstant<3>(key[67]) ^ rotrConstant<4>(key[67]);
175 inline void SIMON128_ExpandKey_4W(word64 key[72],
const word64 k[4])
177 const word64 c = W64LIT(0xfffffffffffffffc);
178 word64 z = W64LIT(0xfdc94c3a046d678b);
180 key[0]=k[3]; key[1]=k[2]; key[2]=k[1]; key[3]=k[0];
181 for (
size_t i=4; i<68; ++i)
183 key[i] = c ^ (z & 1) ^ key[i - 4] ^ rotrConstant<3>(key[i - 1]) ^ key[i - 3] ^ rotrConstant<4>(key[i - 1]) ^ rotrConstant<1>(key[i - 3]);
187 key[68] = c^key[64] ^ rotrConstant<3>(key[67]) ^ key[65] ^ rotrConstant<4>(key[67]) ^ rotrConstant<1>(key[65]);
188 key[69] = c ^ 1 ^ key[65] ^ rotrConstant<3>(key[68]) ^ key[66] ^ rotrConstant<4>(key[68]) ^ rotrConstant<1>(key[66]);
189 key[70] = c^key[66] ^ rotrConstant<3>(key[69]) ^ key[67] ^ rotrConstant<4>(key[69]) ^ rotrConstant<1>(key[67]);
190 key[71] = c^key[67] ^ rotrConstant<3>(key[70]) ^ key[68] ^ rotrConstant<4>(key[70]) ^ rotrConstant<1>(key[68]);
193 ANONYMOUS_NAMESPACE_END
199 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 200 extern size_t SIMON64_Enc_AdvancedProcessBlocks_NEON(
const word32* subKeys,
size_t rounds,
201 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
203 extern size_t SIMON64_Dec_AdvancedProcessBlocks_NEON(
const word32* subKeys,
size_t rounds,
204 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
207 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 208 extern size_t SIMON128_Enc_AdvancedProcessBlocks_NEON(
const word64* subKeys,
size_t rounds,
209 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
211 extern size_t SIMON128_Dec_AdvancedProcessBlocks_NEON(
const word64* subKeys,
size_t rounds,
212 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
215 #if defined(CRYPTOPP_SSE41_AVAILABLE) 216 extern size_t SIMON64_Enc_AdvancedProcessBlocks_SSE41(
const word32* subKeys,
size_t rounds,
217 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
219 extern size_t SIMON64_Dec_AdvancedProcessBlocks_SSE41(
const word32* subKeys,
size_t rounds,
220 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
223 #if defined(CRYPTOPP_SSSE3_AVAILABLE) 224 extern size_t SIMON128_Enc_AdvancedProcessBlocks_SSSE3(
const word64* subKeys,
size_t rounds,
225 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
227 extern size_t SIMON128_Dec_AdvancedProcessBlocks_SSSE3(
const word64* subKeys,
size_t rounds,
228 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
231 #if (CRYPTOPP_ALTIVEC_AVAILABLE) 232 extern size_t SIMON64_Enc_AdvancedProcessBlocks_ALTIVEC(
const word32* subKeys,
size_t rounds,
233 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
235 extern size_t SIMON64_Dec_AdvancedProcessBlocks_ALTIVEC(
const word32* subKeys,
size_t rounds,
236 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
239 #if (CRYPTOPP_POWER8_AVAILABLE) 240 extern size_t SIMON128_Enc_AdvancedProcessBlocks_POWER8(
const word64* subKeys,
size_t rounds,
241 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
243 extern size_t SIMON128_Dec_AdvancedProcessBlocks_POWER8(
const word64* subKeys,
size_t rounds,
244 const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags);
247 std::string SIMON64::Base::AlgorithmProvider()
const 249 #if (CRYPTOPP_SIMON64_ADVANCED_PROCESS_BLOCKS) 250 # if (CRYPTOPP_SSE41_AVAILABLE) 254 # if (CRYPTOPP_ARM_NEON_AVAILABLE) 258 # if (CRYPTOPP_POWER8_AVAILABLE) 262 # if (CRYPTOPP_ALTIVEC_AVAILABLE) 270 void SIMON64::Base::UncheckedSetKey(
const byte *userKey,
unsigned int keyLength,
const NameValuePairs ¶ms)
273 CRYPTOPP_UNUSED(params);
277 m_kwords = keyLength/
sizeof(word32);
282 KeyBlock kblk(userKey);
287 m_rkeys.New((m_rounds = 42));
288 kblk(m_wspace[2])(m_wspace[1])(m_wspace[0]);
289 SIMON64_ExpandKey_3W(m_rkeys, m_wspace);
292 m_rkeys.New((m_rounds = 44));
293 kblk(m_wspace[3])(m_wspace[2])(m_wspace[1])(m_wspace[0]);
294 SIMON64_ExpandKey_4W(m_rkeys, m_wspace);
302 #if CRYPTOPP_ALTIVEC_AVAILABLE 303 m_rkeys.Grow(m_rkeys.size()+4);
307 void SIMON64::Enc::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const 311 InBlock iblk(inBlock); iblk(m_wspace[1])(m_wspace[0]);
316 SIMON_Encrypt<word32, 42>(m_wspace+2, m_wspace+0, m_rkeys);
319 SIMON_Encrypt<word32, 44>(m_wspace+2, m_wspace+0, m_rkeys);
327 OutBlock oblk(xorBlock, outBlock); oblk(m_wspace[3])(m_wspace[2]);
330 void SIMON64::Dec::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const 334 InBlock iblk(inBlock); iblk(m_wspace[1])(m_wspace[0]);
339 SIMON_Decrypt<word32, 42>(m_wspace+2, m_wspace+0, m_rkeys);
342 SIMON_Decrypt<word32, 44>(m_wspace+2, m_wspace+0, m_rkeys);
350 OutBlock oblk(xorBlock, outBlock); oblk(m_wspace[3])(m_wspace[2]);
355 std::string SIMON128::Base::AlgorithmProvider()
const 357 #if (CRYPTOPP_SIMON128_ADVANCED_PROCESS_BLOCKS) 358 # if (CRYPTOPP_SSSE3_AVAILABLE) 362 # if (CRYPTOPP_ARM_NEON_AVAILABLE) 366 # if (CRYPTOPP_POWER8_AVAILABLE) 374 void SIMON128::Base::UncheckedSetKey(
const byte *userKey,
unsigned int keyLength,
const NameValuePairs ¶ms)
376 CRYPTOPP_ASSERT(keyLength == 16 || keyLength == 24 || keyLength == 32);
377 CRYPTOPP_UNUSED(params);
381 m_kwords = keyLength/
sizeof(word64);
386 KeyBlock kblk(userKey);
391 m_rkeys.New((m_rounds = 68));
392 kblk(m_wspace[1])(m_wspace[0]);
393 SIMON128_ExpandKey_2W(m_rkeys, m_wspace);
396 m_rkeys.New((m_rounds = 69));
397 kblk(m_wspace[2])(m_wspace[1])(m_wspace[0]);
398 SIMON128_ExpandKey_3W(m_rkeys, m_wspace);
401 m_rkeys.New((m_rounds = 72));
402 kblk(m_wspace[3])(m_wspace[2])(m_wspace[1])(m_wspace[0]);
403 SIMON128_ExpandKey_4W(m_rkeys, m_wspace);
410 void SIMON128::Enc::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const 414 InBlock iblk(inBlock); iblk(m_wspace[1])(m_wspace[0]);
419 SIMON_Encrypt<word64, 68>(m_wspace+2, m_wspace+0, m_rkeys);
422 SIMON_Encrypt<word64, 69>(m_wspace+2, m_wspace+0, m_rkeys);
425 SIMON_Encrypt<word64, 72>(m_wspace+2, m_wspace+0, m_rkeys);
433 OutBlock oblk(xorBlock, outBlock); oblk(m_wspace[3])(m_wspace[2]);
436 void SIMON128::Dec::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const 440 InBlock iblk(inBlock); iblk(m_wspace[1])(m_wspace[0]);
445 SIMON_Decrypt<word64, 68>(m_wspace+2, m_wspace+0, m_rkeys);
448 SIMON_Decrypt<word64, 69>(m_wspace+2, m_wspace+0, m_rkeys);
451 SIMON_Decrypt<word64, 72>(m_wspace+2, m_wspace+0, m_rkeys);
459 OutBlock oblk(xorBlock, outBlock); oblk(m_wspace[3])(m_wspace[2]);
462 #if defined(CRYPTOPP_SIMON64_ADVANCED_PROCESS_BLOCKS) 463 size_t SIMON64::Enc::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
464 byte *outBlocks,
size_t length, word32 flags)
const 466 #if defined(CRYPTOPP_SSE41_AVAILABLE) 468 return SIMON64_Enc_AdvancedProcessBlocks_SSE41(m_rkeys, (
size_t)m_rounds,
469 inBlocks, xorBlocks, outBlocks, length, flags);
471 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 473 return SIMON64_Enc_AdvancedProcessBlocks_NEON(m_rkeys, (
size_t)m_rounds,
474 inBlocks, xorBlocks, outBlocks, length, flags);
476 #if (CRYPTOPP_ALTIVEC_AVAILABLE) 478 return SIMON64_Enc_AdvancedProcessBlocks_ALTIVEC(m_rkeys, (
size_t)m_rounds,
479 inBlocks, xorBlocks, outBlocks, length, flags);
484 size_t SIMON64::Dec::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
485 byte *outBlocks,
size_t length, word32 flags)
const 487 #if defined(CRYPTOPP_SSE41_AVAILABLE) 489 return SIMON64_Dec_AdvancedProcessBlocks_SSE41(m_rkeys, (
size_t)m_rounds,
490 inBlocks, xorBlocks, outBlocks, length, flags);
492 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 494 return SIMON64_Dec_AdvancedProcessBlocks_NEON(m_rkeys, (
size_t)m_rounds,
495 inBlocks, xorBlocks, outBlocks, length, flags);
497 #if (CRYPTOPP_ALTIVEC_AVAILABLE) 499 return SIMON64_Dec_AdvancedProcessBlocks_ALTIVEC(m_rkeys, (
size_t)m_rounds,
500 inBlocks, xorBlocks, outBlocks, length, flags);
504 #endif // CRYPTOPP_SIMON64_ADVANCED_PROCESS_BLOCKS 506 #if defined(CRYPTOPP_SIMON128_ADVANCED_PROCESS_BLOCKS) 507 size_t SIMON128::Enc::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
508 byte *outBlocks,
size_t length, word32 flags)
const 510 #if defined(CRYPTOPP_SSSE3_AVAILABLE) 512 return SIMON128_Enc_AdvancedProcessBlocks_SSSE3(m_rkeys, (
size_t)m_rounds,
513 inBlocks, xorBlocks, outBlocks, length, flags);
515 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 517 return SIMON128_Enc_AdvancedProcessBlocks_NEON(m_rkeys, (
size_t)m_rounds,
518 inBlocks, xorBlocks, outBlocks, length, flags);
520 #if (CRYPTOPP_POWER8_AVAILABLE) 522 return SIMON128_Enc_AdvancedProcessBlocks_POWER8(m_rkeys, (
size_t)m_rounds,
523 inBlocks, xorBlocks, outBlocks, length, flags);
528 size_t SIMON128::Dec::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
529 byte *outBlocks,
size_t length, word32 flags)
const 531 #if defined(CRYPTOPP_SSSE3_AVAILABLE) 533 return SIMON128_Dec_AdvancedProcessBlocks_SSSE3(m_rkeys, (
size_t)m_rounds,
534 inBlocks, xorBlocks, outBlocks, length, flags);
536 #if (CRYPTOPP_ARM_NEON_AVAILABLE) 538 return SIMON128_Dec_AdvancedProcessBlocks_NEON(m_rkeys, (
size_t)m_rounds,
539 inBlocks, xorBlocks, outBlocks, length, flags);
541 #if (CRYPTOPP_POWER8_AVAILABLE) 543 return SIMON128_Dec_AdvancedProcessBlocks_POWER8(m_rkeys, (
size_t)m_rounds,
544 inBlocks, xorBlocks, outBlocks, length, flags);
548 #endif // CRYPTOPP_SIMON128_ADVANCED_PROCESS_BLOCKS bool HasAltivec()
Determine if a PowerPC processor has Altivec available.
Utility functions for the Crypto++ library.
bool HasSSSE3()
Determines SSSE3 availability.
Library configuration file.
T rotlConstant(T x)
Performs a left rotate.
bool HasPower8()
Determine if a PowerPC processor has Power8 available.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Functions for CPU features and intrinsics.
T rotrConstant(T x)
Performs a right rotate.
Access a block of memory.
Classes for the Simon block cipher.
bool HasSSE41()
Determines SSE4.1 availability.
Access a block of memory.
Crypto++ library namespace.
bool HasNEON()
Determine if an ARM processor has Advanced SIMD available.
Interface for retrieving values given their names.