-
Notifications
You must be signed in to change notification settings - Fork 0
/
pemaead.go
276 lines (234 loc) · 6.06 KB
/
pemaead.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
package pemaead
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
// TODO: we move away from pbkdf2 -> scrypt/argon2id
"io"
"io/ioutil"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
"golang.org/x/crypto/sha3"
)
const (
// argon up to date parameters
ArgonCostTime = 2
ArgonCostMemory = 256 * 1024
ArgonCostThreads = 8
// more up to date parameters
ScryptParamN = 65536
ScryptParamR = 16
ScryptParamP = 4
// insecure by now..
Pbkdf2Iteration = 16384
// ou key length
KeyLength = 32
SaltLength = 32
AEADPemFileHeader = "PEMAEAD FILE"
AEADFormat = "AEAD,%02d%02d,%x,%x"
DerivatePbkdf2 = 0x00
DerivateScrypt = 0x01
DerivateArgon2 = 0x02 // Argon2id by default
CipherAESGCM = 0x00
//CipherNaCL = 0x01 // we dont support it yet
//CipherPQ1 = 0x02 // not yet ready..
// uint16
// 0000 0000 // unused for now..
// 0000 0000
// \_this nibble will handle key derivation
// |
// \_ this nibble will handle encryption used
//
// 00 == pbkdf
// 01 == scrypt
// 02 == argon2
//
// 00 == aes-gcm
// 01 == nacl
Version = "0.1.0"
)
var (
ErrUnsafe = errors.New("unsafe option")
ErrInvalid = errors.New("invalid data")
)
func deriveKey(derivation uint8, salt, password []byte) (dkey []byte, err error) {
//var dkey []byte
zero := make([]byte, len(salt))
switch {
case len(password) < 5:
err = ErrUnsafe
return
case len(salt) < 8:
err = ErrUnsafe
return
case bytes.Equal(zero, salt):
err = ErrUnsafe
return
}
switch derivation {
case DerivatePbkdf2:
dkey = pbkdf2.Key(password, salt, Pbkdf2Iteration, KeyLength, sha3.New256)
return
case DerivateScrypt:
dkey, err = scrypt.Key([]byte(password), salt, ScryptParamN, ScryptParamR, ScryptParamP, KeyLength)
return
case DerivateArgon2:
fallthrough
default:
dkey = argon2.IDKey([]byte(password), salt, ArgonCostTime, ArgonCostMemory, ArgonCostThreads, KeyLength)
return
}
}
type File struct {
// let's build the header right away
// buffer we write onto before Close() (== Seal())
key []byte // derived key
nonce []byte // nonce to be used
cipher uint8 // which cipher we use
derivation uint8 // which derivation algorithm
w io.WriteCloser // the Writer output
c cipher.AEAD // the Cipher in GCM mode
header map[string]string // the PEM file header
buf bytes.Buffer // the buffer we Read/Write from/to
}
//func NewWriter(w io.Writer, password []byte, cipher, derivation uint8) (*AEADPemFile, error) {
func NewWriter(w io.WriteCloser, password []byte, c, d uint8) (io.WriteCloser, error) {
var nonce []byte
var aesGcm cipher.AEAD
salt := make([]byte, SaltLength)
_, err := rand.Read(salt)
if err != nil {
return nil, err
}
key, err := deriveKey(d, salt, password)
if err != nil {
return nil, err
}
switch c {
case CipherAESGCM:
fallthrough
default:
aesBlockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesGcm, err = cipher.NewGCM(aesBlockCipher)
if err != nil {
return nil, err
}
nonce = make([]byte, aesGcm.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return nil, err
}
}
// build the header, not the nicest but well..
// ourHeader["DEK-Info"] = "AEAD" + "," + hex.EncodeToString(nonce) + "," + hex.EncodeToString(salt)
ourHeader := make(map[string]string)
ourHeader["Proc-Type"] = "4,ENCRYPTED"
ourHeader["DEK-Info"] = fmt.Sprintf(AEADFormat, c, d, nonce, salt)
a := &File{
key: key, // already derived
nonce: nonce, // our Nonce
cipher: c, // cipher
derivation: d, // derivation
w: w, // where we write at the end.
c: aesGcm, // AEAD interface
header: ourHeader, // header so that we don't have to build it
}
return a, nil
}
func (a *File) Write(b []byte) (n int, err error) {
return a.buf.Write(b)
}
func (a *File) Close() (err error) {
/* encrypt & authenticate, we're done. */
encrypted := a.c.Seal(nil, a.nonce, a.buf.Bytes(), []byte(a.header["DEK-Info"]))
pemBlock := &pem.Block{
Type: AEADPemFileHeader,
Headers: a.header,
Bytes: encrypted,
}
err = pem.Encode(a.w, pemBlock)
if err != nil {
return err
}
return a.w.Close()
}
//func NewReader(r io.Reader, password []byte) (*AEADPemFile, error) {
func NewReader(r io.Reader, password []byte) (io.Reader, error) {
var nonce, salt []byte
var c, d uint8
var aesGcm cipher.AEAD
rawBuf, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
pemBlock, _ := pem.Decode(rawBuf)
if pemBlock == nil {
return nil, ErrInvalid
}
if !x509.IsEncryptedPEMBlock(pemBlock) {
return nil, ErrInvalid
}
if pemBlock.Type != AEADPemFileHeader {
return nil, ErrInvalid
}
dek, ok := pemBlock.Headers["DEK-Info"]
if !ok {
return nil, ErrInvalid
}
_, err = fmt.Sscanf(dek, AEADFormat, &c, &d, &nonce, &salt)
if err != nil {
return nil, ErrInvalid
}
key, err := deriveKey(d, salt, password)
if err != nil {
return nil, ErrInvalid
}
switch c {
case CipherAESGCM:
fallthrough
default:
aesBlockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, ErrInvalid
}
aesGcm, err = cipher.NewGCM(aesBlockCipher)
if err != nil {
return nil, ErrInvalid
}
}
if len(nonce) != aesGcm.NonceSize() {
return nil, ErrInvalid
}
plaintext, err := aesGcm.Open(nil, nonce, pemBlock.Bytes, []byte(dek))
if err != nil {
return nil, ErrInvalid
}
a := &File{
key: key, // already derived
cipher: c, // cipher
derivation: d, // derivation
c: aesGcm, // AEAD interface
nonce: nonce, // our Nonce
header: pemBlock.Headers, // header so that we don't have to build it
//w: w writer.
}
a.buf.Reset()
_, err = a.buf.Write(plaintext)
if err != nil {
return nil, ErrInvalid
}
// let's split the nonce, salt and cipher, derivation
return a, nil
}
func (a *File) Read(b []byte) (n int, err error) {
return a.buf.Read(b)
}