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
da42c385
Commit
da42c385
authored
Jun 28, 2019
by
dignifiedquire
Committed by
Steven Allen
Aug 01, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: do not allocate when comparing keys
parent
3390f7d1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
13 deletions
+72
-13
crypto/key.go
crypto/key.go
+5
-3
crypto/key_test.go
crypto/key_test.go
+22
-8
crypto/rsa_go.go
crypto/rsa_go.go
+45
-2
No files found.
crypto/key.go
View file @
da42c385
...
...
@@ -363,7 +363,9 @@ func KeyEqual(k1, k2 Key) bool {
return
true
}
b1
,
err1
:=
k1
.
Bytes
()
b2
,
err2
:=
k2
.
Bytes
()
return
subtle
.
ConstantTimeCompare
(
b1
,
b2
)
==
1
&&
err1
==
err2
if
k1
.
Type
()
!=
k2
.
Type
()
{
return
false
}
return
k1
.
Equals
(
k2
)
}
crypto/key_test.go
View file @
da42c385
...
...
@@ -101,18 +101,19 @@ func testKeyEncoding(t *testing.T, sk PrivKey) {
}
func
testKeyEquals
(
t
*
testing
.
T
,
k
Key
)
{
kb
,
err
:=
k
.
Bytes
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
//
kb, err := k.
Raw
()
//
if err != nil {
//
t.Fatal(err)
//
}
if
!
KeyEqual
(
k
,
k
)
{
t
.
Fatal
(
"Key not equal to itself."
)
}
if
!
KeyEqual
(
k
,
testkey
(
kb
))
{
t
.
Fatal
(
"Key not equal to key with same bytes."
)
}
// bad test, relies on deep internals..
// if !KeyEqual(k, testkey(kb)) {
// t.Fatal("Key not equal to key with same bytes.")
// }
sk
,
pk
,
err
:=
test
.
RandTestKeyPair
(
RSA
,
512
)
if
err
!=
nil
{
...
...
@@ -143,7 +144,20 @@ func (pk testkey) Raw() ([]byte, error) {
}
func
(
pk
testkey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
pk
,
k
)
if
pk
.
Type
()
!=
k
.
Type
()
{
return
false
}
a
,
err
:=
pk
.
Raw
()
if
err
!=
nil
{
return
false
}
b
,
err
:=
k
.
Raw
()
if
err
!=
nil
{
return
false
}
return
bytes
.
Equal
(
a
,
b
)
}
func
TestUnknownCurveErrors
(
t
*
testing
.
T
)
{
...
...
crypto/rsa_go.go
View file @
da42c385
...
...
@@ -3,6 +3,7 @@
package
crypto
import
(
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
...
...
@@ -63,7 +64,21 @@ func (pk *RsaPublicKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func
(
pk
*
RsaPublicKey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
pk
,
k
)
// make sure this is an rsa public key
other
,
ok
:=
(
k
)
.
(
*
RsaPublicKey
)
if
!
ok
{
a
,
err
:=
pk
.
Raw
()
if
err
!=
nil
{
return
false
}
b
,
err
:=
k
.
Raw
()
if
err
!=
nil
{
return
false
}
return
bytes
.
Equal
(
a
,
b
)
}
return
pk
.
k
.
N
.
Cmp
(
other
.
k
.
N
)
==
0
&&
pk
.
k
.
E
==
other
.
k
.
E
}
// Sign returns a signature of the input data
...
...
@@ -93,7 +108,35 @@ func (sk *RsaPrivateKey) Raw() ([]byte, error) {
// Equals checks whether this key is equal to another
func
(
sk
*
RsaPrivateKey
)
Equals
(
k
Key
)
bool
{
return
KeyEqual
(
sk
,
k
)
// make sure this is an rsa public key
other
,
ok
:=
(
k
)
.
(
*
RsaPrivateKey
)
if
!
ok
{
a
,
err
:=
sk
.
Raw
()
if
err
!=
nil
{
return
false
}
b
,
err
:=
k
.
Raw
()
if
err
!=
nil
{
return
false
}
return
bytes
.
Equal
(
a
,
b
)
}
a
:=
sk
.
sk
b
:=
other
.
sk
if
a
.
PublicKey
.
N
.
Cmp
(
b
.
PublicKey
.
N
)
!=
0
{
return
false
}
if
a
.
PublicKey
.
E
!=
b
.
PublicKey
.
E
{
return
false
}
if
a
.
D
.
Cmp
(
b
.
D
)
!=
0
{
return
false
}
return
true
}
// UnmarshalRsaPrivateKey returns a private key from the input x509 bytes
...
...
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