forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move and refactor ACT diffieHellman to Session. Add Key and NewFromKe…
…ystore functions. (#16)
- Loading branch information
Showing
9 changed files
with
261 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package mock | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
|
||
"github.com/ethersphere/bee/pkg/crypto" | ||
"github.com/ethersphere/bee/pkg/keystore" | ||
) | ||
|
||
type SessionMock struct { | ||
KeyFunc func(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error) | ||
key *ecdsa.PrivateKey | ||
} | ||
|
||
func (s *SessionMock) Key(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error) { | ||
if s.KeyFunc == nil { | ||
return nil, nil | ||
} | ||
return s.KeyFunc(publicKey, nonces) | ||
|
||
} | ||
|
||
func NewSessionMock(key *ecdsa.PrivateKey) *SessionMock { | ||
return &SessionMock{key: key} | ||
} | ||
|
||
func NewFromKeystore( | ||
ks keystore.Service, | ||
tag, | ||
password string, | ||
keyFunc func(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error), | ||
) *SessionMock { | ||
key, created, err := ks.Key(tag, password, crypto.EDGSecp256_K1) | ||
if !created || err != nil { | ||
return nil | ||
} | ||
return &SessionMock{ | ||
key: key, | ||
KeyFunc: keyFunc, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dynamicaccess | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"errors" | ||
|
||
"github.com/ethersphere/bee/pkg/crypto" | ||
"github.com/ethersphere/bee/pkg/keystore" | ||
) | ||
|
||
// Session represents an interface for a Diffie-Helmann key derivation | ||
type Session interface { | ||
// Key returns a derived key for each nonce | ||
Key(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error) | ||
} | ||
|
||
var _ Session = (*session)(nil) | ||
|
||
type session struct { | ||
key *ecdsa.PrivateKey | ||
} | ||
|
||
func (s *session) Key(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error) { | ||
x, _ := publicKey.Curve.ScalarMult(publicKey.X, publicKey.Y, s.key.D.Bytes()) | ||
if x == nil { | ||
return nil, errors.New("shared secret is point at infinity") | ||
} | ||
|
||
keys := make([][]byte, len(nonces)) | ||
for _, nonce := range nonces { | ||
key, err := crypto.LegacyKeccak256(append(x.Bytes(), nonce...)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
keys = append(keys, key) | ||
} | ||
|
||
return keys, nil | ||
} | ||
|
||
func NewDefaultSession(key *ecdsa.PrivateKey) Session { | ||
return &session{ | ||
key: key, | ||
} | ||
} | ||
|
||
func NewFromKeystore(ks keystore.Service, tag, password string) Session { | ||
return nil | ||
} |
Oops, something went wrong.