27 lines
1003 B
Ziran
27 lines
1003 B
Ziran
// Constant-time equality for caller-owned byte spans. The length check is
|
|
// intentionally explicit; callers protecting secret tags should use fixed-size
|
|
// buffers so unequal lengths do not become an oracle.
|
|
#scope_export
|
|
|
|
ConstantTimeEqual :: (left: []u8, right: []u8) -> bool {
|
|
difference: u8 = cast(u8)(left.count != right.count)
|
|
index: s32 = 0
|
|
while index < cast(s32)left.count && index < cast(s32)right.count {
|
|
difference |= cast(u8)(left[index] ^ right[index])
|
|
index += 1
|
|
}
|
|
return difference == cast(u8)0
|
|
}
|
|
|
|
// Text carries arbitrary bytes without a mutable borrowed view. Compare
|
|
// every byte of equal-length tags just as for caller-owned byte spans.
|
|
ConstantTimeTextEqual :: (left: string, right: string) -> bool {
|
|
difference: u8 = cast(u8)(left.count != right.count)
|
|
index: s64 = 0
|
|
while index < left.count && index < right.count {
|
|
difference |= left[index] ^ right[index]
|
|
index += 1
|
|
}
|
|
return difference == cast(u8)0
|
|
}
|