Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-core
Commits
316b0af7
Unverified
Commit
316b0af7
authored
Sep 19, 2019
by
adam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
incorporates code review from @Stebalien
parent
0468ff83
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
43 deletions
+88
-43
crypto/key.go
crypto/key.go
+1
-43
crypto/key_not_openssl.go
crypto/key_not_openssl.go
+40
-0
crypto/key_openssl.go
crypto/key_openssl.go
+47
-0
No files found.
crypto/key.go
View file @
316b0af7
//
P
ackage crypto implements various cryptographic utilities used by libp2p.
//
C
ackage crypto implements various cryptographic utilities used by libp2p.
// This includes a Public and Private key interface and key implementations
// for supported key algorithms.
package
crypto
import
(
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha512"
"crypto/subtle"
...
...
@@ -18,14 +15,11 @@ import (
"fmt"
"hash"
"io"
"log"
pb
"github.com/libp2p/go-libp2p-core/crypto/pb"
btcec
"github.com/btcsuite/btcd/btcec"
"github.com/gogo/protobuf/proto"
sha256
"github.com/minio/sha256-simd"
"golang.org/x/crypto/ed25519"
)
const
(
...
...
@@ -180,42 +174,6 @@ func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) {
return
pubKey
,
done
,
nil
}
// KeyPairFromKey generates a new private and public key from an input private key
func
KeyPairFromKey
(
priv
crypto
.
PrivateKey
)
(
PrivKey
,
PubKey
,
error
)
{
if
priv
==
nil
{
return
nil
,
nil
,
ErrNilPrivateKey
}
switch
priv
.
(
type
)
{
case
*
rsa
.
PrivateKey
:
log
.
Println
(
"1"
)
rPriv
,
_
:=
priv
.
(
*
rsa
.
PrivateKey
)
return
&
RsaPrivateKey
{
*
rPriv
},
&
RsaPublicKey
{
rPriv
.
PublicKey
},
nil
case
*
ecdsa
.
PrivateKey
:
log
.
Println
(
"2"
)
ePriv
,
_
:=
priv
.
(
*
ecdsa
.
PrivateKey
)
return
&
ECDSAPrivateKey
{
ePriv
},
&
ECDSAPublicKey
{
&
ePriv
.
PublicKey
},
nil
case
*
ed25519
.
PrivateKey
:
log
.
Println
(
"3"
)
ePriv
,
_
:=
priv
.
(
*
ed25519
.
PrivateKey
)
pubIfc
:=
ePriv
.
Public
()
pub
,
_
:=
pubIfc
.
(
ed25519
.
PublicKey
)
return
&
Ed25519PrivateKey
{
*
ePriv
},
&
Ed25519PublicKey
{
pub
},
nil
case
*
btcec
.
PrivateKey
:
log
.
Println
(
"4"
)
bPriv
,
_
:=
priv
.
(
*
btcec
.
PrivateKey
)
sPriv
:=
Secp256k1PrivateKey
(
*
bPriv
)
sPub
:=
Secp256k1PublicKey
(
*
bPriv
.
PubKey
())
return
&
sPriv
,
&
sPub
,
nil
default
:
return
nil
,
nil
,
ErrBadKeyType
}
}
// StretchedKeys ...
type
StretchedKeys
struct
{
IV
[]
byte
...
...
crypto/key_not_openssl.go
0 → 100644
View file @
316b0af7
// +build !openssl
package
crypto
import
(
"crypto"
"crypto/ecdsa"
"crypto/rsa"
btcec
"github.com/btcsuite/btcd/btcec"
"golang.org/x/crypto/ed25519"
)
// KeyPairFromKey generates a new private and public key from an input private key
func
KeyPairFromKey
(
priv
crypto
.
PrivateKey
)
(
PrivKey
,
PubKey
,
error
)
{
if
priv
==
nil
{
return
nil
,
nil
,
ErrNilPrivateKey
}
switch
p
:=
priv
.
(
type
)
{
case
*
rsa
.
PrivateKey
:
return
&
RsaPrivateKey
{
*
p
},
&
RsaPublicKey
{
p
.
PublicKey
},
nil
case
*
ecdsa
.
PrivateKey
:
return
&
ECDSAPrivateKey
{
p
},
&
ECDSAPublicKey
{
&
p
.
PublicKey
},
nil
case
*
ed25519
.
PrivateKey
:
pubIfc
:=
p
.
Public
()
pub
,
_
:=
pubIfc
.
(
ed25519
.
PublicKey
)
return
&
Ed25519PrivateKey
{
*
p
},
&
Ed25519PublicKey
{
pub
},
nil
case
*
btcec
.
PrivateKey
:
sPriv
:=
Secp256k1PrivateKey
(
*
p
)
sPub
:=
Secp256k1PublicKey
(
*
p
.
PubKey
())
return
&
sPriv
,
&
sPub
,
nil
default
:
return
nil
,
nil
,
ErrBadKeyType
}
}
crypto/key_openssl.go
0 → 100644
View file @
316b0af7
// +build openssl
package
crypto
import
(
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
btcec
"github.com/btcsuite/btcd/btcec"
openssl
"github.com/libp2p/go-openssl"
"golang.org/x/crypto/ed25519"
)
// KeyPairFromKey generates a new private and public key from an input private key
func
KeyPairFromKey
(
priv
crypto
.
PrivateKey
)
(
PrivKey
,
PubKey
,
error
)
{
if
priv
==
nil
{
return
nil
,
nil
,
ErrNilPrivateKey
}
switch
p
:=
priv
.
(
type
)
{
case
*
rsa
.
PrivateKey
:
pk
,
err
:=
openssl
.
LoadPrivateKeyFromDER
(
x509
.
MarshalPKCS1PrivateKey
(
p
))
if
err
!=
nil
{
return
nil
,
nil
,
err
}
return
&
opensslPrivateKey
{
pk
},
&
opensslPublicKey
{
pk
},
nil
case
*
ecdsa
.
PrivateKey
:
return
&
ECDSAPrivateKey
{
p
},
&
ECDSAPublicKey
{
&
p
.
PublicKey
},
nil
case
*
ed25519
.
PrivateKey
:
pubIfc
:=
p
.
Public
()
pub
,
_
:=
pubIfc
.
(
ed25519
.
PublicKey
)
return
&
Ed25519PrivateKey
{
*
p
},
&
Ed25519PublicKey
{
pub
},
nil
case
*
btcec
.
PrivateKey
:
sPriv
:=
Secp256k1PrivateKey
(
*
p
)
sPub
:=
Secp256k1PublicKey
(
*
p
.
PubKey
())
return
&
sPriv
,
&
sPub
,
nil
default
:
return
nil
,
nil
,
ErrBadKeyType
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment