-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (109 loc) · 3.21 KB
/
main.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
// Copyright (c) 2020-2024 cions
// Licensed under the MIT License. See LICENSE for details.
package goenc
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"io"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/chacha20poly1305"
)
var (
ErrFormat = errors.New("not a valid goenc file")
ErrInvalidTag = errors.New("tag verification failed (password is wrong or data is corrupted)")
)
// Format represents the file format.
type Format int
const (
// FormatDefault indicates that the format is not specified.
FormatDefault Format = iota
// FormatV1 represents the goenc version 1 format.
//
// Version 1 format uses XChaCha20-Poly1305 for authenticated encryption and Argon2id for key derivation.
FormatV1
)
// Options are encryption parameters.
type Options struct {
Format Format // File format
Time uint32 // KDF time parameter
Memory uint32 // KDF memory parameter
Threads uint8 // KDF parallelism parameter
}
// Encrypt encrypts plaintext with password.
func Encrypt(password, plaintext []byte, opts *Options) ([]byte, error) {
switch opts.Format {
case FormatDefault, FormatV1:
return encryptV1(password, plaintext, opts)
default:
return nil, fmt.Errorf("goenc: invalid Format: %v", opts.Format)
}
}
// Decrypt decrypts input with password.
func Decrypt(password, input []byte) ([]byte, error) {
if len(input) == 0 {
return nil, io.ErrUnexpectedEOF
}
switch input[0] {
case 0x01:
return decryptV1(password, input)
default:
return nil, ErrFormat
}
}
const (
saltSizeV1 = 16
headerSizeV1 = 10 + saltSizeV1
nonceSizeV1 = chacha20poly1305.NonceSizeX
ctStartV1 = headerSizeV1 + nonceSizeV1
minSizeV1 = headerSizeV1 + nonceSizeV1 + chacha20poly1305.Overhead
)
func encryptV1(password, plaintext []byte, opts *Options) ([]byte, error) {
buf := make([]byte, minSizeV1+len(plaintext))
header := buf[:headerSizeV1]
salt := header[10:]
nonce := buf[headerSizeV1:ctStartV1]
dst := buf[:ctStartV1]
header[0] = 0x01
binary.LittleEndian.PutUint32(header[1:5], opts.Time)
binary.LittleEndian.PutUint32(header[5:9], opts.Memory)
header[9] = opts.Threads
if _, err := rand.Read(salt); err != nil {
return nil, fmt.Errorf("crypto/rand: %w", err)
}
if _, err := rand.Read(nonce); err != nil {
return nil, fmt.Errorf("crypto/rand: %w", err)
}
key := argon2.IDKey(password, salt, opts.Time, opts.Memory, opts.Threads, chacha20poly1305.KeySize)
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
return aead.Seal(dst, nonce, plaintext, header), nil
}
func decryptV1(password, input []byte) ([]byte, error) {
if len(input) < minSizeV1 {
return nil, io.ErrUnexpectedEOF
}
if input[0] != 0x01 {
return nil, ErrFormat
}
header := input[:headerSizeV1]
time := binary.LittleEndian.Uint32(header[1:5])
memory := binary.LittleEndian.Uint32(header[5:9])
threads := header[9]
salt := header[10:]
nonce := input[headerSizeV1:ctStartV1]
ciphertext := input[ctStartV1:]
key := argon2.IDKey(password, salt, time, memory, threads, chacha20poly1305.KeySize)
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
plaintext, err := aead.Open(nil, nonce, ciphertext, header)
if err != nil {
return nil, ErrInvalidTag
}
return plaintext, nil
}