Skip to content

Commit e548945

Browse files
fsaminloopfz
andauthored
feat: encrypt large files with convergent encryption (#25)
* chore: add gitignore file Signed-off-by: François SAMIN <francois.samin@corp.ovh.com> * chore: go module Signed-off-by: François SAMIN <francois.samin@corp.ovh.com> * feat: encrypt large files with convergent encryption including: * ChunksWriter and ChunksReader * ConvergentKey Signed-off-by: François SAMIN <francois.samin@corp.ovh.com> * test: chunksRead, dhunksWriter and convergentKey Signed-off-by: François SAMIN <francois.samin@corp.ovh.com> * feat: sequential key and deduplication with locator Signed-off-by: François SAMIN <francois.samin@corp.ovh.com> * fix: lint * chore: golang version in travis file Signed-off-by: francois samin <francois.samin@corp.ovh.com> * chore: avoid travis-ci to get out of memory Signed-off-by: francois samin <francois.samin@corp.ovh.com> * feat: new high-level feature in 'convergent' package Signed-off-by: francois samin <francois.samin@corp.ovh.com> * refactor: rename SequentialKey with SequenceKey Signed-off-by: francois samin <francois.samin@corp.ovh.com> * chore: update golangci-lint version Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix code review * feat: Locator() should be public * feat: try to close the destination writer * Apply suggestions from code review Co-authored-by: Thomas Schaffer <thomas.schaffer@corp.ovh.com> * fix * Update symmecrypt.go Co-authored-by: Thomas Schaffer <thomas.schaffer@corp.ovh.com> * wip * fix: move EncryptPipe + DecryptPipe in stream package Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix: typo Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix: avoid io.ErrShortWrite Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix: avoid io.ErrShortWrite Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix: avoid io.ErrShortWrite Signed-off-by: francois samin <francois.samin@corp.ovh.com> * fix: cr Signed-off-by: francois samin <francois.samin@corp.ovh.com> * Distinguish seal handling errors Signed-off-by: Thomas Schaffer <loopfz@gmail.com> * Uniquely identify sealed key errors from keyloader package Signed-off-by: Thomas Schaffer <loopfz@gmail.com> Co-authored-by: Thomas Schaffer <thomas.schaffer@corp.ovh.com> Co-authored-by: Thomas Schaffer <loopfz@gmail.com>
1 parent 55da9c7 commit e548945

File tree

15 files changed

+1241
-93
lines changed

15 files changed

+1241
-93
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Test*
2+
vendor/
3+
cmd/symmecrypt/symmecrypt

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ notifications:
1515
# build and immediately stop. It's sorta like having set -e enabled in bash.
1616
# Make sure golangci-lint is vendored.
1717
before_script:
18-
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.25.0
18+
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
1919

2020
# script always runs to completion (set +e). If we have linter issues AND a
2121
# failing test, we want to see both. Configure golangci-lint with a

ciphers/hmac/hmac.go

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func init() {
3434

3535
type hmacFactory struct{}
3636

37+
func (f hmacFactory) KeyLen() int {
38+
return KeyLen
39+
}
40+
3741
func (f hmacFactory) NewKey(s string) (symmecrypt.Key, error) {
3842
k, err := symutils.RawKey([]byte(s), KeyLen)
3943
if err != nil {
@@ -50,6 +54,16 @@ func (f hmacFactory) NewRandomKey() (symmecrypt.Key, error) {
5054
return Key(b), nil
5155
}
5256

57+
func (f hmacFactory) NewSequenceKey(s string) (symmecrypt.Key, error) {
58+
// the hmac cipher doesnt use a nonce, so a sequence key == a regular key
59+
return f.NewKey(s)
60+
}
61+
62+
func (f hmacFactory) NewRandomSequenceKey() (symmecrypt.Key, error) {
63+
// the hmac cipher doesnt use a nonce, so a sequence key == a regular key
64+
return f.NewRandomKey()
65+
}
66+
5367
// Key is a simple key which uses plain data + HMAC-sha512 for authentication
5468
type Key []byte
5569

ciphers/hmac/hmac_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestEncrypt(t *testing.T) {
11-
11+
1212
text := []byte("foobar")
1313

1414
k, err := symmecrypt.NewRandomKey(CipherName)

cmd/symmecrypt/main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func readKey() error {
7373
for _, encodedKey := range keys {
7474
plain, err := base64.StdEncoding.DecodeString(encodedKey)
7575
if err != nil {
76-
return fmt.Errorf("Invalid base64 encryption key: %s", err)
76+
return fmt.Errorf("Invalid base64 encryption key: %w", err)
7777
}
7878
keyList = append(keyList, configstore.NewItem("encryption-key", string(plain), 1))
7979
}
@@ -96,11 +96,11 @@ func main() {
9696
}
9797
key, err := keyloader.GenerateKey(*newEncryptionCipher, *keyIdentifier, false, time.Now())
9898
if err != nil {
99-
log.Fatalf("error: unable to generate key: %s", err)
99+
log.Fatalf("error: unable to generate key: %v", err)
100100
}
101101
j, err := json.Marshal(key)
102102
if err != nil {
103-
log.Fatalf("error: unable to generate key: %s", err)
103+
log.Fatalf("error: unable to generate key: %v", err)
104104
}
105105
newKey := string(j)
106106
if *useBase64 {
@@ -120,7 +120,7 @@ func main() {
120120
k, err = keyloader.LoadSingleKey()
121121
}
122122
if err != nil {
123-
log.Fatalf("error: failed to instantiate key: %s", err)
123+
log.Fatalf("error: failed to instantiate key: %v", err)
124124
}
125125
dataStr := readSecret()
126126
extra := [][]byte{}
@@ -129,7 +129,7 @@ func main() {
129129
}
130130
b, err := k.Encrypt([]byte(dataStr), extra...)
131131
if err != nil {
132-
log.Fatalf("error: failed to encrypt: %s", err)
132+
log.Fatalf("error: failed to encrypt: %v", err)
133133
}
134134
outputStr := string(b)
135135
if *useBase64 {
@@ -149,13 +149,13 @@ func main() {
149149
k, err = keyloader.LoadSingleKey()
150150
}
151151
if err != nil {
152-
log.Fatalf("error: failed to instantiate key: %s", err)
152+
log.Fatalf("error: failed to instantiate key: %v", err)
153153
}
154154
dataStr := readSecret()
155155
if *useBase64 {
156156
dataRaw, err := base64.StdEncoding.DecodeString(dataStr)
157157
if err != nil {
158-
log.Fatalf("error: failed to decode base64: %s", err)
158+
log.Fatalf("error: failed to decode base64: %v", err)
159159
}
160160
dataStr = string(dataRaw)
161161
}
@@ -165,7 +165,7 @@ func main() {
165165
}
166166
b, err := k.Decrypt([]byte(dataStr), extra...)
167167
if err != nil {
168-
log.Fatalf("error: failed to decrypt: %s", err)
168+
log.Fatalf("error: failed to decrypt: %v", err)
169169
}
170170
fmt.Print(string(b))
171171
}
@@ -177,7 +177,7 @@ func readSecret() string {
177177
os.Exit(0)
178178
}
179179
if err != nil {
180-
log.Fatalf("error: failed to read input: %s", err)
180+
log.Fatalf("error: failed to read input: %v", err)
181181
}
182182
return string(b)
183183
}

0 commit comments

Comments
 (0)