github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/devices/dummy/rom/rom.go (about)

     1  // Copyright 2020 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rom
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/golang/glog"
    23  	"github.com/google/trillian-examples/binary_transparency/firmware/devices/dummy/common"
    24  	"github.com/google/trillian-examples/binary_transparency/firmware/internal/crypto"
    25  	"github.com/google/trillian-examples/binary_transparency/firmware/internal/verify"
    26  
    27  	"golang.org/x/mod/sumdb/note"
    28  )
    29  
    30  const (
    31  	bundlePath   = "bundle.json"
    32  	firmwarePath = "firmware.bin"
    33  )
    34  
    35  // Chain represents the next stage in the boot process.
    36  type Chain func() error
    37  
    38  // Reset is intended to emulate the early stage boot process of a device.
    39  //
    40  // It's separate from the device emulator code to highlight that the process of
    41  // verifying the local firmware/proofs/etc. could be done on-device at
    42  // as part of the boot-ROM execution to provide some tamper-resistant properties
    43  // to the firmware installed on the device.
    44  //
    45  // Other, real-world, devices with secure elements may be able to optimise this
    46  // process by checking once and leveraging properties of the hardware.
    47  //
    48  // Returns the first link in the boot chain as a func.
    49  func Reset(storagePath string) (Chain, error) {
    50  	glog.Info("----RESET----")
    51  	glog.Info("Powering up bananas, configuring Romulans, feeding the watchdogs")
    52  
    53  	glog.Infof("Configuring flash and loading FT artifacts from %q...", storagePath)
    54  
    55  	fwFile := filepath.Clean(filepath.Join(storagePath, firmwarePath))
    56  	bundleFile := filepath.Clean(filepath.Join(storagePath, bundlePath))
    57  
    58  	bundleRaw, err := os.ReadFile(bundleFile)
    59  	if err != nil {
    60  		return nil, fmt.Errorf("failed to read transparency bundle: %w", err)
    61  	}
    62  
    63  	fw, err := os.ReadFile(fwFile)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("failed to read firmware: %w", err)
    66  	}
    67  
    68  	fwMeasurement, err := common.ExpectedMeasurement(fw)
    69  	if err != nil {
    70  		return nil, fmt.Errorf("failed calculate measurement: %w", err)
    71  	}
    72  
    73  	// validate bundle
    74  	v, err := note.NewVerifier(crypto.TestFTPersonalityPub)
    75  	if err != nil {
    76  		return nil, fmt.Errorf("failed to create sig verifier: %w", err)
    77  	}
    78  	if err := verify.BundleForBoot(bundleRaw, fwMeasurement[:], v); err != nil {
    79  		return nil, fmt.Errorf("failed to verify bundle: %w", err)
    80  	}
    81  
    82  	glog.Info("Bundle verification passed, prepared to boot")
    83  
    84  	boot1 := func() error {
    85  		return bootWasm("main", fw)
    86  	}
    87  	return boot1, nil
    88  }