chacha_ppc64le.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 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. // +build ppc64le,!gccgo,!appengine
  5. package chacha20
  6. import "encoding/binary"
  7. var haveAsm = true
  8. const bufSize = 256
  9. //go:noescape
  10. func chaCha20_ctr32_vmx(out, inp *byte, len int, key *[8]uint32, counter *uint32)
  11. func (c *Cipher) xorKeyStreamAsm(dst, src []byte) {
  12. if len(src) >= bufSize {
  13. chaCha20_ctr32_vmx(&dst[0], &src[0], len(src)-len(src)%bufSize, &c.key, &c.counter)
  14. }
  15. if len(src)%bufSize != 0 {
  16. chaCha20_ctr32_vmx(&c.buf[0], &c.buf[0], bufSize, &c.key, &c.counter)
  17. start := len(src) - len(src)%bufSize
  18. ts, td, tb := src[start:], dst[start:], c.buf[:]
  19. // Unroll loop to XOR 32 bytes per iteration.
  20. for i := 0; i < len(ts)-32; i += 32 {
  21. td, tb = td[:len(ts)], tb[:len(ts)] // bounds check elimination
  22. s0 := binary.LittleEndian.Uint64(ts[0:8])
  23. s1 := binary.LittleEndian.Uint64(ts[8:16])
  24. s2 := binary.LittleEndian.Uint64(ts[16:24])
  25. s3 := binary.LittleEndian.Uint64(ts[24:32])
  26. b0 := binary.LittleEndian.Uint64(tb[0:8])
  27. b1 := binary.LittleEndian.Uint64(tb[8:16])
  28. b2 := binary.LittleEndian.Uint64(tb[16:24])
  29. b3 := binary.LittleEndian.Uint64(tb[24:32])
  30. binary.LittleEndian.PutUint64(td[0:8], s0^b0)
  31. binary.LittleEndian.PutUint64(td[8:16], s1^b1)
  32. binary.LittleEndian.PutUint64(td[16:24], s2^b2)
  33. binary.LittleEndian.PutUint64(td[24:32], s3^b3)
  34. ts, td, tb = ts[32:], td[32:], tb[32:]
  35. }
  36. td, tb = td[:len(ts)], tb[:len(ts)] // bounds check elimination
  37. for i, v := range ts {
  38. td[i] = tb[i] ^ v
  39. }
  40. c.len = bufSize - (len(src) % bufSize)
  41. }
  42. }