/
keys.go
192 lines (164 loc) · 3.65 KB
/
keys.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
package security
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"errors"
"fmt"
"strconv"
"golang.org/x/crypto/pbkdf2"
)
const keyDerivationIterations = 4096
var (
ErrInvalidKey = errors.New("invalid key")
ErrKeyMustBeDecrypted = errors.New("key must be decrypted")
ErrKeyMustBeEncrypted = errors.New("key must be encrypted")
ErrIVRequired = errors.New("IV is required")
TestMode bool
)
type KeyType byte
const (
AES128 KeyType = iota
AES256
)
func (t KeyType) KeySize() int {
switch t {
case AES128:
return 16
case AES256:
return 32
default:
panic(fmt.Sprintf("no key size defined for key type %s", t))
}
}
func (t KeyType) BlockSize() int {
switch t {
case AES128, AES256:
return 16
default:
panic(fmt.Sprintf("no block size defined for key type %s", t))
}
}
func (t KeyType) String() string {
switch t {
case AES128:
return "aes-128"
case AES256:
return "aes-256"
default:
return strconv.Itoa(int(t))
}
}
func (t KeyType) BlockCrypt(iv, key, data []byte, encrypt bool) error {
if len(data)%t.BlockSize() != 0 {
return ErrInvalidKey
}
blockMode, err := t.BlockMode(iv, key, encrypt)
if err != nil {
return err
}
blockMode.CryptBlocks(data, data)
return nil
}
func (t KeyType) BlockMode(iv, key []byte, encrypt bool) (cipher.BlockMode, error) {
if len(iv) != t.BlockSize() {
return nil, ErrInvalidKey
}
blockCipher, err := t.BlockCipher(key)
if err != nil {
return nil, err
}
if encrypt {
return cipher.NewCBCEncrypter(blockCipher, iv), nil
} else {
return cipher.NewCBCDecrypter(blockCipher, iv), nil
}
}
func (t KeyType) BlockCipher(key []byte) (cipher.Block, error) {
if len(key) != t.KeySize() {
return nil, ErrInvalidKey
}
switch t {
case AES128, AES256:
return aes.NewCipher(key)
default:
return nil, ErrInvalidKey
}
}
func (t KeyType) Pad(data []byte) []byte {
padding := t.BlockSize() - len(data)%t.BlockSize()
return append(data, bytes.Repeat([]byte{byte(padding)}, padding)...)
}
func (t KeyType) Unpad(data []byte) []byte {
unpadding := int(data[len(data)-1])
return data[:len(data)-unpadding]
}
type ManagedKey struct {
KeyType
IV []byte
Plaintext []byte
Ciphertext []byte
ContextKey string
ContextValue string
}
func (k *ManagedKey) Clone() ManagedKey {
dup := func(v []byte) []byte {
if v == nil {
return nil
}
w := make([]byte, len(v))
copy(w, v)
return w
}
return ManagedKey{
KeyType: k.KeyType,
IV: dup(k.IV),
Plaintext: dup(k.Plaintext),
Ciphertext: dup(k.Ciphertext),
ContextKey: k.ContextKey,
ContextValue: k.ContextValue,
}
}
func (k *ManagedKey) Encrypted() bool { return k.Ciphertext != nil }
func (k *ManagedKey) Encrypt(keyKey *ManagedKey) error {
if keyKey.Encrypted() || k.Encrypted() {
return ErrKeyMustBeDecrypted
}
if k.IV == nil {
return ErrIVRequired
}
if err := keyKey.BlockCrypt(k.IV, keyKey.Plaintext, k.Plaintext, true); err != nil {
return err
}
k.Ciphertext = k.Plaintext
k.Plaintext = nil
return nil
}
func (k *ManagedKey) Decrypt(keyKey *ManagedKey) error {
if keyKey.Encrypted() {
return ErrKeyMustBeDecrypted
}
if !k.Encrypted() {
return ErrKeyMustBeEncrypted
}
if k.IV == nil {
return ErrIVRequired
}
if err := keyKey.BlockCrypt(k.IV, keyKey.Plaintext, k.Ciphertext, false); err != nil {
return err
}
k.Plaintext = k.Ciphertext
k.Ciphertext = nil
return nil
}
func KeyFromPasscode(passcode, salt []byte, keyType KeyType) *ManagedKey {
iterations := keyDerivationIterations
if TestMode {
iterations = 1
}
return &ManagedKey{
KeyType: keyType,
Plaintext: pbkdf2.Key(passcode, salt, iterations, keyType.KeySize(), sha256.New),
}
}