Source file src/crypto/internal/fips140/subtle/xor.go

     1  // Copyright 2022 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
     6  
     7  import "crypto/internal/fips140/alias"
     8  
     9  // XORBytes sets dst[i] = x[i] ^ y[i] for all i < n = min(len(x), len(y)),
    10  // returning n, the number of bytes written to dst.
    11  //
    12  // If dst does not have length at least n,
    13  // XORBytes panics without writing anything to dst.
    14  //
    15  // dst and x or y may overlap exactly or not at all,
    16  // otherwise XORBytes may panic.
    17  func XORBytes(dst, x, y []byte) int {
    18  	n := min(len(x), len(y))
    19  	if n == 0 {
    20  		return 0
    21  	}
    22  	if n > len(dst) {
    23  		panic("subtle.XORBytes: dst too short")
    24  	}
    25  	if alias.InexactOverlap(dst[:n], x[:n]) || alias.InexactOverlap(dst[:n], y[:n]) {
    26  		panic("subtle.XORBytes: invalid overlap")
    27  	}
    28  	xorBytes(&dst[0], &x[0], &y[0], n) // arch-specific
    29  	return n
    30  }
    31  

View as plain text