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 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 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 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)]; } }
FeedUserPropertyData FeedUserPropertyClient::Get(Ice::Int uid){ char key[32]; GetUserKey(uid, key, 32); pair<int, MemcachedClient*> cli = pool_.GetClient(uid); if(!cli.second){ return FeedUserPropertyData(); } TimeStat1 ts; vector<string> keys; keys.push_back(key); map<string, string> res; map<string, int> flags; cli.second->Get(keys, res, flags); float cost_mem = ts.getTime(); string ip = cli.second->GetIp(); pool_.ReleaseClient(cli.first, cli.second); if(res.size()!=1){ return FeedUserPropertyData(); } if( cost_mem < 160) { MCE_INFO("FeedUserPropertyClient::Get --> uid:" << uid << " cost_total:" << cost_mem << " ip:" << ip); } else { MCE_WARN("FeedUserPropertyClient::Get --> uid:" << uid << " cost_total:" << cost_mem << " ip:" << ip); } return FeedUserPropertyData(res[key]); }
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]); } }
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 GOST::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &) { AssertValidKeyLength(length); PrecalculateSTable(); GetUserKey(LITTLE_ENDIAN_ORDER, m_key.begin(), 8, userKey, KEYLENGTH); }
bool UserProfileClient::Set(const UserProfile & o) { char key[32]; GetUserKey(o.id(), key, 32); const UserProfile & profile = o; string value; o.SerializeToString(&value); return SetMemcached(key, value, 0); }
void CAST128::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &) { AssertValidKeyLength(keylength); reduced = (keylength <= 10); word32 X[4], Z[4]; GetUserKey(BIG_ENDIAN_ORDER, X, 4, userKey, keylength); #define x(i) GETBYTE(X[i/4], 3-i%4) #define z(i) GETBYTE(Z[i/4], 3-i%4) unsigned int i; for (i=0; i<=16; i+=16) { // this part is copied directly from RFC 2144 (with some search and replace) by Wei Dai Z[0] = X[0] ^ S[4][x(0xD)] ^ S[5][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x(0x5)] ^ S[7][x(0x4)] ^ S[4][z(0x1)]; X[3] = Z[3] ^ S[4][x(0xA)] ^ S[5][x(0x9)] ^ S[6][x(0xB)] ^ S[7][x(0x8)] ^ S[5][z(0x3)]; K[i+12] = S[4][x(0x8)] ^ S[5][x(0x9)] ^ S[6][x(0x7)] ^ S[7][x(0x6)] ^ S[4][x(0x3)]; K[i+13] = S[4][x(0xA)] ^ S[5][x(0xB)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[5][x(0x7)]; K[i+14] = S[4][x(0xC)] ^ S[5][x(0xD)] ^ S[6][x(0x3)] ^ S[7][x(0x2)] ^ S[6][x(0x8)]; K[i+15] = S[4][x(0xE)] ^ S[5][x(0xF)] ^ S[6][x(0x1)] ^ S[7][x(0x0)] ^ S[7][x(0xD)]; } for (i=16; i<32; i++) K[i] &= 0x1f; }
void SHACAL2::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &) { AssertValidKeyLength(keylen); word32 *rk = m_key; unsigned int i; GetUserKey(BIG_ENDIAN_ORDER, rk, m_key.size(), userKey, keylen); for (i = 0; i < 48; i++, rk++) { rk[16] = rk[0] + s0(rk[1]) + rk[9] + s1(rk[14]); rk[0] += K[i]; } for (i = 48; i < 64; i++, rk++) { rk[0] += K[i]; } }
bool FeedUserPropertyClient::Set(int uid, FeedUserPropertyData& data){ char key[32]; GetUserKey(uid, key, 32); pair<int, MemcachedClient*> cli = pool_.GetClient(uid); if(!cli.second) return false; TimeStat1 ts; bool res = cli.second->Set(key, data.GetOriginalData(), 0); float cost_mem = ts.getTime(); string ip = cli.second->GetIp(); pool_.ReleaseClient(cli.first, cli.second); if( cost_mem < 160) { MCE_INFO("FeedUserPropertyClient::set --> uid:" << uid << " cost_total:" << cost_mem << " ip:" << ip); } else { MCE_WARN("FeedUserPropertyClient::set --> uid:" << uid << " cost_total:" << cost_mem << " ip:" << ip); } return res; }
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); }
void CAST256::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &) { AssertValidKeyLength(keylength); word32 kappa[8]; GetUserKey(BIG_ENDIAN_ORDER, kappa, 8, userKey, keylength); for(int i=0; i<12; ++i) { Omega(2*i,kappa); Omega(2*i+1,kappa); K[8*i]=kappa[0] & 31; K[8*i+1]=kappa[2] & 31; K[8*i+2]=kappa[4] & 31; K[8*i+3]=kappa[6] & 31; K[8*i+4]=kappa[7]; K[8*i+5]=kappa[5]; K[8*i+6]=kappa[3]; K[8*i+7]=kappa[1]; } if (!IsForwardTransformation()) { for(int j=0; j<6; ++j) { for(int i=0; i<4; ++i) { int i1=8*j+i; int i2=8*(11-j)+i; assert(i1<i2); std::swap(K[i1],K[i2]); std::swap(K[i1+4],K[i2+4]); } } } memset(kappa, 0, sizeof(kappa)); }
bool UserProfileClient::GetSerialized(Ice::Int uid, string * serialized) { char key[32]; GetUserKey(uid, key, 32); uint32_t flag = 0; size_t len; memcached_return rc; pair<int, memcached_st *> memc_pair = GetMemc(); memcached_st * memc = memc_pair.second; char * v = memcached_get(memc, key, strlen(key), &len, &flag, &rc); if (rc != MEMCACHED_SUCCESS) { ReturnMemc(false, memc_pair); return false; } serialized->assign(v, len); ReturnMemc(true, memc_pair); free(v); return true; }
void Rijndael::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylen) { AssertValidKeyLength(keylen); m_rounds = keylen/4 + 6; m_key.New(4*(m_rounds+1)); word32 temp, *rk = m_key; const word32 *rc = rcon; unsigned int i=0; GetUserKey(BIG_ENDIAN_ORDER, rk, keylen/4, userKey, keylen); while (true) { temp = rk[keylen/4-1]; rk[keylen/4] = rk[0] ^ (word32(Se[GETBYTE(temp, 2)]) << 24) ^ (word32(Se[GETBYTE(temp, 1)]) << 16) ^ (word32(Se[GETBYTE(temp, 0)]) << 8) ^ Se[GETBYTE(temp, 3)] ^ *(rc++); rk[keylen/4+1] = rk[1] ^ rk[keylen/4]; rk[keylen/4+2] = rk[2] ^ rk[keylen/4+1]; rk[keylen/4+3] = rk[3] ^ rk[keylen/4+2]; if (rk + keylen/4 + 4 == m_key.end()) break; if (keylen == 24) { rk[10] = rk[ 4] ^ rk[ 9]; rk[11] = rk[ 5] ^ rk[10]; } else if (keylen == 32) { temp = rk[11]; rk[12] = rk[ 4] ^ (word32(Se[GETBYTE(temp, 3)]) << 24) ^ (word32(Se[GETBYTE(temp, 2)]) << 16) ^ (word32(Se[GETBYTE(temp, 1)]) << 8) ^ Se[GETBYTE(temp, 0)]; rk[13] = rk[ 5] ^ rk[12]; rk[14] = rk[ 6] ^ rk[13]; rk[15] = rk[ 7] ^ rk[14]; } rk += keylen/4; } if (dir == DECRYPTION) { unsigned int i, j; rk = m_key; /* invert the order of the round keys: */ for (i = 0, j = 4*m_rounds; i < j; i += 4, j -= 4) { temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; } /* apply the inverse MixColumn transform to all round keys but the first and the last: */ for (i = 1; i < m_rounds; i++) { rk += 4; rk[0] = Td0[Se[GETBYTE(rk[0], 3)]] ^ Td1[Se[GETBYTE(rk[0], 2)]] ^ Td2[Se[GETBYTE(rk[0], 1)]] ^ Td3[Se[GETBYTE(rk[0], 0)]]; rk[1] = Td0[Se[GETBYTE(rk[1], 3)]] ^ Td1[Se[GETBYTE(rk[1], 2)]] ^ Td2[Se[GETBYTE(rk[1], 1)]] ^ Td3[Se[GETBYTE(rk[1], 0)]]; rk[2] = Td0[Se[GETBYTE(rk[2], 3)]] ^ Td1[Se[GETBYTE(rk[2], 2)]] ^ Td2[Se[GETBYTE(rk[2], 1)]] ^ Td3[Se[GETBYTE(rk[2], 0)]]; rk[3] = Td0[Se[GETBYTE(rk[3], 3)]] ^ Td1[Se[GETBYTE(rk[3], 2)]] ^ Td2[Se[GETBYTE(rk[3], 1)]] ^ Td3[Se[GETBYTE(rk[3], 0)]]; } } ConditionalByteReverse(BIG_ENDIAN_ORDER, m_key.begin(), m_key.begin(), 16); ConditionalByteReverse(BIG_ENDIAN_ORDER, m_key + m_rounds*4, m_key + m_rounds*4, 16); }
SEAL_Gamma(const byte *key) : H(5), Z(5), D(16), lastIndex(0xffffffff) { GetUserKey(BIG_ENDIAN_ORDER, H.begin(), 5, key, 20); memset(D, 0, 64); }