-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
446 lines (403 loc) · 10.8 KB
/
common.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package gtp
import (
"crypto/aes"
"errors"
"golang.org/x/crypto/chacha20"
"golang.org/x/crypto/chacha20poly1305"
"strings"
)
// Version 协议版本
type Version uint16
const (
Version_V1_0 Version = 0x0100 // 协议v1.0版本
)
// SecretKeyExchange 秘钥交换函数
type SecretKeyExchange uint8
const (
SecretKeyExchange_None SecretKeyExchange = iota // 未设置
SecretKeyExchange_ECDHE // ECDHE算法
)
// ParseSecretKeyExchange 解析配置字串
func ParseSecretKeyExchange(str string) (SecretKeyExchange, error) {
switch strings.ToLower(str) {
case "none":
return SecretKeyExchange_None, nil
case "ecdhe":
return SecretKeyExchange_ECDHE, nil
default:
return SecretKeyExchange_None, errors.New("invalid SecretKeyExchange")
}
}
// String implements fmt.Stringer
func (ske SecretKeyExchange) String() string {
switch ske {
case SecretKeyExchange_ECDHE:
return "ecdhe"
default:
return "none"
}
}
// AsymmetricEncryption 非对称加密算法
type AsymmetricEncryption uint8
const (
AsymmetricEncryption_None AsymmetricEncryption = iota // 未设置
AsymmetricEncryption_RSA256 // RSA-256算法
AsymmetricEncryption_ECDSA_P256 // ECDSA-NIST-P256算法
)
// ParseAsymmetricEncryption 解析配置字串
func ParseAsymmetricEncryption(str string) (AsymmetricEncryption, error) {
switch strings.ToLower(str) {
case "none":
return AsymmetricEncryption_None, nil
case "rsa256":
return AsymmetricEncryption_RSA256, nil
case "ecdsa_p256":
return AsymmetricEncryption_ECDSA_P256, nil
default:
return AsymmetricEncryption_None, errors.New("invalid AsymmetricEncryption")
}
}
// String implements fmt.Stringer
func (ae AsymmetricEncryption) String() string {
switch ae {
case AsymmetricEncryption_RSA256:
return "rsa256"
case AsymmetricEncryption_ECDSA_P256:
return "ecdsa_p256"
default:
return "none"
}
}
// SymmetricEncryption 对称加密算法
type SymmetricEncryption uint8
const (
SymmetricEncryption_None SymmetricEncryption = iota // 未设置
SymmetricEncryption_AES // AES算法
SymmetricEncryption_ChaCha20 // ChaCha20算法
SymmetricEncryption_XChaCha20 // XChaCha20算法
SymmetricEncryption_ChaCha20_Poly1305 // ChaCha20-Poly1305算法
SymmetricEncryption_XChaCha20_Poly1305 // XChaCha20-Poly1305算法
)
// ParseSymmetricEncryption 解析配置字串
func ParseSymmetricEncryption(str string) (SymmetricEncryption, error) {
switch strings.ToLower(str) {
case "none":
return SymmetricEncryption_None, nil
case "aes":
return SymmetricEncryption_AES, nil
case "chacha20":
return SymmetricEncryption_ChaCha20, nil
case "xchacha20":
return SymmetricEncryption_XChaCha20, nil
case "chacha20_poly1305":
return SymmetricEncryption_ChaCha20_Poly1305, nil
case "xchacha20_poly1305":
return SymmetricEncryption_XChaCha20_Poly1305, nil
default:
return SymmetricEncryption_None, errors.New("invalid SymmetricEncryption")
}
}
// String implements fmt.Stringer
func (se SymmetricEncryption) String() string {
switch se {
case SymmetricEncryption_AES:
return "aes"
case SymmetricEncryption_ChaCha20:
return "chacha20"
case SymmetricEncryption_XChaCha20:
return "xchacha20"
case SymmetricEncryption_ChaCha20_Poly1305:
return "chacha20_poly1305"
case SymmetricEncryption_XChaCha20_Poly1305:
return "xchacha20_poly1305"
default:
return "none"
}
}
// BlockSize 获取block大小
func (se SymmetricEncryption) BlockSize() (int, bool) {
switch se {
case SymmetricEncryption_AES:
return aes.BlockSize, true
default:
return 0, false
}
}
// IV 获取iv大小和是否需要iv,iv大小为0表示与加密算法的blocksize相同
func (se SymmetricEncryption) IV() (int, bool) {
switch se {
case SymmetricEncryption_ChaCha20:
return chacha20.NonceSize, true
case SymmetricEncryption_XChaCha20:
return chacha20.NonceSizeX, true
default:
return 0, false
}
}
// Nonce 是否需要nonce,nonce大小为0表示与加密算法的blocksize相同
func (se SymmetricEncryption) Nonce() (int, bool) {
switch se {
case SymmetricEncryption_ChaCha20_Poly1305:
return chacha20poly1305.NonceSize, true
case SymmetricEncryption_XChaCha20_Poly1305:
return chacha20poly1305.NonceSizeX, true
default:
return 0, false
}
}
// BlockCipherMode 是否需要分组密码工作模式配合
func (se SymmetricEncryption) BlockCipherMode() bool {
switch se {
case SymmetricEncryption_AES:
return true
default:
return false
}
}
// PaddingMode 数据填充方案
type PaddingMode uint8
const (
PaddingMode_None PaddingMode = iota // 未设置
PaddingMode_Pkcs7 // pkcs7方案(用于对称加密)
PaddingMode_X923 // x927方案(用于对称加密)
PaddingMode_Pkcs1v15 // Pkcs1v15方案(用于非对称加密RSA算法)
PaddingMode_PSS // PSS方案(用于非对称加密RSA算法)
)
// ParsePaddingMode 解析配置字串
func ParsePaddingMode(str string) (PaddingMode, error) {
switch strings.ToLower(str) {
case "none":
return PaddingMode_None, nil
case "pkcs7":
return PaddingMode_Pkcs7, nil
case "x923":
return PaddingMode_X923, nil
case "pkcs1v15":
return PaddingMode_Pkcs1v15, nil
case "pss":
return PaddingMode_PSS, nil
default:
return PaddingMode_None, errors.New("invalid PaddingMode")
}
}
// String implements fmt.Stringer
func (pm PaddingMode) String() string {
switch pm {
case PaddingMode_Pkcs7:
return "pkcs7"
case PaddingMode_X923:
return "x923"
case PaddingMode_Pkcs1v15:
return "pkcs1v15"
case PaddingMode_PSS:
return "pss"
default:
return "none"
}
}
// BlockCipherMode 分组密码工作模式
type BlockCipherMode uint8
const (
BlockCipherMode_None BlockCipherMode = iota // 未设置
BlockCipherMode_CTR // CTR模式
BlockCipherMode_CBC // CBC模式
BlockCipherMode_CFB // CFB模式
BlockCipherMode_OFB // OFB模式
BlockCipherMode_GCM // GCM模式
)
// ParseBlockCipherMode 解析配置字串
func ParseBlockCipherMode(str string) (BlockCipherMode, error) {
switch strings.ToLower(str) {
case "none":
return BlockCipherMode_None, nil
case "ctr":
return BlockCipherMode_CTR, nil
case "cbc":
return BlockCipherMode_CBC, nil
case "cfb":
return BlockCipherMode_CFB, nil
case "ofb":
return BlockCipherMode_OFB, nil
case "gcm":
return BlockCipherMode_GCM, nil
default:
return BlockCipherMode_None, errors.New("invalid BlockCipherMode")
}
}
// String implements fmt.Stringer
func (bcm BlockCipherMode) String() string {
switch bcm {
case BlockCipherMode_CTR:
return "ctr"
case BlockCipherMode_CBC:
return "cbc"
case BlockCipherMode_CFB:
return "cfb"
case BlockCipherMode_OFB:
return "ofb"
case BlockCipherMode_GCM:
return "gcm"
default:
return "none"
}
}
// IV 是否需要iv,iv大小与加密算法的blocksize相同
func (bcm BlockCipherMode) IV() bool {
switch bcm {
case BlockCipherMode_CTR, BlockCipherMode_CBC, BlockCipherMode_CFB, BlockCipherMode_OFB:
return true
default:
return false
}
}
// Nonce 是否需要nonce,nonce大小与加密算法的blocksize相同
func (bcm BlockCipherMode) Nonce() bool {
switch bcm {
case BlockCipherMode_GCM:
return true
default:
return false
}
}
// Padding 是否需要分组对齐填充数据
func (bcm BlockCipherMode) Padding() bool {
switch bcm {
case BlockCipherMode_CBC:
return true
default:
return false
}
}
// Hash 摘要函数
type Hash uint8
const (
Hash_None Hash = iota // 未设置
Hash_Fnv1a32 // Fnv-1a 32bit算法(用于MAC)
Hash_Fnv1a64 // Fnv-1a 64bit算法(用于MAC)
Hash_Fnv1a128 // Fnv-1a 128bit算法(用于MAC)
Hash_SHA256 // SHA256算法(用于非对称加密或MAC)
)
// ParseHash 解析配置字串
func ParseHash(str string) (Hash, error) {
switch strings.ToLower(str) {
case "none":
return Hash_None, nil
case "fnv1a32":
return Hash_Fnv1a32, nil
case "fnv1a64":
return Hash_Fnv1a64, nil
case "fnv1a128":
return Hash_Fnv1a128, nil
case "sha256":
return Hash_SHA256, nil
default:
return Hash_None, errors.New("invalid Hash")
}
}
// String implements fmt.Stringer
func (h Hash) String() string {
switch h {
case Hash_Fnv1a32:
return "fnv1a32"
case Hash_Fnv1a64:
return "fnv1a64"
case Hash_Fnv1a128:
return "fnv1a128"
case Hash_SHA256:
return "sha256"
default:
return "none"
}
}
// Bits 位数
func (h Hash) Bits() int {
switch h {
case Hash_Fnv1a32:
return 32
case Hash_Fnv1a64:
return 64
case Hash_Fnv1a128:
return 128
case Hash_SHA256:
return 256
default:
return 0
}
}
// NamedCurve 曲线类型
type NamedCurve uint8
const (
NamedCurve_None NamedCurve = iota // 未设置
NamedCurve_X25519 // 曲线x25519
NamedCurve_P256 // 曲线NIST-P256
)
// ParseNamedCurve 解析配置字串
func ParseNamedCurve(str string) (NamedCurve, error) {
switch strings.ToLower(str) {
case "none":
return NamedCurve_None, nil
case "x25519":
return NamedCurve_X25519, nil
case "p256":
return NamedCurve_P256, nil
default:
return NamedCurve_None, errors.New("invalid NamedCurve")
}
}
// String implements fmt.Stringer
func (nc NamedCurve) String() string {
switch nc {
case NamedCurve_X25519:
return "x25519"
case NamedCurve_P256:
return "p256"
default:
return "none"
}
}
// Compression 压缩函数
type Compression uint8
const (
Compression_None Compression = iota // 未设置
Compression_Gzip // Gzip压缩算法
Compression_Deflate // Deflate压缩算法
Compression_Brotli // Brotli压缩算法
Compression_LZ4 // LZ4压缩算法
Compression_Snappy // Snappy压缩算法
)
// ParseCompression 解析配置字串
func ParseCompression(str string) (Compression, error) {
switch strings.ToLower(str) {
case "none":
return Compression_None, nil
case "gzip":
return Compression_Gzip, nil
case "deflate":
return Compression_Deflate, nil
case "brotli":
return Compression_Brotli, nil
case "lz4":
return Compression_LZ4, nil
case "snappy":
return Compression_Snappy, nil
default:
return Compression_None, errors.New("invalid Compression")
}
}
// String implements fmt.Stringer
func (c Compression) String() string {
switch c {
case Compression_Gzip:
return "gzip"
case Compression_Deflate:
return "deflate"
case Compression_Brotli:
return "brotli"
case Compression_LZ4:
return "lz4"
case Compression_Snappy:
return "snappy"
default:
return "none"
}
}