github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntkey/hash.go (about)

     1  // Copyright 2017-2021 the LinuxBoot 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  //go:generate manifestcodegen
     6  
     7  package cbntkey
     8  
     9  import (
    10  	"fmt"
    11  	"strings"
    12  
    13  	"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
    14  )
    15  
    16  // Hash is "KM hash Structure" defined in document #575623.
    17  type Hash struct {
    18  	// Usage is the digest usage bitmask.
    19  	//
    20  	// More than one bit can be set to indicate shared digest usage.
    21  	// Usage of bit 0 is normative; other usages are informative.
    22  	Usage Usage `json:"hashUsage"`
    23  
    24  	// Digest is the actual digest.
    25  	Digest cbnt.HashStructure `json:"hashStruct"`
    26  }
    27  
    28  // Usage is the digest usage bitmask.
    29  //
    30  // More than one bit can be set to indicate shared digest usage.
    31  // Usage of bit 0 is normative; other usages are informative.
    32  type Usage uint64
    33  
    34  const (
    35  	// UsageBPMSigningPKD is the bit meaning the digest could be used as
    36  	// Boot Policy Manifest signing pubkey digest.
    37  	UsageBPMSigningPKD = Usage(1 << iota)
    38  
    39  	// UsageFITPatchManifestSigningPKD is the bit meaning the digest could be used as
    40  	// FIT Patch Manifest signing pubkey digest.
    41  	UsageFITPatchManifestSigningPKD
    42  
    43  	// UsageACMManifestSigningPKD is the bit meaning the digest could be used as
    44  	// ACM Manifest signing pubkey digest.
    45  	UsageACMManifestSigningPKD
    46  
    47  	// UsageSDEVSigningPKD is the bit meaning the digest could be used as
    48  	// SDEV signing pubkey digest.
    49  	UsageSDEVSigningPKD
    50  
    51  	// UsageReserved is a reserved bit
    52  	UsageReserved
    53  )
    54  
    55  // String implements fmt.Stringer.
    56  func (u Usage) String() string {
    57  	var result []string
    58  	for i := uint(0); i < 64; i++ {
    59  		f := Usage(1 << i)
    60  		if !u.IsSet(f) {
    61  			continue
    62  		}
    63  		var descr string
    64  		switch f {
    65  		case UsageBPMSigningPKD:
    66  			descr = "BPM_signing_pubkey_digest"
    67  		case UsageFITPatchManifestSigningPKD:
    68  			descr = "FIT_patch_manifest_signing_pubkey_digest"
    69  		case UsageACMManifestSigningPKD:
    70  			descr = "ACM_manifest_signing_pubkey_digest"
    71  		case UsageSDEVSigningPKD:
    72  			descr = "SDEV_signing_pubkey_digest"
    73  		case UsageReserved:
    74  			descr = "Reserved"
    75  		default:
    76  			descr = fmt.Sprintf("unexpected_bit_%d", i)
    77  		}
    78  		result = append(result, descr)
    79  	}
    80  
    81  	return strings.Join(result, ",")
    82  }
    83  
    84  // IsSet returns true if bits `f` are set in bitmask `u`.
    85  func (u Usage) IsSet(f Usage) bool {
    86  	return u&f != 0
    87  }
    88  
    89  // Set sets/unsets the bits of `f` in bitmask `u`.
    90  //
    91  // To set the bits `v` should be true, to unset -- false.
    92  func (u *Usage) Set(f Usage, v bool) {
    93  	if v {
    94  		*u |= f
    95  	} else {
    96  		*u &= ^f
    97  	}
    98  }