Esempio n. 1
0
void RC5::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
{
    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()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3);
        b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
    }
}
Esempio n. 2
0
void SHARK::Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
{
	AssertValidKeyLength(keyLen);

	m_rounds = GetRoundsAndThrowIfInvalid(params, this);
	m_roundKeys.New(m_rounds+1);

	// concatenate key enought times to fill a
	for (unsigned int i=0; i<(m_rounds+1)*8; i++)
		((byte *)m_roundKeys.begin())[i] = key[i%keyLen];

	SHARK::Encryption e;
	e.InitForKeySetup();
	byte IV[8] = {0,0,0,0,0,0,0,0};
	CFB_Mode_ExternalCipher::Encryption cfb(e, IV);

	cfb.ProcessString((byte *)m_roundKeys.begin(), (m_rounds+1)*8);

	ConditionalByteReverse(BIG_ENDIAN_ORDER, m_roundKeys.begin(), m_roundKeys.begin(), (m_rounds+1)*8);

	m_roundKeys[m_rounds] = SHARKTransform(m_roundKeys[m_rounds]);

	if (!IsForwardTransformation())
	{
		unsigned int i;

		// transform encryption round keys into decryption round keys
		for (i=0; i<m_rounds/2; i++)
			std::swap(m_roundKeys[i], m_roundKeys[m_rounds-i]);

		for (i=1; i<m_rounds; i++)
			m_roundKeys[i] = SHARKTransform(m_roundKeys[i]);
	}

#ifdef IS_LITTLE_ENDIAN
	m_roundKeys[0] = ByteReverse(m_roundKeys[0]);
	m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]);
#endif
}