github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/pkg/tss/constants.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"
     9  	"fmt"
    10  
    11  	"github.com/google/go-tpm/tpm2"
    12  )
    13  
    14  // TPMVersion is used to configure a preference in
    15  // which TPM to use, if multiple are available.
    16  type TPMVersion uint8
    17  
    18  // TPM versions
    19  const (
    20  	TPMVersionAgnostic TPMVersion = iota
    21  	TPMVersion12
    22  	TPMVersion20
    23  )
    24  
    25  // TPMInterface indicates how the client communicates
    26  // with the TPM.
    27  type TPMInterface uint8
    28  
    29  // TPM interfaces
    30  const (
    31  	TPMInterfaceDirect TPMInterface = iota
    32  	TPMInterfaceKernelManaged
    33  	TPMInterfaceDaemonManaged
    34  )
    35  
    36  // HashAlg is the TPM hash algorithm id
    37  type HashAlg uint8
    38  
    39  var (
    40  	// HashSHA1 is the TPM 1.2 identifier for SHA1
    41  	HashSHA1 = HashAlg(tpm2.AlgSHA1)
    42  	// HashSHA256 is the TPM 2.0 identifier for SHA256
    43  	HashSHA256 = HashAlg(tpm2.AlgSHA256)
    44  )
    45  
    46  func (a HashAlg) cryptoHash() crypto.Hash {
    47  	switch a {
    48  	case HashSHA1:
    49  		return crypto.SHA1
    50  	case HashSHA256:
    51  		return crypto.SHA256
    52  	}
    53  	return 0
    54  }
    55  
    56  func (a HashAlg) goTPMAlg() tpm2.Algorithm {
    57  	switch a {
    58  	case HashSHA1:
    59  		return tpm2.AlgSHA1
    60  	case HashSHA256:
    61  		return tpm2.AlgSHA256
    62  	}
    63  	return 0
    64  }
    65  
    66  // String returns a human-friendly representation of the hash algorithm.
    67  func (a HashAlg) String() string {
    68  	switch a {
    69  	case HashSHA1:
    70  		return "SHA1"
    71  	case HashSHA256:
    72  		return "SHA256"
    73  	}
    74  	return fmt.Sprintf("HashAlg<%d>", int(a))
    75  }