/
aesgcm.go
187 lines (163 loc) · 5.14 KB
/
aesgcm.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package encryption
import (
"crypto/aes"
"crypto/cipher"
"storj.io/common/storj"
"storj.io/common/sync2/race2"
)
type aesgcmEncrypter struct {
blockSize int
key *storj.Key
startingNonce *AESGCMNonce
overhead int
aesgcm cipher.AEAD
}
// NewAESGCMEncrypter returns a Transformer that encrypts the data passing
// through with key.
//
// startingNonce is treated as a big-endian encoded unsigned
// integer, and as blocks pass through, their block number and the starting
// nonce is added together to come up with that block's nonce. Encrypting
// different data with the same key and the same nonce is a huge security
// issue. It's safe to always encode new data with a random key and random
// startingNonce. The monotonically-increasing nonce (that rolls over) is to
// protect against data reordering.
//
// When in doubt, generate a new key from crypto/rand and a startingNonce
// from crypto/rand as often as possible.
func NewAESGCMEncrypter(key *storj.Key, startingNonce *AESGCMNonce, encryptedBlockSize int) (Transformer, error) {
race2.ReadSlice(key[:])
race2.ReadSlice(startingNonce[:])
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, Error.Wrap(err)
}
aesgcmEncrypt, err := cipher.NewGCM(block)
if err != nil {
return nil, Error.Wrap(err)
}
if encryptedBlockSize <= aesgcmEncrypt.Overhead() {
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
}
return &aesgcmEncrypter{
blockSize: encryptedBlockSize - aesgcmEncrypt.Overhead(),
key: key,
startingNonce: startingNonce,
overhead: aesgcmEncrypt.Overhead(),
aesgcm: aesgcmEncrypt,
}, nil
}
func (s *aesgcmEncrypter) InBlockSize() int {
return s.blockSize
}
func (s *aesgcmEncrypter) OutBlockSize() int {
return s.blockSize + s.overhead
}
func calcGCMNonce(startingNonce *AESGCMNonce, blockNum int64) (rv [12]byte, err error) {
if copy(rv[:], (*startingNonce)[:]) != len(rv) {
return rv, Error.New("didn't copy memory?!")
}
_, err = incrementBytes(rv[:], blockNum)
return rv, err
}
func (s *aesgcmEncrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
race2.ReadSlice(in)
race2.WriteSlice(out)
nonce, err := calcGCMNonce(s.startingNonce, blockNum)
if err != nil {
return nil, err
}
cipherData := s.aesgcm.Seal(out, nonce[:], in, nil)
return cipherData, nil
}
type aesgcmDecrypter struct {
blockSize int
key *storj.Key
startingNonce *AESGCMNonce
overhead int
aesgcm cipher.AEAD
}
// NewAESGCMDecrypter returns a Transformer that decrypts the data passing
// through with key. See the comments for NewAESGCMEncrypter about
// startingNonce.
func NewAESGCMDecrypter(key *storj.Key, startingNonce *AESGCMNonce, encryptedBlockSize int) (Transformer, error) {
race2.ReadSlice(key[:])
race2.ReadSlice(startingNonce[:])
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, Error.Wrap(err)
}
aesgcmDecrypt, err := cipher.NewGCM(block)
if err != nil {
return nil, Error.Wrap(err)
}
if encryptedBlockSize <= aesgcmDecrypt.Overhead() {
return nil, ErrInvalidConfig.New("encrypted block size %d too small", encryptedBlockSize)
}
return &aesgcmDecrypter{
blockSize: encryptedBlockSize - aesgcmDecrypt.Overhead(),
key: key,
startingNonce: startingNonce,
overhead: aesgcmDecrypt.Overhead(),
aesgcm: aesgcmDecrypt,
}, nil
}
func (s *aesgcmDecrypter) InBlockSize() int {
return s.blockSize + s.overhead
}
func (s *aesgcmDecrypter) OutBlockSize() int {
return s.blockSize
}
func (s *aesgcmDecrypter) Transform(out, in []byte, blockNum int64) ([]byte, error) {
race2.ReadSlice(in)
race2.WriteSlice(out)
nonce, err := calcGCMNonce(s.startingNonce, blockNum)
if err != nil {
return nil, err
}
plainData, err := s.aesgcm.Open(out, nonce[:], in, nil)
if err != nil {
return nil, ErrDecryptFailed.Wrap(err)
}
return plainData, nil
}
// EncryptAESGCM encrypts byte data with a key and nonce. It returns the cipher data.
func EncryptAESGCM(data []byte, key *storj.Key, nonce *AESGCMNonce) (cipherData []byte, err error) {
race2.ReadSlice(data)
race2.ReadSlice(key[:])
race2.ReadSlice(nonce[:])
block, err := aes.NewCipher(key[:])
if err != nil {
return []byte{}, Error.Wrap(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return []byte{}, Error.Wrap(err)
}
cipherData = aesgcm.Seal(nil, nonce[:], data, nil)
return cipherData, nil
}
// DecryptAESGCM decrypts byte data with a key and nonce. It returns the plain data.
func DecryptAESGCM(cipherData []byte, key *storj.Key, nonce *AESGCMNonce) (data []byte, err error) {
if len(cipherData) == 0 {
return []byte{}, Error.New("empty cipher data")
}
race2.ReadSlice(cipherData)
race2.ReadSlice(key[:])
race2.ReadSlice(nonce[:])
block, err := aes.NewCipher(key[:])
if err != nil {
return []byte{}, Error.Wrap(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return []byte{}, Error.Wrap(err)
}
plainData, err := aesgcm.Open(nil, nonce[:], cipherData, nil)
if err != nil {
return []byte{}, ErrDecryptFailed.Wrap(err)
}
return plainData, nil
}