Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/3way.cpp
2 views
// 3way.cpp - modified by Wei Dai from Joan Daemen's 3way.c1// The original code and all modifications are in the public domain.23#include "pch.h"4#include "3way.h"5#include "misc.h"67NAMESPACE_BEGIN(CryptoPP)89#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)10void ThreeWay_TestInstantiations()11{12ThreeWay::Encryption x1;13ThreeWay::Decryption x2;14}15#endif1617namespace18{19const word32 START_E = 0x0b0b; // round constant of first encryption round20const word32 START_D = 0xb1b1; // round constant of first decryption round21}2223static inline word32 reverseBits(word32 a)24{25a = ((a & 0xAAAAAAAA) >> 1) | ((a & 0x55555555) << 1);26a = ((a & 0xCCCCCCCC) >> 2) | ((a & 0x33333333) << 2);27return ((a & 0xF0F0F0F0) >> 4) | ((a & 0x0F0F0F0F) << 4);28}2930#define mu(a0, a1, a2) \31{ \32a1 = reverseBits(a1); \33word32 t = reverseBits(a0); \34a0 = reverseBits(a2); \35a2 = t; \36}3738#define pi_gamma_pi(a0, a1, a2) \39{ \40word32 b0, b2; \41b2 = rotlConstant<1>(a2); \42b0 = rotlConstant<22>(a0); \43a0 = rotlConstant<1>(b0 ^ (a1|(~b2))); \44a2 = rotlConstant<22>(b2 ^ (b0|(~a1))); \45a1 ^= (b2|(~b0)); \46}4748// thanks to Paulo Barreto for this optimized theta()49#define theta(a0, a1, a2) \50{ \51word32 b0, b1, c; \52c = a0 ^ a1 ^ a2; \53c = rotlConstant<16>(c) ^ rotlConstant<8>(c); \54b0 = (a0 << 24) ^ (a2 >> 8) ^ (a1 << 8) ^ (a0 >> 24); \55b1 = (a1 << 24) ^ (a0 >> 8) ^ (a2 << 8) ^ (a1 >> 24); \56a0 ^= c ^ b0; \57a1 ^= c ^ b1; \58a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \59}6061#define rho(a0, a1, a2) \62{ \63theta(a0, a1, a2); \64pi_gamma_pi(a0, a1, a2); \65}6667void ThreeWay::Base::UncheckedSetKey(const byte *uk, unsigned int length, const NameValuePairs ¶ms)68{69AssertValidKeyLength(length);7071m_rounds = GetRoundsAndThrowIfInvalid(params, this);7273for (unsigned int i=0; i<3; i++)74m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24);7576if (!IsForwardTransformation())77{78theta(m_k[0], m_k[1], m_k[2]);79mu(m_k[0], m_k[1], m_k[2]);80m_k[0] = ByteReverse(m_k[0]);81m_k[1] = ByteReverse(m_k[1]);82m_k[2] = ByteReverse(m_k[2]);83}84}8586void ThreeWay::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const87{88typedef BlockGetAndPut<word32, BigEndian> Block;8990word32 a0, a1, a2;91Block::Get(inBlock)(a0)(a1)(a2);9293word32 rc = START_E;9495for(unsigned i=0; i<m_rounds; i++)96{97a0 ^= m_k[0] ^ (rc<<16);98a1 ^= m_k[1];99a2 ^= m_k[2] ^ rc;100rho(a0, a1, a2);101102rc <<= 1;103if (rc&0x10000) rc ^= 0x11011;104}105a0 ^= m_k[0] ^ (rc<<16);106a1 ^= m_k[1];107a2 ^= m_k[2] ^ rc;108theta(a0, a1, a2);109110Block::Put(xorBlock, outBlock)(a0)(a1)(a2);111}112113void ThreeWay::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const114{115typedef BlockGetAndPut<word32, LittleEndian> Block;116117word32 a0, a1, a2;118Block::Get(inBlock)(a0)(a1)(a2);119120word32 rc = START_D;121122mu(a0, a1, a2);123for(unsigned i=0; i<m_rounds; i++)124{125a0 ^= m_k[0] ^ (rc<<16);126a1 ^= m_k[1];127a2 ^= m_k[2] ^ rc;128rho(a0, a1, a2);129130rc <<= 1;131if (rc&0x10000) rc ^= 0x11011;132}133a0 ^= m_k[0] ^ (rc<<16);134a1 ^= m_k[1];135a2 ^= m_k[2] ^ rc;136theta(a0, a1, a2);137mu(a0, a1, a2);138139Block::Put(xorBlock, outBlock)(a0)(a1)(a2);140}141142NAMESPACE_END143144145