/
derive.go
159 lines (139 loc) · 4.13 KB
/
derive.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
package vault
import (
"crypto/aes"
"crypto/rand"
"crypto/sha256"
"fmt"
"golang.org/x/crypto/pbkdf2"
)
func (fks FixedKeySource) Derive(alg string, resolve func(string) ([]byte, error)) (Cipher, error) {
if resolve == nil {
resolve = func(in string) ([]byte, error) {
return []byte(in), nil
}
}
c := Cipher{Algorithm: alg}
var key, salt []byte
if fks.PBKDF2 != "" {
k, err := resolve(fks.PBKDF2)
if err != nil {
return Cipher{}, err
}
if len(k) < 24 {
return Cipher{}, fmt.Errorf("insufficient keying material provided for pbkdf2: only %d bytes found (need at least 24 bytes)", len(k))
}
key = k[len(k)/3:]
salt = k[:len(k)/2]
}
algorithm, _ := parse(alg)
switch algorithm {
case "aes128":
if key != nil && salt != nil {
c.Key = pbkdf2.Key(key, salt, 4096, 16, sha256.New)
c.IV = pbkdf2.Key(key, salt, 4096, aes.BlockSize, sha256.New)
return c, nil
}
if fks.Literal.AES128.Key != "" && fks.Literal.AES128.IV != "" {
keysz := 16
key, err := resolve(fks.Literal.AES128.Key)
if err != nil {
return Cipher{}, err
}
iv, err := resolve(fks.Literal.AES128.IV)
if err != nil {
return Cipher{}, err
}
if len(key) != keysz {
return Cipher{}, fmt.Errorf("insufficient key size (%d bytes): want exactly %d bytes", len(key), keysz)
}
if len(iv) != aes.BlockSize {
return Cipher{}, fmt.Errorf("insufficient initialization vector size (%d bytes): want exactly %d bytes", len(iv), aes.BlockSize)
}
c.Key = key
c.IV = iv
return c, nil
}
case "aes192":
if key != nil && salt != nil {
c.Key = pbkdf2.Key(key, salt, 4096, 24, sha256.New)
c.IV = pbkdf2.Key(key, salt, 4096, aes.BlockSize, sha256.New)
return c, nil
}
if fks.Literal.AES192.Key != "" && fks.Literal.AES192.IV != "" {
keysz := 24
key, err := resolve(fks.Literal.AES192.Key)
if err != nil {
return Cipher{}, err
}
iv, err := resolve(fks.Literal.AES192.IV)
if err != nil {
return Cipher{}, err
}
if len(key) != keysz {
return Cipher{}, fmt.Errorf("insufficient key size (%d bytes): want exactly %d bytes", len(key), keysz)
}
if len(iv) != aes.BlockSize {
return Cipher{}, fmt.Errorf("insufficient initialization vector size (%d bytes): want exactly %d bytes", len(iv), aes.BlockSize)
}
c.Key = key
c.IV = iv
return c, nil
}
case "aes256":
if key != nil && salt != nil {
c.Key = pbkdf2.Key(key, salt, 4096, 32, sha256.New)
c.IV = pbkdf2.Key(key, salt, 4096, aes.BlockSize, sha256.New)
return c, nil
}
if fks.Literal.AES256.Key != "" && fks.Literal.AES256.IV != "" {
keysz := 32
key, err := resolve(fks.Literal.AES256.Key)
if err != nil {
return Cipher{}, err
}
iv, err := resolve(fks.Literal.AES256.IV)
if err != nil {
return Cipher{}, err
}
if len(key) != keysz {
return Cipher{}, fmt.Errorf("insufficient key size (%d bytes): want exactly %d bytes", len(key), keysz)
}
if len(iv) != aes.BlockSize {
return Cipher{}, fmt.Errorf("insufficient initialization vector size (%d bytes): want exactly %d bytes", len(iv), aes.BlockSize)
}
c.Key = key
c.IV = iv
return c, nil
}
default:
return Cipher{}, fmt.Errorf("unrecognized encryption algorithm: '%s'", alg)
}
return Cipher{}, fmt.Errorf("unable to derive %s fixed cipher: no methods left to try", algorithm)
}
func (v Vault) Cipher(alg string) (Cipher, error) {
if v.FixedKey.Enabled {
return v.FixedKey.Derive(alg, v.Provider.FixedKeyResolver())
}
c := Cipher{Algorithm: alg}
algorithm, _ := parse(alg)
switch algorithm {
case "aes128":
c.Key = make([]byte, 16)
c.IV = make([]byte, aes.BlockSize)
case "aes192":
c.Key = make([]byte, 24)
c.IV = make([]byte, aes.BlockSize)
case "aes256":
c.Key = make([]byte, 32)
c.IV = make([]byte, aes.BlockSize)
default:
return Cipher{}, fmt.Errorf("unrecognized encryption algorithm: '%s'", alg)
}
if _, err := rand.Read(c.Key); err != nil {
return Cipher{}, fmt.Errorf("failed to generate %s encryption key: %s", alg, err)
}
if _, err := rand.Read(c.IV); err != nil {
return Cipher{}, fmt.Errorf("failed to generate %s initialization vector: %s", alg, err)
}
return c, nil
}