Skip to content
Snippets Groups Projects
Commit 370e7ae2 authored by Josh Brooks's avatar Josh Brooks
Browse files

Refactor Sign/VerifyWithTimestamp to pass in timestamp int64 (unixNano)

parent b1407d70
No related branches found
No related tags found
1 merge request!6Release
...@@ -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,
ts time.Time, userPubKeyPem string) ([]byte, error) { timestampNano 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(), ts, userPubKeyPem) hashedData := digest(options.Hash.New(), timestampNano, 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,
ts time.Time, userPubKeyPem string) error { timestampNano 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(), ts, userPubKeyPem) hashedData := digest(options.Hash.New(), timestampNano, 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(ts.UnixNano())) binary.BigEndian.PutUint64(tsBytes, uint64(timestampNano))
// Hash the data and verify // Hash the data and verify
h.Write(tsBytes) h.Write(tsBytes)
......
...@@ -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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment