You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

44 lines
1.2 KiB

  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found src the LICENSE file.
  4. package chacha20
  5. import (
  6. "runtime"
  7. )
  8. // Platforms that have fast unaligned 32-bit little endian accesses.
  9. const unaligned = runtime.GOARCH == "386" ||
  10. runtime.GOARCH == "amd64" ||
  11. runtime.GOARCH == "arm64" ||
  12. runtime.GOARCH == "ppc64le" ||
  13. runtime.GOARCH == "s390x"
  14. // xor reads a little endian uint32 from src, XORs it with u and
  15. // places the result in little endian byte order in dst.
  16. func xor(dst, src []byte, u uint32) {
  17. _, _ = src[3], dst[3] // eliminate bounds checks
  18. if unaligned {
  19. // The compiler should optimize this code into
  20. // 32-bit unaligned little endian loads and stores.
  21. // TODO: delete once the compiler does a reliably
  22. // good job with the generic code below.
  23. // See issue #25111 for more details.
  24. v := uint32(src[0])
  25. v |= uint32(src[1]) << 8
  26. v |= uint32(src[2]) << 16
  27. v |= uint32(src[3]) << 24
  28. v ^= u
  29. dst[0] = byte(v)
  30. dst[1] = byte(v >> 8)
  31. dst[2] = byte(v >> 16)
  32. dst[3] = byte(v >> 24)
  33. } else {
  34. dst[0] = src[0] ^ byte(u)
  35. dst[1] = src[1] ^ byte(u>>8)
  36. dst[2] = src[2] ^ byte(u>>16)
  37. dst[3] = src[3] ^ byte(u>>24)
  38. }
  39. }