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); }
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); }
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 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)); } }
void RC5::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs ¶ms) { AssertValidKeyLength(keylen); r = GetRoundsAndThrowIfInvalid(params, this); sTable.New(2*(r+1)); static const RC5_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize static const RC5_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize static const int U=sizeof(RC5_WORD); const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0 SecBlock<RC5_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; RC5_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()] = rotlConstant<3>((sTable[h % sTable.size()] + a + b)); b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b)); } }
void RC5::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { const RC5_WORD *sptr = sTable; RC5_WORD a, b; Block::Get(inBlock)(a)(b); a += sptr[0]; b += sptr[1]; sptr += 2; for(unsigned i=0; i<r; i++) { a = rotlMod(a^b,b) + sptr[2*i+0]; b = rotlMod(a^b,a) + sptr[2*i+1]; } Block::Put(xorBlock, outBlock)(a)(b); }
void GOST::Base::PrecalculateSTable() { if (!sTableCalculated) { for (unsigned i = 0; i < 4; i++) for (unsigned j = 0; j < 256; j++) { word32 temp = sBox[2*i][j%16] | (sBox[2*i+1][j/16] << 4); sTable[i][j] = rotlMod(temp, 11+8*i); } sTableCalculated=true; } }