-
Notifications
You must be signed in to change notification settings - Fork 22
/
cast.go
143 lines (107 loc) · 3.06 KB
/
cast.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
package cast
import (
"strconv"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const BlockSize = 16
type KeySizeError int
func (k KeySizeError) Error() string {
return "cryptobin/cast: invalid key size " + strconv.Itoa(int(k))
}
type castCipher struct {
km []uint32
kr []byte
}
// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
k := len(key)
switch k {
case 16, 20, 24, 28, 32:
break
default:
return nil, KeySizeError(len(key))
}
c := new(castCipher)
c.expandKey(key)
return c, nil
}
func (this *castCipher) BlockSize() int {
return BlockSize
}
func (this *castCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/cast: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/cast: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/cast: invalid buffer overlap")
}
this.encrypt(dst, src)
}
func (this *castCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/cast: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/cast: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/cast: invalid buffer overlap")
}
this.decrypt(dst, src)
}
func (this *castCipher) encrypt(dst, src []byte) {
blk := bytesToUint32s(src)
a, b, c, d := blk[0], blk[1], blk[2], blk[3]
kr := this.kr
km := this.km
var i int
for i = 0; i < 24; i += 4 {
c ^= f1(d, kr[i + 0], km[i + 0])
b ^= f2(c, kr[i + 1], km[i + 1])
a ^= f3(b, kr[i + 2], km[i + 2])
d ^= f1(a, kr[i + 3], km[i + 3])
}
for i = 28; i < 52; i += 4 {
d ^= f1(a, kr[i - 1], km[i - 1])
a ^= f3(b, kr[i - 2], km[i - 2])
b ^= f2(c, kr[i - 3], km[i - 3])
c ^= f1(d, kr[i - 4], km[i - 4])
}
dstBytes := uint32sToBytes([4]uint32{a, b, c, d})
copy(dst, dstBytes[:])
}
func (this *castCipher) decrypt(dst, src []byte) {
blk := bytesToUint32s(src)
a, b, c, d := blk[0], blk[1], blk[2], blk[3]
kr := this.kr
km := this.km
var i int
for i = 44; i > 20; i -= 4 {
c ^= f1(d, kr[i + 0], km[i + 0])
b ^= f2(c, kr[i + 1], km[i + 1])
a ^= f3(b, kr[i + 2], km[i + 2])
d ^= f1(a, kr[i + 3], km[i + 3])
}
for i = 24; i > 0; i -= 4 {
d ^= f1(a, kr[i - 1], km[i - 1])
a ^= f3(b, kr[i - 2], km[i - 2])
b ^= f2(c, kr[i - 3], km[i - 3])
c ^= f1(d, kr[i - 4], km[i - 4])
}
dstBytes := uint32sToBytes([4]uint32{a, b, c, d})
copy(dst, dstBytes[:])
}
func (this *castCipher) expandKey(key []byte) {
var keys [8]uint32
inKey := keyToUint32s(key)
for i := 0; i < len(inKey); i++ {
keys[i] = inKey[i]
}
this.km = make([]uint32, 48)
this.kr = make([]byte, 48)
keyInit(keys, this.km, this.kr)
}