-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecb.go
135 lines (112 loc) · 2.96 KB
/
ecb.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
package main
import (
"crypto/cipher"
)
type cbc struct {
b cipher.Block
blockSize int
IV []byte
}
func newCBC(b cipher.Block, IV []byte) *cbc {
return &cbc{
b: b,
blockSize: b.BlockSize(),
IV: IV,
}
}
type cbcEncrypter cbc
func NewCBCEncrypter(b cipher.Block, IV []byte) cipher.BlockMode {
return (*cbcEncrypter)(newCBC(b, IV))
}
func (x *cbcEncrypter) BlockSize() int { return x.blockSize }
func (x *cbcEncrypter) 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")
}
blockToEncrypt := xorEncryptBytes(src[:x.blockSize], x.IV)
for len(src) > 0 {
x.b.Encrypt(dst, blockToEncrypt)
src = src[x.blockSize:]
blockToEncrypt = xorEncryptBytes(dst[:x.blockSize], src[:x.blockSize])
dst = dst[x.blockSize:]
}
}
type cbcDecrypter cbc
func NewCBCDecrypter(b cipher.Block, IV []byte) cipher.BlockMode {
return (*cbcDecrypter)(newCBC(b, IV))
}
func (x *cbcDecrypter) BlockSize() int { return x.blockSize }
func (x *cbcDecrypter) 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")
}
pos := len(src) - x.blockSize
for pos >= 0 {
var block []byte
x.b.Decrypt(dst[pos:], src[pos:pos+x.blockSize])
if pos == 0 {
block = x.IV
} else {
block = src[pos-x.blockSize:pos]
}
xored := xorEncryptBytes(dst[pos:pos+x.blockSize], block)
copy(dst[pos:pos+x.blockSize], xored)
pos -= x.blockSize
}
}
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
// mode, using the given Block.
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) 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")
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecrypter ecb
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
// mode, using the given Block.
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
func (x *ecbDecrypter) 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")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}