void RC2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { word16 R0, R1, R2, R3; Block::Get(inBlock)(R0)(R1)(R2)(R3); for (int i = 0; i < 16; i++) { R0 += (R1 & ~R3) + (R2 & R3) + K[4*i+0]; R0 = rotlFixed(R0, 1); R1 += (R2 & ~R0) + (R3 & R0) + K[4*i+1]; R1 = rotlFixed(R1, 2); R2 += (R3 & ~R1) + (R0 & R1) + K[4*i+2]; R2 = rotlFixed(R2, 3); R3 += (R0 & ~R2) + (R1 & R2) + K[4*i+3]; R3 = rotlFixed(R3, 5); if (i == 4 || i == 10) { R0 = word16(R0 + K[R3 & 63]); R1 = word16(R1 + K[R0 & 63]); R2 = word16(R2 + K[R1 & 63]); R3 = word16(R3 + K[R2 & 63]); } } Block::Put(xorBlock, outBlock)(R0)(R1)(R2)(R3); }
void Twofish::SetKey(const byte* userKey, word32 keylen, CipherDir /*dummy*/) { unsigned int len = (keylen <= 16 ? 2 : (keylen <= 24 ? 3 : 4)); word32 key[8]; GetUserKey(LittleEndianOrder, key, len*2, userKey, keylen); unsigned int i; for (i=0; i<40; i+=2) { word32 a = h(i, key, len); word32 b = rotlFixed(h(i+1, key+1, len), 8); k_[i] = a+b; k_[i+1] = rotlFixed(a+2*b, 9); } word32 svec[8]; for (i=0; i<len; i++) svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]); for (i=0; i<256; i++) { word32 t = h0(i, svec, len); s_[0][i] = mds_[0][GETBYTE(t, 0)]; s_[1][i] = mds_[1][GETBYTE(t, 1)]; s_[2][i] = mds_[2][GETBYTE(t, 2)]; s_[3][i] = mds_[3][GETBYTE(t, 3)]; } }
void RC6::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { const RC6_WORD *sptr = sTable.end(); RC6_WORD a, b, c, d, t, u; Block::Get(inBlock)(a)(b)(c)(d); sptr -= 2; c -= sptr[1]; a -= sptr[0]; for (unsigned i=0; i < r; i++) { sptr -= 2; t = a; a = d; d = c; c = b; b = t; u = rotlFixed(d*(2*d+1), 5); t = rotlFixed(b*(2*b+1), 5); c = rotrMod(c-sptr[1], t) ^ u; a = rotrMod(a-sptr[0], u) ^ t; } sptr -= 2; d -= sTable[1]; b -= sTable[0]; Block::Put(xorBlock, outBlock)(a)(b)(c)(d); }
static INLINE void FPERM(word32* left, word32* right) { word32 work; *right = rotrFixed(*right, 1U); work = (*left ^ *right) & 0xaaaaaaaa; *right ^= work; *left = rotrFixed(*left^work, 9U); work = (*left ^ *right) & 0x00ff00ff; *right ^= work; *left = rotlFixed(*left^work, 6U); work = (*left ^ *right) & 0x33333333; *right ^= work; *left = rotlFixed(*left^work, 18U); work = (*left ^ *right) & 0xffff0000; *right ^= work; *left = rotlFixed(*left^work, 20U); work = (*left ^ *right) & 0xf0f0f0f0; *right ^= work; *left = rotrFixed(*left^work, 4U); }
void CData::CryptBlock(Byte *buf, bool encrypt) { Byte inBuf[16]; UInt32 A, B, C, D, T, TA, TB; A = GetUi32(buf + 0) ^ Keys[0]; B = GetUi32(buf + 4) ^ Keys[1]; C = GetUi32(buf + 8) ^ Keys[2]; D = GetUi32(buf + 12) ^ Keys[3]; if (!encrypt) memcpy(inBuf, buf, sizeof(inBuf)); for (int i = 0; i < kNumRounds; i++) { UInt32 key = Keys[(encrypt ? i : (kNumRounds - 1 - i)) & 3]; T = ((C + rotlFixed(D, 11)) ^ key); TA = A ^ SubstLong(T); T = ((D ^ rotlFixed(C, 17)) + key); TB = B ^ SubstLong(T); A = C; B = D; C = TA; D = TB; } SetUi32(buf + 0, C ^ Keys[0]); SetUi32(buf + 4, D ^ Keys[1]); SetUi32(buf + 8, A ^ Keys[2]); SetUi32(buf + 12, B ^ Keys[3]); UpdateKeys(encrypt ? buf : inBuf); }
void RC6Encryption::ProcessBlock(const byte *in, byte *out) const { const RC6_WORD *sptr = sTable; RC6_WORD a, b, c, d, t, u; GetBlockLittleEndian(in, a, b, c, d); b += sptr[0]; d += sptr[1]; sptr += 2; for(unsigned i=0; i<r; i++) { t = rotlFixed(b*(2*b+1), 5); u = rotlFixed(d*(2*d+1), 5); a = rotlMod(a^t,u) + sptr[0]; c = rotlMod(c^u,t) + sptr[1]; t = a; a = b; b = c; c = d; d = t; sptr += 2; } a += sptr[0]; c += sptr[1]; PutBlockLittleEndian(out, a, b, c, d); }
static inline void IPERM(word32& left, word32& right) { word32 work; right = rotlFixed(right, 4U); work = (left ^ right) & 0xf0f0f0f0; left ^= work; right = rotrFixed(right^work, 20U); work = (left ^ right) & 0xffff0000; left ^= work; right = rotrFixed(right^work, 18U); work = (left ^ right) & 0x33333333; left ^= work; right = rotrFixed(right^work, 6U); work = (left ^ right) & 0x00ff00ff; left ^= work; right = rotlFixed(right^work, 9U); work = (left ^ right) & 0xaaaaaaaa; left = rotlFixed(left^work, 1U); right ^= work; }
void CRC2Encryptor::Transform(TDes8& aBlock) { assert(aBlock.Size() == KRC2BlockBytes); TUint16 R0, R1, R2, R3; GetBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3); TInt i = 0; for (; i < 16; i++) { R0 += (R1 & ~R3) + (R2 & R3) + iK[4*i+0]; R0 = rotlFixed(R0, 1); R1 += (R2 & ~R0) + (R3 & R0) + iK[4*i+1]; R1 = rotlFixed(R1, 2); R2 += (R3 & ~R1) + (R0 & R1) + iK[4*i+2]; R2 = rotlFixed(R2, 3); R3 += (R0 & ~R2) + (R1 & R2) + iK[4*i+3]; R3 = rotlFixed(R3, 5); if (i == 4 || i == 10) { R0 += iK[R3 & 63]; R1 += iK[R0 & 63]; R2 += iK[R1 & 63]; R3 += iK[R2 & 63]; } } PutBlockLittleEndian((TUint8*)&aBlock[0], R0, R1, R2, R3); }
void RC6Decryption::ProcessBlock(const byte *in, byte *out) const { const RC6_WORD *sptr = sTable+sTable.size; RC6_WORD a, b, c, d, t, u; GetBlockLittleEndian(in, a, b, c, d); sptr -= 2; c -= sptr[1]; a -= sptr[0]; for (unsigned i=0; i < r; i++) { sptr -= 2; t = a; a = d; d = c; c = b; b = t; u = rotlFixed(d*(2*d+1), 5); t = rotlFixed(b*(2*b+1), 5); c = rotrMod(c-sptr[1], t) ^ u; a = rotrMod(a-sptr[0], u) ^ t; } sptr -= 2; d -= sTable[1]; b -= sTable[0]; PutBlockLittleEndian(out, a, b, c, d); }
void RC6::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { const RC6_WORD *sptr = sTable; RC6_WORD a, b, c, d, t, u; Block::Get(inBlock)(a)(b)(c)(d); b += sptr[0]; d += sptr[1]; sptr += 2; for(unsigned i=0; i<r; i++) { t = rotlFixed(b*(2*b+1), 5); u = rotlFixed(d*(2*d+1), 5); a = rotlMod(a^t,u) + sptr[0]; c = rotlMod(c^u,t) + sptr[1]; t = a; a = b; b = c; c = d; d = t; sptr += 2; } a += sptr[0]; c += sptr[1]; Block::Put(xorBlock, outBlock)(a)(b)(c)(d); }
void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen) { FixedSizeSecBlock<word32, 8> k0; GetUserKey(LITTLE_ENDIAN_ORDER, k0.begin(), 8, userKey, keylen); if (keylen < 32) k0[keylen/4] |= word32(1) << ((keylen%4)*8); word32 t = k0[7]; unsigned int i; for (i = 0; i < 8; ++i) k[i] = k0[i] = t = rotlFixed(k0[i] ^ k0[(i+3)%8] ^ k0[(i+5)%8] ^ t ^ 0x9e3779b9 ^ i, 11); for (i = 8; i < 4*(rounds+1); ++i) k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11); k -= 20; word32 a,b,c,d,e; for (i=0; i<rounds/8; i++) { afterS2(LK); afterS2(S3); afterS3(SK); afterS1(LK); afterS1(S2); afterS2(SK); afterS0(LK); afterS0(S1); afterS1(SK); beforeS0(LK); beforeS0(S0); afterS0(SK); k += 8*4; afterS6(LK); afterS6(S7); afterS7(SK); afterS5(LK); afterS5(S6); afterS6(SK); afterS4(LK); afterS4(S5); afterS5(SK); afterS3(LK); afterS3(S4); afterS4(SK); } afterS2(LK); afterS2(S3); afterS3(SK); }
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &) { AssertValidKeyLength(length); static const word32 offset[ROUNDS] = { 0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL, 0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL, }; GetUserKey(BIG_ENDIAN_ORDER, m_roundkeys.data(), KEYLENGTH/4, userKey, KEYLENGTH); /* apply the key evolution function */ for (int i = 1; i < ROUNDS+1; i++) { roundkeys(i, 0) = roundkeys(i-1, 0) ^ rotlFixed(roundkeys(i-1, 3), 8U) ^ offset[i-1]; roundkeys(i, 1) = roundkeys(i-1, 1) ^ roundkeys(i, 0); roundkeys(i, 2) = roundkeys(i-1, 2) ^ roundkeys(i, 1); roundkeys(i, 3) = roundkeys(i-1, 3) ^ roundkeys(i, 2); } /* produce the round keys */ if (IsForwardTransformation()) { for (int i = 0; i < ROUNDS; i++) SquareTransform (roundkeys4(i), roundkeys4(i)); } else { for (int i = 0; i < ROUNDS/2; i++) for (int j = 0; j < 4; j++) std::swap(roundkeys(i, j), roundkeys(ROUNDS-i, j)); SquareTransform (roundkeys4(ROUNDS), roundkeys4(ROUNDS)); } }
void serpent_set_key(const unsigned __int8 userKey[], int keylen, unsigned __int8 *ks) { unsigned __int32 a,b,c,d,e; unsigned __int32 *k = (unsigned __int32 *)ks; unsigned __int32 t; int i; for (i = 0; i < keylen / (int)sizeof(__int32); i++) k[i] = LE32(((unsigned __int32*)userKey)[i]); if (keylen < 32) k[keylen/4] |= (unsigned __int32)1 << ((keylen%4)*8); k += 8; t = k[-1]; for (i = 0; i < 132; ++i) k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11); k -= 20; for (i=0; i<4; i++) { LKf (k, 20, &a, &e, &b, &d); S3f (&a, &e, &b, &d, &c); SKf (k, 16, &e, &b, &d, &c); LKf (k, 24, &c, &b, &a, &e); S2f (&c, &b, &a, &e, &d); SKf (k, 20, &a, &e, &b, &d); LKf (k, 28, &b, &e, &c, &a); S1f (&b, &e, &c, &a, &d); SKf (k, 24, &c, &b, &a, &e); LKf (k, 32, &a, &b, &c, &d); S0f (&a, &b, &c, &d, &e); SKf (k, 28, &b, &e, &c, &a); k += 8*4; LKf (k, 4, &a, &c, &d, &b); S7f (&a, &c, &d, &b, &e); SKf (k, 0, &d, &e, &b, &a); LKf (k, 8, &a, &c, &b, &e); S6f (&a, &c, &b, &e, &d); SKf (k, 4, &a, &c, &d, &b); LKf (k, 12, &b, &a, &e, &c); S5f (&b, &a, &e, &c, &d); SKf (k, 8, &a, &c, &b, &e); LKf (k, 16, &e, &b, &d, &c); S4f (&e, &b, &d, &c, &a); SKf (k, 12, &b, &a, &e, &c); } LKf (k, 20, &a, &e, &b, &d); S3f (&a, &e, &b, &d, &c); SKf (k, 16, &e, &b, &d, &c); }
void RC6::Base::UncheckedSetKey(CipherDir direction, const byte *k, unsigned int keylen, unsigned int rounds) { AssertValidKeyLength(keylen); AssertValidRounds(rounds); r = rounds; sTable.New(2*(r+2)); static const RC6_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize static const RC6_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize static const int U=sizeof(RC6_WORD); const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0 SecBlock<RC6_WORD> l(c); GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen); sTable[0] = MAGIC_P; for (unsigned j=1; j<sTable.size();j++) sTable[j] = sTable[j-1] + MAGIC_Q; RC6_WORD a=0, b=0; const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c); for (unsigned h=0; h < n; h++) { a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3); b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b)); } }
RC6Base::RC6Base(const byte *k, unsigned int keylen, unsigned int rounds) : r(rounds), sTable((2*r)+4) { assert(keylen == KeyLength(keylen)); static const RC6_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize static const RC6_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize static const int U=sizeof(RC6_WORD); const unsigned int c=(keylen-1)/U + 1; SecBlock<RC6_WORD> l(c); GetUserKeyLittleEndian(l.ptr, c, k, keylen); sTable[0] = MAGIC_P; for (unsigned j=1; j<sTable.size;j++) sTable[j] = sTable[j-1] + MAGIC_Q; RC6_WORD a=0, b=0; const unsigned n = 3*STDMAX(sTable.size,c); for (unsigned h=0; h < n; h++) { a = sTable[h % sTable.size] = rotlFixed((sTable[h % sTable.size] + a + b), 3); b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b)); } }
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &) { AssertValidKeyLength(length); static const word32 offset[ROUNDS] = { 0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL, 0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL, }; GetUserKey(BIG_ENDIAN_ORDER, roundkeys[0], KEYLENGTH/4, userKey, KEYLENGTH); /* apply the key evolution function */ for (int i = 1; i < ROUNDS+1; i++) { roundkeys[i][0] = roundkeys[i-1][0] ^ rotlFixed(roundkeys[i-1][3], 8U) ^ offset[i-1]; roundkeys[i][1] = roundkeys[i-1][1] ^ roundkeys[i][0]; roundkeys[i][2] = roundkeys[i-1][2] ^ roundkeys[i][1]; roundkeys[i][3] = roundkeys[i-1][3] ^ roundkeys[i][2]; } /* produce the round keys */ if (IsForwardTransformation()) { for (int i = 0; i < ROUNDS; i++) SquareTransform (roundkeys[i], roundkeys[i]); } else { for (int i = 0; i < ROUNDS/2; i++) for (int j = 0; j < 4; j++) std::swap(roundkeys[i][j], roundkeys[ROUNDS-i][j]); SquareTransform (roundkeys[ROUNDS], roundkeys[ROUNDS]); } }
static void Transform(Sha* sha) { word32 W[80], i; /* Copy context->state[] to working vars */ word32 a = sha->digest[0]; word32 b = sha->digest[1]; word32 c = sha->digest[2]; word32 d = sha->digest[3]; word32 e = sha->digest[4]; for (i = 0; i < 16; i++) W[i] = sha->buffer[i]; for (i = 16; i < 80; i++) W[i] = rotlFixed(W[i-3]^W[i-8]^W[i-14]^W[i-16],1); /* 4 rounds of 20 operations each. */ for (i = 0; i < 20; ) { R0(a,b,c,d,e,i); i++; R0(e,a,b,c,d,i); i++; R0(d,e,a,b,c,i); i++; R0(c,d,e,a,b,i); i++; R0(b,c,d,e,a,i); i++; } for (i = 20; i < 40; ) { R2(a,b,c,d,e,i); i++; R2(e,a,b,c,d,i); i++; R2(d,e,a,b,c,i); i++; R2(c,d,e,a,b,i); i++; R2(b,c,d,e,a,i); i++; } for (i = 40; i < 60; ) { R3(a,b,c,d,e,i); i++; R3(e,a,b,c,d,i); i++; R3(d,e,a,b,c,i); i++; R3(c,d,e,a,b,i); i++; R3(b,c,d,e,a,i); i++; } for (i = 60; i < 80; ) { R4(a,b,c,d,e,i); i++; R4(e,a,b,c,d,i); i++; R4(d,e,a,b,c,i); i++; R4(c,d,e,a,b,i); i++; R4(b,c,d,e,a,i); i++; } /* Add the working vars back into digest state[] */ sha->digest[0] += a; sha->digest[1] += b; sha->digest[2] += c; sha->digest[3] += d; sha->digest[4] += e; }
void SAFER::Base::UncheckedSetKey(const byte *userkey_1, unsigned int length, const NameValuePairs ¶ms) { bool strengthened = Strengthened(); unsigned int nof_rounds = params.GetIntValueWithDefault(Name::Rounds(), length == 8 ? (strengthened ? 8 : 6) : 10); const byte *userkey_2 = length == 8 ? userkey_1 : userkey_1 + 8; keySchedule.New(1 + BLOCKSIZE * (1 + 2 * nof_rounds)); unsigned int i, j; byte *key = keySchedule; SecByteBlock ka(BLOCKSIZE + 1), kb(BLOCKSIZE + 1); if (MAX_ROUNDS < nof_rounds) nof_rounds = MAX_ROUNDS; *key++ = (unsigned char)nof_rounds; ka[BLOCKSIZE] = 0; kb[BLOCKSIZE] = 0; for (j = 0; j < BLOCKSIZE; j++) { ka[BLOCKSIZE] ^= ka[j] = rotlFixed(userkey_1[j], 5U); kb[BLOCKSIZE] ^= kb[j] = *key++ = userkey_2[j]; } for (i = 1; i <= nof_rounds; i++) { for (j = 0; j < BLOCKSIZE + 1; j++) { ka[j] = rotlFixed(ka[j], 6U); kb[j] = rotlFixed(kb[j], 6U); } for (j = 0; j < BLOCKSIZE; j++) if (strengthened) *key++ = (ka[(j + 2 * i - 1) % (BLOCKSIZE + 1)] + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; else *key++ = (ka[j] + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; for (j = 0; j < BLOCKSIZE; j++) if (strengthened) *key++ = (kb[(j + 2 * i) % (BLOCKSIZE + 1)] + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; else *key++ = (kb[j] + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; } }
SAFER::SAFER(const byte *userkey_1, const byte *userkey_2, unsigned nof_rounds, bool strengthened) : keySchedule(1 + BLOCKSIZE * (1 + 2 * nof_rounds)) { unsigned int i, j; byte *key = keySchedule; SecByteBlock ka(BLOCKSIZE + 1), kb(BLOCKSIZE + 1); if (MAX_ROUNDS < nof_rounds) nof_rounds = MAX_ROUNDS; *key++ = (unsigned char)nof_rounds; ka[BLOCKSIZE] = 0; kb[BLOCKSIZE] = 0; for (j = 0; j < BLOCKSIZE; j++) { ka[BLOCKSIZE] ^= ka[j] = rotlFixed(userkey_1[j], 5U); kb[BLOCKSIZE] ^= kb[j] = *key++ = userkey_2[j]; } for (i = 1; i <= nof_rounds; i++) { for (j = 0; j < BLOCKSIZE + 1; j++) { ka[j] = rotlFixed(ka[j], 6U); kb[j] = rotlFixed(kb[j], 6U); } for (j = 0; j < BLOCKSIZE; j++) if (strengthened) *key++ = (ka[(j + 2 * i - 1) % (BLOCKSIZE + 1)] + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; else *key++ = (ka[j] + exp_tab[exp_tab[18 * i + j + 1]]) & 0xFF; for (j = 0; j < BLOCKSIZE; j++) if (strengthened) *key++ = (kb[(j + 2 * i) % (BLOCKSIZE + 1)] + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; else *key++ = (kb[j] + exp_tab[exp_tab[18 * i + j + 10]]) & 0xFF; } }
static void LTf (uint32 *a, uint32 *b, uint32 *c, uint32 *d) { *a = rotlFixed(*a, 13); *c = rotlFixed(*c, 3); *d = rotlFixed(*d ^ *c ^ (*a << 3), 7); *b = rotlFixed(*b ^ *a ^ *c, 1); *a = rotlFixed(*a ^ *b ^ *d, 5); *c = rotlFixed(*c ^ *d ^ (*b << 7), 22); }
void serpent_set_key(const unsigned __int8 userKey[], int keylen, unsigned __int8 *ks) { unsigned __int32 a,b,c,d,e; unsigned __int32 *k = (unsigned __int32 *)ks; unsigned __int32 t; int i; for (i = 0; i < keylen / (int)sizeof(__int32); i++) k[i] = LE32(((unsigned __int32*)userKey)[i]); if (keylen < 32) k[keylen/4] |= (unsigned __int32)1 << ((keylen%4)*8); k += 8; t = k[-1]; for (i = 0; i < 132; ++i) k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11); k -= 20; #define LK(r, a, b, c, d, e) {\ a = k[(8-r)*4 + 0]; \ b = k[(8-r)*4 + 1]; \ c = k[(8-r)*4 + 2]; \ d = k[(8-r)*4 + 3];} #define SK(r, a, b, c, d, e) {\ k[(8-r)*4 + 4] = a; \ k[(8-r)*4 + 5] = b; \ k[(8-r)*4 + 6] = c; \ k[(8-r)*4 + 7] = d;} \ for (i=0; i<4; i++) { afterS2(LK); afterS2(S3); afterS3(SK); afterS1(LK); afterS1(S2); afterS2(SK); afterS0(LK); afterS0(S1); afterS1(SK); beforeS0(LK); beforeS0(S0); afterS0(SK); k += 8*4; afterS6(LK); afterS6(S7); afterS7(SK); afterS5(LK); afterS5(S6); afterS6(SK); afterS4(LK); afterS4(S5); afterS5(SK); afterS3(LK); afterS3(S4); afterS4(SK); } afterS2(LK); afterS2(S3); afterS3(SK); }
void Serpent::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &) { AssertValidKeyLength(keylen); word32 *k = m_key; GetUserKey(LITTLE_ENDIAN_ORDER, k, 8, userKey, keylen); if (keylen < 32) k[keylen/4] |= word32(1) << ((keylen%4)*8); k += 8; word32 t = k[-1]; signed int i; for (i = 0; i < 132; ++i) k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11); k -= 20; #define LK(r, a, b, c, d, e) {\ a = k[(8-r)*4 + 0]; \ b = k[(8-r)*4 + 1]; \ c = k[(8-r)*4 + 2]; \ d = k[(8-r)*4 + 3];} #define SK(r, a, b, c, d, e) {\ k[(8-r)*4 + 4] = a; \ k[(8-r)*4 + 5] = b; \ k[(8-r)*4 + 6] = c; \ k[(8-r)*4 + 7] = d;} \ word32 a,b,c,d,e; for (i=0; i<4; i++) { afterS2(LK); afterS2(S3); afterS3(SK); afterS1(LK); afterS1(S2); afterS2(SK); afterS0(LK); afterS0(S1); afterS1(SK); beforeS0(LK); beforeS0(S0); afterS0(SK); k += 8*4; afterS6(LK); afterS6(S7); afterS7(SK); afterS5(LK); afterS5(S6); afterS6(SK); afterS4(LK); afterS4(S5); afterS5(SK); afterS3(LK); afterS3(S4); afterS4(SK); } afterS2(LK); afterS2(S3); afterS3(SK); }
static void KeccakF1600(word64 *state) { { word64 Aba, Abe, Abi, Abo, Abu; word64 Aga, Age, Agi, Ago, Agu; word64 Aka, Ake, Aki, Ako, Aku; word64 Ama, Ame, Ami, Amo, Amu; word64 Asa, Ase, Asi, Aso, Asu; word64 BCa, BCe, BCi, BCo, BCu; word64 Da, De, Di, Do, Du; word64 Eba, Ebe, Ebi, Ebo, Ebu; word64 Ega, Ege, Egi, Ego, Egu; word64 Eka, Eke, Eki, Eko, Eku; word64 Ema, Eme, Emi, Emo, Emu; word64 Esa, Ese, Esi, Eso, Esu; //copyFromState(A, state) typedef BlockGetAndPut<word64, LittleEndian, true, true> Block; Block::Get(state)(Aba)(Abe)(Abi)(Abo)(Abu)(Aga)(Age)(Agi)(Ago)(Agu)(Aka)(Ake)(Aki)(Ako)(Aku)(Ama)(Ame)(Ami)(Amo)(Amu)(Asa)(Ase)(Asi)(Aso)(Asu); for( unsigned int round = 0; round < 24; round += 2 ) { // prepareTheta BCa = Aba^Aga^Aka^Ama^Asa; BCe = Abe^Age^Ake^Ame^Ase; BCi = Abi^Agi^Aki^Ami^Asi; BCo = Abo^Ago^Ako^Amo^Aso; BCu = Abu^Agu^Aku^Amu^Asu; //thetaRhoPiChiIotaPrepareTheta(round , A, E) Da = BCu^rotlFixed(BCe, 1); De = BCa^rotlFixed(BCi, 1); Di = BCe^rotlFixed(BCo, 1); Do = BCi^rotlFixed(BCu, 1); Du = BCo^rotlFixed(BCa, 1); Aba ^= Da; BCa = Aba; Age ^= De; BCe = rotlFixed(Age, 44); Aki ^= Di; BCi = rotlFixed(Aki, 43); Amo ^= Do; BCo = rotlFixed(Amo, 21); Asu ^= Du; BCu = rotlFixed(Asu, 14); Eba = BCa ^((~BCe)& BCi ); Eba ^= (word64)KeccakF_RoundConstants[round]; Ebe = BCe ^((~BCi)& BCo ); Ebi = BCi ^((~BCo)& BCu ); Ebo = BCo ^((~BCu)& BCa ); Ebu = BCu ^((~BCa)& BCe ); Abo ^= Do; BCa = rotlFixed(Abo, 28); Agu ^= Du; BCe = rotlFixed(Agu, 20); Aka ^= Da; BCi = rotlFixed(Aka, 3); Ame ^= De; BCo = rotlFixed(Ame, 45); Asi ^= Di; BCu = rotlFixed(Asi, 61); Ega = BCa ^((~BCe)& BCi ); Ege = BCe ^((~BCi)& BCo ); Egi = BCi ^((~BCo)& BCu ); Ego = BCo ^((~BCu)& BCa ); Egu = BCu ^((~BCa)& BCe ); Abe ^= De; BCa = rotlFixed(Abe, 1); Agi ^= Di; BCe = rotlFixed(Agi, 6); Ako ^= Do; BCi = rotlFixed(Ako, 25); Amu ^= Du; BCo = rotlFixed(Amu, 8); Asa ^= Da; BCu = rotlFixed(Asa, 18); Eka = BCa ^((~BCe)& BCi ); Eke = BCe ^((~BCi)& BCo ); Eki = BCi ^((~BCo)& BCu ); Eko = BCo ^((~BCu)& BCa ); Eku = BCu ^((~BCa)& BCe ); Abu ^= Du; BCa = rotlFixed(Abu, 27); Aga ^= Da; BCe = rotlFixed(Aga, 36); Ake ^= De; BCi = rotlFixed(Ake, 10); Ami ^= Di; BCo = rotlFixed(Ami, 15); Aso ^= Do; BCu = rotlFixed(Aso, 56); Ema = BCa ^((~BCe)& BCi ); Eme = BCe ^((~BCi)& BCo ); Emi = BCi ^((~BCo)& BCu ); Emo = BCo ^((~BCu)& BCa ); Emu = BCu ^((~BCa)& BCe ); Abi ^= Di; BCa = rotlFixed(Abi, 62); Ago ^= Do; BCe = rotlFixed(Ago, 55); Aku ^= Du; BCi = rotlFixed(Aku, 39); Ama ^= Da; BCo = rotlFixed(Ama, 41); Ase ^= De; BCu = rotlFixed(Ase, 2); Esa = BCa ^((~BCe)& BCi ); Ese = BCe ^((~BCi)& BCo ); Esi = BCi ^((~BCo)& BCu ); Eso = BCo ^((~BCu)& BCa ); Esu = BCu ^((~BCa)& BCe ); // prepareTheta BCa = Eba^Ega^Eka^Ema^Esa; BCe = Ebe^Ege^Eke^Eme^Ese; BCi = Ebi^Egi^Eki^Emi^Esi; BCo = Ebo^Ego^Eko^Emo^Eso; BCu = Ebu^Egu^Eku^Emu^Esu; //thetaRhoPiChiIotaPrepareTheta(round+1, E, A) Da = BCu^rotlFixed(BCe, 1); De = BCa^rotlFixed(BCi, 1); Di = BCe^rotlFixed(BCo, 1); Do = BCi^rotlFixed(BCu, 1); Du = BCo^rotlFixed(BCa, 1); Eba ^= Da; BCa = Eba; Ege ^= De; BCe = rotlFixed(Ege, 44); Eki ^= Di; BCi = rotlFixed(Eki, 43); Emo ^= Do; BCo = rotlFixed(Emo, 21); Esu ^= Du; BCu = rotlFixed(Esu, 14); Aba = BCa ^((~BCe)& BCi ); Aba ^= (word64)KeccakF_RoundConstants[round+1]; Abe = BCe ^((~BCi)& BCo ); Abi = BCi ^((~BCo)& BCu ); Abo = BCo ^((~BCu)& BCa ); Abu = BCu ^((~BCa)& BCe ); Ebo ^= Do; BCa = rotlFixed(Ebo, 28); Egu ^= Du; BCe = rotlFixed(Egu, 20); Eka ^= Da; BCi = rotlFixed(Eka, 3); Eme ^= De; BCo = rotlFixed(Eme, 45); Esi ^= Di; BCu = rotlFixed(Esi, 61); Aga = BCa ^((~BCe)& BCi ); Age = BCe ^((~BCi)& BCo ); Agi = BCi ^((~BCo)& BCu ); Ago = BCo ^((~BCu)& BCa ); Agu = BCu ^((~BCa)& BCe ); Ebe ^= De; BCa = rotlFixed(Ebe, 1); Egi ^= Di; BCe = rotlFixed(Egi, 6); Eko ^= Do; BCi = rotlFixed(Eko, 25); Emu ^= Du; BCo = rotlFixed(Emu, 8); Esa ^= Da; BCu = rotlFixed(Esa, 18); Aka = BCa ^((~BCe)& BCi ); Ake = BCe ^((~BCi)& BCo ); Aki = BCi ^((~BCo)& BCu ); Ako = BCo ^((~BCu)& BCa ); Aku = BCu ^((~BCa)& BCe ); Ebu ^= Du; BCa = rotlFixed(Ebu, 27); Ega ^= Da; BCe = rotlFixed(Ega, 36); Eke ^= De; BCi = rotlFixed(Eke, 10); Emi ^= Di; BCo = rotlFixed(Emi, 15); Eso ^= Do; BCu = rotlFixed(Eso, 56); Ama = BCa ^((~BCe)& BCi ); Ame = BCe ^((~BCi)& BCo ); Ami = BCi ^((~BCo)& BCu ); Amo = BCo ^((~BCu)& BCa ); Amu = BCu ^((~BCa)& BCe ); Ebi ^= Di; BCa = rotlFixed(Ebi, 62); Ego ^= Do; BCe = rotlFixed(Ego, 55); Eku ^= Du; BCi = rotlFixed(Eku, 39); Ema ^= Da; BCo = rotlFixed(Ema, 41); Ese ^= De; BCu = rotlFixed(Ese, 2); Asa = BCa ^((~BCe)& BCi ); Ase = BCe ^((~BCi)& BCo ); Asi = BCi ^((~BCo)& BCu ); Aso = BCo ^((~BCu)& BCa ); Asu = BCu ^((~BCa)& BCe ); } //copyToState(state, A) Block::Put(NULL, state)(Aba)(Abe)(Abi)(Abo)(Abu)(Aga)(Age)(Agi)(Ago)(Agu)(Aka)(Ake)(Aki)(Ako)(Aku)(Ama)(Ame)(Ami)(Amo)(Amu)(Asa)(Ase)(Asi)(Aso)(Asu); } }