22ANONYMOUS_NAMESPACE_BEGIN
24using CryptoPP::rotlConstant;
25using CryptoPP::rotrConstant;
45template <
unsigned int RR,
unsigned int KW,
class T>
46inline void CHAM_EncRound(T x[4],
const T k[KW],
unsigned int i)
48 CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
49 CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
50 CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
51 CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 1 : 8);
52 CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 8 : 1);
55 const T kk = k[i % KW];
56 const T aa = x[IDX0] ^
static_cast<T
>(i);
57 const T bb = rotlConstant<R1>(x[IDX1]) ^ kk;
58 x[IDX3] = rotlConstant<R2>(
static_cast<T
>(aa + bb));
79template <
unsigned int RR,
unsigned int KW,
class T>
80inline void CHAM_DecRound(T x[4],
const T k[KW],
unsigned int i)
82 CRYPTOPP_CONSTANT(IDX0 = (RR+0) % 4);
83 CRYPTOPP_CONSTANT(IDX1 = (RR+1) % 4);
84 CRYPTOPP_CONSTANT(IDX3 = (RR+3+1) % 4);
85 CRYPTOPP_CONSTANT(R1 = (RR % 2 == 0) ? 8 : 1);
86 CRYPTOPP_CONSTANT(R2 = (RR % 2 == 0) ? 1 : 8);
89 const T kk = k[i % KW];
90 const T aa = rotrConstant<R1>(x[IDX3]);
91 const T bb = rotlConstant<R2>(x[IDX1]) ^ kk;
92 x[IDX0] =
static_cast<T
>(aa - bb) ^
static_cast<T
>(i);
95ANONYMOUS_NAMESPACE_END
97NAMESPACE_BEGIN(CryptoPP)
99#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
100# if (CRYPTOPP_SSSE3_AVAILABLE)
101extern size_t CHAM64_Enc_AdvancedProcessBlocks_SSSE3(
const word16* subKeys,
size_t rounds,
102 const byte *inBlocks,
const byte *xorBlocks,
byte *outBlocks,
size_t length,
word32 flags);
104extern size_t CHAM64_Dec_AdvancedProcessBlocks_SSSE3(
const word16* subKeys,
size_t rounds,
105 const byte *inBlocks,
const byte *xorBlocks,
byte *outBlocks,
size_t length,
word32 flags);
107extern size_t CHAM128_Enc_AdvancedProcessBlocks_SSSE3(
const word32* subKeys,
size_t rounds,
108 const byte *inBlocks,
const byte *xorBlocks,
byte *outBlocks,
size_t length,
word32 flags);
110extern size_t CHAM128_Dec_AdvancedProcessBlocks_SSSE3(
const word32* subKeys,
size_t rounds,
111 const byte *inBlocks,
const byte *xorBlocks,
byte *outBlocks,
size_t length,
word32 flags);
115void CHAM64::Base::UncheckedSetKey(
const byte *userKey,
unsigned int keyLength,
const NameValuePairs ¶ms)
117 CRYPTOPP_UNUSED(params);
118 m_kw = keyLength/
sizeof(
word16);
121 for (
size_t i = 0; i < m_kw; userKey +=
sizeof(
word32))
127 m_rk[i] = rk1 ^ rotlConstant<1>(rk1) ^ rotlConstant<8>(rk1);
128 m_rk[(i + m_kw) ^ 1] = rk1 ^ rotlConstant<1>(rk1) ^ rotlConstant<11>(rk1);
132 m_rk[i] = rk2 ^ rotlConstant<1>(rk2) ^ rotlConstant<8>(rk2);
133 m_rk[(i + m_kw) ^ 1] = rk2 ^ rotlConstant<1>(rk2) ^ rotlConstant<11>(rk2);
138void CHAM64::Enc::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
142 iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
145 for (
int i = 0; i < R; i+=16)
147 CHAM_EncRound< 0, 16>(m_x.begin(), m_rk.begin(), i+0);
148 CHAM_EncRound< 1, 16>(m_x.begin(), m_rk.begin(), i+1);
149 CHAM_EncRound< 2, 16>(m_x.begin(), m_rk.begin(), i+2);
150 CHAM_EncRound< 3, 16>(m_x.begin(), m_rk.begin(), i+3);
151 CHAM_EncRound< 4, 16>(m_x.begin(), m_rk.begin(), i+4);
152 CHAM_EncRound< 5, 16>(m_x.begin(), m_rk.begin(), i+5);
153 CHAM_EncRound< 6, 16>(m_x.begin(), m_rk.begin(), i+6);
154 CHAM_EncRound< 7, 16>(m_x.begin(), m_rk.begin(), i+7);
155 CHAM_EncRound< 8, 16>(m_x.begin(), m_rk.begin(), i+8);
156 CHAM_EncRound< 9, 16>(m_x.begin(), m_rk.begin(), i+9);
157 CHAM_EncRound<10, 16>(m_x.begin(), m_rk.begin(), i+10);
158 CHAM_EncRound<11, 16>(m_x.begin(), m_rk.begin(), i+11);
159 CHAM_EncRound<12, 16>(m_x.begin(), m_rk.begin(), i+12);
160 CHAM_EncRound<13, 16>(m_x.begin(), m_rk.begin(), i+13);
161 CHAM_EncRound<14, 16>(m_x.begin(), m_rk.begin(), i+14);
162 CHAM_EncRound<15, 16>(m_x.begin(), m_rk.begin(), i+15);
166 oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
169void CHAM64::Dec::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
173 iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
176 for (
int i = R-1; i >=0 ; i-=16)
178 CHAM_DecRound<15, 16>(m_x.begin(), m_rk.begin(), i-0);
179 CHAM_DecRound<14, 16>(m_x.begin(), m_rk.begin(), i-1);
180 CHAM_DecRound<13, 16>(m_x.begin(), m_rk.begin(), i-2);
181 CHAM_DecRound<12, 16>(m_x.begin(), m_rk.begin(), i-3);
182 CHAM_DecRound<11, 16>(m_x.begin(), m_rk.begin(), i-4);
183 CHAM_DecRound<10, 16>(m_x.begin(), m_rk.begin(), i-5);
184 CHAM_DecRound< 9, 16>(m_x.begin(), m_rk.begin(), i-6);
185 CHAM_DecRound< 8, 16>(m_x.begin(), m_rk.begin(), i-7);
186 CHAM_DecRound< 7, 16>(m_x.begin(), m_rk.begin(), i-8);
187 CHAM_DecRound< 6, 16>(m_x.begin(), m_rk.begin(), i-9);
188 CHAM_DecRound< 5, 16>(m_x.begin(), m_rk.begin(), i-10);
189 CHAM_DecRound< 4, 16>(m_x.begin(), m_rk.begin(), i-11);
190 CHAM_DecRound< 3, 16>(m_x.begin(), m_rk.begin(), i-12);
191 CHAM_DecRound< 2, 16>(m_x.begin(), m_rk.begin(), i-13);
192 CHAM_DecRound< 1, 16>(m_x.begin(), m_rk.begin(), i-14);
193 CHAM_DecRound< 0, 16>(m_x.begin(), m_rk.begin(), i-15);
197 oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
200std::string CHAM128::Base::AlgorithmProvider()
const
202#if defined(CRYPTOPP_SSSE3_AVAILABLE)
209void CHAM128::Base::UncheckedSetKey(
const byte *userKey,
unsigned int keyLength,
const NameValuePairs ¶ms)
211 CRYPTOPP_UNUSED(params);
212 m_kw = keyLength/
sizeof(
word32);
215 for (
size_t i = 0; i < m_kw; userKey +=
sizeof(
word32))
219 m_rk[i] = rk ^ rotlConstant<1>(rk) ^ rotlConstant<8>(rk);
220 m_rk[(i + m_kw) ^ 1] = rk ^ rotlConstant<1>(rk) ^ rotlConstant<11>(rk);
225void CHAM128::Enc::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
229 iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
236 for (
int i = 0; i < R; i+=8)
238 CHAM_EncRound<0, 8>(m_x.begin(), m_rk.begin(), i+0);
239 CHAM_EncRound<1, 8>(m_x.begin(), m_rk.begin(), i+1);
240 CHAM_EncRound<2, 8>(m_x.begin(), m_rk.begin(), i+2);
241 CHAM_EncRound<3, 8>(m_x.begin(), m_rk.begin(), i+3);
242 CHAM_EncRound<4, 8>(m_x.begin(), m_rk.begin(), i+4);
243 CHAM_EncRound<5, 8>(m_x.begin(), m_rk.begin(), i+5);
244 CHAM_EncRound<6, 8>(m_x.begin(), m_rk.begin(), i+6);
245 CHAM_EncRound<7, 8>(m_x.begin(), m_rk.begin(), i+7);
252 for (
int i = 0; i < R; i+=16)
254 CHAM_EncRound< 0, 16>(m_x.begin(), m_rk.begin(), i+0);
255 CHAM_EncRound< 1, 16>(m_x.begin(), m_rk.begin(), i+1);
256 CHAM_EncRound< 2, 16>(m_x.begin(), m_rk.begin(), i+2);
257 CHAM_EncRound< 3, 16>(m_x.begin(), m_rk.begin(), i+3);
258 CHAM_EncRound< 4, 16>(m_x.begin(), m_rk.begin(), i+4);
259 CHAM_EncRound< 5, 16>(m_x.begin(), m_rk.begin(), i+5);
260 CHAM_EncRound< 6, 16>(m_x.begin(), m_rk.begin(), i+6);
261 CHAM_EncRound< 7, 16>(m_x.begin(), m_rk.begin(), i+7);
262 CHAM_EncRound< 8, 16>(m_x.begin(), m_rk.begin(), i+8);
263 CHAM_EncRound< 9, 16>(m_x.begin(), m_rk.begin(), i+9);
264 CHAM_EncRound<10, 16>(m_x.begin(), m_rk.begin(), i+10);
265 CHAM_EncRound<11, 16>(m_x.begin(), m_rk.begin(), i+11);
266 CHAM_EncRound<12, 16>(m_x.begin(), m_rk.begin(), i+12);
267 CHAM_EncRound<13, 16>(m_x.begin(), m_rk.begin(), i+13);
268 CHAM_EncRound<14, 16>(m_x.begin(), m_rk.begin(), i+14);
269 CHAM_EncRound<15, 16>(m_x.begin(), m_rk.begin(), i+15);
278 oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
281void CHAM128::Dec::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
285 iblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
292 for (
int i = R-1; i >= 0; i-=8)
294 CHAM_DecRound<7, 8>(m_x.begin(), m_rk.begin(), i-0);
295 CHAM_DecRound<6, 8>(m_x.begin(), m_rk.begin(), i-1);
296 CHAM_DecRound<5, 8>(m_x.begin(), m_rk.begin(), i-2);
297 CHAM_DecRound<4, 8>(m_x.begin(), m_rk.begin(), i-3);
298 CHAM_DecRound<3, 8>(m_x.begin(), m_rk.begin(), i-4);
299 CHAM_DecRound<2, 8>(m_x.begin(), m_rk.begin(), i-5);
300 CHAM_DecRound<1, 8>(m_x.begin(), m_rk.begin(), i-6);
301 CHAM_DecRound<0, 8>(m_x.begin(), m_rk.begin(), i-7);
308 for (
int i = R-1; i >= 0; i-=16)
310 CHAM_DecRound<15, 16>(m_x.begin(), m_rk.begin(), i-0);
311 CHAM_DecRound<14, 16>(m_x.begin(), m_rk.begin(), i-1);
312 CHAM_DecRound<13, 16>(m_x.begin(), m_rk.begin(), i-2);
313 CHAM_DecRound<12, 16>(m_x.begin(), m_rk.begin(), i-3);
314 CHAM_DecRound<11, 16>(m_x.begin(), m_rk.begin(), i-4);
315 CHAM_DecRound<10, 16>(m_x.begin(), m_rk.begin(), i-5);
316 CHAM_DecRound< 9, 16>(m_x.begin(), m_rk.begin(), i-6);
317 CHAM_DecRound< 8, 16>(m_x.begin(), m_rk.begin(), i-7);
318 CHAM_DecRound< 7, 16>(m_x.begin(), m_rk.begin(), i-8);
319 CHAM_DecRound< 6, 16>(m_x.begin(), m_rk.begin(), i-9);
320 CHAM_DecRound< 5, 16>(m_x.begin(), m_rk.begin(), i-10);
321 CHAM_DecRound< 4, 16>(m_x.begin(), m_rk.begin(), i-11);
322 CHAM_DecRound< 3, 16>(m_x.begin(), m_rk.begin(), i-12);
323 CHAM_DecRound< 2, 16>(m_x.begin(), m_rk.begin(), i-13);
324 CHAM_DecRound< 1, 16>(m_x.begin(), m_rk.begin(), i-14);
325 CHAM_DecRound< 0, 16>(m_x.begin(), m_rk.begin(), i-15);
334 oblock(m_x[0])(m_x[1])(m_x[2])(m_x[3]);
337#if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
338size_t CHAM128::Enc::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
339 byte *outBlocks,
size_t length,
word32 flags)
const
341# if (CRYPTOPP_SSSE3_AVAILABLE)
343 const size_t rounds = (m_kw == 4 ? 80 : 96);
344 return CHAM128_Enc_AdvancedProcessBlocks_SSSE3(m_rk, rounds,
345 inBlocks, xorBlocks, outBlocks, length, flags);
351size_t CHAM128::Dec::AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks,
352 byte *outBlocks,
size_t length,
word32 flags)
const
354# if (CRYPTOPP_SSSE3_AVAILABLE)
356 const size_t rounds = (m_kw == 4 ? 80 : 96);
357 return CHAM128_Dec_AdvancedProcessBlocks_SSSE3(m_rk, rounds,
358 inBlocks, xorBlocks, outBlocks, length, flags);
Classes for the CHAM block cipher.
Access a block of memory.
Interface for retrieving values given their names.
Access a block of memory.
void New(size_type newSize)
Change size without preserving contents.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
unsigned short word16
16-bit unsigned datatype
Functions for CPU features and intrinsics.
@ BIG_ENDIAN_ORDER
byte order is big-endian
Utility functions for the Crypto++ library.