// construct an SHARK_Enc object with fixed round keys, to be used to initialize actual round keys void SHARK::Enc::InitForKeySetup() { m_rounds = DEFAULT_ROUNDS; m_roundKeys.New(DEFAULT_ROUNDS+1); for (unsigned int i=0; i<DEFAULT_ROUNDS; i++) m_roundKeys[i] = cbox[0][i]; m_roundKeys[DEFAULT_ROUNDS] = SHARKTransform(cbox[0][DEFAULT_ROUNDS]); #ifdef IS_LITTLE_ENDIAN m_roundKeys[0] = ByteReverse(m_roundKeys[0]); m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]); #endif }
void SHA256::Transform(word32 *state, const word32 *data) { word32 W[16]; #if defined(CRYPTOPP_X86_ASM_AVAILABLE) || defined(CRYPTOPP_X32_ASM_AVAILABLE) || defined(CRYPTOPP_X64_MASM_AVAILABLE) // this byte reverse is a waste of time, but this function is only called by MDC ByteReverse(W, data, BLOCKSIZE); X86_SHA256_HashBlocks(state, W, BLOCKSIZE - !HasSSE2()); #else word32 T[8]; /* Copy context->state[] to working vars */ memcpy(T, state, sizeof(T)); /* 64 operations, partially loop unrolled */ for (unsigned int j=0; j<64; j+=16) { R( 0); R( 1); R( 2); R( 3); R( 4); R( 5); R( 6); R( 7); R( 8); R( 9); R(10); R(11); R(12); R(13); R(14); R(15); } /* Add the working vars back into context.state[] */ state[0] += a(0); state[1] += b(0); state[2] += c(0); state[3] += d(0); state[4] += e(0); state[5] += f(0); state[6] += g(0); state[7] += h(0); #endif }
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length) { const unsigned int blockSize = this->BlockSize(); bool noReverse = NativeByteOrderIs(this->GetByteOrder()); T* dataBuf = this->DataBuf(); // Alignment checks due to http://github.com/weidai11/cryptopp/issues/690. // Sparc requires 8-byte aligned buffer when HashWordType is word64. // We also had to provide a GetAlignmentOf specialization for word64 on Sparc. do { if (noReverse) { if (IsAligned<HashWordType>(input)) { // Sparc bus error with non-aligned input. this->HashEndianCorrectedBlock(input); } else { std::memcpy(dataBuf, input, blockSize); this->HashEndianCorrectedBlock(dataBuf); } } else { if (IsAligned<HashWordType>(input)) { // Sparc bus error with non-aligned input. ByteReverse(dataBuf, input, blockSize); this->HashEndianCorrectedBlock(dataBuf); } else { std::memcpy(dataBuf, input, blockSize); ByteReverse(dataBuf, dataBuf, blockSize); this->HashEndianCorrectedBlock(dataBuf); } } input += blockSize/sizeof(T); length -= blockSize; } while (length >= blockSize); return length; }
uint8_t MTSysPingProc(uint8_t *pBuf, uint8_t *returnBuffer) { uint8_t SysPingRsp[SYSPINGRSP_LEN]; ByteReverse(pBuf+SYSPINGRSP_POS, SYSPINGRSP_LEN, SysPingRsp); LOG_ERROR_MESSAGE("Sys ping response: %02x%02x",SysPingRsp[1],SysPingRsp[0]); *returnBuffer = SysPingRsp[0]; *(returnBuffer+1) = SysPingRsp[1]; return 0; }
void SHARK::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int keyLen, unsigned int rounds) { AssertValidKeyLength(keyLen); AssertValidRounds(rounds); m_rounds = rounds; 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 (dir == DECRYPTION) { 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 }
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length) { unsigned int blockSize = BlockSize(); bool noReverse = NativeByteOrderIs(GetByteOrder()); do { if (noReverse) HashEndianCorrectedBlock(input); else { ByteReverse(this->m_data.begin(), input, this->BlockSize()); HashEndianCorrectedBlock(this->m_data); } input += blockSize/sizeof(T); length -= blockSize; } while (length >= blockSize); return length; }
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length) { // Hardware based SHA1 and SHA256 correct blocks themselves due to hardware requirements. // For Intel, SHA1 will effectively call ByteReverse(). SHA256 formats data to Intel // requirements, which means eight words ABCD EFGH are transformed to ABEF CDGH. unsigned int blockSize = this->BlockSize(); bool noReverse = NativeByteOrderIs(this->GetByteOrder()); T* dataBuf = this->DataBuf(); do { if (noReverse) this->HashEndianCorrectedBlock(input); else { ByteReverse(dataBuf, input, this->BlockSize()); this->HashEndianCorrectedBlock(dataBuf); } input += blockSize/sizeof(T); length -= blockSize; } while (length >= blockSize); return length; }