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

Added password to storage module #16

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions storage/localstate_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
folder string
writeLock *sync.RWMutex
encryptMode bool
key []byte
passkey []byte
keyGen *KeygenLocalState
}

// NewFileStateMgr create a new instance of the FileStateMgr which implements LocalStateManager
func NewFileStateMgr(folder string) (*FileStateMgr, error) {
func NewFileStateMgr(folder string, password string) (*FileStateMgr, error) {
if len(folder) > 0 {
_, err := os.Stat(folder)
if err != nil && os.IsNotExist(err) {
Expand All @@ -62,15 +63,16 @@
}
}
encryptMode := true
key, err := getFragmentSeed()
key, err := getFragmentSeed(password)
if err != nil {
encryptMode = false
}
return &FileStateMgr{
folder: folder,
writeLock: &sync.RWMutex{},
encryptMode: encryptMode,
key: key,
passkey: key,
keyGen: nil,
}, nil
}

Expand Down Expand Up @@ -109,6 +111,9 @@

// GetLocalState read the local state from file system
func (fsm *FileStateMgr) GetLocalState(pubKey string) (KeygenLocalState, error) {
if fsm.keyGen != nil {
return *fsm.keyGen, nil
}
if len(pubKey) == 0 {
return KeygenLocalState{}, errors.New("pub key is empty")
}
Expand All @@ -133,6 +138,7 @@
if err := json.Unmarshal(pt, &localState); nil != err {
return KeygenLocalState{}, fmt.Errorf("fail to unmarshal KeygenLocalState:%x %w", pt, err)
}
fsm.keyGen = &localState
lumtis marked this conversation as resolved.
Show resolved Hide resolved
return localState, nil
}

Expand Down Expand Up @@ -199,7 +205,7 @@
if !fsm.encryptMode {
return plainText, nil
}
block, err := aes.NewCipher(fsm.key)
block, err := aes.NewCipher(fsm.passkey)
if err != nil {
return nil, err
}
Expand All @@ -221,7 +227,7 @@
if !fsm.encryptMode {
return buf, nil
}
block, err := aes.NewCipher(fsm.key)
block, err := aes.NewCipher(fsm.passkey)
if err != nil {
return nil, err
}
Expand All @@ -240,11 +246,16 @@
return plainText, nil
}

func getFragmentSeed() ([]byte, error) {
func getFragmentSeed(password string) ([]byte, error) {
seedStr := os.Getenv(keyFragmentSeed)
if seedStr == "" {
return nil, errors.New("empty fragment seed, please populate env variable: " + keyFragmentSeed)
if password == "" {
return nil, errors.New("empty fragment seed, please check password: " + password)
}
fmt.Println("using provided password !!!", password, len(password))
Fixed Show fixed Hide fixed
seedStr = password
}

h := sha256.New()
h.Write([]byte(seedStr))
seed := h.Sum(nil)
Expand Down
8 changes: 4 additions & 4 deletions storage/localstate_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *FileStateMgrTestSuite) TestNewFileStateMgr(c *C) {
err := os.RemoveAll(f)
c.Assert(err, IsNil)
}()
fsm, err := NewFileStateMgr(f)
fsm, err := NewFileStateMgr(f, "password")
c.Assert(err, IsNil)
c.Assert(fsm, NotNil)
_, err = os.Stat(f)
Expand All @@ -61,7 +61,7 @@ func (s *FileStateMgrTestSuite) TestSaveLocalState(c *C) {
err := os.RemoveAll(f)
c.Assert(err, IsNil)
}()
fsm, err := NewFileStateMgr(f)
fsm, err := NewFileStateMgr(f, "password")
c.Assert(err, IsNil)
c.Assert(fsm, NotNil)
c.Assert(fsm.SaveLocalState(stateItem), NotNil)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (s *FileStateMgrTestSuite) TestSaveAddressBook(c *C) {
err := os.RemoveAll(f)
c.Assert(err, IsNil)
}()
fsm, err := NewFileStateMgr(f)
fsm, err := NewFileStateMgr(f, "password")
c.Assert(err, IsNil)
c.Assert(fsm, NotNil)
c.Assert(fsm.SaveAddressBook(testAddresses), IsNil)
Expand All @@ -112,7 +112,7 @@ func (s *FileStateMgrTestSuite) TestEncryption(c *C) {
err := os.RemoveAll(f)
c.Assert(err, IsNil)
}()
fsm, err := NewFileStateMgr(f)
fsm, err := NewFileStateMgr(f, "password")
c.Assert(err, IsNil)
c.Assert(fsm, NotNil)

Expand Down
3 changes: 2 additions & 1 deletion tss/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewTss(
conf common.TssConfig,
preParams *bkeygen.LocalPreParams,
externalIP string,
tssPassword string,
) (*TssServer, error) {
pk := coskey.PubKey{
Key: priKey.PubKey().Bytes()[:],
Expand All @@ -61,7 +62,7 @@ func NewTss(
return nil, fmt.Errorf("fail to genearte the key: %w", err)
}

stateManager, err := storage.NewFileStateMgr(baseFolder)
stateManager, err := storage.NewFileStateMgr(baseFolder, tssPassword)
if err != nil {
return nil, fmt.Errorf("fail to create file state manager")
}
Expand Down