-
Notifications
You must be signed in to change notification settings - Fork 104
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
Darc ECDSA #2484
base: darc_identity_test
Are you sure you want to change the base?
Darc ECDSA #2484
Changes from 5 commits
0e73fd2
596a5c7
5c21329
3632b20
4a85f31
ec6cdb2
b63fc6f
2d78896
fdfc3eb
b67548c
a06494a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,7 @@ package darc | |||||||||||||
import ( | ||||||||||||||
"bytes" | ||||||||||||||
"crypto/ecdsa" | ||||||||||||||
"crypto/elliptic" | ||||||||||||||
"crypto/sha256" | ||||||||||||||
"crypto/sha512" | ||||||||||||||
"crypto/x509" | ||||||||||||||
|
@@ -773,6 +774,8 @@ func (s Signer) Type() int { | |||||||||||||
return 3 | ||||||||||||||
case s.EvmContract != nil: | ||||||||||||||
return 4 | ||||||||||||||
case s.ECDSA != nil: | ||||||||||||||
return 5 | ||||||||||||||
default: | ||||||||||||||
return -1 | ||||||||||||||
} | ||||||||||||||
|
@@ -790,6 +793,8 @@ func (s Signer) Identity() Identity { | |||||||||||||
return NewIdentityProxy(s.Proxy) | ||||||||||||||
case 4: | ||||||||||||||
return NewIdentityEvmContract(s.EvmContract) | ||||||||||||||
case 5: | ||||||||||||||
return NewIdentityECDSA(s.ECDSA.PublicKey) | ||||||||||||||
default: | ||||||||||||||
return Identity{} | ||||||||||||||
} | ||||||||||||||
|
@@ -863,6 +868,8 @@ func (id Identity) Type() int { | |||||||||||||
return 3 | ||||||||||||||
case id.EvmContract != nil: | ||||||||||||||
return 4 | ||||||||||||||
case id.ECDSA != nil: | ||||||||||||||
return 5 | ||||||||||||||
} | ||||||||||||||
return -1 | ||||||||||||||
} | ||||||||||||||
|
@@ -937,6 +944,8 @@ func (id Identity) Verify(msg, sig []byte) error { | |||||||||||||
return id.Proxy.Verify(msg, sig) | ||||||||||||||
case 4: | ||||||||||||||
return id.EvmContract.Verify(msg, sig) | ||||||||||||||
case 5: | ||||||||||||||
return id.ECDSA.Verify(msg, sig) | ||||||||||||||
default: | ||||||||||||||
return errors.New("unknown identity") | ||||||||||||||
} | ||||||||||||||
|
@@ -964,6 +973,10 @@ func (id Identity) GetPublicBytes() []byte { | |||||||||||||
return buf | ||||||||||||||
case 4: | ||||||||||||||
return id.EvmContract.Address[:] | ||||||||||||||
case 5: | ||||||||||||||
buf := elliptic.Marshal(id.ECDSA.PublicKey.Curve, id.ECDSA.PublicKey.X, id.ECDSA.PublicKey.Y) | ||||||||||||||
//TODO: add error check here? | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of error check can you do here? The |
||||||||||||||
return buf | ||||||||||||||
default: | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
@@ -1012,6 +1025,25 @@ func NewIdentityX509EC(public []byte) Identity { | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// NewIdentityECDSA creates a new ECDSA identity struct given a public key | ||||||||||||||
func NewIdentityECDSA(publicKey ecdsa.PublicKey) Identity { | ||||||||||||||
return Identity{ | ||||||||||||||
ECDSA: &IdentityECDSA{ | ||||||||||||||
PublicKey: publicKey, | ||||||||||||||
}, | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
//TODO make calls to tsm available | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please resolve all TODOs. From what I understand, the |
||||||||||||||
func (ide IdentityECDSA) Verify(msg []byte, sig []byte) error { | ||||||||||||||
hashMsg := sha256.Sum256(msg) | ||||||||||||||
valid := ecdsa.VerifyASN1(&ide.PublicKey, hashMsg[:], sig) | ||||||||||||||
if !valid { | ||||||||||||||
return errors.New("Signature failed to verify") | ||||||||||||||
} | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// NewIdentityProxy creates a new OpenID Connect identity struct. | ||||||||||||||
func NewIdentityProxy(s *SignerProxy) Identity { | ||||||||||||||
return Identity{ | ||||||||||||||
|
@@ -1120,6 +1152,8 @@ func ParseIdentity(in string) (Identity, error) { | |||||||||||||
return parseIDProxy(fields[1]) | ||||||||||||||
case "evm_contract": | ||||||||||||||
return parseIDEvmContract(fields[1]) | ||||||||||||||
case "secp256k1": | ||||||||||||||
return parseIDECDSA(fields[1]) | ||||||||||||||
default: | ||||||||||||||
return Identity{}, fmt.Errorf("unknown identity type %v", fields[0]) | ||||||||||||||
} | ||||||||||||||
|
@@ -1142,6 +1176,25 @@ func parseIDX509ec(in string) (Identity, error) { | |||||||||||||
return Identity{X509EC: &IdentityX509EC{Public: id}}, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
//necessary function, needs to be refactored only supports elliptic.P256 curve | ||||||||||||||
//needs to be tested Unmarshal might not work | ||||||||||||||
Comment on lines
+1204
to
+1205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
func parseIDECDSA(in string) (Identity, error) { | ||||||||||||||
id := make([]byte, hex.DecodedLen(len(in))) | ||||||||||||||
_, err := hex.Decode(id, []byte(in)) | ||||||||||||||
Comment on lines
+1207
to
+1208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
x, y := elliptic.Unmarshal(elliptic.P256(), id) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
pubkey := ecdsa.PublicKey{ | ||||||||||||||
Curve: elliptic.P256(), | ||||||||||||||
X: x, | ||||||||||||||
Y: y, | ||||||||||||||
} | ||||||||||||||
if err != nil { | ||||||||||||||
return Identity{}, err | ||||||||||||||
} | ||||||||||||||
Comment on lines
+1217
to
+1219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Treat the error as close as possible to the source. Else it is very confusing. |
||||||||||||||
return Identity{ECDSA: &IdentityECDSA{PublicKey: pubkey}}, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func parseIDDarc(in string) (Identity, error) { | ||||||||||||||
id := make([]byte, hex.DecodedLen(len(in))) | ||||||||||||||
_, err := hex.Decode(id, []byte(in)) | ||||||||||||||
|
@@ -1447,6 +1500,22 @@ func (kcs SignerX509EC) Sign(msg []byte) ([]byte, error) { | |||||||||||||
return nil, errors.New("not yet implemented") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// new signer creates a signer only with a public key used to verify signatures | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
func NewSignerECDSA(public ecdsa.PublicKey) Signer { | ||||||||||||||
if public.X == nil { | ||||||||||||||
return Signer{} | ||||||||||||||
} | ||||||||||||||
return Signer{ECDSA: &SignerECDSA{ | ||||||||||||||
PublicKey: public, | ||||||||||||||
}} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
//TODO | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way to implement sign would be to give the necessary configuration when calling
Suggested change
|
||||||||||||||
func (kcs SignerECDSA) Sign(msg []byte) ([]byte, error) { | ||||||||||||||
//call tsm to sign | ||||||||||||||
return nil, errors.New("not yet implemented") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// NewSignerProxy creates a new SignerProxy. When Sign is called, the getSignature | ||||||||||||||
// callback will be called, so that the caller can use the appropriate mechanism | ||||||||||||||
// to retrieve and/or construct the signature. | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,12 @@ | ||||||||
package darc | ||||||||
|
||||||||
import ( | ||||||||
"crypto/ecdsa" | ||||||||
"crypto/elliptic" | ||||||||
"encoding/hex" | ||||||||
"errors" | ||||||||
"fmt" | ||||||||
"math/big" | ||||||||
"net/url" | ||||||||
"strings" | ||||||||
"testing" | ||||||||
|
@@ -750,17 +754,25 @@ func TestParseIdentity(t *testing.T) { | |||||||
} | ||||||||
|
||||||||
// Test any identity | ||||||||
func testIdentity(t *testing.T, sig Signer) { | ||||||||
msg := []byte("something secret") | ||||||||
signed, err := sig.Sign(msg) | ||||||||
require.NoError(t, err) | ||||||||
func testIdentity(t *testing.T, id Identity) { | ||||||||
msg := []byte(`Hello World`) | ||||||||
|
||||||||
//Signature from code example go-tsm-sdk corresponding to ecdsa public key example | ||||||||
signed, _ := hex.DecodeString("304402204f0b20a44efacec7b0514683233a79552026fe80e468078f6fed6cfe3f3e8a0402201eb12db7f6fe0828cafe8b0a032a37ff377b342799cfe77cfbac40c8ec1fa9e8") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
ALWAYS do error checking! You will save yourself a lot of pain! |
||||||||
|
||||||||
id := sig.Identity() | ||||||||
require.NoError(t, id.Verify(msg, signed)) | ||||||||
require.Error(t, id.Verify([]byte("wrong message"), signed)) | ||||||||
} | ||||||||
|
||||||||
// Test the different identities available - currently only Ed25519. | ||||||||
func TestIdentities(t *testing.T) { | ||||||||
testIdentity(t, NewSignerEd25519(nil, nil)) | ||||||||
//Ecdsa public key example | ||||||||
var x, _ = new(big.Int).SetString("25613385885653880697990944418179706546134037329992108968315147853972798913688", 10) | ||||||||
var y, _ = new(big.Int).SetString("74946767262888349555270609195205284686604880870734462312238891495596941025713", 10) | ||||||||
Comment on lines
+775
to
+776
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error checking needed. |
||||||||
pk := ecdsa.PublicKey{ | ||||||||
Curve: elliptic.P256(), | ||||||||
X: x, | ||||||||
Y: y, | ||||||||
} | ||||||||
testIdentity(t, NewIdentityECDSA(pk)) | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and everywhere else: we should call the
ECDSA
identityMPCECDSA
to indicate we're doing something special..