forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request perlin-network#212 from perlin-network/signature-s…
…cheme Separate out signature schemes from identity keypairs.
- Loading branch information
Showing
12 changed files
with
170 additions
and
161 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package eddsa | ||
|
||
import ( | ||
"github.com/perlin-network/noise/internal/edwards25519" | ||
"github.com/perlin-network/noise/signature" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var _ signature.Scheme = (*policy)(nil) | ||
|
||
type policy struct{} | ||
|
||
func (p policy) Sign(privateKey, messageBuf []byte) ([]byte, error) { | ||
return Sign(privateKey, messageBuf) | ||
} | ||
|
||
func (p policy) Verify(publicKeyBuf, messageBuf, signatureBuf []byte) error { | ||
return Verify(publicKeyBuf, messageBuf, signatureBuf) | ||
} | ||
|
||
func New() *policy { | ||
return new(policy) | ||
} | ||
|
||
func Sign(privateKeyBuf, messageBuf []byte) ([]byte, error) { | ||
if len(privateKeyBuf) != edwards25519.PrivateKeySize { | ||
return nil, errors.Errorf("edwards25519: private key expected to be %d bytes, but is %d bytes", edwards25519.PrivateKeySize, len(privateKeyBuf)) | ||
} | ||
|
||
return edwards25519.Sign(privateKeyBuf, messageBuf), nil | ||
} | ||
|
||
func Verify(publicKeyBuf, messageBuf, signature []byte) error { | ||
if len(publicKeyBuf) != edwards25519.PublicKeySize { | ||
return errors.Errorf("edwards25519: public key expected to be %d bytes, but is %d bytes", edwards25519.PublicKeySize, len(publicKeyBuf)) | ||
} | ||
|
||
if edwards25519.Verify(publicKeyBuf, messageBuf, signature) { | ||
return nil | ||
} else { | ||
return errors.New("unable to verify signature") | ||
} | ||
} |
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,71 @@ | ||
package eddsa | ||
|
||
import ( | ||
"bytes" | ||
"github.com/perlin-network/noise/internal/edwards25519" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
func TestBadKey(t *testing.T) { | ||
badKey := []byte("this is a bad key") | ||
message := []byte("this is a message") | ||
|
||
scheme := New() | ||
_, err := scheme.Sign(badKey, message) | ||
assert.Error(t, err) | ||
|
||
_, err = Sign(badKey, message) | ||
assert.Error(t, err) | ||
|
||
err = scheme.Verify(badKey, message, []byte("random signature")) | ||
assert.Error(t, err) | ||
|
||
err = Verify(badKey, message, []byte("random signature")) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestSignAndVerify(t *testing.T) { | ||
scheme := New() | ||
|
||
quick.Check(func(message []byte, bad []byte) bool { | ||
publicKey, privateKey, err := edwards25519.GenerateKey(nil) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
sign1, err := scheme.Sign(privateKey, message) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
sign2, err := Sign(privateKey, message) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
if !bytes.Equal(sign1, sign2) { | ||
return false | ||
} | ||
|
||
if scheme.Verify(publicKey, message, sign2) != nil { | ||
return false | ||
} | ||
|
||
if Verify(publicKey, message, sign1) != nil { | ||
return false | ||
} | ||
|
||
// Check invalid signature | ||
if scheme.Verify(publicKey, message, append(sign2, bad...)) != nil { | ||
return false | ||
} | ||
|
||
if Verify(publicKey, message, append(sign1, bad...)) != nil { | ||
return false | ||
} | ||
|
||
return true | ||
}, nil) | ||
} |
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,6 @@ | ||
package signature | ||
|
||
type Scheme interface { | ||
Sign(privateKey, messageBuf []byte) ([]byte, error) | ||
Verify(publicKeyBuf, messageBuf, signatureBuf []byte) error | ||
} |
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
Oops, something went wrong.