chacha_ppc64le.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 (
  7. "encoding/binary"
  8. )
  9. var haveAsm = true
  10. const bufSize = 256
  11. //go:noescape
  12. func chaCha20_ctr32_vsx(out, inp *byte, len int, key *[8]uint32, counter *uint32)
  13. func (c *Cipher) xorKeyStreamAsm(dst, src []byte) {
  14. // This implementation can handle buffers that aren't multiples of
  15. // 256.
  16. if len(src) >= bufSize {
  17. chaCha20_ctr32_vsx(&dst[0], &src[0], len(src), &c.key, &c.counter)
  18. } else if len(src)%bufSize != 0 {
  19. chaCha20_ctr32_vsx(&c.buf[0], &c.buf[0], bufSize, &c.key, &c.counter)
  20. start := len(src) - len(src)%bufSize
  21. ts, td, tb := src[start:], dst[start:], c.buf[:]
  22. // Unroll loop to XOR 32 bytes per iteration.
  23. for i := 0; i < len(ts)-32; i += 32 {
  24. td, tb = td[:len(ts)], tb[:len(ts)] // bounds check elimination
  25. s0 := binary.LittleEndian.Uint64(ts[0:8])
  26. s1 := binary.LittleEndian.Uint64(ts[8:16])
  27. s2 := binary.LittleEndian.Uint64(ts[16:24])
  28. s3 := binary.LittleEndian.Uint64(ts[24:32])
  29. b0 := binary.LittleEndian.Uint64(tb[0:8])
  30. b1 := binary.LittleEndian.Uint64(tb[8:16])
  31. b2 := binary.LittleEndian.Uint64(tb[16:24])
  32. b3 := binary.LittleEndian.Uint64(tb[24:32])
  33. binary.LittleEndian.PutUint64(td[0:8], s0^b0)
  34. binary.LittleEndian.PutUint64(td[8:16], s1^b1)
  35. binary.LittleEndian.PutUint64(td[16:24], s2^b2)
  36. binary.LittleEndian.PutUint64(td[24:32], s3^b3)
  37. ts, td, tb = ts[32:], td[32:], tb[32:]
  38. }
  39. td, tb = td[:len(ts)], tb[:len(ts)] // bounds check elimination
  40. for i, v := range ts {
  41. td[i] = tb[i] ^ v
  42. }
  43. c.len = bufSize - (len(src) % bufSize)
  44. }
  45. }