summaryrefslogtreecommitdiffstats
path: root/internal/cryptoutil/cryptoutil.go
blob: 09ed9964faa13d83b9f19f83fecd3d52cef1fa97 (plain) (blame)
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
// Package cryptoutil contains friendly wrappers around the algorithms from the
// standard library's crypto package.
package cryptoutil

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/hkdf"
	"crypto/hmac"
	"crypto/pbkdf2"
	"crypto/rand"
	"crypto/sha512"
	"crypto/subtle"
	"encoding/hex"
	"errors"
	"fmt"
	"hash"
	"io"
	"slices"
)

const (
	defaultIterations = 4718580 // from cmd/finditers

	oneSaltSize = 64
	hashSize    = 64
	certSize    = sha512.Size
	aesKeySize  = 32
	nonceSize   = aes.BlockSize
)

// SaltSize is the expected salt length for HashIter.
const SaltSize = 2 * oneSaltSize

// A PasswordHash must be PasswordHashSize bytes.
const PasswordHashSize = SaltSize + hashSize

// A PasswordHash is derived from the user's password and can be passed to
// CheckPassword to verify if two passwords match. A PasswordHash must be
// PasswordHashSize bytes.
type PasswordHash []byte

func (h PasswordHash) String() string { return hex.EncodeToString(h) }

// A RawKey is generated from a password hash and can be used to derive further
// key material.
type RawKey struct {
	key []byte
}

// An HMACKey must be HMACKeySize bytes.
const HMACKeySize = sha512.BlockSize

// An HMAC key can be used to symmetrically sign and verify messages
// using HMAC-SHA512. An HMACKey must be HMACKeyLen bytes.
type HMACKey []byte

// A SignedMessage is a message that has been cryptographically signed
// with HMAC-SHA512.
type SignedMessage []byte

// An EncryptionKey must be EncryptionKeySize bytes.
const EncryptionKeySize = aesKeySize + HMACKeySize

// An EncryptionKey is used for encrypting and decrypting data. An EncryptionKey
// must be EncryptionKeySize bytes.
type EncryptionKey []byte

// An EncryptedMessage is encrypted with AES-256-CTR-HMAC-SHA512.
type EncryptedMessage []byte

// HashIter runs PBKDF2-SHA512 for iter iterations. Useful for benchmarking. The
// salt must be SaltSize bytes.
func HashIter(password string, salt []byte, iter int) ([]byte, error) {
	return pbkdf2.Key(sha512.New, password, salt, iter, sha512.Size)
}

func hashWithSalt(password string, salt []byte) (RawKey, []byte, error) {
	hash, err := HashIter(password, salt[:oneSaltSize], defaultIterations)
	if err != nil {
		return RawKey{}, nil, err
	}
	key, err := hkdf.Extract(sha512.New, hash, salt[oneSaltSize:])
	if err != nil {
		return RawKey{}, nil, err
	}
	pwHash, err := hkdf.Expand(sha512.New, key, "pwhash", hashSize)
	if err != nil {
		return RawKey{}, nil, err
	}
	return RawKey{key}, pwHash, nil
}

// Hash hashes a user password using PBKDF2-SHA512.
func Hash(password string) (PasswordHash, error) {
	salt := make([]byte, SaltSize, SaltSize+hashSize)
	rand.Read(salt)
	_, pwHash, err := hashWithSalt(password, salt)
	if err != nil {
		return nil, err
	}
	return append(salt, pwHash...), nil
}

// CheckPassword verifies password against a PasswordHash and returns an
// EncryptionKey derived from the password if successful.
func (h PasswordHash) CheckPassword(password string) (RawKey, error) {
	salt, expectedHash := h[:SaltSize], h[SaltSize:]
	key, pwHash, err := hashWithSalt(password, salt)
	if err != nil {
		return RawKey{}, err
	}
	if subtle.ConstantTimeCompare(pwHash, expectedHash) == 0 {
		return RawKey{}, errors.New("incorrect password")
	}
	return key, nil
}

// Sign generates an HMAC-SHA512 signature and appends it to msg.
func (k HMACKey) Sign(msg []byte) SignedMessage {
	mac := hmac.New(sha512.New, k)
	mac.Write(msg)
	return mac.Sum(msg)
}

// Verify checks whether the given message has a valid signature, and returns
// the raw message if it does.
func (k HMACKey) Verify(msg SignedMessage) ([]byte, bool) {
	if len(msg) < certSize {
		return nil, false
	}
	msg, sig := msg[:len(msg)-certSize], msg[len(msg)-certSize:]
	mac := hmac.New(sha512.New, k)
	mac.Write(msg)
	if !hmac.Equal(sig, mac.Sum(nil)) {
		return nil, false
	}
	return msg, true
}

// EncryptionKey derives an EncryptionKey.
func (k RawKey) EncryptionKey() (EncryptionKey, error) {
	return hkdf.Expand(sha512.New, k.key, "encrypt", EncryptionKeySize)
}

// Encrypt encrypts a message with AES-256-CTR-HMAC-SHA512.
func (k EncryptionKey) Encrypt(msg []byte) (EncryptedMessage, error) {
	aesKey, hmacKey := k[:aesKeySize], HMACKey(k[aesKeySize:])
	block, err := aes.NewCipher(aesKey)
	if err != nil {
		return nil, err
	}
	cipherText := make([]byte, len(msg)+nonceSize, len(msg)+nonceSize+certSize)
	nonce := cipherText[len(msg) : len(msg)+nonceSize]
	rand.Read(nonce)
	cipher.NewCTR(block, nonce).XORKeyStream(cipherText, msg)
	return EncryptedMessage(hmacKey.Sign(cipherText)), nil
}

// Decrypt verifies and decrypts an encrypted message. It overwrites msg with
// the resulting plain text.
func (k EncryptionKey) Decrypt(msg EncryptedMessage) ([]byte, error) {
	aesKey, hmacKey := k[:aesKeySize], HMACKey(k[aesKeySize:])
	msg, ok := hmacKey.Verify(SignedMessage(msg))
	if !ok {
		return nil, errors.New("bad signature")
	}
	block, err := aes.NewCipher(aesKey)
	if err != nil {
		return nil, err
	}
	msg, nonce := msg[:len(msg)-nonceSize], msg[len(msg)-nonceSize:]
	cipher.NewCTR(block, nonce).XORKeyStream(msg, msg)
	return msg, nil
}

// An EncryptingWriter encrypts the output and writes it to the underlying
// writer. It's very important to call .Flush() to write the MAC after the data
// has been written.
type EncryptingWriter struct {
	w      io.Writer
	stream cipher.Stream
	mac    hash.Hash
	buf    []byte
}

// Writer returns a new writer that encrypts its output. Don't forget to call
// .Flush() to write the MAC.
func (k EncryptionKey) Writer(w io.Writer) (*EncryptingWriter, error) {
	aesKey, hmacKey := k[:aesKeySize], k[aesKeySize:]
	block, err := aes.NewCipher(aesKey)
	if err != nil {
		return nil, err
	}
	mac := hmac.New(sha512.New, hmacKey)
	nonce := make([]byte, nonceSize)
	rand.Read(nonce)
	mac.Write(nonce)
	if _, err := w.Write(nonce); err != nil {
		return nil, err
	}
	return &EncryptingWriter{
		w:      w,
		stream: cipher.NewCTR(block, nonce),
		mac:    mac,
	}, nil
}

// Write encrypts buf and writes it to the underlying writer.
func (w *EncryptingWriter) Write(buf []byte) (int, error) {
	if len(w.buf) < len(buf) {
		w.buf = slices.Grow(w.buf, len(buf)-len(w.buf))
	}
	w.buf = w.buf[:len(buf)]
	// Encrypt-then-MAC
	w.stream.XORKeyStream(w.buf, buf)
	w.mac.Write(w.buf)
	return w.w.Write(w.buf)
}

// Flush writes the MAC for the encrypted message. Flush must be called after
// all data has been written.
func (w *EncryptingWriter) Flush() error {
	_, err := w.w.Write(w.mac.Sum(nil))
	return err
}

// A DecryptingReader decrypts data from an underlying reader.
type DecryptingReader struct {
	r cipher.StreamReader
}

// Reader returns a new DecryptingReader. The data is processed in two passes,
// first to verify the MAC, then the io.Seeker interface is used to reset the
// reader for decryption.
func (k EncryptionKey) Reader(r io.ReadSeeker) (*DecryptingReader, error) {
	aesKey, hmacKey := k[:aesKeySize], k[aesKeySize:]
	block, err := aes.NewCipher(aesKey)
	if err != nil {
		return nil, err
	}
	totalLen, err := r.Seek(0, io.SeekEnd)
	if err != nil {
		return nil, err
	}
	if totalLen < certSize {
		return nil, errors.New("file too short")
	}
	dataLen := totalLen - certSize
	if _, err := r.Seek(0, io.SeekStart); err != nil {
		return nil, err
	}
	mac := hmac.New(sha512.New, hmacKey)
	if _, err := io.Copy(mac, &io.LimitedReader{R: r, N: dataLen}); err != nil {
		return nil, err
	}
	expectedMAC := mac.Sum(nil)
	sig := make([]byte, certSize)
	if _, err := io.ReadFull(r, sig); err != nil {
		return nil, err
	}
	if !hmac.Equal(sig, expectedMAC) {
		return nil, fmt.Errorf("invalid mac (got %x, want %x)", sig, expectedMAC)
	}
	if _, err := r.Seek(0, io.SeekStart); err != nil {
		return nil, err
	}
	nonce := make([]byte, nonceSize)
	if _, err := io.ReadFull(r, nonce); err != nil {
		return nil, err
	}
	return &DecryptingReader{cipher.StreamReader{S: cipher.NewCTR(block, nonce), R: &io.LimitedReader{R: r, N: dataLen - nonceSize}}}, nil
}

// Read reads and decrypts data from the underlying reader.
func (r *DecryptingReader) Read(buf []byte) (int, error) {
	return r.r.Read(buf)
}