/
snacl.go
265 lines (227 loc) · 7.46 KB
/
snacl.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
// Copyright (c) 2014-2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package snacl
import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/binary"
"io"
"runtime/debug"
"github.com/fonero-project/fnowallet/errors"
"github.com/fonero-project/fnowallet/internal/zero"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/scrypt"
)
var (
prng = rand.Reader
)
// Various constants needed for encryption scheme.
const (
// Expose secretbox's Overhead const here for convenience.
Overhead = secretbox.Overhead
KeySize = 32
NonceSize = 24
DefaultN = 16384 // 2^14
DefaultR = 8
DefaultP = 1
)
// CryptoKey represents a secret key which can be used to encrypt and decrypt
// data.
type CryptoKey [KeySize]byte
// Encrypt encrypts the passed data.
func (ck *CryptoKey) Encrypt(in []byte) ([]byte, error) {
const op errors.Op = "cryptokey.Encrypt"
var nonce [NonceSize]byte
_, err := io.ReadFull(prng, nonce[:])
if err != nil {
return nil, errors.E(op, err)
}
blob := secretbox.Seal(nil, in, &nonce, (*[KeySize]byte)(ck))
return append(nonce[:], blob...), nil
}
// Decrypt decrypts the passed data. The must be the output of the Encrypt
// function.
func (ck *CryptoKey) Decrypt(in []byte) ([]byte, error) {
const op errors.Op = "cryptokey.Decrypt"
if len(in) < NonceSize {
return nil, errors.E(op, errors.Invalid, "missing nonce")
}
var nonce [NonceSize]byte
copy(nonce[:], in[:NonceSize])
blob := in[NonceSize:]
opened, ok := secretbox.Open(nil, blob, &nonce, (*[KeySize]byte)(ck))
if !ok {
return nil, errors.E(op, errors.Crypto)
}
return opened, nil
}
// Zero clears the key by manually zeroing all memory. This is for security
// conscience application which wish to zero the memory after they've used it
// rather than waiting until it's reclaimed by the garbage collector. The
// key is no longer usable after this call.
func (ck *CryptoKey) Zero() {
zero.Bytea32((*[KeySize]byte)(ck))
}
// GenerateCryptoKey generates a new crypotgraphically random key.
func GenerateCryptoKey() (*CryptoKey, error) {
const op errors.Op = "snacl.GenerateCryptoKey"
var key CryptoKey
_, err := io.ReadFull(prng, key[:])
if err != nil {
return nil, errors.E(op, err)
}
return &key, nil
}
// Parameters are not secret and can be stored in plain text.
type Parameters struct {
Salt [KeySize]byte
Digest [sha256.Size]byte
N int
R int
P int
}
// SecretKey houses a crypto key and the parameters needed to derive it from a
// passphrase. It should only be used in memory.
type SecretKey struct {
Key *CryptoKey
Parameters Parameters
}
// deriveKey fills out the Key field.
func (sk *SecretKey) deriveKey(op errors.Op, password *[]byte) error {
key, err := scrypt.Key(*password, sk.Parameters.Salt[:],
sk.Parameters.N,
sk.Parameters.R,
sk.Parameters.P,
len(sk.Key))
if err != nil {
return errors.E(op, err)
}
copy(sk.Key[:], key)
zero.Bytes(key)
// I'm not a fan of forced garbage collections, but scrypt allocates a
// ton of memory and calling it back to back without a GC cycle in
// between means you end up needing twice the amount of memory. For
// example, if your scrypt parameters are such that you require 1GB and
// you call it twice in a row, without this you end up allocating 2GB
// since the first GB probably hasn't been released yet.
debug.FreeOSMemory()
// I'm not a fan of forced garbage collections, but scrypt allocates a
// ton of memory and calling it back to back without a GC cycle in
// between means you end up needing twice the amount of memory. For
// example, if your scrypt parameters are such that you require 1GB and
// you call it twice in a row, without this you end up allocating 2GB
// since the first GB probably hasn't been released yet.
debug.FreeOSMemory()
return nil
}
// Marshal returns the Parameters field marshalled into a format suitable for
// storage. This result of this can be stored in clear text.
func (sk *SecretKey) Marshal() []byte {
params := &sk.Parameters
// The marshalled format for the the params is as follows:
// <salt><digest><N><R><P>
//
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
marshalled := make([]byte, KeySize+sha256.Size+24)
b := marshalled
copy(b[:KeySize], params.Salt[:])
b = b[KeySize:]
copy(b[:sha256.Size], params.Digest[:])
b = b[sha256.Size:]
binary.LittleEndian.PutUint64(b[:8], uint64(params.N))
b = b[8:]
binary.LittleEndian.PutUint64(b[:8], uint64(params.R))
b = b[8:]
binary.LittleEndian.PutUint64(b[:8], uint64(params.P))
return marshalled
}
// Unmarshal unmarshalls the parameters needed to derive the secret key from a
// passphrase into sk.
func (sk *SecretKey) Unmarshal(marshalled []byte) error {
const op errors.Op = "secretkey.Unmarshal"
if sk.Key == nil {
sk.Key = (*CryptoKey)(&[KeySize]byte{})
}
// The marshalled format for the the params is as follows:
// <salt><digest><N><R><P>
//
// KeySize + sha256.Size + N (8 bytes) + R (8 bytes) + P (8 bytes)
if len(marshalled) != KeySize+sha256.Size+24 {
return errors.E(op, errors.Encoding, errors.Errorf("bad marshalled data len %d", len(marshalled)))
}
params := &sk.Parameters
copy(params.Salt[:], marshalled[:KeySize])
marshalled = marshalled[KeySize:]
copy(params.Digest[:], marshalled[:sha256.Size])
marshalled = marshalled[sha256.Size:]
params.N = int(binary.LittleEndian.Uint64(marshalled[:8]))
marshalled = marshalled[8:]
params.R = int(binary.LittleEndian.Uint64(marshalled[:8]))
marshalled = marshalled[8:]
params.P = int(binary.LittleEndian.Uint64(marshalled[:8]))
return nil
}
// Zero zeroes the underlying secret key while leaving the parameters intact.
// This effectively makes the key unusable until it is derived again via the
// DeriveKey function.
func (sk *SecretKey) Zero() {
sk.Key.Zero()
}
// DeriveKey derives the underlying secret key and ensures it matches the
// expected digest. This should only be called after previously calling the
// Zero function or on an initial Unmarshal.
func (sk *SecretKey) DeriveKey(password *[]byte) error {
const op errors.Op = "secretkey.DeriveKey"
if err := sk.deriveKey(op, password); err != nil {
return err
}
// verify password
digest := sha256.Sum256(sk.Key[:])
if subtle.ConstantTimeCompare(digest[:], sk.Parameters.Digest[:]) != 1 {
return errors.E(op, errors.Passphrase)
}
return nil
}
// Encrypt encrypts in bytes and returns a JSON blob.
func (sk *SecretKey) Encrypt(in []byte) ([]byte, error) {
const op errors.Op = "secretkey.Encrypt"
out, err := sk.Key.Encrypt(in)
if err != nil {
return nil, errors.E(op, err)
}
return out, nil
}
// Decrypt takes in a JSON blob and returns it's decrypted form.
func (sk *SecretKey) Decrypt(in []byte) ([]byte, error) {
const op errors.Op = "secretkey.Decrypt"
out, err := sk.Key.Decrypt(in)
if err != nil {
return nil, errors.E(op, err)
}
return out, nil
}
// NewSecretKey returns a SecretKey structure based on the passed parameters.
func NewSecretKey(password *[]byte, N, r, p int) (*SecretKey, error) {
const op errors.Op = "snacl.NewSecretKey"
sk := SecretKey{
Key: (*CryptoKey)(&[KeySize]byte{}),
}
// setup parameters
sk.Parameters.N = N
sk.Parameters.R = r
sk.Parameters.P = p
_, err := io.ReadFull(prng, sk.Parameters.Salt[:])
if err != nil {
return nil, errors.E(op, err)
}
// derive key
err = sk.deriveKey(op, password)
if err != nil {
return nil, err
}
// store digest
sk.Parameters.Digest = sha256.Sum256(sk.Key[:])
return &sk, nil
}