Skip to content

Commit

Permalink
Merge pull request #39 from it-chain/feature/auth
Browse files Browse the repository at this point in the history
authmanager deleted, hashmanager deleted, init added
  • Loading branch information
junbeomlee authored Apr 28, 2018
2 parents c5f579d + aa546ea commit 22a61e6
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 146 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,14 @@ priv, pub, err = keyManager.GetKey()

sampleData = []byte("This is the data will be transmitted.")

hashManager, err := hashing.NewHashManager()

// Convert raw data to hashed data by using SHA512 function
digest, err := hashManager.Hash(sampleData, nil, hashing.SHA512)

authManager, err := auth.NewAuth()
digest, err := hashing.Hash(sampleData, nil, hashing.SHA512)

// The option will be used in signing process of RSA-PSS algorithm for making EM(Encoded Message)
signerOpts := auth.EQUAL_SHA256.SignerOptsToPSSOptions()

// AuthManager make hashed-data(digest) to signature with the generated private key
signature, err := authManager.Sign(priv, digest, signerOpts)
// Auth make hashed-data(digest) to signature with the generated private key
signature, err := auth.Sign(priv, digest, signerOpts)

/* --------- After data transmitted --------- */
// Reconstruct public key from byte(PEM) formatted public key and store the key into keyManager
Expand All @@ -58,8 +54,8 @@ err := keyManager.ByteToKey(byteFormatPubKey, key.RSA4096, key.PUBLIC_KEY)
// Get the reconstructed public key
_, pub, err := keyManager.GetKey()

// AuthManager verify that received data has any forgery during transmitting process
ok, err := authManager.Verify(pub, signature, digest, signerOpts)
// Auth verify that received data has any forgery during transmitting process
ok, err := auth.Verify(pub, signature, digest, signerOpts)

fmt.println(ok) // true
```
Expand Down
16 changes: 0 additions & 16 deletions auth/auth.go

This file was deleted.

30 changes: 10 additions & 20 deletions auth/authImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,23 @@ import (
)

// authImpl contains signers and verifiers that is used for signing and verifying process.
type authImpl struct {
signers map[reflect.Type]signer
verifiers map[reflect.Type]verifier
}
var signers map[reflect.Type]signer
var verifiers map[reflect.Type]verifier

// NewAuth initialize the struct authImpl.
func NewAuth() (Auth, error) {
//initialize signer and verifiers
func init() {

signers := make(map[reflect.Type]signer)
signers = make(map[reflect.Type]signer)
signers[reflect.TypeOf(&key.RSAPrivateKey{})] = &RSASigner{}
signers[reflect.TypeOf(&key.ECDSAPrivateKey{})] = &ECDSASigner{}

verifiers := make(map[reflect.Type]verifier)
verifiers = make(map[reflect.Type]verifier)
verifiers[reflect.TypeOf(&key.RSAPublicKey{})] = &RSAVerifier{}
verifiers[reflect.TypeOf(&key.ECDSAPublicKey{})] = &ECDSAVerifier{}

ai := &authImpl{
signers: signers,
verifiers: verifiers,
}

return ai, nil

}

// Sign signs a digest(hash) using priKey(private key), and returns signature.
func (ai *authImpl) Sign(priKey key.Key, digest []byte, opts SignerOpts) ([]byte, error) {
func Sign(priKey key.Key, digest []byte, opts SignerOpts) ([]byte, error) {

var err error

Expand All @@ -48,7 +38,7 @@ func (ai *authImpl) Sign(priKey key.Key, digest []byte, opts SignerOpts) ([]byte
return nil, errors.New("Private key is not exist.")
}

signer, found := ai.signers[reflect.TypeOf(priKey)]
signer, found := signers[reflect.TypeOf(priKey)]
if !found {
return nil, errors.New("unsupported key type.")
}
Expand All @@ -63,7 +53,7 @@ func (ai *authImpl) Sign(priKey key.Key, digest []byte, opts SignerOpts) ([]byte
}

// Verify verifies the signature using pubKey(public key) and digest of original message, then returns boolean value.
func (ai *authImpl) Verify(pubKey key.Key, signature, digest []byte, opts SignerOpts) (bool, error) {
func Verify(pubKey key.Key, signature, digest []byte, opts SignerOpts) (bool, error) {

if pubKey == nil {
return false, errors.New("invalid key")
Expand All @@ -77,7 +67,7 @@ func (ai *authImpl) Verify(pubKey key.Key, signature, digest []byte, opts Signer
return false, errors.New("invalid digest")
}

verifier, found := ai.verifiers[reflect.TypeOf(pubKey)]
verifier, found := verifiers[reflect.TypeOf(pubKey)]
if !found {
return false, errors.New("unsupported key type")
}
Expand Down
71 changes: 28 additions & 43 deletions auth/authImpl_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package auth

import (
"testing"
"github.com/stretchr/testify/assert"
"crypto/rsa"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"github.com/it-chain/heimdall/key"
"crypto/rsa"
"crypto/sha512"
"crypto/elliptic"
"crypto/ecdsa"
)

func TestNewAuth(t *testing.T) {

authManager, err := NewAuth()
assert.NoError(t, err)
assert.NotNil(t, authManager)
"testing"

}
"github.com/it-chain/heimdall/key"
"github.com/stretchr/testify/assert"
)

func TestAuth_RSASignVerify(t *testing.T) {

Expand Down Expand Up @@ -50,47 +43,43 @@ func TestAuth_RSASignVerify(t *testing.T) {
hash.Write(rawData)
wrongDigest := hash.Sum(nil)

authManager, err := NewAuth()
assert.NoError(t, err)
assert.NotNil(t, authManager)

signature, err := authManager.Sign(pri, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
signature, err := Sign(pri, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.NoError(t, err)
assert.NotNil(t, signature)

diffSignature, err := authManager.Sign(diffPri, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
diffSignature, err := Sign(diffPri, digest, EQUAL_SHA512.SignerOptsToPSSOptions())

// normal case
ok, err := authManager.Verify(pub, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
ok, err := Verify(pub, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.NoError(t, err)
assert.True(t, ok)

// passing different signature case
_, err = authManager.Verify(pub, diffSignature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
_, err = Verify(pub, diffSignature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.Error(t, err)

// public key missing case
_, err = authManager.Verify(nil, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
_, err = Verify(nil, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.Error(t, err)

// passing different public key case
_, err = authManager.Verify(diffPub, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
_, err = Verify(diffPub, signature, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.Error(t, err)

// signature missing case
_, err = authManager.Verify(pub, nil, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
_, err = Verify(pub, nil, digest, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.Error(t, err)

// digest missing case
_, err = authManager.Verify(pub, signature, nil, EQUAL_SHA512.SignerOptsToPSSOptions())
_, err = Verify(pub, signature, nil, EQUAL_SHA512.SignerOptsToPSSOptions())
assert.Error(t, err)

// passing wrong digest case
_, err = authManager.Verify(pub, signature, wrongDigest, EQUAL_SHA256.SignerOptsToPSSOptions())
_, err = Verify(pub, signature, wrongDigest, EQUAL_SHA256.SignerOptsToPSSOptions())
assert.Error(t, err)

// passing wrong signer option case
ok, err = authManager.Verify(pub, signature, digest, EQUAL_SHA256.SignerOptsToPSSOptions())
ok, err = Verify(pub, signature, digest, EQUAL_SHA256.SignerOptsToPSSOptions())
assert.Error(t, err)
assert.False(t, ok)

Expand All @@ -104,7 +93,7 @@ func TestAuth_ECDSASignVerify(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, generatedKey)

pri := &key.ECDSAPrivateKey{PrivKey:generatedKey}
pri := &key.ECDSAPrivateKey{PrivKey: generatedKey}
assert.NotNil(t, pri)

pub, err := pri.PublicKey()
Expand All @@ -126,43 +115,39 @@ func TestAuth_ECDSASignVerify(t *testing.T) {
hash.Write(rawData)
wrongDigest := hash.Sum(nil)

authManager, err := NewAuth()
assert.NoError(t, err)
assert.NotNil(t, authManager)

signature, err := authManager.Sign(pri, digest, nil)
signature, err := Sign(pri, digest, nil)
assert.NoError(t, err)
assert.NotNil(t, signature)

diffSignature, err := authManager.Sign(diffPri, digest, nil)
diffSignature, err := Sign(diffPri, digest, nil)

// normal case
ok, err := authManager.Verify(pub, signature, digest, nil)
ok, err := Verify(pub, signature, digest, nil)
assert.NoError(t, err)
assert.True(t, ok)

// passing different signature case
_, err = authManager.Verify(pub, diffSignature, digest, nil)
_, err = Verify(pub, diffSignature, digest, nil)
assert.Error(t, err)

// public key missing case
_, err = authManager.Verify(nil, signature, digest, nil)
_, err = Verify(nil, signature, digest, nil)
assert.Error(t, err)

// passing different public key case
_, err = authManager.Verify(diffPub, signature, digest, nil)
_, err = Verify(diffPub, signature, digest, nil)
assert.Error(t, err)

// signature missing case
_, err = authManager.Verify(pub, nil, digest, nil)
_, err = Verify(pub, nil, digest, nil)
assert.Error(t, err)

// digest missing case
_, err = authManager.Verify(pub, signature, nil, nil)
_, err = Verify(pub, signature, nil, nil)
assert.Error(t, err)

// passing wrong digest case
_, err = authManager.Verify(pub, signature, wrongDigest, nil)
_, err = Verify(pub, signature, wrongDigest, nil)
assert.Error(t, err)

}
}
10 changes: 0 additions & 10 deletions hashing/hashManager.go

This file was deleted.

14 changes: 1 addition & 13 deletions hashing/hashManagerImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ import (
"hash"
)

// hashManagerImpl represents a instance of hashManager.
type hashManagerImpl struct{}

// NewHashManager initialize a hash manager.
func NewHashManager() (HashManager, error) {

hm := &hashManagerImpl{}

return hm, nil

}

// Hash hashes the input data.
func (hm *hashManagerImpl) Hash(data []byte, tail []byte, opts HashOpts) ([]byte, error) {
func Hash(data []byte, tail []byte, opts HashOpts) ([]byte, error) {

if data == nil {
return nil, errors.New("Data should not be NIL")
Expand Down
21 changes: 5 additions & 16 deletions hashing/hashManagerImpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,25 @@ package hashing

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewHashManager(t *testing.T) {

hashManager, err := NewHashManager()
assert.NoError(t, err)
assert.NotNil(t, hashManager)

}

func TestHashManager_Hash(t *testing.T) {

hashManager, err := NewHashManager()
assert.NoError(t, err)
assert.NotNil(t, hashManager)

rawData := []byte("This data will be hashed by hashManager")

// normal case
digest, err := hashManager.Hash(rawData, nil, SHA512)
digest, err := Hash(rawData, nil, SHA512)
assert.NoError(t, err)
assert.NotNil(t, digest)

// compare between hashed data by the same hash function
anotherDigest, err := hashManager.Hash(rawData, nil, SHA512)
anotherDigest, err := Hash(rawData, nil, SHA512)
assert.Equal(t, digest, anotherDigest)

// compare between hashed data by the different hash function
anotherDigest, err = hashManager.Hash(rawData, nil, SHA256)
anotherDigest, err = Hash(rawData, nil, SHA256)
assert.NotEqual(t, digest, anotherDigest)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,22 @@ func main() {

sampleData := []byte("This is sample data from heimdall.")

hashManager, err := hashing.NewHashManager()
errorCheck(err)

// Convert raw data to digest(hash value) by using SHA512 function.
digest, err := hashManager.Hash(sampleData, nil, hashing.SHA512)
errorCheck(err)

authManager, err := auth.NewAuth()
digest, err := hashing.Hash(sampleData, nil, hashing.SHA512)
errorCheck(err)

// AuthManager makes digest(hash value) to signature with private key.
signature, err := authManager.Sign(pri, digest, nil)
signature, err := auth.Sign(pri, digest, nil)
errorCheck(err)

/* --------- After data transmitted --------- */

// AuthManager verify that received data has any forgery during transmitting process by digest.
// and verify that the received data is surely from the expected sender by public key.
ok, err := authManager.Verify(pub, signature, digest, nil)
ok, err := auth.Verify(pub, signature, digest, nil)
errorCheck(err)

fmt.Println(ok)

}

func errorCheck(err error) {
Expand Down
Loading

0 comments on commit 22a61e6

Please sign in to comment.