Skip to content

Commit

Permalink
Add an env var DUPLICACY_DECRYPT_WITH_HMACSHA256 to force using HMAC-…
Browse files Browse the repository at this point in the history
…SHA256 for encryption key in order to be able to manage backups created by Vertical Backup
  • Loading branch information
gilbertchen committed May 3, 2018
1 parent 23a2d91 commit b1c1b47
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/duplicacy_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package duplicacy

import (
"os"
"bytes"
"compress/zlib"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
Expand Down Expand Up @@ -267,6 +270,17 @@ func (chunk *Chunk) Encrypt(encryptionKey []byte, derivationKey string) (err err

}

// This is to ensure compability with Vertical Backup, which still uses HMAC-SHA256 (instead of HMAC-BLAKE2) to
// derive the key used to encrypt/decrypt files and chunks.

var DecryptWithHMACSHA256 = false

func init() {
if value, found := os.LookupEnv("DUPLICACY_DECRYPT_WITH_HMACSHA256"); found && value != "0" {
DecryptWithHMACSHA256 = true
}
}

// Decrypt decrypts the encrypted data stored in the chunk buffer. If derivationKey is not nil, the actual
// encryption key will be HMAC-SHA256(encryptionKey, derivationKey).
func (chunk *Chunk) Decrypt(encryptionKey []byte, derivationKey string) (err error) {
Expand All @@ -286,7 +300,13 @@ func (chunk *Chunk) Decrypt(encryptionKey []byte, derivationKey string) (err err
key := encryptionKey

if len(derivationKey) > 0 {
hasher := chunk.config.NewKeyedHasher([]byte(derivationKey))
var hasher hash.Hash
if DecryptWithHMACSHA256 {
hasher = hmac.New(sha256.New, []byte(derivationKey))
} else {
hasher = chunk.config.NewKeyedHasher([]byte(derivationKey))
}

hasher.Write(encryptionKey)
key = hasher.Sum(nil)
}
Expand Down Expand Up @@ -325,6 +345,7 @@ func (chunk *Chunk) Decrypt(encryptionKey []byte, derivationKey string) (err err
return err
}


paddingLength := int(decryptedBytes[len(decryptedBytes)-1])
if paddingLength == 0 {
paddingLength = 256
Expand Down

0 comments on commit b1c1b47

Please sign in to comment.