This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
add openssl support #61
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test_data/** binary |
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,132 @@ | ||
package crypto_test | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
crypto "github.com/libp2p/go-libp2p-crypto" | ||
crypto_pb "github.com/libp2p/go-libp2p-crypto/pb" | ||
) | ||
|
||
var message = []byte("Libp2p is the _best_!") | ||
|
||
type testCase struct { | ||
keyType crypto_pb.KeyType | ||
gen func(i io.Reader) (crypto.PrivKey, crypto.PubKey, error) | ||
sigDeterministic bool | ||
} | ||
|
||
var keyTypes = []testCase{ | ||
{ | ||
keyType: crypto_pb.KeyType_ECDSA, | ||
gen: crypto.GenerateECDSAKeyPair, | ||
}, | ||
{ | ||
keyType: crypto_pb.KeyType_Secp256k1, | ||
sigDeterministic: true, | ||
gen: crypto.GenerateSecp256k1Key, | ||
}, | ||
{ | ||
keyType: crypto_pb.KeyType_RSA, | ||
sigDeterministic: true, | ||
gen: func(i io.Reader) (crypto.PrivKey, crypto.PubKey, error) { | ||
return crypto.GenerateRSAKeyPair(2048, i) | ||
}, | ||
}, | ||
} | ||
|
||
func fname(kt crypto_pb.KeyType, ext string) string { | ||
return fmt.Sprintf("test_data/%d.%s", kt, ext) | ||
} | ||
|
||
func TestFixtures(t *testing.T) { | ||
for _, tc := range keyTypes { | ||
t.Run(tc.keyType.String(), func(t *testing.T) { | ||
pubBytes, err := ioutil.ReadFile(fname(tc.keyType, "pub")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
privBytes, err := ioutil.ReadFile(fname(tc.keyType, "priv")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
sigBytes, err := ioutil.ReadFile(fname(tc.keyType, "sig")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
pub, err := crypto.UnmarshalPublicKey(pubBytes) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
pubBytes2, err := pub.Bytes() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(pubBytes2, pubBytes) { | ||
t.Fatal("encoding round-trip failed") | ||
} | ||
priv, err := crypto.UnmarshalPrivateKey(privBytes) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
privBytes2, err := priv.Bytes() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(privBytes2, privBytes) { | ||
t.Fatal("encoding round-trip failed") | ||
} | ||
ok, err := pub.Verify(message, sigBytes) | ||
if !ok || err != nil { | ||
t.Fatal("failed to validate signature with public key") | ||
} | ||
|
||
if tc.sigDeterministic { | ||
sigBytes2, err := priv.Sign(message) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(sigBytes2, sigBytes) { | ||
t.Fatal("signature not deterministic") | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func init() { | ||
// set to true to re-generate test data | ||
if false { | ||
generate() | ||
panic("generated") | ||
} | ||
} | ||
|
||
// generate re-generates test data | ||
func generate() { | ||
for _, tc := range keyTypes { | ||
priv, pub, err := tc.gen(rand.Reader) | ||
if err != nil { | ||
panic(err) | ||
} | ||
pubb, err := pub.Bytes() | ||
if err != nil { | ||
panic(err) | ||
} | ||
privb, err := priv.Bytes() | ||
if err != nil { | ||
panic(err) | ||
} | ||
sig, err := priv.Sign(message) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ioutil.WriteFile(fname(tc.keyType, "pub"), pubb, 0666) | ||
ioutil.WriteFile(fname(tc.keyType, "priv"), privb, 0666) | ||
ioutil.WriteFile(fname(tc.keyType, "sig"), sig, 0666) | ||
} | ||
} |
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,98 @@ | ||
// +build openssl | ||
|
||
package crypto | ||
|
||
import ( | ||
pb "github.com/libp2p/go-libp2p-crypto/pb" | ||
|
||
openssl "github.com/spacemonkeygo/openssl" | ||
) | ||
|
||
// define these as separate types so we can add more key types later and reuse | ||
// code. | ||
|
||
type opensslPublicKey struct { | ||
key openssl.PublicKey | ||
} | ||
|
||
type opensslPrivateKey struct { | ||
key openssl.PrivateKey | ||
} | ||
|
||
func unmarshalOpensslPrivateKey(b []byte) (opensslPrivateKey, error) { | ||
sk, err := openssl.LoadPrivateKeyFromDER(b) | ||
if err != nil { | ||
return opensslPrivateKey{}, err | ||
} | ||
return opensslPrivateKey{sk}, nil | ||
} | ||
|
||
func unmarshalOpensslPublicKey(b []byte) (opensslPublicKey, error) { | ||
sk, err := openssl.LoadPublicKeyFromDER(b) | ||
if err != nil { | ||
return opensslPublicKey{}, err | ||
} | ||
return opensslPublicKey{sk}, nil | ||
} | ||
|
||
// Verify compares a signature against input data | ||
func (pk *opensslPublicKey) Verify(data, sig []byte) (bool, error) { | ||
err := pk.key.VerifyPKCS1v15(openssl.SHA256_Method, data, sig) | ||
return err == nil, err | ||
} | ||
|
||
func (pk *opensslPublicKey) Type() pb.KeyType { | ||
switch pk.key.KeyType() { | ||
case openssl.KeyTypeRSA: | ||
return pb.KeyType_RSA | ||
default: | ||
return -1 | ||
} | ||
} | ||
|
||
// Bytes returns protobuf bytes of a public key | ||
func (pk *opensslPublicKey) Bytes() ([]byte, error) { | ||
return MarshalPublicKey(pk) | ||
} | ||
|
||
func (pk *opensslPublicKey) Raw() ([]byte, error) { | ||
return pk.key.MarshalPKIXPublicKeyDER() | ||
} | ||
|
||
// Equals checks whether this key is equal to another | ||
func (pk *opensslPublicKey) Equals(k Key) bool { | ||
return KeyEqual(pk, k) | ||
} | ||
|
||
// Sign returns a signature of the input data | ||
func (sk *opensslPrivateKey) Sign(message []byte) ([]byte, error) { | ||
return sk.key.SignPKCS1v15(openssl.SHA256_Method, message) | ||
} | ||
|
||
// GetPublic returns a public key | ||
func (sk *opensslPrivateKey) GetPublic() PubKey { | ||
return &opensslPublicKey{sk.key} | ||
} | ||
|
||
func (sk *opensslPrivateKey) Type() pb.KeyType { | ||
switch sk.key.KeyType() { | ||
case openssl.KeyTypeRSA: | ||
return pb.KeyType_RSA | ||
default: | ||
return -1 | ||
} | ||
} | ||
|
||
// Bytes returns protobuf bytes from a private key | ||
func (sk *opensslPrivateKey) Bytes() ([]byte, error) { | ||
return MarshalPrivateKey(sk) | ||
} | ||
|
||
func (sk *opensslPrivateKey) Raw() ([]byte, error) { | ||
return sk.key.MarshalPKCS1PrivateKeyDER() | ||
} | ||
|
||
// Equals checks whether this key is equal to another | ||
func (sk *opensslPrivateKey) Equals(k Key) bool { | ||
return KeyEqual(sk, k) | ||
} |
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,10 @@ | ||
package crypto | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// ErrRsaKeyTooSmall is returned when trying to generate or parse an RSA key | ||
// that's smaller than 512 bits. Keys need to be larger enough to sign a 256bit | ||
// hash so this is a reasonable absolute minimum. | ||
var ErrRsaKeyTooSmall = errors.New("rsa keys must be >= 512 bits to be useful") |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unfortunately, RSA is the only place where we use the PKCS1v15 signature format, as far as I can tell and I can't find an easy way to get something else from this openssl library.