@@ -11,36 +11,11 @@ import (
1111 "crypto/pbkdf2"
1212 "crypto/rand"
1313 "crypto/sha512"
14- "encoding/binary"
1514 "errors"
1615 "fmt"
1716 "io"
1817)
1918
20- // Option is the default options used to generate the encrypt and decrypt writer.
21- // NOTE: the defined options need to be same for both the Reader and the writer.
22- type Option struct {
23- Generator bytesGen
24- IterationsCount int
25- KeyLength int
26- SaltLength int
27- IVLength int
28-
29- // BlockSize must be a factor of aes.BlockSize
30- BlockSize int
31- }
32-
33- // DefaultOptions is the default options to use when creating the writer, changing might decrease
34- // the efficacity of the encryption.
35- var DefaultOptions = & Option {
36- IterationsCount : 10000 ,
37- KeyLength : 32 ,
38- SaltLength : 64 ,
39- IVLength : 12 ,
40- Generator : randomBytes ,
41- BlockSize : bytes .MinRead ,
42- }
43-
4419// versionMagicHeader is the format version that will be added at the beginning of the header and
4520// can be used to change how the decryption work in future version.
4621var versionMagicHeader = []byte ("v2" )
@@ -122,7 +97,7 @@ func (w *Writer) Write(b []byte) (int, error) {
12297 return 0 , w .err
12398 }
12499
125- aesgcm , err := cipher . NewGCM (block )
100+ aesgcm , err := getCipherAEAD (block )
126101 if err != nil {
127102 w .err = fmt .Errorf ("could not create the GCM to encrypt: %w" , err )
128103 return 0 , w .err
@@ -160,33 +135,6 @@ func (w *Writer) Write(b []byte) (int, error) {
160135 return len (b ), nil
161136}
162137
163- func (w * Writer ) writeBlock (b []byte ) error {
164- // randomly generate the salt and the initialization vector, this information will be saved
165- // on disk in the file as part of the header
166- iv , err := w .generator (w .option .IVLength )
167- if err != nil {
168- w .err = fmt .Errorf ("fail to generate random IV: %w" , err )
169- return w .err
170- }
171-
172- //nolint:errcheck // Ignore the error at this point.
173- w .writer .Write (iv )
174-
175- encodedBytes := w .gcm .Seal (nil , iv , b , nil )
176-
177- l := make ([]byte , 4 )
178- binary .LittleEndian .PutUint32 (l , uint32 (len (encodedBytes ))) //nolint:gosec // ignoring unsafe type conversion
179- //nolint:errcheck // Ignore the error at this point.
180- w .writer .Write (l )
181-
182- _ , err = w .writer .Write (encodedBytes )
183- if err != nil {
184- return fmt .Errorf ("fail to encode data: %w" , err )
185- }
186-
187- return nil
188- }
189-
190138// Reader implements the io.Reader interface and allow to decrypt bytes from the Writer. The reader
191139// will lazy read the header from the wrapper reader to initialize everything required to decrypt
192140// the data.
@@ -262,7 +210,7 @@ func (r *Reader) Read(b []byte) (int, error) {
262210 return 0 , r .err
263211 }
264212
265- aesgcm , err := cipher . NewGCM (block )
213+ aesgcm , err := getCipherAEAD (block )
266214 if err != nil {
267215 r .err = fmt .Errorf ("could not create the GCM to decrypt the data: %w" , err )
268216 return 0 , r .err
@@ -300,43 +248,6 @@ func (r *Reader) readTo(b []byte) (int, error) {
300248 return n , r .err
301249}
302250
303- func (r * Reader ) consumeBlock () error {
304- // Retrieve block information:
305- // - Initialization vector
306- // - Length of the block
307- iv , l , err := r .readBlockInfo ()
308- if err != nil {
309- return err
310- }
311-
312- encodedBytes := make ([]byte , l )
313- _ , err = io .ReadAtLeast (r .reader , encodedBytes , l )
314- if err != nil {
315- r .err = fmt .Errorf ("fail read the block of %d bytes: %w" , l , err )
316- }
317-
318- decodedBytes , err := r .gcm .Open (nil , iv , encodedBytes , nil )
319- if err != nil {
320- return fmt .Errorf ("fail to decode bytes: %w" , err )
321- }
322- r .buf = append (r .buf [:], decodedBytes ... )
323-
324- return nil
325- }
326-
327- func (r * Reader ) readBlockInfo () ([]byte , int , error ) {
328- buf := make ([]byte , r .option .IVLength + 4 )
329- _ , err := io .ReadAtLeast (r .reader , buf , len (buf ))
330- if err != nil {
331- return nil , 0 , err
332- }
333-
334- iv := buf [0 :r .option .IVLength ]
335- l := binary .LittleEndian .Uint32 (buf [r .option .IVLength :])
336-
337- return iv , int (l ), nil
338- }
339-
340251// Close will propagate the Close call to the wrapped reader.
341252func (r * Reader ) Close () error {
342253 a , ok := r .reader .(io.ReadCloser )
0 commit comments