Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
crypto
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
crypto
Commits
b1407d70
Commit
b1407d70
authored
May 6, 2021
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Refactor Sign/VerifyWithTimestamp to accept PEM string of userPubKey
parent
21898bb1
No related branches found
No related tags found
1 merge request
!6
Release
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
registration/timestamp.go
+7
-9
7 additions, 9 deletions
registration/timestamp.go
registration/timestamp_test.go
+5
-4
5 additions, 4 deletions
registration/timestamp_test.go
with
12 additions
and
13 deletions
registration/timestamp.go
+
7
−
9
View file @
b1407d70
...
...
@@ -9,7 +9,6 @@ package registration
import
(
"encoding/binary"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/crypto/xx"
"hash"
"io"
"time"
...
...
@@ -19,13 +18,13 @@ import (
// This is used to verify that a user has registered with the network at a specific data and time
// SignWithTimestamp signs a hash of the timestamp and the user's public key
func
SignWithTimestamp
(
rand
io
.
Reader
,
priv
*
rsa
.
PrivateKey
,
ts
time
.
Time
,
userPubKey
*
rsa
.
PublicKey
)
([]
byte
,
error
)
{
func
SignWithTimestamp
(
rand
io
.
Reader
,
priv
*
rsa
.
PrivateKey
,
ts
time
.
Time
,
userPubKeyPem
string
)
([]
byte
,
error
)
{
// Construct the hash
options
:=
rsa
.
NewDefaultOptions
()
// Digest the timestamp and public key
hashedData
:=
digest
(
options
.
Hash
.
New
(),
ts
,
userPubKey
)
hashedData
:=
digest
(
options
.
Hash
.
New
(),
ts
,
userPubKey
Pem
)
// Sign the data
return
rsa
.
Sign
(
rand
,
priv
,
options
.
Hash
,
hashedData
,
options
)
...
...
@@ -34,12 +33,12 @@ func SignWithTimestamp(rand io.Reader, priv *rsa.PrivateKey, ts time.Time,
// VerifyWithTimestamp verifies the signature provided against serverPubKey and the
// digest of the timestamp ts and userPubKey
func
VerifyWithTimestamp
(
sig
[]
byte
,
serverPubKey
*
rsa
.
PublicKey
,
ts
time
.
Time
,
userPubKey
*
rsa
.
PublicKey
)
error
{
ts
time
.
Time
,
userPubKey
Pem
string
)
error
{
// Construct the hash
options
:=
rsa
.
NewDefaultOptions
()
// Digest the timestamp and public key
hashedData
:=
digest
(
options
.
Hash
.
New
(),
ts
,
userPubKey
)
hashedData
:=
digest
(
options
.
Hash
.
New
(),
ts
,
userPubKey
Pem
)
// Verify the signature
return
rsa
.
Verify
(
serverPubKey
,
options
.
Hash
,
hashedData
,
sig
,
options
)
...
...
@@ -47,9 +46,8 @@ func VerifyWithTimestamp(sig []byte, serverPubKey *rsa.PublicKey,
// digest is a helper function which digests the timestamp ts and
// rsa.PublicKey userPubKey given hash h
func
digest
(
h
hash
.
Hash
,
ts
time
.
Time
,
userPubKey
*
rsa
.
PublicKey
)
[]
byte
{
func
digest
(
h
hash
.
Hash
,
ts
time
.
Time
,
userPubKey
Pem
string
)
[]
byte
{
// Serialize the public key
pubKeyBytes
:=
xx
.
PublicKeyBytes
(
&
(
*
userPubKey
)
.
PublicKey
)
// Serialize the timestamp
tsBytes
:=
make
([]
byte
,
8
)
...
...
@@ -57,7 +55,7 @@ func digest(h hash.Hash, ts time.Time, userPubKey *rsa.PublicKey) []byte {
// Hash the data and verify
h
.
Write
(
tsBytes
)
h
.
Write
(
pubKeyBytes
)
h
.
Write
(
[]
byte
(
userPubKeyPem
)
)
return
h
.
Sum
(
nil
)
}
This diff is collapsed.
Click to expand it.
registration/timestamp_test.go
+
5
−
4
View file @
b1407d70
...
...
@@ -122,7 +122,8 @@ func TestSignVerify(t *testing.T) {
}
// Sign data
sig
,
err
:=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userPrivKey
.
GetPublic
())
userPubKeyPem
:=
string
(
rsa
.
CreatePublicKeyPem
(
userPrivKey
.
GetPublic
()))
sig
,
err
:=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userPubKeyPem
)
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
"Could not sign data: %v"
,
err
.
Error
())
...
...
@@ -136,7 +137,7 @@ func TestSignVerify(t *testing.T) {
}
// Test the verification
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userP
rivKey
.
GetPublic
()
)
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userP
ubKeyPem
)
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
"Could not verify signature: %v"
,
err
.
Error
())
...
...
@@ -156,14 +157,14 @@ func TestSignVerify(t *testing.T) {
"Could not generate key: %v"
,
err
.
Error
())
}
sig
,
err
=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userP
rivKey
.
GetPublic
()
)
sig
,
err
=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userP
ubKeyPem
)
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
"Could not sign data: %v"
,
err
.
Error
())
}
// Test the verification
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userP
rivKey
.
GetPublic
()
)
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userP
ubKeyPem
)
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
"Could not verify signature: %v"
,
err
.
Error
())
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment