我试图对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