我試圖對Java Card做一些定時攻擊。我需要一種方法來測量發送命令和獲取答案。我使用的接口是winscard.h
,語言是c++
。 。我創建了一個包裝到winscard.h
界面,以便讓我的工作更輕鬆。例如,發送一個APDU
現在我使用這個代碼似乎工作。 基於this答案,我更新了我的代碼在C++中測量APDU命令到Java卡的時間的最佳方法
byte pbRecvBuffer[258];
long rv;
if (this->sessionHandle >= this->internal.vSessions.size())
throw new SmartCardException("There is no card inserted");
SCARD_IO_REQUEST pioRecvPci;
pioRecvPci.dwProtocol = (this->internal.vSessions)[sessionHandle].dwActiveProtocol;
pioRecvPci.cbPciLength = sizeof(pioRecvPci);
LPSCARD_IO_REQUEST pioSendPci;
if ((this->internal.vSessions)[sessionHandle].dwActiveProtocol == SCARD_PROTOCOL_T1)
pioSendPci = (LPSCARD_IO_REQUEST)SCARD_PCI_T1;
else
pioSendPci = (LPSCARD_IO_REQUEST)SCARD_PCI_T0;
word expected_length = 258;//apdu.getExpectedLen();
word send_length = apdu.getApduLength();
CardSession session = (this->internal.vSessions).operator[](sessionHandle);
byte * data = const_cast<Apdu&>(apdu).getNonConstantData();
auto start = Timer::now();
rv = SCardTransmit(session.hCard, pioSendPci,data,
send_length, &pioRecvPci, pbRecvBuffer,&expected_length);
auto end = Timer::now();
auto duration = (float)(end - start)/Timer::ticks();
return *new ApduResponse(pbRecvBuffer, expected_length,duration);
class Timer
{
public:
static inline int ticks()
{
LARGE_INTEGER ticks;
QueryPerformanceFrequency(&ticks);
return ticks.LowPart;
}
static inline __int64 now()
{
struct { __int32 low, high; } counter;
__asm cpuid
__asm push EDX
__asm rdtsc
__asm mov counter.low, EAX
__asm mov counter.high, EDX
__asm pop EDX
__asm pop EAX
return *(__int64 *)(&counter);
}
};
我的代碼失敗,出現錯誤The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
。我的猜測是,我的英特爾處理器不支持rdtsc
指令。我有英特爾Broadwell 5500U。 。我正在尋找一種合適的方式來做這種測量,並最終得到更準確的答案。
可能的[在Linux/Windows上測量CPU時間和掛鐘時間?]的重複(http://stackoverflow.com/questions/17432502/how-can-i-measure-cpu-time-and -wall-clock-time-on-both-linux-windows) –
@MichaelRoland更新了代碼,更清楚地解釋了我的問題 – Marga