-
Notifications
You must be signed in to change notification settings - Fork 22
/
pcbc.go
156 lines (120 loc) · 3.38 KB
/
pcbc.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cipher
import (
"bytes"
"crypto/cipher"
"crypto/subtle"
"github.com/deatil/go-cryptobin/tool/alias"
)
/**
* 填充密码块链接模式
*
* @create 2023-4-21
* @author deatil
*/
type pcbc struct {
b cipher.Block
blockSize int
iv []byte
}
func newPCBC(b cipher.Block, iv []byte) *pcbc {
return &pcbc{
b: b,
blockSize: b.BlockSize(),
iv: bytes.Clone(iv),
}
}
type pcbcEncrypter pcbc
type pcbcEncAble interface {
NewPCBCEncrypter(iv []byte) cipher.BlockMode
}
func NewPCBCEncrypter(b cipher.Block, iv []byte) cipher.BlockMode {
if len(iv) != b.BlockSize() {
panic("cipher.NewPCBCEncrypter: IV length must equal block size")
}
if pcbc, ok := b.(pcbcEncAble); ok {
return pcbc.NewPCBCEncrypter(iv)
}
return (*pcbcEncrypter)(newPCBC(b, iv))
}
func newPCBCGenericEncrypter(b cipher.Block, iv []byte) cipher.BlockMode {
if len(iv) != b.BlockSize() {
panic("cipher.NewPCBCEncrypter: IV length must equal block size")
}
return (*pcbcEncrypter)(newPCBC(b, iv))
}
func (x *pcbcEncrypter) BlockSize() int {
return x.blockSize
}
func (x *pcbcEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("crypto/cipher: invalid buffer overlap")
}
iv := x.iv
bs := x.blockSize
for i := 0; i < len(src); i += bs {
subtle.XORBytes(dst[i:i+bs], src[i:i+bs], iv)
x.b.Encrypt(dst[i:i+bs], dst[i:i+bs])
subtle.XORBytes(iv, src[i:i+bs], dst[i:i+bs])
}
}
func (x *pcbcEncrypter) SetIV(iv []byte) {
if len(iv) != len(x.iv) {
panic("cipher: incorrect length IV")
}
copy(x.iv, iv)
}
type pcbcDecrypter pcbc
type pcbcDecAble interface {
NewPCBCDecrypter(iv []byte) cipher.BlockMode
}
func NewPCBCDecrypter(b cipher.Block, iv []byte) cipher.BlockMode {
if len(iv) != b.BlockSize() {
panic("cipher.NewPCBCDecrypter: IV length must equal block size")
}
if pcbc, ok := b.(pcbcDecAble); ok {
return pcbc.NewPCBCDecrypter(iv)
}
return (*pcbcDecrypter)(newPCBC(b, iv))
}
func newPCBCGenericDecrypter(b cipher.Block, iv []byte) cipher.BlockMode {
if len(iv) != b.BlockSize() {
panic("cipher.NewPCBCDecrypter: IV length must equal block size")
}
return (*pcbcDecrypter)(newPCBC(b, iv))
}
func (x *pcbcDecrypter) BlockSize() int {
return x.blockSize
}
func (x *pcbcDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("crypto/cipher: invalid buffer overlap")
}
if len(src) == 0 {
return
}
iv := x.iv
bs := x.blockSize
for i := 0; i < len(src); i += bs {
x.b.Decrypt(dst[i:i+bs], src[i:i+bs])
subtle.XORBytes(dst[i:i+bs], dst[i:i+bs], iv)
subtle.XORBytes(iv, dst[i:i+bs], src[i:i+bs])
}
}
func (x *pcbcDecrypter) SetIV(iv []byte) {
if len(iv) != len(x.iv) {
panic("cipher: incorrect length IV")
}
copy(x.iv, iv)
}