-
Notifications
You must be signed in to change notification settings - Fork 0
/
materials.go
235 lines (211 loc) · 5.8 KB
/
materials.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package crypto
import (
"bytes"
"context"
"errors"
"io"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/encryption"
"go.uber.org/zap"
)
// AESGCMMaterials ...
type AESGCMMaterials struct {
mode int
reader io.Reader
eof bool
bufferedRead *bytes.Buffer
//bufferedUnread *bytes.Buffer
encryptionKey []byte
blockSize int32
initialBlockSize int32
blockSizeFixed bool
nonceBytes []byte
nonceBuffer *bytes.Buffer
blockCount int
totalRead int64
}
// NewAESGCMMaterials creates an encryption materials that use AES GCM
func NewAESGCMMaterials(key []byte, t *encryption.Params) *AESGCMMaterials {
m := new(AESGCMMaterials)
m.encryptionKey = key
if t != nil {
m.initialBlockSize = t.BlockSize
m.blockSize = t.BlockSize
m.nonceBuffer = bytes.NewBuffer(t.Nonce)
m.nonceBytes = t.Nonce
} else {
m.initialBlockSize = 4096
m.blockSize = 4096
m.nonceBuffer = bytes.NewBuffer([]byte{})
}
return m
}
// Close closes the underlying stream
func (m *AESGCMMaterials) Close() error {
closer, ok := m.reader.(io.Closer)
if ok {
return closer.Close()
}
return nil
}
func (m *AESGCMMaterials) Read(b []byte) (int, error) {
switch m.mode {
case 1:
return m.encryptRead(b)
case 2:
return m.decryptRead(b)
default:
return 0, errors.New("mode not set")
}
}
// GetIV returns the IV used to encrypt/decrypt as a string
func (m *AESGCMMaterials) GetIV() (iv string) {
return ""
}
// GetKey returns the key used to encrypt/decrypt
func (m *AESGCMMaterials) GetKey() (key string) {
return ""
}
// GetDesc returns a string description of the materials
func (m *AESGCMMaterials) GetDesc() (desc string) {
return ""
}
// SetupEncryptMode set underlying read function in encrypt mode
func (m *AESGCMMaterials) SetupEncryptMode(stream io.Reader) error {
m.bufferedRead = bytes.NewBuffer([]byte{})
m.reader = stream
m.blockSizeFixed = false
m.blockSize = m.initialBlockSize
m.eof = false
m.mode = 1
m.blockCount = 0
m.totalRead = 0
return nil
}
// SetupDecryptMode set underlying read function in decrypt mode
func (m *AESGCMMaterials) SetupDecryptMode(stream io.Reader, iv string, key string) error {
m.bufferedRead = bytes.NewBuffer([]byte{})
m.blockSizeFixed = true
m.blockSize = m.initialBlockSize + 16
if m.nonceBytes == nil {
//Rewind buffer
m.nonceBytes = m.nonceBuffer.Bytes()
m.nonceBuffer = bytes.NewBuffer(m.nonceBytes)
} else {
m.nonceBuffer = bytes.NewBuffer(m.nonceBytes)
}
m.reader = stream
m.eof = false
m.mode = 2
m.blockCount = 0
m.totalRead = 0
return nil
}
// GetEncryptedParameters returns the additional parameters that are generated for encryption
func (m *AESGCMMaterials) GetEncryptedParameters() *encryption.Params {
return &encryption.Params{
Nonce: m.nonceBuffer.Bytes(),
BlockSize: m.blockSize,
}
}
func (m *AESGCMMaterials) encryptRead(b []byte) (int, error) {
var totalSet = 0
var cursor = 0
l := len(b)
buff := make([]byte, m.blockSize)
for totalSet < l {
readAvailable := m.bufferedRead.Len()
if readAvailable > 0 {
n, _ := m.bufferedRead.Read(b[totalSet:])
totalSet += n
if totalSet == l {
return totalSet, nil
}
} else if m.eof {
return totalSet, io.EOF
}
n, err := m.reader.Read(buff[cursor:])
m.totalRead += int64(n)
if err != nil {
m.eof = err == io.EOF
if !m.eof {
return n, err
}
}
m.blockCount++
cursor += n
if cursor != 0 && (cursor == int(m.blockSize) || m.eof) {
sealed, err := Seal(m.encryptionKey, buff[:cursor])
if err != nil {
log.Logger(context.Background()).Error("failed to seal block", zap.Int("Block Num", m.blockCount), zap.Int32("Block size", m.blockSize), zap.Int64("Total Read", m.totalRead), zap.Error(err))
return n, err
}
cursor = 0
m.nonceBuffer.Write(sealed[:12])
m.bufferedRead.Write(sealed[12:])
}
}
return totalSet, nil
}
func (m *AESGCMMaterials) decryptRead(b []byte) (int, error) {
var totalSet = 0
var cursor = 0
l := len(b)
buff := make([]byte, m.blockSize)
for totalSet < l {
readAvailable := m.bufferedRead.Len()
if readAvailable > 0 {
n, _ := m.bufferedRead.Read(b[totalSet:])
totalSet += n
if totalSet == l {
return totalSet, nil
}
} else if m.eof {
return totalSet, io.EOF
}
n, err := m.reader.Read(buff[cursor:])
m.totalRead += int64(n)
if err != nil {
m.eof = err == io.EOF
if !m.eof {
return n, err
}
}
m.blockCount++
cursor += n
if cursor != 0 && (cursor == int(m.blockSize) || m.eof) {
nonce := make([]byte, 12)
nl, err := m.nonceBuffer.Read(nonce)
if err != nil || nl < 12 {
return 0, errors.New("Read nonce failed")
}
opened, err := Open(m.encryptionKey, nonce, buff[:cursor])
if err != nil {
log.Logger(context.Background()).Error("failed to decrypt block", zap.Int("Block Num", m.blockCount), zap.Int32("Block size", m.blockSize), zap.Int64("Total Read", m.totalRead), zap.Error(err))
return 0, err
}
cursor = 0
m.bufferedRead.Write(opened)
}
}
return totalSet, nil
}