Skip to content

Commit

Permalink
Move AddrOf to its own package.
Browse files Browse the repository at this point in the history
This allows us to replace the clunky `util.AddrOf` with the shorter and
more descriptive `addr.Of`.
  • Loading branch information
NullHypothesis committed Nov 1, 2024
1 parent 1199530 commit dad41e9
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 35 deletions.
3 changes: 2 additions & 1 deletion cmd/veil/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/enclave/nitro"
"github.com/Amnesic-Systems/veil/internal/enclave/noop"
Expand Down Expand Up @@ -295,7 +296,7 @@ func TestHashes(t *testing.T) {
return testutil.Client.Get(intSrv("/enclave/hashes"))
}
)
hashes.SetAppHash(util.AddrOf(sha256.Sum256([]byte("foo"))))
hashes.SetAppHash(addr.Of(sha256.Sum256([]byte("foo"))))

cases := []struct {
name string
Expand Down
6 changes: 6 additions & 0 deletions internal/addr/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package addr

// Of returns a pointer to the given value.
func Of[T any](v T) *T {
return &v
}
14 changes: 14 additions & 0 deletions internal/addr/addr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package addr

import (
"testing"

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

func TestOf(t *testing.T) {
t.Parallel()

x := 1
require.Equal(t, &x, Of(x))
}
2 changes: 2 additions & 0 deletions internal/addr/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package addr implements helper functions for dealing with pointers.
package addr
8 changes: 4 additions & 4 deletions internal/enclave/noop/attester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package noop
import (
"testing"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/stretchr/testify/require"
)

Expand All @@ -17,9 +17,9 @@ func TestSuccessfulVerification(t *testing.T) {
var (
a = NewAttester()
origAux = &enclave.AuxInfo{
PublicKey: util.AddrOf([enclave.AuxFieldLen]byte{'a', 'b', 'c'}),
UserData: util.AddrOf([enclave.AuxFieldLen]byte{'d', 'e', 'f'}),
Nonce: util.AddrOf([enclave.AuxFieldLen]byte{'g', 'h', 'i'}),
PublicKey: addr.Of([enclave.AuxFieldLen]byte{'a', 'b', 'c'}),
UserData: addr.Of([enclave.AuxFieldLen]byte{'d', 'e', 'f'}),
Nonce: addr.Of([enclave.AuxFieldLen]byte{'g', 'h', 'i'}),
}
)

Expand Down
8 changes: 4 additions & 4 deletions internal/service/attestation/aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"crypto/sha256"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/errs"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
)

// Builder is a helper for setting auxiliary attestation both at initialization
Expand Down Expand Up @@ -42,7 +42,7 @@ func (b *Builder) Attest(opts ...AuxField) (*enclave.AttestationDoc, error) {
func WithHashes(h *Hashes) AuxField {
return func(b *Builder) {
if b.aux.PublicKey == nil {
b.aux.PublicKey = util.AddrOf([enclave.AuxFieldLen]byte{})
b.aux.PublicKey = addr.Of([enclave.AuxFieldLen]byte{})
}
copy(b.aux.PublicKey[:], h.Serialize())
}
Expand All @@ -52,7 +52,7 @@ func WithHashes(h *Hashes) AuxField {
func WithNonce(n *nonce.Nonce) AuxField {
return func(b *Builder) {
if b.aux.Nonce == nil {
b.aux.Nonce = util.AddrOf([enclave.AuxFieldLen]byte{})
b.aux.Nonce = addr.Of([enclave.AuxFieldLen]byte{})
}
copy(b.aux.Nonce[:], n[:])
}
Expand All @@ -62,7 +62,7 @@ func WithNonce(n *nonce.Nonce) AuxField {
func WithSHA256(sha [sha256.Size]byte) AuxField {
return func(b *Builder) {
if b.aux.UserData == nil {
b.aux.UserData = util.AddrOf([enclave.AuxFieldLen]byte{})
b.aux.UserData = addr.Of([enclave.AuxFieldLen]byte{})
}
copy(b.aux.UserData[:], sha[:])
}
Expand Down
13 changes: 7 additions & 6 deletions internal/service/attestation/aux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"testing"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/enclave/nitro"
"github.com/Amnesic-Systems/veil/internal/enclave/noop"
Expand All @@ -15,11 +16,11 @@ import (

func TestGetters(t *testing.T) {
n := util.Must(nonce.New())
s := util.AddrOf(sha256.Sum256([]byte("foo")))
h1 := &Hashes{TlsKeyHash: util.AddrOf(sha256.Sum256([]byte("foo")))}
s := addr.Of(sha256.Sum256([]byte("foo")))
h1 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo")))}
h2 := &Hashes{
TlsKeyHash: util.AddrOf(sha256.Sum256([]byte("foo"))),
AppKeyHash: util.AddrOf(sha256.Sum256([]byte("bar"))),
TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo"))),
AppKeyHash: addr.Of(sha256.Sum256([]byte("bar"))),
}

cases := []struct {
Expand Down Expand Up @@ -82,8 +83,8 @@ func TestBuilder(t *testing.T) {
}
nonce1, nonce2 := util.Must(nonce.New()), util.Must(nonce.New())
sha1, sha2 := sha256.Sum256([]byte("foo")), sha256.Sum256([]byte("bar"))
hashes1 := &Hashes{TlsKeyHash: util.AddrOf(sha256.Sum256([]byte("foo")))}
hashes2 := &Hashes{TlsKeyHash: util.AddrOf(sha256.Sum256([]byte("bar")))}
hashes1 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo")))}
hashes2 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("bar")))}

cases := []struct {
name string
Expand Down
6 changes: 3 additions & 3 deletions internal/service/attestation/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"sync"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/errs"
"github.com/Amnesic-Systems/veil/internal/util"
)

// Hashes contains hashes over public key material which we embed in
Expand Down Expand Up @@ -61,7 +61,7 @@ func DeserializeHashes(b []byte) (h *Hashes, err error) {
tlsKeyHash := []byte(strings.TrimPrefix(s[0], "sha256:"))
appKeyHash := []byte(strings.TrimPrefix(s[1], "sha256:"))
h = &Hashes{
TlsKeyHash: util.AddrOf([sha256.Size]byte{}),
TlsKeyHash: addr.Of([sha256.Size]byte{}),
}

if _, err := base64.StdEncoding.Decode(
Expand All @@ -76,7 +76,7 @@ func DeserializeHashes(b []byte) (h *Hashes, err error) {
return h, nil
}

h.AppKeyHash = util.AddrOf([sha256.Size]byte{})
h.AppKeyHash = addr.Of([sha256.Size]byte{})
if _, err := base64.StdEncoding.Decode(
h.AppKeyHash[:],
appKeyHash,
Expand Down
6 changes: 3 additions & 3 deletions internal/service/attestation/hashes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"crypto/sha256"
"testing"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/errs"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/stretchr/testify/require"
)

func TestDeSerialization(t *testing.T) {
var (
origHashes = new(Hashes)
)
origHashes.SetAppHash(util.AddrOf(sha256.Sum256([]byte("foo"))))
origHashes.SetTLSHash(util.AddrOf(sha256.Sum256([]byte("bar"))))
origHashes.SetAppHash(addr.Of(sha256.Sum256([]byte("foo"))))
origHashes.SetTLSHash(addr.Of(sha256.Sum256([]byte("bar"))))

hashes, err := DeserializeHashes(origHashes.Serialize())
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions internal/service/handle/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"sync"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/config"
"github.com/Amnesic-Systems/veil/internal/httperr"
"github.com/Amnesic-Systems/veil/internal/httputil"
Expand Down Expand Up @@ -98,8 +99,8 @@ func AppHash(
setAppHash func(*[sha256.Size]byte),
) http.HandlerFunc {
b := util.Must(json.Marshal(&attestation.Hashes{
TlsKeyHash: util.AddrOf(sha256.Sum256([]byte("foo"))),
AppKeyHash: util.AddrOf(sha256.Sum256([]byte("bar"))),
TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo"))),
AppKeyHash: addr.Of(sha256.Sum256([]byte("bar"))),
}))
maxHashesLen := len(b) + 1 // Allow extra byte for the \n.

Expand Down
3 changes: 2 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"

"github.com/Amnesic-Systems/veil/internal/addr"
"github.com/Amnesic-Systems/veil/internal/config"
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/errs"
Expand Down Expand Up @@ -47,7 +48,7 @@ func Run(

// Initialize hashes for the attestation document.
hashes := new(attestation.Hashes)
hashes.SetTLSHash(util.AddrOf(sha256.Sum256(cert)))
hashes.SetTLSHash(addr.Of(sha256.Sum256(cert)))

// Initialize Web servers.
intSrv := newIntSrv(config, keys, hashes, appReady)
Expand Down
4 changes: 0 additions & 4 deletions internal/util/common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package util

func AddrOf[T any](v T) *T {
return &v
}

func Must[T any](v T, err error) T {
if err != nil {
panic(err)
Expand Down
7 changes: 0 additions & 7 deletions internal/util/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestAddrOf(t *testing.T) {
t.Parallel()

x := 1
require.Equal(t, &x, AddrOf(x))
}

func TestMust(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit dad41e9

Please sign in to comment.