Debian Codegraph @

chacha20/chacha/chacha_amd64.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // +build amd64,!gccgo,!appengine
5
6 package chacha
7
8 import (
9 "unsafe"
10
11 "github.com/enceve/crypto"
12 )
13
14 // XORKeyStream crypts bytes from src to dst using the given key, nonce and counter.
15 // The rounds argument specifies the number of rounds (must be even) performed for
16 // keystream generation. (Common values are 20, 12 or 8) Src and dst may be the same
17 // slice but otherwise should not overlap. If len(dst) < len(src) this function panics.
18 func XORKeyStream(dst, src []byte, nonce *[12]byte, key *[32]byte, counter uint32, rounds int) {
19 length := len(src)
20 if len(dst) < length {
21 panic("chacha20/chacha: dst buffer is to small")
22 }
23 if rounds <= 0 || rounds%2 != 0 {
24 panic("chacha20/chacha: rounds must be a multiple of 2")
25 }
26
27 var state [64]byte
28
29 copy(state[:], constants[:])
30
31 statePtr := (*[8]uint64)(unsafe.Pointer(&state[0]))
32 keyPtr := (*[4]uint64)(unsafe.Pointer(&key[0]))
33
34 statePtr[2] = keyPtr[0]
35 statePtr[3] = keyPtr[1]
36 statePtr[4] = keyPtr[2]
37 statePtr[5] = keyPtr[3]
38
39 statePtr[6] = (*(*uint64)(unsafe.Pointer(&nonce[0])) << 32) | uint64(counter)
40
41 statePtr[7] = *(*uint64)(unsafe.Pointer(&nonce[4]))
42
43 if length >= 64 {
44 XORBlocks(dst, src, &state, rounds)
45 }
46
47 if n := length & (^(64 - 1)); length-n > 0 {
48 var block [64]byte
49 Core(&block, &state, rounds)
50
51 crypto.XOR(dst[n:], src[n:], block[:])
52 }
53 }
54
55 // NewCipher returns a new *chacha.Cipher implementing the ChaCha/X (X = even number of rounds)
56 // stream cipher. The nonce must be unique for one key for all time.
57 func NewCipher(nonce *[12]byte, key *[32]byte, rounds int) *Cipher {
58 if rounds <= 0 || rounds%2 != 0 {
59 panic("chacha20/chacha: rounds must be a multiply of 2")
60 }
61 c := new(Cipher)
62 c.rounds = rounds
63
64 copy(c.state[:], constants[:])
65
66 statePtr := (*[8]uint64)(unsafe.Pointer(&(c.state[0])))
67 keyPtr := (*[4]uint64)(unsafe.Pointer(&key[0]))
68
69 statePtr[2] = keyPtr[0]
70 statePtr[3] = keyPtr[1]
71 statePtr[4] = keyPtr[2]
72 statePtr[5] = keyPtr[3]
73
74 statePtr[6] = (*(*uint64)(unsafe.Pointer(&nonce[0])) << 32)
75
76 statePtr[7] = *(*uint64)(unsafe.Pointer(&nonce[4]))
77
78 return c
79 }
80
81 // XORKeyStream crypts bytes from src to dst. Src and dst may be the same slice
82 // but otherwise should not overlap. If len(dst) < len(src) the function panics.
83 func (c *Cipher) XORKeyStream(dst, src []byte) {
84 length := len(src)
85 if len(dst) < length {
86 panic("chacha20/chacha: dst buffer is to small")
87 }
88
89 if c.off > 0 {
90 n := crypto.XOR(dst, src, c.block[c.off:])
91 if n == length {
92 c.off += n
93 return
94 }
95 src = src[n:]
96 dst = dst[n:]
97 length -= n
98 c.off = 0
99 }
100
101 if length >= 64 {
102 XORBlocks(dst, src, &(c.state), c.rounds)
103 }
104
105 if n := length & (^(64 - 1)); length-n > 0 {
106 Core(&(c.block), &(c.state), c.rounds)
107
108 c.off += crypto.XOR(dst[n:], src[n:], c.block[:])
109 }
110 }
111
112 // XORBlocks crypts full block ( len(src) - (len(src) mod 64) bytes ) from src to
113 // dst using the state. Src and dst may be the same slice but otherwise should not
114 // overlap. This function increments the counter of state.
115 // If len(src) > len(dst), XORBlocks does nothing.
116 func XORBlocks(dst, src []byte, state *[64]byte, rounds int)
117
118 // Core generates 64 byte keystream from the given state performing 'rounds' rounds
119 // and writes them to dst. This function expects valid values. (no nil ptr etc.)
120 // Core increments the counter of state.
121 func Core(dst *[64]byte, state *[64]byte, rounds int)