-
Notifications
You must be signed in to change notification settings - Fork 0
/
cts.go
66 lines (58 loc) · 2.05 KB
/
cts.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
package blowfishj
import "github.com/c0mm4nd/go-ripemd"
type blowfishCTS struct {
ecb blowfishECB
feedback []byte
}
func (cts *blowfishCTS) initialize(key []byte) error {
cts.feedback = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
hash := ripemd.New256()
_, err := hash.Write(key)
if err != nil {
return err
}
hashedKey := hash.Sum(nil)
cts.ecb.initialize(hashedKey, 0, len(hashedKey))
cts.ecb.encrypt(cts.feedback, 0, cts.feedback, 0, len(cts.feedback))
backupFeedback := make([]byte, len(cts.feedback))
copy(backupFeedback, cts.feedback)
cts.encrypt(hashedKey, 0, hashedKey, 0, len(hashedKey))
copy(cts.feedback, backupFeedback)
return nil
}
func (cts *blowfishCTS) encrypt(inbuf []byte, inpos int, outbuf []byte, outpos int, len int) int {
buf := make([]byte, blockSize)
for offset := 0; len-offset >= 8; offset += blockSize {
xorBuffers(cts.feedback, blockSize, inbuf, inpos+offset, buf, 0)
cts.ecb.encrypt(buf, 0, outbuf, outpos+offset, blockSize)
xorBuffers(cts.feedback, blockSize, outbuf, outpos+offset, cts.feedback, 0)
}
if len%blockSize > 0 {
nleft := len % blockSize
offset := len - nleft
copy(buf, cts.feedback)
cts.ecb.encrypt(buf, 0, buf, 0, blockSize)
xorBuffers(buf, nleft, inbuf, offset, outbuf, offset)
xorBuffers(cts.feedback, blockSize, buf, 0, cts.feedback, 0)
}
return len
}
func (cts *blowfishCTS) decrypt(inbuf []byte, inpos int, outbuf []byte, outpos int, len int) int {
buf := make([]byte, blockSize)
copy(outbuf, inbuf)
for offset := 0; len-offset >= 8; offset += blockSize {
xorBuffers(cts.feedback, blockSize, outbuf, outpos+offset, buf, 0)
cts.ecb.decrypt(inbuf, inpos+offset, outbuf, outpos+offset, blockSize)
xorBuffers(cts.feedback, blockSize, outbuf, outpos+offset, outbuf, outpos+offset)
copy(cts.feedback, buf)
}
if len%blockSize > 0 {
nleft := len % blockSize
offset := len - nleft
copy(buf, cts.feedback)
cts.ecb.encrypt(buf, 0, buf, 0, blockSize)
xorBuffers(buf, nleft, inbuf, offset, outbuf, offset)
xorBuffers(cts.feedback, blockSize, buf, 0, cts.feedback, 0)
}
return len
}