使用Windows crypto API在固定时间内比较两个秘密
使用Windows cryptography API,如何在恒定时间内比较两字节数组的相等性使用Windows crypto API在固定时间内比较两个秘密,windows,cryptography,cng,timing-attack,Windows,Cryptography,Cng,Timing Attack,使用Windows cryptography API,如何在恒定时间内比较两字节数组的相等性 编辑:秘密的长度是固定的,是公开的。定时安全比较需要知道哪个数组来自用户(这决定了它将花费的时间),哪个数组是你的秘密(你不想泄露它有多长的秘密) 这是我第一次看到的一种巧妙的技术。我认为代码有一些错误。语法上,因为大括号不匹配,其次,为什么要在索引中添加1?您不是在比较way@PaulBastian它是从Delphi转录的,在我的脑海中,字符串从1开始索引。所提供的代码没有特定的语言,需要读者根据自己
编辑:秘密的长度是固定的,是公开的。定时安全比较需要知道哪个数组来自用户(这决定了它将花费的时间),哪个数组是你的秘密(你不想泄露它有多长的秘密)
这是我第一次看到的一种巧妙的技术。我认为代码有一些错误。语法上,因为大括号不匹配,其次,为什么要在索引中添加1?您不是在比较way@PaulBastian它是从Delphi转录的,在我的脑海中,字符串从
1
开始索引。所提供的代码没有特定的语言,需要读者根据自己的特定语言调整算法。
//Code released into public domain. No attribution required.
Boolean TimingSafeArrayCompare(Byte[] safe, Byte[] user)
{
/*
A timing safe array comparison.
To prevent leaking length information,
it is important that user input is always used as the second parameter.
safe: The internal (safe) value to be checked
user: The user submitted (unsafe) value
Returns True if the two arrays are identical.
*/
int safeLen = safe.Length;
int userLen = user.Length;
// Set the result to the difference between the lengths.
// This means that arrays of different length will already cause nDiff to be non-zero
int nDiff = safeLen - userLen;
// Note that we ALWAYS iterate over the user-supplied length
// This is to prevent leaking length information
for (i = 0 to userLen-1)
{
//Using mod here is a trick to prevent leaking.
//It's safe, since if the lengths are different, nDiff will already be non-zero
nDiff = nDiff | ( User[i] xor Safe[i mod safeLen] );
}
// They are only identical strings if nDiff is exactly zero
return (nDiff == 0);
}