/
ctr_amd64.go
134 lines (118 loc) · 2.85 KB
/
ctr_amd64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package AesCtr
import (
"crypto/cipher"
"sync"
"unsafe"
)
func xorBytes(dst, a, b []byte) int
func fillEightBlocks(nr int, xk uintptr, dst uintptr, counter uintptr)
const streamBufferSize = 32 * BlockSize
type aesctr struct {
block *aesCipherAsm
nr int
ctr [BlockSize]byte
buffer []byte
storage [streamBufferSize]byte
}
func (c *aesCipherAsm) NewCTR(iv []byte) cipher.Stream {
if len(iv) != BlockSize {
panic("cipher.NewCTR: IV length must equal block size")
}
var ac aesctr
ac.block = c
ac.nr = len(c.enc)/4 - 1
copy(ac.ctr[:], iv)
ac.buffer = ac.storage[:0]
return &ac
}
func aesCtrRefill(c *aesctr) {
c.buffer = c.storage[:streamBufferSize]
encPtr := uintptr(unsafe.Pointer(&c.block.enc[0]))
bufPtr := uintptr(unsafe.Pointer(&c.buffer[0]))
ctrPtr := uintptr(unsafe.Pointer(&c.ctr[0]))
fillEightBlocks(c.nr, encPtr, bufPtr, ctrPtr)
bufPtr += 128
fillEightBlocks(c.nr, encPtr, bufPtr, ctrPtr)
bufPtr += 128
fillEightBlocks(c.nr, encPtr, bufPtr, ctrPtr)
bufPtr += 128
fillEightBlocks(c.nr, encPtr, bufPtr, ctrPtr)
}
type ctrResetAble interface {
ctrReset(block cipher.Block, iv []byte) cipher.Stream
}
func (ac *aesctr) ctrReset(block cipher.Block, iv []byte) cipher.Stream {
if len(iv) != BlockSize {
return cipher.NewCTR(block, iv)
}
switch obj1 := block.(type) {
case *aesCipherAsm:
ac.block = obj1
case *aesCipherGCM:
ac.block = &obj1.aesCipherAsm
default:
return cipher.NewCTR(block, iv)
}
ac.nr = len(ac.block.enc)/4 - 1
copy(ac.ctr[:], iv)
ac.buffer = ac.storage[:0]
return ac
}
func (c *aesctr) XORKeyStream(dst, src []byte) {
if len(src) > 0 {
_ = dst[len(src)-1]
}
for len(src) > 0 {
if len(c.buffer) == 0 {
aesCtrRefill(c)
}
n := xorBytes(dst, src, c.buffer)
c.buffer = c.buffer[n:]
src = src[n:]
dst = dst[n:]
}
}
func (c *aesCipherAsm) ctrOnce(req CtrForAesOnceXORKeyStreamRequest) {
if len(req.TmpBuf) < 144 {
req.TmpBuf = make([]byte, 144)
}
ctr := req.TmpBuf[:16]
blockOutBuf := req.TmpBuf[16 : 16+128]
copy(ctr[:], req.Iv)
pos := 0
nr := len(c.enc)/4 - 1
for {
if pos >= len(req.Src) {
return
}
fillEightBlocks(nr, uintptr(unsafe.Pointer(&c.enc[0])), uintptr(unsafe.Pointer(&blockOutBuf[0])), uintptr(unsafe.Pointer(&ctr[0])))
thisXorSize := len(req.Src) - pos
if thisXorSize > 128 {
thisXorSize = 128
}
xorBytes(req.Dst[pos:pos+thisXorSize], req.Src[pos:pos+thisXorSize], blockOutBuf[:thisXorSize])
pos += thisXorSize
}
}
var gAesCtrPool = sync.Pool{}
func PoolGetAesCtr(block cipher.Block, iv []byte) cipher.Stream {
stream := gAesCtrPool.Get()
if stream == nil {
return cipher.NewCTR(block, iv)
}
obj1, ok := stream.(ctrResetAble)
if ok {
return obj1.ctrReset(block, iv)
}
return cipher.NewCTR(block, iv)
}
func PoolPutAesCtr(ctrObj cipher.Stream) {
if ctrObj == nil {
return
}
_, ok := ctrObj.(*aesctr)
if !ok {
return
}
gAesCtrPool.Put(ctrObj)
}