void Camellia::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { #define KS(i, j) ks[i*4 + EFI(j/2)*2 + EFI(j%2)] #define FL(klh, kll, krh, krl) \ ll ^= rotlFixed(lh & klh, 1); \ lh ^= (ll | kll); \ rh ^= (rl | krl); \ rl ^= rotlFixed(rh & krh, 1); word32 lh, ll, rh, rl; typedef BlockGetAndPut<word32, BigEndian> Block; Block::Get(inBlock)(lh)(ll)(rh)(rl); const word32 *ks = m_key.data(); lh ^= KS(0,0); ll ^= KS(0,1); rh ^= KS(0,2); rl ^= KS(0,3); // timing attack countermeasure. see comments at top for more details const int cacheLineSize = GetCacheLineSize(); unsigned int i; volatile word32 _u = 0; word32 u = _u; assert(IsAlignedOn(s1,GetAlignmentOf<word32>())); for (i=0; i<256; i+=cacheLineSize) u &= *(const word32 *)(void*)(s1+i); u &= *(const word32 *)(void*)(s1+252); lh |= u; ll |= u; SLOW_ROUND(lh, ll, rh, rl, KS(1,0), KS(1,1)) SLOW_ROUND(rh, rl, lh, ll, KS(1,2), KS(1,3)) for (i = m_rounds-1; i > 0; --i) { DOUBLE_ROUND(lh, ll, rh, rl, KS(2,0), KS(2,1), KS(2,2), KS(2,3)) DOUBLE_ROUND(lh, ll, rh, rl, KS(3,0), KS(3,1), KS(3,2), KS(3,3)) FL(KS(4,0), KS(4,1), KS(4,2), KS(4,3)); DOUBLE_ROUND(lh, ll, rh, rl, KS(5,0), KS(5,1), KS(5,2), KS(5,3)) ks += 16; } DOUBLE_ROUND(lh, ll, rh, rl, KS(2,0), KS(2,1), KS(2,2), KS(2,3)) ROUND(lh, ll, rh, rl, KS(3,0), KS(3,1)) SLOW_ROUND(rh, rl, lh, ll, KS(3,2), KS(3,3)) lh ^= KS(4,0); ll ^= KS(4,1); rh ^= KS(4,2); rl ^= KS(4,3); Block::Put(xorBlock, outBlock)(rh)(rl)(lh)(ll); }
void ProcessorInfo::getAllInfo() { this->ctCores = tbb::task_scheduler_init::default_num_threads(); this->cache_line_size = GetCacheLineSize(); this->bSupportAVX = SimdDetectFeature(AVXFlag); this->bOSSupportAVX = SimdDetectFeature(OSXSAVEFlag); if(bSupportAVX) this->simd_float_lines = 8; else this->simd_float_lines = 4; this->ctCacheInfo = 0; for(int iLevel = 1; iLevel <= MAX_CACHE_LEVEL; iLevel++) getLevelInfo(iLevel); }
/* * Get address space information */ LOCAL ER _GetSpaceInfo( VP addr, INT len, T_SPINFO *pk_spinfo ) { T_RSMB rsmb; ER ercd = E_OK; INT cont, sz; VP page, va, pa; if ( len <= 0 ) { ercd = E_PAR; goto err_ret; } ercd = ChkSpaceR(addr, len); if ( ercd < E_OK ){ goto err_ret; } ercd = RefSysMemInfo(&rsmb); if ( ercd < E_OK ) { goto err_ret; } pk_spinfo->paddr = toPhysicalAddress(addr); pk_spinfo->page = (VP)((UW)(pk_spinfo->paddr) & ~(rsmb.blksz-1)); pk_spinfo->pagesz = rsmb.blksz; pk_spinfo->cachesz = GetCacheLineSize(); cont = 0; page = pk_spinfo->page; va = addr; while ( cont < len ) { pa = toPhysicalAddress(va); if ( (pa < page) || ((page + rsmb.blksz) <= pa) ) { break; } sz = rsmb.blksz - (pa - page); cont += sz; page += rsmb.blksz; va += sz; } pk_spinfo->cont = ( cont <= len )? cont: len; return ercd; err_ret: DEBUG_PRINT(("_GetSpaceInfo ercd = %d\n", ercd)); return ercd; }
/* * Get address space information */ LOCAL ER _GetSpaceInfo( CONST void *addr, INT len, T_SPINFO *pk_spinfo ) { T_RSMB rsmb; ER ercd = E_OK; if ( len <= 0 ) { ercd = E_PAR; goto err_ret; } ercd = ChkSpaceR(addr, len); if ( ercd < E_OK ){ goto err_ret; } ercd = RefSysMemInfo(&rsmb); if ( ercd < E_OK ) { goto err_ret; } pk_spinfo->paddr = toPhysicalAddress(addr); pk_spinfo->page = (void*)((UW)(pk_spinfo->paddr) & ~(rsmb.blksz-1)); pk_spinfo->pagesz = rsmb.blksz; pk_spinfo->cachesz = GetCacheLineSize(); /* Assumes here that logical and physical addresses are mapped linear. This assures that physical page addresses are also contiguous if logical page addresses are contiguous. */ pk_spinfo->cont = len; return ercd; err_ret: #ifdef DEBUG TM_DEBUG_PRINT(("_GetSpaceInfo ercd = %d\n", ercd)); #endif return ercd; }
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; }