Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor + al test #28

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/dynamicaccess/accesslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var hashFunc = sha3.NewLegacyKeccak256
// Read-only interface for the ACT
type Decryptor interface {
// DecryptRef will return a decrypted reference, for given encrypted reference and grantee
DecryptRef(storage kvs.KeyValueStore, encryped_ref swarm.Address, publisher *ecdsa.PublicKey) (swarm.Address, error)
DecryptRef(storage kvs.KeyValueStore, encrypedRef swarm.Address, publisher *ecdsa.PublicKey) (swarm.Address, error)
ottpeter marked this conversation as resolved.
Show resolved Hide resolved
// Embedding the Session interface
Session
}
Expand All @@ -26,14 +26,14 @@ type Control interface {
// Adds a new grantee to the ACT
AddGrantee(storage kvs.KeyValueStore, publisherPubKey, granteePubKey *ecdsa.PublicKey, accessKey *encryption.Key) error
// Encrypts a Swarm reference for a given grantee
EncryptRef(storage kvs.KeyValueStore, grantee *ecdsa.PublicKey, ref swarm.Address) error
EncryptRef(storage kvs.KeyValueStore, grantee *ecdsa.PublicKey, ref swarm.Address) (swarm.Address, error)
}

type ActLogic struct {
Session
}

var _ Decryptor = (*ActLogic)(nil)
var _ Control = (*ActLogic)(nil)

// Adds a new publisher to an empty act
func (al ActLogic) AddPublisher(storage kvs.KeyValueStore, publisher *ecdsa.PublicKey) error {
Expand Down Expand Up @@ -116,9 +116,9 @@ func (al *ActLogic) getKeys(publicKey *ecdsa.PublicKey) ([][]byte, error) {
return al.Session.Key(publicKey, [][]byte{zeroByteArray, oneByteArray})
}

// DecryptRef will return a decrypted reference, for given encrypted reference and grantee
func (al ActLogic) DecryptRef(storage kvs.KeyValueStore, encryped_ref swarm.Address, grantee *ecdsa.PublicKey) (swarm.Address, error) {
keys, err := al.getKeys(grantee)
// DecryptRef will return a decrypted reference, for given encrypted reference and publisher
func (al ActLogic) DecryptRef(storage kvs.KeyValueStore, encrypedRef swarm.Address, publisher *ecdsa.PublicKey) (swarm.Address, error) {
ottpeter marked this conversation as resolved.
Show resolved Hide resolved
keys, err := al.getKeys(publisher)
if err != nil {
return swarm.EmptyAddress, err
}
Expand All @@ -140,7 +140,7 @@ func (al ActLogic) DecryptRef(storage kvs.KeyValueStore, encryped_ref swarm.Addr

// Decrypt reference
refCipher := encryption.New(accessKey, 0, uint32(0), hashFunc)
ref, err := refCipher.Decrypt(encryped_ref.Bytes())
ref, err := refCipher.Decrypt(encrypedRef.Bytes())
if err != nil {
return swarm.EmptyAddress, err
}
Expand Down
46 changes: 45 additions & 1 deletion pkg/dynamicaccess/accesslogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dynamicaccess_test
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"math/big"
"testing"
Expand Down Expand Up @@ -72,6 +73,49 @@ func TestDecryptRef_Success(t *testing.T) {
}
}

func TestDecryptRefWithGrantee_Success(t *testing.T) {
id0, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
diffieHellman := dynamicaccess.NewDefaultSession(id0)
al := dynamicaccess.NewLogic(diffieHellman)

s := kvsmock.New()
err := al.AddPublisher(s, &id0.PublicKey)
if err != nil {
t.Errorf("AddPublisher: expected no error, got %v", err)
}

id1, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
err = al.AddGrantee(s, &id0.PublicKey, &id1.PublicKey, nil)
if err != nil {
t.Errorf("AddNewGrantee: expected no error, got %v", err)
}

byteRef, _ := hex.DecodeString("39a5ea87b141fe44aa609c3327ecd896c0e2122897f5f4bbacf74db1033c5559")

expectedRef := swarm.NewAddress(byteRef)
t.Logf("encryptedRef: %s", expectedRef.String())

encryptedRef, err := al.EncryptRef(s, &id0.PublicKey, expectedRef)
t.Logf("encryptedRef: %s", encryptedRef.String())
if err != nil {
t.Errorf("There was an error while calling EncryptRef: ")
t.Error(err)
}

diffieHellman2 := dynamicaccess.NewDefaultSession(id1)
granteeAccessLogic := dynamicaccess.NewLogic(diffieHellman2)
acutalRef, err := granteeAccessLogic.DecryptRef(s, encryptedRef, &id0.PublicKey)
if err != nil {
t.Errorf("There was an error while calling Get: ")
t.Error(err)
}

if expectedRef.Compare(acutalRef) != 0 {

t.Errorf("Get gave back wrong Swarm reference!")
}
}

func TestDecryptRef_Error(t *testing.T) {
id0 := generateFixPrivateKey(0)

Expand All @@ -89,7 +133,7 @@ func TestDecryptRef_Error(t *testing.T) {
r, err := al.DecryptRef(s, encryptedRef, nil)
if err == nil {
t.Logf("r: %s", r.String())
t.Errorf("Get should give back encrypted access key not found error!")
t.Errorf("Get should return encrypted access key not found error!")
}
}

Expand Down
12 changes: 0 additions & 12 deletions pkg/dynamicaccess/publish.go

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/dynamicaccess/publish_test.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/dynamicaccess/timestamp.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/dynamicaccess/timestamp_test.go

This file was deleted.