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

     1  // Copyright 2017-2023 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 bg
     8  
     9  // HashStructure describes a digest.
    10  type HashStructure struct {
    11  	HashAlg    Algorithm `default:"0x10" json:"hsAlg"`
    12  	HashBuffer []byte    `json:"hsBuffer"`
    13  }
    14  
    15  type HashStructureFill struct {
    16  	HashAlg    Algorithm `default:"0x0b" json:"hsAlg"`
    17  	HashBuffer []byte    `countValue:"hashSize()" prettyValue:"hashSizePrint()" json:"hsBuffer"`
    18  }
    19  
    20  func (a Algorithm) size() uint16 {
    21  	switch a {
    22  	case AlgUnknown:
    23  		return 0
    24  	case AlgNull:
    25  		return 0
    26  	case AlgSHA1:
    27  		return 20
    28  	case AlgSHA256:
    29  		return 32
    30  	default:
    31  		return 0
    32  	}
    33  }
    34  
    35  func (h HashStructureFill) hashSize() uint16 {
    36  	const hashSizeFieldLen = 2
    37  	if h.HashAlg.IsNull() {
    38  		// Evil hack, more investigation needed
    39  		return AlgSHA256.size() + hashSizeFieldLen
    40  	} else {
    41  		return h.HashAlg.size() + hashSizeFieldLen
    42  	}
    43  }
    44  
    45  func (h HashStructureFill) hashSizePrint() interface{} {
    46  	if h.HashAlg.IsNull() {
    47  		// Evil hack, more investigation needed
    48  		return make([]byte, AlgSHA256.size())
    49  	} else {
    50  		return h.HashBuffer
    51  	}
    52  }