| 1 |
package tpm2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"reflect" |
| 7 |
) |
| 8 |
|
| 9 |
// HandleName returns the TPM Name of a PCR, session, or permanent value |
| 10 |
// (e.g., hierarchy) handle. |
| 11 |
func HandleName(h TPMHandle) TPM2BName { |
| 12 |
result := make([]byte, 4) |
| 13 |
binary.BigEndian.PutUint32(result, uint32(h)) |
| 14 |
return TPM2BName{ |
| 15 |
Buffer: result, |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
// objectOrNVName calculates the Name of an NV index or object. |
| 20 |
// pub is a pointer to either a TPMTPublic or TPMSNVPublic. |
| 21 |
func objectOrNVName(alg TPMAlgID, pub interface{}) (*TPM2BName, error) { |
| 22 |
h, err := alg.Hash() |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} |
| 26 |
|
| 27 |
// Create a byte slice with the correct reserved size and marshal the |
| 28 |
// NameAlg to it. |
| 29 |
result := make([]byte, 2, 2+h.Size()) |
| 30 |
binary.BigEndian.PutUint16(result, uint16(alg)) |
| 31 |
|
| 32 |
// Calculate the hash of the entire Public contents and append it to the |
| 33 |
// result. |
| 34 |
ha := h.New() |
| 35 |
var buf bytes.Buffer |
| 36 |
if err := marshal(&buf, reflect.ValueOf(pub)); err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
ha.Write(buf.Bytes()) |
| 40 |
result = ha.Sum(result) |
| 41 |
|
| 42 |
return &TPM2BName{ |
| 43 |
Buffer: result, |
| 44 |
}, nil |
| 45 |
} |
| 46 |
|
| 47 |
// ObjectName returns the TPM Name of an object. |
| 48 |
func ObjectName(p *TPMTPublic) (*TPM2BName, error) { |
| 49 |
return objectOrNVName(p.NameAlg, p) |
| 50 |
} |
| 51 |
|
| 52 |
// NVName returns the TPM Name of an NV index. |
| 53 |
func NVName(p *TPMSNVPublic) (*TPM2BName, error) { |
| 54 |
return objectOrNVName(p.NameAlg, p) |
| 55 |
} |
| 56 |
|
| 57 |
// PrimaryHandleName returns the TPM Name of a primary handle. |
| 58 |
func PrimaryHandleName(h TPMHandle) []byte { |
| 59 |
result := make([]byte, 4) |
| 60 |
binary.BigEndian.PutUint32(result, uint32(h)) |
| 61 |
return result |
| 62 |
} |