Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
a97f2e79
Commit
a97f2e79
authored
3 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Handle file pathing edge cases
parent
3e595537
No related branches found
No related tags found
2 merge requests
!23
Release
,
!19
Implement mnemonic in client
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/mnemonic.go
+27
-3
27 additions, 3 deletions
api/mnemonic.go
api/mnemonic_test.go
+7
-1
7 additions, 1 deletion
api/mnemonic_test.go
bindings/mnemonic.go
+1
-0
1 addition, 0 deletions
bindings/mnemonic.go
with
35 additions
and
4 deletions
api/mnemonic.go
+
27
−
3
View file @
a97f2e79
...
@@ -14,9 +14,11 @@ import (
...
@@ -14,9 +14,11 @@ import (
xxMnemonic
"gitlab.com/xx_network/crypto/mnemonic"
xxMnemonic
"gitlab.com/xx_network/crypto/mnemonic"
"gitlab.com/xx_network/primitives/utils"
"gitlab.com/xx_network/primitives/utils"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/chacha20poly1305"
"path/filepath"
"strings"
)
)
const
mnemonicFile
=
"
/
.recovery"
const
mnemonicFile
=
".recovery"
// StoreSecretWithMnemonic creates a mnemonic and uses it to encrypt the secret.
// StoreSecretWithMnemonic creates a mnemonic and uses it to encrypt the secret.
// This encrypted data saved in storage.
// This encrypted data saved in storage.
...
@@ -24,12 +26,18 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
...
@@ -24,12 +26,18 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
// Use fastRNG for RNG ops (AES fortuna based RNG using system RNG)
// Use fastRNG for RNG ops (AES fortuna based RNG using system RNG)
rng
:=
fastRNG
.
NewStreamGenerator
(
12
,
3
,
csprng
.
NewSystemRNG
)
.
GetStream
()
rng
:=
fastRNG
.
NewStreamGenerator
(
12
,
3
,
csprng
.
NewSystemRNG
)
.
GetStream
()
// Ensure path is appended by filepath separator "/"
if
!
strings
.
HasSuffix
(
path
,
string
(
filepath
.
Separator
))
{
path
=
path
+
string
(
filepath
.
Separator
)
}
// Create a mnemonic
// Create a mnemonic
mnemonic
,
err
:=
xxMnemonic
.
GenerateMnemonic
(
rng
,
32
)
mnemonic
,
err
:=
xxMnemonic
.
GenerateMnemonic
(
rng
,
32
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
errors
.
Errorf
(
"Failed to generate mnemonic: %v"
,
err
)
return
""
,
errors
.
Errorf
(
"Failed to generate mnemonic: %v"
,
err
)
}
}
// Decode mnemonic
decodedMnemonic
,
err
:=
xxMnemonic
.
DecodeMnemonic
(
mnemonic
)
decodedMnemonic
,
err
:=
xxMnemonic
.
DecodeMnemonic
(
mnemonic
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
errors
.
Errorf
(
"Failed to decode mnemonic: %v"
,
err
)
return
""
,
errors
.
Errorf
(
"Failed to decode mnemonic: %v"
,
err
)
...
@@ -42,7 +50,8 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
...
@@ -42,7 +50,8 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
}
}
// Save encrypted secret to file
// Save encrypted secret to file
err
=
utils
.
WriteFileDef
(
path
+
mnemonicFile
,
ciphertext
)
recoveryFile
:=
path
+
mnemonicFile
err
=
utils
.
WriteFileDef
(
recoveryFile
,
ciphertext
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
errors
.
Errorf
(
"Failed to save mnemonic information to file"
)
return
""
,
errors
.
Errorf
(
"Failed to save mnemonic information to file"
)
}
}
...
@@ -53,16 +62,31 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
...
@@ -53,16 +62,31 @@ func StoreSecretWithMnemonic(secret []byte, path string) (string, error) {
// LoadSecretWithMnemonic loads the encrypted secret from storage and decrypts
// LoadSecretWithMnemonic loads the encrypted secret from storage and decrypts
// the secret using the given mnemonic.
// the secret using the given mnemonic.
func
LoadSecretWithMnemonic
(
mnemonic
,
path
string
)
(
secret
[]
byte
,
err
error
)
{
func
LoadSecretWithMnemonic
(
mnemonic
,
path
string
)
(
secret
[]
byte
,
err
error
)
{
data
,
err
:=
utils
.
ReadFile
(
path
+
mnemonicFile
)
// Ensure path is appended by filepath separator "/"
if
!
strings
.
HasSuffix
(
path
,
string
(
filepath
.
Separator
))
{
path
=
path
+
string
(
filepath
.
Separator
)
}
// Ensure that the recovery file exists
recoveryFile
:=
path
+
mnemonicFile
if
!
utils
.
Exists
(
recoveryFile
)
{
return
nil
,
errors
.
Errorf
(
"Recovery file does not exist. "
+
"Did you properly set up recovery or provide an incorrect filepath?"
)
}
// Read file from storage
data
,
err
:=
utils
.
ReadFile
(
recoveryFile
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Failed to load mnemonic information: %v"
,
err
)
return
nil
,
errors
.
Errorf
(
"Failed to load mnemonic information: %v"
,
err
)
}
}
// Decode mnemonic
decodedMnemonic
,
err
:=
xxMnemonic
.
DecodeMnemonic
(
mnemonic
)
decodedMnemonic
,
err
:=
xxMnemonic
.
DecodeMnemonic
(
mnemonic
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Failed to decode mnemonic: %v"
,
err
)
return
nil
,
errors
.
Errorf
(
"Failed to decode mnemonic: %v"
,
err
)
}
}
// Decrypt the stored secret
secret
,
err
=
decryptWithMnemonic
(
data
,
decodedMnemonic
)
secret
,
err
=
decryptWithMnemonic
(
data
,
decodedMnemonic
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Errorf
(
"Failed to decrypt secret: %v"
,
err
)
return
nil
,
errors
.
Errorf
(
"Failed to decrypt secret: %v"
,
err
)
...
...
This diff is collapsed.
Click to expand it.
api/mnemonic_test.go
+
7
−
1
View file @
a97f2e79
...
@@ -19,7 +19,7 @@ import (
...
@@ -19,7 +19,7 @@ import (
func
TestStoreSecretWithMnemonic
(
t
*
testing
.
T
)
{
func
TestStoreSecretWithMnemonic
(
t
*
testing
.
T
)
{
secret
:=
[]
byte
(
"test123"
)
secret
:=
[]
byte
(
"test123"
)
storageDir
:=
"ignore.1"
storageDir
:=
"ignore.1
/
"
mnemonic
,
err
:=
StoreSecretWithMnemonic
(
secret
,
storageDir
)
mnemonic
,
err
:=
StoreSecretWithMnemonic
(
secret
,
storageDir
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"StoreSecretWithMnemonic error; %v"
,
err
)
t
.
Errorf
(
"StoreSecretWithMnemonic error; %v"
,
err
)
...
@@ -90,6 +90,12 @@ func TestLoadSecretWithMnemonic(t *testing.T) {
...
@@ -90,6 +90,12 @@ func TestLoadSecretWithMnemonic(t *testing.T) {
t
.
Fatalf
(
"Loaded secret does not match original data."
+
t
.
Fatalf
(
"Loaded secret does not match original data."
+
"
\n\t
Expected: %v
\n\t
Received: %v"
,
secret
,
received
)
"
\n\t
Expected: %v
\n\t
Received: %v"
,
secret
,
received
)
}
}
_
,
err
=
LoadSecretWithMnemonic
(
mnemonic
,
"badDirectory"
)
if
err
==
nil
{
t
.
Fatalf
(
"LoadSecretWithMnemonic should error when provided a path "
+
"where a recovery file does not exist."
)
}
}
}
// Prng is a PRNG that satisfies the csprng.Source interface.
// Prng is a PRNG that satisfies the csprng.Source interface.
...
...
This diff is collapsed.
Click to expand it.
bindings/mnemonic.go
0 → 100644
+
1
−
0
View file @
a97f2e79
package
bindings
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment