/
boxer.go
218 lines (177 loc) · 6.76 KB
/
boxer.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
// Copyright (c) 2015, xrstf | MIT licensed
// Package boxer implements a convenience wrapper around the NaCl package.
//
// Its main goal is to hide the management of nonces and salts from the user.
// Passwords will be automatically stretched using scrypt, the nonce includes
// the current time to make collisions less likely.
//
// Note that the key stretching is performed on each encryption/decryption, so
// if you plan to perform a lot of crypto operations in a short time, this might
// be too expensive.
package boxer
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"time"
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/scrypt"
)
// The length of the salt used for scrypt.
const SaltLength = 24
// The length of the nonce used for the secretbox implementation.
const NonceLength = 24
// The length of the encryption key for the secretbox implementation.
const KeyLength = 32
// This structure contains the parameters used for running scrypt. As time passes,
// they need to be adjusted to take newer CPU/GPU generations into account. Use
// DefaultScryptParameters() to get a good set of parameters as of 2015. Note that
// the Cost factor highly depends on your usecase: if you plan to do many crypto
// runs in a short time, a high Cost factor can open you up to Denial-of-Service
// attacks.
type ScryptParameters struct {
// cost parameter
Cost int
// R value
R int
// P value
P int
}
// Return a sane set of default values as of 2015.
func DefaultScryptParameters() ScryptParameters {
return ScryptParameters{16384, 8, 1}
}
// A Boxer can open and seal boxes to encrypt/decrypt data.
//
// Boxes created by this type are only compatible to other Boxers with the
// identical params struct. Also, use *either* password-based encryption
// *or* key-based encryption, but don't mix the two, as their random salt
// will make it basically impossible to get the keys right.
type Boxer struct {
params ScryptParameters
}
// Create a new Boxer based on a given scrypt parameter set.
func NewBoxer(params ScryptParameters) *Boxer {
return &Boxer{params}
}
// Create a new Boxer with the default scrypt parameters of this package.
func NewDefaultBoxer() *Boxer {
return &Boxer{DefaultScryptParameters()}
}
// Encrypt some data with a password
//
// This function automatically stretches the password to meet the KeyLength
// requirement, as well as calculate a fresh nonce. The function returns an
// error if the data/password is empty or not enough data is available in
// rand.Reader, otherwise the first value will be the encryption result,
// containing the salt and nonce.
func (b *Boxer) Encrypt(data []byte, password []byte) ([]byte, error) {
if len(data) == 0 {
return nil, errors.New("Cannot encrypt zero-length data.")
}
if len(password) == 0 {
return nil, errors.New("Empty passwords are not allowed for encryption.")
}
// derive a new encryption key for this message
key, salt, err := b.DeriveKeyFromPassword(password)
if err != nil {
return nil, errors.New("Could not derive encryption key from password: " + err.Error())
}
// create a fresh nonce
nonce, err := b.CreateNonce()
if err != nil {
return nil, errors.New("Could not create nonce: " + err.Error())
}
// seal the data in a nacl box; the box will have the kd salt and nonce prepended
box := make([]byte, SaltLength+NonceLength)
copy(box, salt[:])
copy(box[SaltLength:], nonce[:])
// let the magic happen
box = secretbox.Seal(box, data, nonce, key)
return box, nil
}
// Decrypt data
//
// This function decrypts data originally encrypted using Encrypt(). It will extract
// the scrypt salt and nonce and apply the required transformations. An error is
// returned when the ciphertext is too short or the password does not match. Otherwise,
// the raw decrypted message is returned.
func (b *Boxer) Decrypt(ciphertext []byte, password []byte) ([]byte, error) {
minLength := SaltLength + NonceLength + secretbox.Overhead + 1
if len(ciphertext) < minLength {
return nil, fmt.Errorf("The ciphertext is too short (%d bytes) to be valid. It needs to be at least %d bytes.", len(ciphertext), minLength)
}
// figure out the salt to derive the key used for encryption
salt := new([SaltLength]byte)
copy(salt[:], ciphertext[:SaltLength])
key, err := b.DeriveKey(password, salt)
if err != nil {
return nil, err
}
// find the secretbox nonce (if follows the SaltLength bytes at the beginning of ciphertext)
nonce := new([NonceLength]byte)
copy(nonce[:], ciphertext[SaltLength:(SaltLength+NonceLength)])
// slice out the secretbox
box := ciphertext[(SaltLength + NonceLength):]
// ... and open it
plain, success := secretbox.Open(nil, box, nonce, key)
if !success {
return nil, errors.New("Decrypting failed, probably due to a wrong password.")
}
return plain, nil
}
// Derive a key for NaCl's secretbox from a password
//
// secretbox requires a key with exactly 32 bytes. This function uses scrypt to
// derive a key from a given password. Note that the result is randomized, as it
// contains a random salt.
// The first byte array is the key, the second one is the generated salt. You need
// to keep the salt (it does not need to be encrypted, consider it "public") and
// use it again to re-create the same key from a password.
func (b *Boxer) DeriveKeyFromPassword(password []byte) (*[KeyLength]byte, *[SaltLength]byte, error) {
// create the salt for key derivation
salt := new([SaltLength]byte)
_, err := rand.Reader.Read(salt[:])
if err != nil {
return nil, nil, errors.New("Could not gather sufficient random data to perform encryption: " + err.Error())
}
key, err := b.DeriveKey(password, salt)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}
// Derive a key from a password and salt
//
// This is just a wrapper around scrypt.
func (b *Boxer) DeriveKey(password []byte, salt *[SaltLength]byte) (*[KeyLength]byte, error) {
// create encryption key (32byte) from the password
key := new([KeyLength]byte)
result, err := scrypt.Key(password, salt[:], b.params.Cost, b.params.R, b.params.P, KeyLength)
if err != nil {
return nil, err
}
copy(key[:], result)
return key, nil
}
// Create a nonce
//
// Nonces MUST be used exactly once for encrypting a message. They can be appended
// to the ciphertext, as they do not need to stay secret.
// This implementation returns a nonce consisting of the current time including
// milliseconds and 16 random bytes. It returns an error if rand.Reader could not
// provide enough entropy.
func (b *Boxer) CreateNonce() (*[NonceLength]byte, error) {
return createNonce(time.Now())
}
func createNonce(t time.Time) (*[NonceLength]byte, error) {
nonce := new([NonceLength]byte)
now := t.UnixNano()
binary.BigEndian.PutUint64(nonce[:], uint64(now))
_, err := rand.Reader.Read(nonce[8:])
if err != nil {
return nil, err
}
return nonce, nil
}