mirror of
https://github.com/golang/go
synced 2024-11-12 00:20:22 -07:00
crypto/subtle: normalize constant time ops docs
ConstantTimeCompare is fairly useless if you can't rely on it being zero when the slices are different, but thankfully it has that property thanks to the final ConstantTimeByteEq. Change-Id: Id51100ed7d8237abbbb15778a259065b162a48ad Reviewed-on: https://go-review.googlesource.com/c/158643 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
This commit is contained in:
parent
8d2e65d224
commit
ef82ecd0f6
@ -6,9 +6,9 @@
|
|||||||
// code but require careful thought to use correctly.
|
// code but require careful thought to use correctly.
|
||||||
package subtle
|
package subtle
|
||||||
|
|
||||||
// ConstantTimeCompare returns 1 if and only if the two slices, x
|
// ConstantTimeCompare returns 1 if the two slices, x and y, have equal contents
|
||||||
// and y, have equal contents. The time taken is a function of the length of
|
// and 0 otherwise. The time taken is a function of the length of the slices and
|
||||||
// the slices and is independent of the contents.
|
// is independent of the contents.
|
||||||
func ConstantTimeCompare(x, y []byte) int {
|
func ConstantTimeCompare(x, y []byte) int {
|
||||||
if len(x) != len(y) {
|
if len(x) != len(y) {
|
||||||
return 0
|
return 0
|
||||||
@ -23,7 +23,7 @@ func ConstantTimeCompare(x, y []byte) int {
|
|||||||
return ConstantTimeByteEq(v, 0)
|
return ConstantTimeByteEq(v, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConstantTimeSelect returns x if v is 1 and y if v is 0.
|
// ConstantTimeSelect returns x if v == 1 and y if v == 0.
|
||||||
// Its behavior is undefined if v takes any other value.
|
// Its behavior is undefined if v takes any other value.
|
||||||
func ConstantTimeSelect(v, x, y int) int { return ^(v-1)&x | (v-1)&y }
|
func ConstantTimeSelect(v, x, y int) int { return ^(v-1)&x | (v-1)&y }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user