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