/* ================= Sys_GetProcessorFeatures ================= */ int Sys_GetProcessorFeatures( void ) { int features = 0; //cpuFeatures_t features = (cpuFeatures_t)0; CPUINFO cpuinfo; GetCPUInfo( &cpuinfo, CI_FALSE ); if( HasCPUID() ) features |= CF_RDTSC; if( HasMMX( &cpuinfo ) ) features |= CF_MMX; if( HasMMXExt( &cpuinfo ) ) features |= CF_MMX_EXT; if( Has3DNow( &cpuinfo ) ) features |= CF_3DNOW; if( Has3DNowExt( &cpuinfo ) ) features |= CF_3DNOW_EXT; if( HasSSE( &cpuinfo ) ) features |= CF_SSE; if( HasSSE2( &cpuinfo ) ) features |= CF_SSE2; if( HasSSE3( &cpuinfo ) ) features |= CF_SSE3; if( HasSSSE3( &cpuinfo ) ) features |= CF_SSSE3; if( HasSSE4_1( &cpuinfo ) ) features |= CF_SSE4_1; if( HasSSE4_2( &cpuinfo ) ) features |= CF_SSE4_2; if( HasHTT( &cpuinfo ) ) features |= CF_HasHTT; if( HasSerial( &cpuinfo ) ) features |= CF_HasSerial; if( Is64Bit( &cpuinfo ) ) features |= CF_Is64Bit; return features; }
std::string ChaCha_Policy::AlgorithmProvider() const { #if (CRYPTOPP_AVX2_AVAILABLE) if (HasAVX2()) return "AVX2"; else #endif #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE) if (HasSSE2()) return "SSE2"; else #endif #if (CRYPTOPP_ARM_NEON_AVAILABLE) if (HasNEON()) return "NEON"; else #endif #if (CRYPTOPP_POWER7_AVAILABLE) if (HasPower7()) return "Power7"; else #endif #if (CRYPTOPP_ALTIVEC_AVAILABLE) if (HasAltivec()) return "Altivec"; else #endif return "C++"; }
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 }
void PanamaCipherPolicy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) { #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE) if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2()) Panama_SSE2_Pull(iterationCount, this->m_state, (word32 *)output, (const word32 *)input); else #endif this->Iterate(iterationCount, NULL, (word32 *)output, (const word32 *)input); }
unsigned int PanamaCipherPolicy<B>::GetAlignment() const { #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE) if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2()) return 16; else #endif return 1; }
unsigned int ChaCha_Policy<R>::GetOptimalBlockSize() const { #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0 if (HasSSE2()) return 4*BYTES_PER_ITERATION; else #endif return BYTES_PER_ITERATION; }
unsigned int ChaCha_Policy<R>::GetAlignment() const { #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && 0 if (HasSSE2()) return 16; else #endif return GetAlignmentOf<word32>(); }
bool CipherSerpent::IsHwSupportAvailable () const { #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE static bool state = false; static bool stateValid = false; if (!stateValid) { state = HasSSE2() ? true : false; stateValid = true; } return state; #else return false; #endif }
unsigned int ChaCha_Policy::GetAlignment() const { #if (CRYPTOPP_AVX2_AVAILABLE) if (HasAVX2()) return 16; else #endif #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE) if (HasSSE2()) return 16; else #endif #if (CRYPTOPP_ALTIVEC_AVAILABLE) if (HasAltivec()) return 16; else #endif return GetAlignmentOf<word32>(); }
nglString nglCPUInfo::Dump() { nglString text, buffer; uint count = GetCount(); if (count == 0) { text = _T("no host CPU information available"); return text; } switch (GetFamily()) { case eUnknown: text += _T("unknown"); break; case eIA32 : text += _T("IA32"); break; case eIA64 : text += _T("IA64"); break; case ePPC : text += _T("PPC"); break; case eAlpha : text += _T("Alpha"); break; case eMIPS : text += _T("MIPS"); break; } if (count > 1) { buffer.Format(_T(" x %d"), count); text += _T(" x %d"); } buffer.Format(_T("%s%s%s%s%s"), HasMMX() ? _T(" MMX") : _T(""), HasSSE() ? _T(" SSE") : _T(""), HasSSE2() ? _T(" SSE2") : _T(""), Has3DNow() ? _T(" 3DNow") : _T(""), HasAltivec() ? _T(" Altivec") : _T("")); if (buffer.GetLength()) { text += _T(" with"); text += buffer; } return text; }
void SHA512::Transform(word64 *state, const word64 *data) { #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) if (HasSSE2()) { SHA512_SSE2_Transform(state, data); return; } #endif #define S0(x) (rotrFixed(x,28)^rotrFixed(x,34)^rotrFixed(x,39)) #define S1(x) (rotrFixed(x,14)^rotrFixed(x,18)^rotrFixed(x,41)) #define s0(x) (rotrFixed(x,1)^rotrFixed(x,8)^(x>>7)) #define s1(x) (rotrFixed(x,19)^rotrFixed(x,61)^(x>>6)) #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+SHA512_K[i+j]+(j?blk2(i):blk0(i));\ d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i)) word64 W[16]; word64 T[8]; /* Copy context->state[] to working vars */ memcpy(T, state, sizeof(T)); /* 80 operations, partially loop unrolled */ for (unsigned int j=0; j<80; 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); }
void PanamaCipherPolicy<B>::CipherResynchronize(byte *keystreamBuffer, const byte *iv) { this->Reset(); this->Iterate(1, m_key); if (iv && IsAligned<word32>(iv)) this->Iterate(1, (const word32 *)iv); else { FixedSizeSecBlock<word32, 8> buf; if (iv) memcpy(buf, iv, 32); else memset(buf, 0, 32); this->Iterate(1, buf); } #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE) if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2()) Panama_SSE2_Pull(32, this->m_state, NULL, NULL); else #endif this->Iterate(32); }
unsigned int ChaCha_Policy::GetOptimalBlockSize() const { #if (CRYPTOPP_AVX2_AVAILABLE) if (HasAVX2()) return 8 * BYTES_PER_ITERATION; else #endif #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE) if (HasSSE2()) return 4*BYTES_PER_ITERATION; else #endif #if (CRYPTOPP_ARM_NEON_AVAILABLE) if (HasNEON()) return 4*BYTES_PER_ITERATION; else #endif #if (CRYPTOPP_ALTIVEC_AVAILABLE) if (HasAltivec()) return 4*BYTES_PER_ITERATION; else #endif return BYTES_PER_ITERATION; }
// OperateKeystream always produces a key stream. The key stream is written // to output. Optionally a message may be supplied to xor with the key stream. // The message is input, and output = output ^ input. void ChaCha_Policy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) { do { #if (CRYPTOPP_AVX2_AVAILABLE) if (HasAVX2()) { while (iterationCount >= 8 && MultiBlockSafe(8)) { const bool xorInput = (operation & INPUT_NULL) != INPUT_NULL; ChaCha_OperateKeystream_AVX2(m_state, xorInput ? input : NULLPTR, output, m_rounds); // MultiBlockSafe avoids overflow on the counter words m_state[12] += 8; //if (m_state[12] < 8) // m_state[13]++; input += (!!xorInput) * 8 * BYTES_PER_ITERATION; output += 8 * BYTES_PER_ITERATION; iterationCount -= 8; } } #endif #if (CRYPTOPP_SSE2_INTRIN_AVAILABLE || CRYPTOPP_SSE2_ASM_AVAILABLE) if (HasSSE2()) { while (iterationCount >= 4 && MultiBlockSafe(4)) { const bool xorInput = (operation & INPUT_NULL) != INPUT_NULL; ChaCha_OperateKeystream_SSE2(m_state, xorInput ? input : NULLPTR, output, m_rounds); // MultiBlockSafe avoids overflow on the counter words m_state[12] += 4; //if (m_state[12] < 4) // m_state[13]++; input += (!!xorInput)*4*BYTES_PER_ITERATION; output += 4*BYTES_PER_ITERATION; iterationCount -= 4; } } #endif #if (CRYPTOPP_ARM_NEON_AVAILABLE) if (HasNEON()) { while (iterationCount >= 4 && MultiBlockSafe(4)) { const bool xorInput = (operation & INPUT_NULL) != INPUT_NULL; ChaCha_OperateKeystream_NEON(m_state, xorInput ? input : NULLPTR, output, m_rounds); // MultiBlockSafe avoids overflow on the counter words m_state[12] += 4; //if (m_state[12] < 4) // m_state[13]++; input += (!!xorInput)*4*BYTES_PER_ITERATION; output += 4*BYTES_PER_ITERATION; iterationCount -= 4; } } #endif #if (CRYPTOPP_ALTIVEC_AVAILABLE) if (HasAltivec()) { while (iterationCount >= 4 && MultiBlockSafe(4)) { const bool xorInput = (operation & INPUT_NULL) != INPUT_NULL; ChaCha_OperateKeystream_POWER7(m_state, xorInput ? input : NULLPTR, output, m_rounds); // MultiBlockSafe avoids overflow on the counter words m_state[12] += 4; //if (m_state[12] < 4) // m_state[13]++; input += (!!xorInput)*4*BYTES_PER_ITERATION; output += 4*BYTES_PER_ITERATION; iterationCount -= 4; } } #endif if (iterationCount) { word32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; x0 = m_state[0]; x1 = m_state[1]; x2 = m_state[2]; x3 = m_state[3]; x4 = m_state[4]; x5 = m_state[5]; x6 = m_state[6]; x7 = m_state[7]; x8 = m_state[8]; x9 = m_state[9]; x10 = m_state[10]; x11 = m_state[11]; x12 = m_state[12]; x13 = m_state[13]; x14 = m_state[14]; x15 = m_state[15]; for (int i = static_cast<int>(m_rounds); i > 0; i -= 2) { CHACHA_QUARTER_ROUND(x0, x4, x8, x12); CHACHA_QUARTER_ROUND(x1, x5, x9, x13); CHACHA_QUARTER_ROUND(x2, x6, x10, x14); CHACHA_QUARTER_ROUND(x3, x7, x11, x15); CHACHA_QUARTER_ROUND(x0, x5, x10, x15); CHACHA_QUARTER_ROUND(x1, x6, x11, x12); CHACHA_QUARTER_ROUND(x2, x7, x8, x13); CHACHA_QUARTER_ROUND(x3, x4, x9, x14); } CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(CHACHA_OUTPUT, BYTES_PER_ITERATION); if (++m_state[12] == 0) m_state[13]++; } // We may re-enter a SIMD keystream operation from here. } while (iterationCount--); }
size_t SHA224::HashMultipleBlocks(const word32 *input, size_t length) { X86_SHA256_HashBlocks(m_state, input, (length&(size_t(0)-BLOCKSIZE)) - !HasSSE2()); return length % BLOCKSIZE; }
bool TestSettings() { bool pass = true; cout << "\nTesting Settings...\n\n"; if (*(word32 *)"\x01\x02\x03\x04" == 0x04030201L) { #ifdef IS_LITTLE_ENDIAN cout << "passed: "; #else cout << "FAILED: "; pass = false; #endif cout << "Your machine is little endian.\n"; } else if (*(word32 *)"\x01\x02\x03\x04" == 0x01020304L) { #ifndef IS_LITTLE_ENDIAN cout << "passed: "; #else cout << "FAILED: "; pass = false; #endif cout << "Your machine is big endian.\n"; } else { cout << "FAILED: Your machine is neither big endian nor little endian.\n"; pass = false; } if (sizeof(byte) == 1) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(byte) == " << sizeof(byte) << endl; if (sizeof(word16) == 2) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(word16) == " << sizeof(word16) << endl; if (sizeof(word32) == 4) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(word32) == " << sizeof(word32) << endl; #ifdef WORD64_AVAILABLE if (sizeof(word64) == 8) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(word64) == " << sizeof(word64) << endl; #elif defined(CRYPTOPP_NATIVE_DWORD_AVAILABLE) if (sizeof(dword) >= 8) { cout << "FAILED: sizeof(dword) >= 8, but WORD64_AVAILABLE not defined" << endl; pass = false; } else cout << "passed: word64 not available" << endl; #endif #ifdef CRYPTOPP_WORD128_AVAILABLE if (sizeof(word128) == 16) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(word128) == " << sizeof(word128) << endl; #endif if (sizeof(word) == 2*sizeof(hword) #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE && sizeof(dword) == 2*sizeof(word) #endif ) cout << "passed: "; else { cout << "FAILED: "; pass = false; } cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word); #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE cout << ", sizeof(dword) == " << sizeof(dword); #endif cout << endl; bool hasMMX = HasMMX(); bool hasISSE = HasISSE(); bool hasSSE2 = HasSSE2(); bool hasSSSE3 = HasSSSE3(); bool isP4 = IsP4(); int cacheLineSize = GetCacheLineSize(); if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize))) { cout << "FAILED: "; pass = false; } else cout << "passed: "; cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize; if (!pass) { cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl; abort(); } return pass; }