-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.go
348 lines (298 loc) · 8.54 KB
/
crypto.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
// +build go1.10
// Copyright 2016-2018 (c) Eric "eau" Augé <[email protected]>
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Package naclpipe provides io.Reader / io.Writer compatible crypto interface
// it is possible to create a transparent crypto interface on top of an
// io.Reader/io.Writer pattern.
package naclpipe
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
"golang.org/x/crypto/argon2" //let's add argon2id
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/scrypt" // let's keep scrypt
"golang.org/x/crypto/sha3"
)
//
//
// INIT / INTERNAL
//
//
const (
// we increase scrypt params for configuration purposes
scryptCostParam = 65536
scryptCostN = 16
scryptCostP = 4
// our argon 2 parameters (we are in 2018)
argonCostTime = 2
argonCostMemory = 256 * 1024
argonCostThread = 8
// generic
keyLength = 32
SaltLength = 32
OldSaltLength = 16
// we use argon 2id by default
DerivateScrypt = iota
DerivateArgon2id
)
var (
// ErrUnsupported triggers for using an unsupported derivation function.
ErrUnsupported = errors.New("unsupported option")
// ErrUnsafe triggers for unsafe key derivation function.
ErrUnsafe = errors.New("unsafe option")
// ErrRead triggers on an error from the underlying io.Reader
ErrRead = errors.New("read error")
// ErrWrite triggers on an error from the underlying io.Writer
ErrWrite = errors.New("write error")
)
// ScryptParams describes the parameters used for calling the scrypt key derivation function.
type ScryptParams struct {
CostParam int
CostN int
CostP int
SaltLen int
KeyLength int
}
// Argon2Params describes the parameters used for calling the Argon2id key derivation function.
type Argon2Params struct {
CostTime uint32
CostMemory uint32
CostThreads uint8
KeyLength uint32
}
// NaclPipe define the structure that handle the crypto pipe operation
// it also holds all internal datas related to the running pipe.
type NaclPipe struct {
dKey *[32]byte // derived key
cntNonce *[24]byte
cnt uint64 // nonce counter
salt []byte // salt value mainly to avoid the writer writing before the first block is written.
wr io.Writer
rd io.Reader
params interface{}
//stdioSize uint32
}
// initialize the params
func (c *NaclPipe) initialize(d int) {
c.cntNonce = new([24]byte)
c.dKey = new([32]byte)
c.cnt = 0
c.salt = make([]byte, SaltLength)
switch d {
case DerivateScrypt:
c.params = ScryptParams{
CostParam: scryptCostParam,
CostN: scryptCostN,
CostP: scryptCostP,
SaltLen: SaltLength,
KeyLength: keyLength,
}
case DerivateArgon2id:
fallthrough
default:
c.params = Argon2Params{
CostTime: argonCostTime,
CostMemory: argonCostMemory,
CostThreads: argonCostThread,
KeyLength: keyLength,
}
}
}
// key derivation wrapper call
func (c *NaclPipe) deriveKey(salt []byte, password string) (err error) {
var dKey []byte
// check salt is NOT all zero print a warning
zero := make([]byte, len(salt))
switch {
case len(password) < 5:
err = ErrUnsafe
return
case len(salt) < 12:
err = ErrUnsafe
return
case bytes.Equal(zero, salt):
err = ErrUnsafe
return
}
switch v := c.params.(type) {
case ScryptParams:
/* let's derive a key */
dKey, err = scrypt.Key([]byte(password), c.salt, v.CostParam, v.CostN, v.CostP, v.KeyLength)
if err != nil {
return
}
case Argon2Params:
//fmt.Fprintf(os.Stderr, "ARGON DERIVATION\n")
dKey = argon2.IDKey([]byte(password), c.salt, v.CostTime, v.CostMemory, v.CostThreads, v.KeyLength)
default:
err = ErrUnsupported
return
}
copy(c.dKey[:], dKey)
return
}
// shazam function does an SHA3 on the counter and update the counter/Nonce value generated.
// stream operate in blocks, then each blocks will be encrypted with its nonce.
func (c *NaclPipe) shazam() {
out := sha3.Sum256([]byte(fmt.Sprintf("%d", c.cnt)))
copy(c.cntNonce[:], out[:24])
return
}
//
//
// READER
//
//
func (c *NaclPipe) initReader(r io.Reader, password string) (err error) {
//c.salt = make([]byte, SaltLength)
// we read the salt immediately
_, err = r.Read(c.salt)
if err != nil {
return
}
/* let's derive a key */
err = c.deriveKey(c.salt, password)
if err != nil {
return
}
c.rd = r
return
}
// NewReader initialize an io.Reader using 'password' and the selected derivation function.
// Example:
// cryptoReader, err := naclpipe.NewReader(os.Stdin, "mypassword", naclpipe.DerivateScrypt)
// if err != nil {
// return err
// }
func NewReader(r io.Reader, password string, derivation int) (io.Reader, error) {
return newCryptoReader(r, password, derivation)
}
//func newCryptoReader(r io.Reader, strKey string, derivation int) (c *NaclPipe, err error) {
func newCryptoReader(r io.Reader, password string, derivation int) (io.Reader, error) {
//salt := make([]byte, 16)
c := new(NaclPipe)
/* init values/vars */
c.initialize(derivation)
/* let's derive a key */
err := c.initReader(r, password)
if err != nil {
return nil, err
}
return c, nil
}
// Read will read the amount of
func (c *NaclPipe) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
c.shazam()
//b := make([]byte, len(p))
b := make([]byte, len(p)+secretbox.Overhead)
//n, err = c.rd.Read(b)
n, err = io.ReadFull(c.rd, b)
if err != nil && err != io.ErrUnexpectedEOF {
return n, err
}
pt, res := secretbox.Open(nil, b[:n], c.cntNonce, c.dKey)
if res == true {
copy(p, pt)
c.cnt++
return len(pt), nil
}
return 0, ErrRead
}
//
//
// WRITER
//
//
func (c *NaclPipe) writeSalt() (err error) {
n, err := c.wr.Write(c.salt)
fmt.Printf("writeSalt(salt: %d bytes): n: %d err: %v\n", len(c.salt), n, err)
return
}
func (c *NaclPipe) initWriter(w io.Writer, password string) (err error) {
//c.salt = make([]byte, scryptSaltLen)
// initialize a CSPRNG salt
_, err = rand.Read(c.salt)
if err != nil {
return
}
/* let's derive a key */
err = c.deriveKey(c.salt, password)
if err != nil {
return
}
c.wr = w
return
}
// NewWriter initialize an io.Writer using 'password' and the selected derivation function.
// Example:
// cryptoWriter, err := naclpipe.NewWriter(os.Stdout, "mypassword", naclpipe.DerivateScrypt)
// if err != nil {
// return err
// }
func NewWriter(w io.Writer, password string, derivation int) (io.Writer, error) {
return newCryptoWriter(w, password, derivation)
}
//func newCryptoWriter(w io.Writer, strKey string, derivation int) (c *NaclPipe, err error) {
func newCryptoWriter(w io.Writer, password string, derivation int) (io.Writer, error) {
//salt := make([]byte, 16)
c := new(NaclPipe)
/* init values/vars */
c.initialize(derivation)
/* let's derive a key */
err := c.initWriter(w, password)
if err != nil {
return nil, err
}
return c, nil
}
// SHA3 the counter use it as nonce
func (c *NaclPipe) Write(p []byte) (n int, err error) {
c.shazam()
if c.cnt == 0 {
n, err = c.wr.Write(c.salt)
if err != nil {
return
}
}
// Seal
ct := secretbox.Seal(nil, p, c.cntNonce, c.dKey)
c.cnt++
// now Write()
n, err = c.wr.Write(ct)
if err != nil || n != len(ct) {
fmt.Fprintf(os.Stderr, "we should write %d but wrote %d (err:%v)\n", len(ct), n, err)
}
n = len(p)
return
}