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
370e7ae2
Commit
370e7ae2
authored
May 6, 2021
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Refactor Sign/VerifyWithTimestamp to pass in timestamp int64 (unixNano)
parent
b1407d70
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
+4
-4
4 additions, 4 deletions
registration/timestamp_test.go
with
11 additions
and
13 deletions
registration/timestamp.go
+
7
−
9
View file @
370e7ae2
...
@@ -11,7 +11,6 @@ import (
...
@@ -11,7 +11,6 @@ import (
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/crypto/signature/rsa"
"hash"
"hash"
"io"
"io"
"time"
)
)
// This file handles signature and verification logic of the timestamp for a user's verification.
// This file handles signature and verification logic of the timestamp for a user's verification.
...
@@ -19,12 +18,12 @@ import (
...
@@ -19,12 +18,12 @@ import (
// SignWithTimestamp signs a hash of the timestamp and the user's public key
// SignWithTimestamp signs a hash of the timestamp and the user's public key
func
SignWithTimestamp
(
rand
io
.
Reader
,
priv
*
rsa
.
PrivateKey
,
func
SignWithTimestamp
(
rand
io
.
Reader
,
priv
*
rsa
.
PrivateKey
,
t
s
time
.
Time
,
userPubKeyPem
string
)
([]
byte
,
error
)
{
t
imestampNano
int64
,
userPubKeyPem
string
)
([]
byte
,
error
)
{
// Construct the hash
// Construct the hash
options
:=
rsa
.
NewDefaultOptions
()
options
:=
rsa
.
NewDefaultOptions
()
// Digest the timestamp and public key
// Digest the timestamp and public key
hashedData
:=
digest
(
options
.
Hash
.
New
(),
t
s
,
userPubKeyPem
)
hashedData
:=
digest
(
options
.
Hash
.
New
(),
t
imestampNano
,
userPubKeyPem
)
// Sign the data
// Sign the data
return
rsa
.
Sign
(
rand
,
priv
,
options
.
Hash
,
hashedData
,
options
)
return
rsa
.
Sign
(
rand
,
priv
,
options
.
Hash
,
hashedData
,
options
)
...
@@ -32,13 +31,13 @@ func SignWithTimestamp(rand io.Reader, priv *rsa.PrivateKey,
...
@@ -32,13 +31,13 @@ func SignWithTimestamp(rand io.Reader, priv *rsa.PrivateKey,
// VerifyWithTimestamp verifies the signature provided against serverPubKey and the
// VerifyWithTimestamp verifies the signature provided against serverPubKey and the
// digest of the timestamp ts and userPubKey
// digest of the timestamp ts and userPubKey
func
VerifyWithTimestamp
(
sig
[]
byte
,
serverPubKey
*
rsa
.
PublicKey
,
func
VerifyWithTimestamp
(
serverPubKey
*
rsa
.
PublicKey
,
t
s
time
.
Time
,
userPubKeyPem
string
)
error
{
t
imestampNano
int64
,
userPubKeyPem
string
,
sig
[]
byte
)
error
{
// Construct the hash
// Construct the hash
options
:=
rsa
.
NewDefaultOptions
()
options
:=
rsa
.
NewDefaultOptions
()
// Digest the timestamp and public key
// Digest the timestamp and public key
hashedData
:=
digest
(
options
.
Hash
.
New
(),
t
s
,
userPubKeyPem
)
hashedData
:=
digest
(
options
.
Hash
.
New
(),
t
imestampNano
,
userPubKeyPem
)
// Verify the signature
// Verify the signature
return
rsa
.
Verify
(
serverPubKey
,
options
.
Hash
,
hashedData
,
sig
,
options
)
return
rsa
.
Verify
(
serverPubKey
,
options
.
Hash
,
hashedData
,
sig
,
options
)
...
@@ -46,12 +45,11 @@ func VerifyWithTimestamp(sig []byte, serverPubKey *rsa.PublicKey,
...
@@ -46,12 +45,11 @@ func VerifyWithTimestamp(sig []byte, serverPubKey *rsa.PublicKey,
// digest is a helper function which digests the timestamp ts and
// digest is a helper function which digests the timestamp ts and
// rsa.PublicKey userPubKey given hash h
// rsa.PublicKey userPubKey given hash h
func
digest
(
h
hash
.
Hash
,
ts
time
.
Time
,
userPubKeyPem
string
)
[]
byte
{
func
digest
(
h
hash
.
Hash
,
timestampNano
int64
,
userPubKeyPem
string
)
[]
byte
{
// Serialize the public key
// Serialize the timestamp
// Serialize the timestamp
tsBytes
:=
make
([]
byte
,
8
)
tsBytes
:=
make
([]
byte
,
8
)
binary
.
BigEndian
.
PutUint64
(
tsBytes
,
uint64
(
t
s
.
Unix
Nano
()
))
binary
.
BigEndian
.
PutUint64
(
tsBytes
,
uint64
(
t
imestamp
Nano
))
// Hash the data and verify
// Hash the data and verify
h
.
Write
(
tsBytes
)
h
.
Write
(
tsBytes
)
...
...
This diff is collapsed.
Click to expand it.
registration/timestamp_test.go
+
4
−
4
View file @
370e7ae2
...
@@ -123,7 +123,7 @@ func TestSignVerify(t *testing.T) {
...
@@ -123,7 +123,7 @@ func TestSignVerify(t *testing.T) {
// Sign data
// Sign data
userPubKeyPem
:=
string
(
rsa
.
CreatePublicKeyPem
(
userPrivKey
.
GetPublic
()))
userPubKeyPem
:=
string
(
rsa
.
CreatePublicKeyPem
(
userPrivKey
.
GetPublic
()))
sig
,
err
:=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userPubKeyPem
)
sig
,
err
:=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
.
UnixNano
()
,
userPubKeyPem
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
t
.
Fatalf
(
"SignVerify error: "
+
"Could not sign data: %v"
,
err
.
Error
())
"Could not sign data: %v"
,
err
.
Error
())
...
@@ -137,7 +137,7 @@ func TestSignVerify(t *testing.T) {
...
@@ -137,7 +137,7 @@ func TestSignVerify(t *testing.T) {
}
}
// Test the verification
// Test the verification
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userPubKeyPem
)
err
=
VerifyWithTimestamp
(
serverPrivKey
.
GetPublic
(),
testTime
.
UnixNano
()
,
userPubKeyPem
,
sig
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
t
.
Fatalf
(
"SignVerify error: "
+
"Could not verify signature: %v"
,
err
.
Error
())
"Could not verify signature: %v"
,
err
.
Error
())
...
@@ -157,14 +157,14 @@ func TestSignVerify(t *testing.T) {
...
@@ -157,14 +157,14 @@ func TestSignVerify(t *testing.T) {
"Could not generate key: %v"
,
err
.
Error
())
"Could not generate key: %v"
,
err
.
Error
())
}
}
sig
,
err
=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
,
userPubKeyPem
)
sig
,
err
=
SignWithTimestamp
(
notRand
,
serverPrivKey
,
testTime
.
UnixNano
()
,
userPubKeyPem
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
t
.
Fatalf
(
"SignVerify error: "
+
"Could not sign data: %v"
,
err
.
Error
())
"Could not sign data: %v"
,
err
.
Error
())
}
}
// Test the verification
// Test the verification
err
=
VerifyWithTimestamp
(
sig
,
serverPrivKey
.
GetPublic
(),
testTime
,
userPubKeyPem
)
err
=
VerifyWithTimestamp
(
serverPrivKey
.
GetPublic
(),
testTime
.
UnixNano
()
,
userPubKeyPem
,
sig
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"SignVerify error: "
+
t
.
Fatalf
(
"SignVerify error: "
+
"Could not verify signature: %v"
,
err
.
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
register
or
sign in
to comment