Source file src/crypto/subtle/constant_time.go
1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package subtle implements functions that are often useful in cryptographic 6 // code but require careful thought to use correctly. 7 package subtle 8 9 import ( 10 "crypto/internal/constanttime" 11 "crypto/internal/fips140/subtle" 12 ) 13 14 // These functions are forwarded to crypto/internal/constanttime for intrinsified 15 // operations, and to crypto/internal/fips140/subtle for byte slice operations. 16 17 // ConstantTimeCompare returns 1 if the two slices, x and y, have equal contents 18 // and 0 otherwise. The time taken is a function of the length of the slices and 19 // is independent of the contents. If the lengths of x and y do not match it 20 // returns 0 immediately. 21 func ConstantTimeCompare(x, y []byte) int { 22 return subtle.ConstantTimeCompare(x, y) 23 } 24 25 // ConstantTimeSelect returns x if v == 1 and y if v == 0. 26 // Its behavior is undefined if v takes any other value. 27 func ConstantTimeSelect(v, x, y int) int { 28 return constanttime.Select(v, x, y) 29 } 30 31 // ConstantTimeByteEq returns 1 if x == y and 0 otherwise. 32 func ConstantTimeByteEq(x, y uint8) int { 33 return constanttime.ByteEq(x, y) 34 } 35 36 // ConstantTimeEq returns 1 if x == y and 0 otherwise. 37 func ConstantTimeEq(x, y int32) int { 38 return constanttime.Eq(x, y) 39 } 40 41 // ConstantTimeCopy copies the contents of y into x (a slice of equal length) 42 // if v == 1. If v == 0, x is left unchanged. Its behavior is undefined if v 43 // takes any other value. 44 func ConstantTimeCopy(v int, x, y []byte) { 45 subtle.ConstantTimeCopy(v, x, y) 46 } 47 48 // ConstantTimeLessOrEq returns 1 if x <= y and 0 otherwise. 49 // Its behavior is undefined if x or y are negative or > 2**31 - 1. 50 func ConstantTimeLessOrEq(x, y int) int { 51 return constanttime.LessOrEq(x, y) 52 } 53