Skip to content

Commit 889ceb7

Browse files
authored
Move away from sha1 (#2)
Use sha256 instead of sha1 for calculating the filename hash used to derive the nonce. This is only applied to new files so that old files can still be read.
1 parent 3571398 commit 889ceb7

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

storage.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"compress/gzip"
3131
"crypto/rand"
3232
"crypto/sha1"
33+
"crypto/sha256"
3334
"encoding"
3435
"encoding/binary"
3536
"encoding/gob"
@@ -62,6 +63,7 @@ const (
6263
optEncrypted = 0x10
6364
optCompressed = 0x20
6465
optPadded = 0x40
66+
optSHA256 = 0x80
6567
)
6668

6769
var (
@@ -342,7 +344,11 @@ func (s *Storage) OpenManyForUpdate(files []string, objects interface{}) (func(c
342344
}, nil
343345
}
344346

345-
func context(s string) []byte {
347+
func context(s string, useSHA2 bool) []byte {
348+
if useSHA2 {
349+
h := sha256.Sum256([]byte(s))
350+
return h[:]
351+
}
346352
h := sha1.Sum([]byte(s))
347353
return h[:]
348354
}
@@ -363,6 +369,7 @@ func (s *Storage) ReadDataFile(filename string, obj interface{}) error {
363369
return errors.New("wrong file type")
364370
}
365371
flags := hdr[4]
372+
useSHA2 := flags&optSHA256 != 0
366373
if flags&optEncrypted != 0 && s.masterKey == nil {
367374
return errors.New("file is encrypted, but a master key was not provided")
368375
}
@@ -376,7 +383,7 @@ func (s *Storage) ReadDataFile(filename string, obj interface{}) error {
376383
}
377384
defer k.Wipe()
378385
// Use the file key to decrypt the rest of the file.
379-
if r, err = k.StartReader(context(filename), f); err != nil {
386+
if r, err = k.StartReader(context(filename, useSHA2), f); err != nil {
380387
return err
381388
}
382389
// Read the header again.
@@ -463,7 +470,7 @@ func (s *Storage) ReadDataFile(filename string, obj interface{}) error {
463470
// SaveDataFile atomically replace an object in a file.
464471
func (s *Storage) SaveDataFile(filename string, obj interface{}) error {
465472
t := fmt.Sprintf("%s.tmp-%d", filename, time.Now().UnixNano())
466-
if err := s.writeFile(context(filename), t, obj); err != nil {
473+
if err := s.writeFile(context(filename, true), t, obj); err != nil {
467474
return err
468475
}
469476
// Atomically replace the file.
@@ -472,7 +479,7 @@ func (s *Storage) SaveDataFile(filename string, obj interface{}) error {
472479

473480
// CreateEmptyFile creates an empty file.
474481
func (s *Storage) CreateEmptyFile(filename string, empty interface{}) error {
475-
return s.writeFile(context(filename), filename, empty)
482+
return s.writeFile(context(filename, true), filename, empty)
476483
}
477484

478485
// writeFile writes obj to a file.
@@ -482,15 +489,15 @@ func (s *Storage) writeFile(ctx []byte, filename string, obj interface{}) (retEr
482489
return err
483490
}
484491

485-
var flags byte
492+
flags := byte(optSHA256)
486493
if _, ok := obj.(encoding.BinaryMarshaler); ok {
487-
flags = optBinaryEncoded
494+
flags |= optBinaryEncoded
488495
} else if _, ok := obj.(*[]byte); ok {
489-
flags = optRawBytes
496+
flags |= optRawBytes
490497
} else if s.useGOB {
491-
flags = optGOBEncoded
498+
flags |= optGOBEncoded
492499
} else {
493-
flags = optJSONEncoded
500+
flags |= optJSONEncoded
494501
}
495502
if s.masterKey != nil {
496503
flags |= optEncrypted
@@ -563,12 +570,12 @@ func (s *Storage) OpenBlobWrite(writeFileName, finalFileName string) (io.WriteCl
563570
if err := createParentIfNotExist(fn); err != nil {
564571
return nil, err
565572
}
566-
var flags byte = optRawBytes
573+
var flags byte = optRawBytes | optSHA256
567574
if s.masterKey != nil {
568575
flags |= optEncrypted
569576
flags |= optPadded
570577
}
571-
return s.openWriteStream(context(finalFileName), fn, flags, 1024*1024)
578+
return s.openWriteStream(context(finalFileName, true), fn, flags, 1024*1024)
572579
}
573580

574581
// OpenBlobRead opens a blob file for reading.
@@ -591,6 +598,7 @@ func (s *Storage) OpenBlobRead(filename string) (stream io.ReadSeekCloser, retEr
591598
return nil, errors.New("wrong file type")
592599
}
593600
flags := hdr[4]
601+
useSHA2 := flags&optSHA256 != 0
594602
if flags&optRawBytes == 0 {
595603
return nil, errors.New("blob files is not raw bytes")
596604
}
@@ -610,7 +618,7 @@ func (s *Storage) OpenBlobRead(filename string) (stream io.ReadSeekCloser, retEr
610618
}
611619
defer k.Wipe()
612620
// Use the file key to decrypt the rest of the file.
613-
if r, err = k.StartReader(context(filename), f); err != nil {
621+
if r, err = k.StartReader(context(filename, useSHA2), f); err != nil {
614622
return nil, err
615623
}
616624
// Read the header again.

storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func RunBenchmarkOpenForUpdate(b *testing.B, kb int, k crypto.EncryptionKey, com
403403
}
404404
obj.M[string(key)] = string(value)
405405
}
406-
if err := s.writeFile(context("testfile"), "testfile", &obj); err != nil {
406+
if err := s.writeFile(context("testfile", true), "testfile", &obj); err != nil {
407407
b.Fatalf("s.writeFile: %v", err)
408408
}
409409
fi, err := os.Stat(file)

0 commit comments

Comments
 (0)