-
Notifications
You must be signed in to change notification settings - Fork 61
/
enc_mode.go
279 lines (245 loc) · 8.32 KB
/
enc_mode.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package sm9
import (
"crypto/cipher"
"io"
_cipher "github.com/emmansun/gmsm/cipher"
"github.com/emmansun/gmsm/internal/subtle"
"github.com/emmansun/gmsm/padding"
"github.com/emmansun/gmsm/sm4"
)
// EncrypterOpts is an interface implemented by detail encrypt/decrypt mode.
type EncrypterOpts interface {
// GetEncryptType returns the encrypt type/mode.
GetEncryptType() encryptType
// GetKeySize returns key size used by this encrypt mode.
GetKeySize(plaintext []byte) int
// Encrypt encrypts the plaintext with the key, returns ciphertext.
Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error)
// Decrypt decrypts the ciphertext with the key, returns plaintext.
Decrypt(key, ciphertext []byte) ([]byte, error)
}
// XOREncrypterOpts represents XOR mode.
type XOREncrypterOpts struct{}
func (opts *XOREncrypterOpts) GetEncryptType() encryptType {
return ENC_TYPE_XOR
}
func (opts *XOREncrypterOpts) GetKeySize(plaintext []byte) int {
return len(plaintext)
}
func (opts *XOREncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
subtle.XORBytes(key, key, plaintext)
return key, nil
}
func (opts *XOREncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
if len(ciphertext) == 0 {
return nil, ErrDecryption
}
subtle.XORBytes(key, ciphertext, key)
return key, nil
}
type newCipher func(key []byte) (cipher.Block, error)
type baseBlockEncrypterOpts struct {
encryptType encryptType
newCipher newCipher
cipherKeySize int
}
func (opts *baseBlockEncrypterOpts) GetEncryptType() encryptType {
return opts.encryptType
}
func (opts *baseBlockEncrypterOpts) GetKeySize(plaintext []byte) int {
return opts.cipherKeySize
}
// CBCEncrypterOpts represents CBC (Cipher block chaining) mode.
type CBCEncrypterOpts struct {
baseBlockEncrypterOpts
padding padding.Padding
}
func NewCBCEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts {
opts := new(CBCEncrypterOpts)
opts.encryptType = ENC_TYPE_CBC
opts.padding = padding
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *CBCEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
paddedPlainText := opts.padding.Pad(plaintext)
blockSize := block.BlockSize()
ciphertext := make([]byte, blockSize+len(paddedPlainText))
iv := ciphertext[:blockSize]
if _, err := io.ReadFull(rand, iv); err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext[blockSize:], paddedPlainText)
return ciphertext, nil
}
func (opts *CBCEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(ciphertext) <= blockSize {
return nil, ErrDecryption
}
iv := ciphertext[:blockSize]
ciphertext = ciphertext[blockSize:]
plaintext := make([]byte, len(ciphertext))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, ciphertext)
return opts.padding.Unpad(plaintext)
}
// ECBEncrypterOpts represents ECB (Electronic Code Book) mode.
type ECBEncrypterOpts struct {
baseBlockEncrypterOpts
padding padding.Padding
}
func NewECBEncrypterOpts(padding padding.Padding, newCipher newCipher, keySize int) EncrypterOpts {
opts := new(ECBEncrypterOpts)
opts.encryptType = ENC_TYPE_ECB
opts.padding = padding
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
func (opts *ECBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
paddedPlainText := opts.padding.Pad(plaintext)
ciphertext := make([]byte, len(paddedPlainText))
mode := _cipher.NewECBEncrypter(block)
mode.CryptBlocks(ciphertext, paddedPlainText)
return ciphertext, nil
}
func (opts *ECBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
if len(ciphertext) == 0 {
return nil, ErrDecryption
}
plaintext := make([]byte, len(ciphertext))
mode := _cipher.NewECBDecrypter(block)
mode.CryptBlocks(plaintext, ciphertext)
return opts.padding.Unpad(plaintext)
}
// CFBEncrypterOpts represents CFB (Cipher Feedback) mode.
type CFBEncrypterOpts struct {
baseBlockEncrypterOpts
}
func NewCFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
opts := new(CFBEncrypterOpts)
opts.encryptType = ENC_TYPE_CFB
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *CFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
ciphertext := make([]byte, blockSize+len(plaintext))
iv := ciphertext[:blockSize]
if _, err := io.ReadFull(rand, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[blockSize:], plaintext)
return ciphertext, nil
}
func (opts *CFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(ciphertext) <= blockSize {
return nil, ErrDecryption
}
iv := ciphertext[:blockSize]
ciphertext = ciphertext[blockSize:]
plaintext := make([]byte, len(ciphertext))
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}
// OFBEncrypterOpts represents OFB (Output Feedback) mode.
type OFBEncrypterOpts struct {
baseBlockEncrypterOpts
}
func NewOFBEncrypterOpts(newCipher newCipher, keySize int) EncrypterOpts {
opts := new(OFBEncrypterOpts)
opts.encryptType = ENC_TYPE_OFB
opts.newCipher = newCipher
opts.cipherKeySize = keySize
return opts
}
// Encrypt encrypts the plaintext with the key, includes generated IV at the beginning of the ciphertext.
func (opts *OFBEncrypterOpts) Encrypt(rand io.Reader, key, plaintext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
ciphertext := make([]byte, blockSize+len(plaintext))
iv := ciphertext[:blockSize]
if _, err := io.ReadFull(rand, iv); err != nil {
return nil, err
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(ciphertext[blockSize:], plaintext)
return ciphertext, nil
}
func (opts *OFBEncrypterOpts) Decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := opts.newCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(ciphertext) <= blockSize {
return nil, ErrDecryption
}
iv := ciphertext[:blockSize]
ciphertext = ciphertext[blockSize:]
plaintext := make([]byte, len(ciphertext))
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}
// DefaultEncrypterOpts default option represents XOR mode
var DefaultEncrypterOpts = new(XOREncrypterOpts)
// SM4ECBEncrypterOpts option represents SM4 ECB mode
var SM4ECBEncrypterOpts = NewECBEncrypterOpts(padding.NewPKCS7Padding(sm4.BlockSize), sm4.NewCipher, sm4.BlockSize)
// SM4CBCEncrypterOpts option represents SM4 CBC mode
var SM4CBCEncrypterOpts = NewCBCEncrypterOpts(padding.NewPKCS7Padding(sm4.BlockSize), sm4.NewCipher, sm4.BlockSize)
// SM4CFBEncrypterOpts option represents SM4 CFB mode
var SM4CFBEncrypterOpts = NewCFBEncrypterOpts(sm4.NewCipher, sm4.BlockSize)
// SM4OFBEncrypterOpts option represents SM4 OFB mode
var SM4OFBEncrypterOpts = NewOFBEncrypterOpts(sm4.NewCipher, sm4.BlockSize)
func shangMiEncrypterOpts(encType encryptType) EncrypterOpts {
switch encType {
case ENC_TYPE_XOR:
return DefaultEncrypterOpts
case ENC_TYPE_CBC:
return SM4CBCEncrypterOpts
case ENC_TYPE_ECB:
return SM4ECBEncrypterOpts
case ENC_TYPE_CFB:
return SM4CFBEncrypterOpts
case ENC_TYPE_OFB:
return SM4OFBEncrypterOpts
}
return nil
}