github.com/rminnich/u-root@v7.0.0+incompatible/pkg/tss/nvram.go (about)

     1  // Copyright 2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tss
     6  
     7  import (
     8  	"crypto/sha1"
     9  	"fmt"
    10  	"io"
    11  
    12  	tpm1 "github.com/google/go-tpm/tpm"
    13  	tpm2 "github.com/google/go-tpm/tpm2"
    14  	tpmutil "github.com/google/go-tpm/tpmutil"
    15  )
    16  
    17  func nvRead12(rwc io.ReadWriteCloser, index, offset, len uint32, auth string) ([]byte, error) {
    18  	var ownAuth [20]byte //owner well known
    19  	if auth != "" {
    20  		ownAuth = sha1.Sum([]byte(auth))
    21  	}
    22  
    23  	// Get TPMInfo
    24  	indexData, err := tpm1.GetNVIndex(rwc, index)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	if indexData == nil {
    29  		return nil, fmt.Errorf("index not found")
    30  	}
    31  
    32  	// Check if authData is needed
    33  	// AuthRead 0x00200000 | OwnerRead 0x00100000
    34  	needAuthData := 1 >> (indexData.Permission.Attributes & (nvPerAuthRead | nvPerOwnerRead))
    35  	authread := 1 >> (indexData.Permission.Attributes & nvPerAuthRead)
    36  
    37  	if needAuthData == 0 {
    38  		if authread != 0 {
    39  			return tpm1.NVReadValue(rwc, index, offset, len, ownAuth[:])
    40  		}
    41  		return tpm1.NVReadValueAuth(rwc, index, offset, len, ownAuth[:])
    42  	}
    43  	return tpm1.NVReadValue(rwc, index, offset, len, nil)
    44  }
    45  
    46  func nvRead20(rwc io.ReadWriteCloser, index, authHandle tpmutil.Handle, password string, blocksize int) ([]byte, error) {
    47  	return tpm2.NVReadEx(rwc, index, authHandle, password, blocksize)
    48  }