Skip to content

Commit

Permalink
Use a stable unit-specific derived MAC
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Dec 12, 2023
1 parent b1e46ee commit 436c09c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions trusted_applet/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package main

import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
Expand All @@ -35,6 +35,7 @@ import (

"github.com/beevik/ntp"
"github.com/transparency-dev/armored-witness-applet/third_party/dhcp"
"github.com/transparency-dev/armored-witness-os/api"
"golang.org/x/term"

"github.com/usbarmory/GoTEE/applet"
Expand Down Expand Up @@ -332,11 +333,10 @@ func (n *txNotification) WriteNotify() {
syscall.Write(TX, buf, uint(len(buf)))
}

func mac() string {
m := make([]uint8, 6)
if _, err := rand.Read(m); err != nil {
panic(fmt.Sprintf("failed to read %d bytes for randomised MAC address: %v", len(m), err))
}
// mac creates a stable "local administered" MAC address for the network based on the
// provided unit serial number.
func mac(serial string) string {
m := sha256.Sum256([]byte(fmt.Sprintf("MAC:%s", serial)))
// The first byte of the MAC address has a couple of flags which must be set correctly:
// - Unicast(0)/multicast(1) in the least significant bit of the byte.
// This must be set to unicast.
Expand All @@ -352,7 +352,12 @@ func startNetworking() (err error) {
// Set the default resolver from the config, if we're using DHCP this may be updated.
net.DefaultNS = []string{cfg.Resolver}

if iface, err = enet.Init(nil, cfg.IP, cfg.Netmask, mac(), cfg.Gateway, int(nicID)); err != nil {
var status api.Status
if err := syscall.Call("RPC.Status", nil, &status); err != nil {
return fmt.Errorf("failed to fetch Status: %v", err)
}

if iface, err = enet.Init(nil, cfg.IP, cfg.Netmask, mac(status.Serial), cfg.Gateway, int(nicID)); err != nil {
return
}

Expand Down

0 comments on commit 436c09c

Please sign in to comment.