-
Notifications
You must be signed in to change notification settings - Fork 21
/
ofbnlf.go
161 lines (117 loc) · 3.24 KB
/
ofbnlf.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
157
158
159
160
161
package cipher
import (
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
type KeyCreator = func([]byte) (cipher.Block, error)
type ofbnlf struct {
newKey KeyCreator
b cipher.Block
blockSize int
iv []byte
}
func newOFBNLF(b cipher.Block, newKey KeyCreator, iv []byte) *ofbnlf {
c := &ofbnlf{
b: b,
newKey: newKey,
blockSize: b.BlockSize(),
iv: make([]byte, b.BlockSize()),
}
copy(c.iv, iv)
return c
}
type ofbnlfEncrypter ofbnlf
func NewOFBNLFEncrypter(newKey KeyCreator, key, iv []byte) cipher.BlockMode {
b, err := newKey(key)
if err != nil {
panic("cryptobin/ofbnlf.NewOFBNLFEncrypter: " + err.Error())
}
if len(iv) != b.BlockSize() {
panic("cryptobin/ofbnlf: IV length must equal block size")
}
c := newOFBNLF(b, newKey, iv)
return (*ofbnlfEncrypter)(c)
}
func (x *ofbnlfEncrypter) BlockSize() int {
return x.blockSize
}
func (x *ofbnlfEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("cryptobin/ofbnlf: input not full blocks")
}
if len(dst) < len(src) {
panic("cryptobin/ofbnlf: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("cryptobin/ofbnlf: invalid buffer overlap")
}
iv := x.iv
k := make([]byte, x.blockSize)
for len(src) > 0 {
x.b.Encrypt(k, iv)
c, err := x.newKey(k)
if err != nil {
panic("cryptobin/ofbnlf: " + err.Error())
}
c.Encrypt(dst, src)
src = src[x.blockSize:]
dst = dst[x.blockSize:]
copy(iv, k)
}
copy(x.iv, iv)
}
func (x *ofbnlfEncrypter) SetIV(iv []byte) {
if len(iv) != len(x.iv) {
panic("cryptobin/ofbnlf: incorrect length IV")
}
copy(x.iv, iv)
}
type ofbnlfDecrypter ofbnlf
func NewOFBNLFDecrypter(newKey KeyCreator, key, iv []byte) cipher.BlockMode {
b, err := newKey(key)
if err != nil {
panic("cryptobin/ofbnlf.NewOFBNLFDecrypter: " + err.Error())
}
if len(iv) != b.BlockSize() {
panic("cryptobin/ofbnlf: IV length must equal block size")
}
c := newOFBNLF(b, newKey, iv)
return (*ofbnlfDecrypter)(c)
}
func (x *ofbnlfDecrypter) BlockSize() int {
return x.blockSize
}
func (x *ofbnlfDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("cryptobin/ofbnlf: input not full blocks")
}
if len(dst) < len(src) {
panic("cryptobin/ofbnlf: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("cryptobin/ofbnlf: invalid buffer overlap")
}
if len(src) == 0 {
return
}
iv := x.iv
k := make([]byte, x.blockSize)
for len(src) > 0 {
x.b.Encrypt(k, iv)
c, err := x.newKey(k)
if err != nil {
panic("cryptobin/ofbnlf: " + err.Error())
}
c.Decrypt(dst, src)
src = src[x.blockSize:]
dst = dst[x.blockSize:]
copy(iv, k)
}
copy(x.iv, iv)
}
func (x *ofbnlfDecrypter) SetIV(iv []byte) {
if len(iv) != len(x.iv) {
panic("cryptobin/ofbnlf: incorrect length IV")
}
copy(x.iv, iv)
}