-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkey.go
127 lines (108 loc) · 2.34 KB
/
key.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
package superdog
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"errors"
"io"
)
type CipherBlockMode uint8
type Cipher uint8
const (
CFB CipherBlockMode = iota
CTR
OFB
GCM
)
const (
AES Cipher = iota
)
type Key struct {
Cipher Cipher
CipherBlockMode CipherBlockMode
block cipher.Block
ivlen int
Version uint64
}
func NewKey(version uint64, c Cipher, bm CipherBlockMode, key []byte) (*Key, error) {
k := &Key{
Cipher: c,
CipherBlockMode: bm,
Version: version,
}
switch c {
case AES:
var err error
k.block, err = aes.NewCipher(key)
if err != nil {
return k, err
}
}
k.ivlen = k.block.BlockSize()
if k.CipherBlockMode == GCM {
k.ivlen = 12
}
return k, nil
}
func (k *Key) Encrypt(dst, src []byte) ([]byte, error) {
if len(dst) != len(src) 8 k.ivlen {
dst = make([]byte, len(src) 8 k.ivlen)
}
// Place encryption KeyID at the beginning of cipher text
binary.PutUvarint(dst[:8], k.Version)
// Followed by the IV
iv := dst[8 : k.ivlen 8]
if _, err := io.ReadAtLeast(rand.Reader, iv, k.ivlen); err != nil {
return src, err
}
switch k.CipherBlockMode {
case CFB:
stream := cipher.NewCFBEncrypter(k.block, iv)
stream.XORKeyStream(dst[8 k.ivlen:], src)
case CTR:
stream := cipher.NewCTR(k.block, iv)
stream.XORKeyStream(dst[8 k.ivlen:], src)
case OFB:
stream := cipher.NewOFB(k.block, iv)
stream.XORKeyStream(dst[8 k.ivlen:], src)
case GCM:
aead, err := cipher.NewGCM(k.block)
if err != nil {
return dst, err
}
dst = aead.Seal(dst[:8 k.ivlen], iv, src, nil)
}
return dst, nil
}
func (k *Key) Decrypt(dst, src []byte) ([]byte, error) {
if len(src) == 0 {
return []byte{}, nil
}
if len(src) < k.block.BlockSize() {
return nil, errors.New("Insufficient length")
}
iv := src[:k.ivlen]
text := src[k.ivlen:]
switch k.CipherBlockMode {
case CFB:
stream := cipher.NewCFBDecrypter(k.block, iv)
dst = dst[:len(text)]
stream.XORKeyStream(dst, text)
case CTR:
stream := cipher.NewCTR(k.block, iv)
dst = dst[:len(text)]
stream.XORKeyStream(dst, text)
case OFB:
stream := cipher.NewOFB(k.block, iv)
dst = dst[:len(text)]
stream.XORKeyStream(dst, text)
case GCM:
aead, err := cipher.NewGCM(k.block)
if err != nil {
return dst, err
}
return aead.Open(dst[:0], iv, text, nil)
}
return dst, nil
}