-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.go
182 lines (142 loc) · 4.2 KB
/
cipher.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package golem
import (
"bytes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"errors"
)
// Cipher Modes
const (
ModeCBC cipherMode = 1 << iota
ModeCFB
ModeCTR
ModeOFB
)
var (
blockModeFuncMap = map[cipherMode]map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
ModeCBC: map[string]func(b cipher.Block, iv []byte) cipher.BlockMode{
"encrypt": cipher.NewCBCEncrypter,
"decrypt": cipher.NewCBCDecrypter,
},
}
streamFuncMap = map[cipherMode]map[string]func(b cipher.Block, iv []byte) cipher.Stream{
ModeCFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
"encrypt": cipher.NewCFBEncrypter,
"decrypt": cipher.NewCFBDecrypter,
},
ModeCTR: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
"encrypt": cipher.NewCTR,
"decrypt": cipher.NewCTR,
},
ModeOFB: map[string]func(b cipher.Block, iv []byte) cipher.Stream{
"encrypt": cipher.NewOFB,
"decrypt": cipher.NewOFB,
},
}
)
// Cipher is an interface for all encryption ciphers
type Cipher interface {
SetKey(string) error
Encrypt([]byte) []byte
Decrypt([]byte) ([]byte, error)
}
// cipherMode wraps int type to enforce AES mode
type cipherMode int
type encryptor struct {
cipherFunc func([]byte) (cipher.Block, error)
blockSize int
block cipher.Block
keylen int
}
type blockModeEncryption struct {
*encryptor
encrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
decrypterFunc func(b cipher.Block, iv []byte) cipher.BlockMode
}
type streamEncryption struct {
*encryptor
encrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
decrypterFunc func(b cipher.Block, iv []byte) cipher.Stream
}
func newBlockModeEncryption(mode cipherMode) blockModeEncryption {
return blockModeEncryption{
encryptor: &encryptor{},
encrypterFunc: blockModeFuncMap[mode]["encrypt"],
decrypterFunc: blockModeFuncMap[mode]["decrypt"],
}
}
func newSteamEncryption(mode cipherMode) streamEncryption {
return streamEncryption{
encryptor: &encryptor{},
encrypterFunc: streamFuncMap[mode]["encrypt"],
decrypterFunc: streamFuncMap[mode]["decrypt"],
}
}
func getCipher(mode cipherMode, encryptor *encryptor) Cipher {
switch mode {
case ModeCBC:
be := newBlockModeEncryption(mode)
be.encryptor = encryptor
return be
case ModeCFB, ModeCTR, ModeOFB:
se := newSteamEncryption(mode)
se.encryptor = encryptor
return se
default:
panic("unsupported cipher mode")
}
}
func (e *encryptor) SetKey(key string) error {
keyHash := sha256.Sum256([]byte(key))
block, err := e.cipherFunc(keyHash[:e.keylen])
e.block = block
return err
}
func (a blockModeEncryption) Encrypt(data []byte) []byte {
iv := make([]byte, a.blockSize)
rand.Read(iv)
mode := a.encrypterFunc(a.block, iv)
plaintextBlockRem := len(data) % a.blockSize
if plaintextBlockRem != 0 {
data = append(data, make([]byte, int(a.blockSize-plaintextBlockRem))...)
}
ciphertext := make([]byte, a.blockSize+len(data))
ciphertext = append(iv, ciphertext[a.blockSize:]...)
mode.CryptBlocks(ciphertext[a.blockSize:], data)
return ciphertext
}
func (a blockModeEncryption) Decrypt(data []byte) ([]byte, error) {
if len(data) < a.blockSize {
return nil, errors.New("ciphertext is too short")
}
iv := data[:a.blockSize]
ciphertext := data[a.blockSize:]
if len(ciphertext)%a.blockSize != 0 {
return nil, errors.New("ciphertext is not a multiple of the block size")
}
mode := a.decrypterFunc(a.block, iv)
plaintext := make([]byte, len(data)-a.blockSize)
mode.CryptBlocks(plaintext, data[a.blockSize:])
return bytes.Trim(plaintext, "\x00"), nil
}
func (a streamEncryption) Encrypt(data []byte) []byte {
iv := make([]byte, a.blockSize)
rand.Read(iv)
stream := a.encrypterFunc(a.block, iv)
ciphertext := make([]byte, a.blockSize+len(data))
ciphertext = append(iv, ciphertext[a.blockSize:]...)
stream.XORKeyStream(ciphertext[a.blockSize:], data)
return ciphertext
}
func (a streamEncryption) Decrypt(data []byte) ([]byte, error) {
if len(data) < a.blockSize {
return nil, errors.New("ciphertext is too short")
}
iv := data[:a.blockSize]
ciphertext := data[a.blockSize:]
stream := a.decrypterFunc(a.block, iv)
plaintext := make([]byte, len(ciphertext))
stream.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}