Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to encrypt an mnemonic #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package bip39

import (
"crypto/aes"
"crypto/cipher"
"crypto/sha512"
"golang.org/x/crypto/pbkdf2"
)

func EncryptMnemonic(mnemonic string, password string) (string, error) {
entropy, err := EntropyFromMnemonic(mnemonic)
if err != nil {
return "", err
}
encEntropy, err := EncryptEntropy(entropy, password)
if err != nil {
return "", err
}
return NewMnemonic(encEntropy)
}

func EncryptEntropy(entropy []byte, password string) ([]byte, error) {
pwHash := pbkdf2.Key([]byte(password), []byte("mnemonic-encryption"), 2048, 32, sha512.New)

ci, err := aes.NewCipher(pwHash[:])
if err != nil {
return nil, err
}

iv := make([]byte, 16)
enc := cipher.NewCTR(ci, iv)

out := make([]byte, len(entropy))
enc.XORKeyStream(out, entropy)

return out, nil
}
48 changes: 48 additions & 0 deletions encyrption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bip39

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestEcryptMnemonic(t *testing.T) {
mnmonic := "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform"
expectedEncMnemonic := "father level place shallow review foil illegal elbow wine warm soft penalty token banner cage century someone warfare horn vote crumble now attack gorilla"
password := "securePassword"

encMnemonic, err := EncryptMnemonic(mnmonic, password)
assert.NoError(t, err)
assert.Equal(t, expectedEncMnemonic, encMnemonic)
}

func TestEcryptEntropy(t *testing.T) {
mnmonic := "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform"
expectedEncMnemonic := "father level place shallow review foil illegal elbow wine warm soft penalty token banner cage century someone warfare horn vote crumble now attack gorilla"
password := "securePassword"

entropy, err := EntropyFromMnemonic(mnmonic)
assert.NoError(t, err)

mnemonicFromEntropy, err := NewMnemonic(entropy)
assert.NoError(t, err)
assert.Equal(t, mnemonicFromEntropy, mnmonic)

encEntropy, err := EncryptEntropy(entropy, password)
assert.NoError(t, err)
assert.Equal(t, len(encEntropy), len(entropy))

encMnemonic, err := NewMnemonic(encEntropy)
assert.NoError(t, err)

// Check the encMnemonic
assert.Equal(t, expectedEncMnemonic, encMnemonic)

// Get back to original mnemonic
encEntropyNew, err := EntropyFromMnemonic(encMnemonic)
assert.Equal(t, encEntropy, encEntropyNew)
assert.NoError(t, err)

// Decrypt the encrypted entropy
plainEntropy, err := EncryptEntropy(encEntropy, password)
assert.Equal(t, entropy, plainEntropy)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/tyler-smith/go-bip39
go 1.11

require (
github.com/stretchr/testify v1.7.0
github.com/tyler-smith/assert v1.0.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/text v0.3.0
)
12 changes: 11 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tyler-smith/assert v1.0.1 h1:AymyVNGyHOAVaDUEo0WouAMB/wwi5NdbJTDb7lsfFGk=
github.com/tyler-smith/assert v1.0.1/go.mod h1:oPc3QyIN2k0t/9Xp0sNhi3suiWuGXzIcdf/dbKQ6QGk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -6,5 +13,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=