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.

chacha_ppc64le.go 1.6KB

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