github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/cbntkey/manifest_manifestcodegen.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:build !manifestcodegen
     6  // +build !manifestcodegen
     7  
     8  // Code generated by "menifestcodegen". DO NOT EDIT.
     9  // To reproduce: go run github.com/linuxboot/fiano/pkg/intel/metadata/common/manifestcodegen/cmd/manifestcodegen github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntkey
    10  
    11  package cbntkey
    12  
    13  import (
    14  	"encoding/binary"
    15  	"fmt"
    16  	"io"
    17  	"strings"
    18  
    19  	"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
    20  	"github.com/linuxboot/fiano/pkg/intel/metadata/common/pretty"
    21  )
    22  
    23  var (
    24  	// Just to avoid errors in "import" above in case if it wasn't used below
    25  	_ = binary.LittleEndian
    26  	_ = (fmt.Stringer)(nil)
    27  	_ = (io.Reader)(nil)
    28  	_ = pretty.Header
    29  	_ = strings.Join
    30  	_ = cbnt.StructInfo{}
    31  )
    32  
    33  // NewManifest returns a new instance of Manifest with
    34  // all default values set.
    35  func NewManifest() *Manifest {
    36  	s := &Manifest{}
    37  	copy(s.StructInfo.ID[:], []byte(StructureIDManifest))
    38  	s.StructInfo.Version = 0x21
    39  	// Recursively initializing a child structure:
    40  	s.KeyAndSignature = *cbnt.NewKeySignature()
    41  	s.Rehash()
    42  	return s
    43  }
    44  
    45  // Validate (recursively) checks the structure if there are any unexpected
    46  // values. It returns an error if so.
    47  func (s *Manifest) Validate() error {
    48  	// See tag "rehashValue"
    49  	{
    50  		expectedValue := uint16(s.KeyAndSignatureOffset())
    51  		if s.KeyManifestSignatureOffset != expectedValue {
    52  			return fmt.Errorf("field 'KeyManifestSignatureOffset' expects write-value '%v', but has %v", expectedValue, s.KeyManifestSignatureOffset)
    53  		}
    54  	}
    55  	// Recursively validating a child structure:
    56  	if err := s.KeyAndSignature.Validate(); err != nil {
    57  		return fmt.Errorf("error on field 'KeyAndSignature': %w", err)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // StructureIDManifest is the StructureID (in terms of
    64  // the document #575623) of element 'Manifest'.
    65  const StructureIDManifest = "__KEYM__"
    66  
    67  // GetStructInfo returns current value of StructInfo of the structure.
    68  //
    69  // StructInfo is a set of standard fields with presented in any element
    70  // ("element" in terms of document #575623).
    71  func (s *Manifest) GetStructInfo() cbnt.StructInfo {
    72  	return s.StructInfo
    73  }
    74  
    75  // SetStructInfo sets new value of StructInfo to the structure.
    76  //
    77  // StructInfo is a set of standard fields with presented in any element
    78  // ("element" in terms of document #575623).
    79  func (s *Manifest) SetStructInfo(newStructInfo cbnt.StructInfo) {
    80  	s.StructInfo = newStructInfo
    81  }
    82  
    83  // ReadFrom reads the Manifest from 'r' in format defined in the document #575623.
    84  func (s *Manifest) ReadFrom(r io.Reader) (int64, error) {
    85  	var totalN int64
    86  
    87  	err := binary.Read(r, binary.LittleEndian, &s.StructInfo)
    88  	if err != nil {
    89  		return totalN, fmt.Errorf("unable to read structure info at %d: %w", totalN, err)
    90  	}
    91  	totalN += int64(binary.Size(s.StructInfo))
    92  
    93  	n, err := s.ReadDataFrom(r)
    94  	if err != nil {
    95  		return totalN, fmt.Errorf("unable to read data: %w", err)
    96  	}
    97  	totalN += n
    98  
    99  	return totalN, nil
   100  }
   101  
   102  // ReadDataFrom reads the Manifest from 'r' excluding StructInfo,
   103  // in format defined in the document #575623.
   104  func (s *Manifest) ReadDataFrom(r io.Reader) (int64, error) {
   105  	totalN := int64(0)
   106  
   107  	// StructInfo (ManifestFieldType: structInfo)
   108  	{
   109  		// ReadDataFrom does not read Struct, use ReadFrom for that.
   110  	}
   111  
   112  	// KeyManifestSignatureOffset (ManifestFieldType: endValue)
   113  	{
   114  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.KeyManifestSignatureOffset)
   115  		if err != nil {
   116  			return totalN, fmt.Errorf("unable to read field 'KeyManifestSignatureOffset': %w", err)
   117  		}
   118  		totalN += int64(n)
   119  	}
   120  
   121  	// Reserved2 (ManifestFieldType: arrayStatic)
   122  	{
   123  		n, err := 3, binary.Read(r, binary.LittleEndian, s.Reserved2[:])
   124  		if err != nil {
   125  			return totalN, fmt.Errorf("unable to read field 'Reserved2': %w", err)
   126  		}
   127  		totalN += int64(n)
   128  	}
   129  
   130  	// Revision (ManifestFieldType: endValue)
   131  	{
   132  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.Revision)
   133  		if err != nil {
   134  			return totalN, fmt.Errorf("unable to read field 'Revision': %w", err)
   135  		}
   136  		totalN += int64(n)
   137  	}
   138  
   139  	// KMSVN (ManifestFieldType: endValue)
   140  	{
   141  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.KMSVN)
   142  		if err != nil {
   143  			return totalN, fmt.Errorf("unable to read field 'KMSVN': %w", err)
   144  		}
   145  		totalN += int64(n)
   146  	}
   147  
   148  	// KMID (ManifestFieldType: endValue)
   149  	{
   150  		n, err := 1, binary.Read(r, binary.LittleEndian, &s.KMID)
   151  		if err != nil {
   152  			return totalN, fmt.Errorf("unable to read field 'KMID': %w", err)
   153  		}
   154  		totalN += int64(n)
   155  	}
   156  
   157  	// PubKeyHashAlg (ManifestFieldType: endValue)
   158  	{
   159  		n, err := 2, binary.Read(r, binary.LittleEndian, &s.PubKeyHashAlg)
   160  		if err != nil {
   161  			return totalN, fmt.Errorf("unable to read field 'PubKeyHashAlg': %w", err)
   162  		}
   163  		totalN += int64(n)
   164  	}
   165  
   166  	// Hash (ManifestFieldType: list)
   167  	{
   168  		var count uint16
   169  		err := binary.Read(r, binary.LittleEndian, &count)
   170  		if err != nil {
   171  			return totalN, fmt.Errorf("unable to read the count for field 'Hash': %w", err)
   172  		}
   173  		totalN += int64(binary.Size(count))
   174  		s.Hash = make([]Hash, count)
   175  
   176  		for idx := range s.Hash {
   177  			n, err := s.Hash[idx].ReadFrom(r)
   178  			if err != nil {
   179  				return totalN, fmt.Errorf("unable to read field 'Hash[%d]': %w", idx, err)
   180  			}
   181  			totalN += int64(n)
   182  		}
   183  	}
   184  
   185  	// KeyAndSignature (ManifestFieldType: subStruct)
   186  	{
   187  		n, err := s.KeyAndSignature.ReadFrom(r)
   188  		if err != nil {
   189  			return totalN, fmt.Errorf("unable to read field 'KeyAndSignature': %w", err)
   190  		}
   191  		totalN += int64(n)
   192  	}
   193  
   194  	return totalN, nil
   195  }
   196  
   197  // RehashRecursive calls Rehash (see below) recursively.
   198  func (s *Manifest) RehashRecursive() {
   199  	s.StructInfo.Rehash()
   200  	s.KeyAndSignature.Rehash()
   201  	s.Rehash()
   202  }
   203  
   204  // Rehash sets values which are calculated automatically depending on the rest
   205  // data. It is usually about the total size field of an element.
   206  func (s *Manifest) Rehash() {
   207  	s.Variable0 = 0
   208  	s.ElementSize = 0
   209  	s.KeyManifestSignatureOffset = uint16(s.KeyAndSignatureOffset())
   210  }
   211  
   212  // WriteTo writes the Manifest into 'w' in format defined in
   213  // the document #575623.
   214  func (s *Manifest) WriteTo(w io.Writer) (int64, error) {
   215  	totalN := int64(0)
   216  	s.Rehash()
   217  
   218  	// StructInfo (ManifestFieldType: structInfo)
   219  	{
   220  		n, err := s.StructInfo.WriteTo(w)
   221  		if err != nil {
   222  			return totalN, fmt.Errorf("unable to write field 'StructInfo': %w", err)
   223  		}
   224  		totalN += int64(n)
   225  	}
   226  
   227  	// KeyManifestSignatureOffset (ManifestFieldType: endValue)
   228  	{
   229  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.KeyManifestSignatureOffset)
   230  		if err != nil {
   231  			return totalN, fmt.Errorf("unable to write field 'KeyManifestSignatureOffset': %w", err)
   232  		}
   233  		totalN += int64(n)
   234  	}
   235  
   236  	// Reserved2 (ManifestFieldType: arrayStatic)
   237  	{
   238  		n, err := 3, binary.Write(w, binary.LittleEndian, s.Reserved2[:])
   239  		if err != nil {
   240  			return totalN, fmt.Errorf("unable to write field 'Reserved2': %w", err)
   241  		}
   242  		totalN += int64(n)
   243  	}
   244  
   245  	// Revision (ManifestFieldType: endValue)
   246  	{
   247  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.Revision)
   248  		if err != nil {
   249  			return totalN, fmt.Errorf("unable to write field 'Revision': %w", err)
   250  		}
   251  		totalN += int64(n)
   252  	}
   253  
   254  	// KMSVN (ManifestFieldType: endValue)
   255  	{
   256  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.KMSVN)
   257  		if err != nil {
   258  			return totalN, fmt.Errorf("unable to write field 'KMSVN': %w", err)
   259  		}
   260  		totalN += int64(n)
   261  	}
   262  
   263  	// KMID (ManifestFieldType: endValue)
   264  	{
   265  		n, err := 1, binary.Write(w, binary.LittleEndian, &s.KMID)
   266  		if err != nil {
   267  			return totalN, fmt.Errorf("unable to write field 'KMID': %w", err)
   268  		}
   269  		totalN += int64(n)
   270  	}
   271  
   272  	// PubKeyHashAlg (ManifestFieldType: endValue)
   273  	{
   274  		n, err := 2, binary.Write(w, binary.LittleEndian, &s.PubKeyHashAlg)
   275  		if err != nil {
   276  			return totalN, fmt.Errorf("unable to write field 'PubKeyHashAlg': %w", err)
   277  		}
   278  		totalN += int64(n)
   279  	}
   280  
   281  	// Hash (ManifestFieldType: list)
   282  	{
   283  		count := uint16(len(s.Hash))
   284  		err := binary.Write(w, binary.LittleEndian, &count)
   285  		if err != nil {
   286  			return totalN, fmt.Errorf("unable to write the count for field 'Hash': %w", err)
   287  		}
   288  		totalN += int64(binary.Size(count))
   289  		for idx := range s.Hash {
   290  			n, err := s.Hash[idx].WriteTo(w)
   291  			if err != nil {
   292  				return totalN, fmt.Errorf("unable to write field 'Hash[%d]': %w", idx, err)
   293  			}
   294  			totalN += int64(n)
   295  		}
   296  	}
   297  
   298  	// KeyAndSignature (ManifestFieldType: subStruct)
   299  	{
   300  		n, err := s.KeyAndSignature.WriteTo(w)
   301  		if err != nil {
   302  			return totalN, fmt.Errorf("unable to write field 'KeyAndSignature': %w", err)
   303  		}
   304  		totalN += int64(n)
   305  	}
   306  
   307  	return totalN, nil
   308  }
   309  
   310  // StructInfoSize returns the size in bytes of the value of field StructInfo
   311  func (s *Manifest) StructInfoTotalSize() uint64 {
   312  	return s.StructInfo.TotalSize()
   313  }
   314  
   315  // KeyManifestSignatureOffsetSize returns the size in bytes of the value of field KeyManifestSignatureOffset
   316  func (s *Manifest) KeyManifestSignatureOffsetTotalSize() uint64 {
   317  	return 2
   318  }
   319  
   320  // Reserved2Size returns the size in bytes of the value of field Reserved2
   321  func (s *Manifest) Reserved2TotalSize() uint64 {
   322  	return 3
   323  }
   324  
   325  // RevisionSize returns the size in bytes of the value of field Revision
   326  func (s *Manifest) RevisionTotalSize() uint64 {
   327  	return 1
   328  }
   329  
   330  // KMSVNSize returns the size in bytes of the value of field KMSVN
   331  func (s *Manifest) KMSVNTotalSize() uint64 {
   332  	return 1
   333  }
   334  
   335  // KMIDSize returns the size in bytes of the value of field KMID
   336  func (s *Manifest) KMIDTotalSize() uint64 {
   337  	return 1
   338  }
   339  
   340  // PubKeyHashAlgSize returns the size in bytes of the value of field PubKeyHashAlg
   341  func (s *Manifest) PubKeyHashAlgTotalSize() uint64 {
   342  	return 2
   343  }
   344  
   345  // HashSize returns the size in bytes of the value of field Hash
   346  func (s *Manifest) HashTotalSize() uint64 {
   347  	var size uint64
   348  	size += uint64(binary.Size(uint16(0)))
   349  	for idx := range s.Hash {
   350  		size += s.Hash[idx].TotalSize()
   351  	}
   352  	return size
   353  }
   354  
   355  // KeyAndSignatureSize returns the size in bytes of the value of field KeyAndSignature
   356  func (s *Manifest) KeyAndSignatureTotalSize() uint64 {
   357  	return s.KeyAndSignature.TotalSize()
   358  }
   359  
   360  // StructInfoOffset returns the offset in bytes of field StructInfo
   361  func (s *Manifest) StructInfoOffset() uint64 {
   362  	return 0
   363  }
   364  
   365  // KeyManifestSignatureOffsetOffset returns the offset in bytes of field KeyManifestSignatureOffset
   366  func (s *Manifest) KeyManifestSignatureOffsetOffset() uint64 {
   367  	return s.StructInfoOffset() + s.StructInfoTotalSize()
   368  }
   369  
   370  // Reserved2Offset returns the offset in bytes of field Reserved2
   371  func (s *Manifest) Reserved2Offset() uint64 {
   372  	return s.KeyManifestSignatureOffsetOffset() + s.KeyManifestSignatureOffsetTotalSize()
   373  }
   374  
   375  // RevisionOffset returns the offset in bytes of field Revision
   376  func (s *Manifest) RevisionOffset() uint64 {
   377  	return s.Reserved2Offset() + s.Reserved2TotalSize()
   378  }
   379  
   380  // KMSVNOffset returns the offset in bytes of field KMSVN
   381  func (s *Manifest) KMSVNOffset() uint64 {
   382  	return s.RevisionOffset() + s.RevisionTotalSize()
   383  }
   384  
   385  // KMIDOffset returns the offset in bytes of field KMID
   386  func (s *Manifest) KMIDOffset() uint64 {
   387  	return s.KMSVNOffset() + s.KMSVNTotalSize()
   388  }
   389  
   390  // PubKeyHashAlgOffset returns the offset in bytes of field PubKeyHashAlg
   391  func (s *Manifest) PubKeyHashAlgOffset() uint64 {
   392  	return s.KMIDOffset() + s.KMIDTotalSize()
   393  }
   394  
   395  // HashOffset returns the offset in bytes of field Hash
   396  func (s *Manifest) HashOffset() uint64 {
   397  	return s.PubKeyHashAlgOffset() + s.PubKeyHashAlgTotalSize()
   398  }
   399  
   400  // KeyAndSignatureOffset returns the offset in bytes of field KeyAndSignature
   401  func (s *Manifest) KeyAndSignatureOffset() uint64 {
   402  	return s.HashOffset() + s.HashTotalSize()
   403  }
   404  
   405  // Size returns the total size of the Manifest.
   406  func (s *Manifest) TotalSize() uint64 {
   407  	if s == nil {
   408  		return 0
   409  	}
   410  
   411  	var size uint64
   412  	size += s.StructInfoTotalSize()
   413  	size += s.KeyManifestSignatureOffsetTotalSize()
   414  	size += s.Reserved2TotalSize()
   415  	size += s.RevisionTotalSize()
   416  	size += s.KMSVNTotalSize()
   417  	size += s.KMIDTotalSize()
   418  	size += s.PubKeyHashAlgTotalSize()
   419  	size += s.HashTotalSize()
   420  	size += s.KeyAndSignatureTotalSize()
   421  	return size
   422  }
   423  
   424  // PrettyString returns the content of the structure in an easy-to-read format.
   425  func (s *Manifest) PrettyString(depth uint, withHeader bool, opts ...pretty.Option) string {
   426  	var lines []string
   427  	if withHeader {
   428  		lines = append(lines, pretty.Header(depth, "CBnT Key Manifest", s))
   429  	}
   430  	if s == nil {
   431  		return strings.Join(lines, "\n")
   432  	}
   433  	// ManifestFieldType is structInfo
   434  	lines = append(lines, pretty.SubValue(depth+1, "Struct Info", "", &s.StructInfo, opts...)...)
   435  	// ManifestFieldType is endValue
   436  	lines = append(lines, pretty.SubValue(depth+1, "Key Manifest Signature Offset", "", &s.KeyManifestSignatureOffset, opts...)...)
   437  	// ManifestFieldType is arrayStatic
   438  	lines = append(lines, pretty.SubValue(depth+1, "Reserved 2", "", &s.Reserved2, opts...)...)
   439  	// ManifestFieldType is endValue
   440  	lines = append(lines, pretty.SubValue(depth+1, "Revision", "", &s.Revision, opts...)...)
   441  	// ManifestFieldType is endValue
   442  	lines = append(lines, pretty.SubValue(depth+1, "KMSVN", "", &s.KMSVN, opts...)...)
   443  	// ManifestFieldType is endValue
   444  	lines = append(lines, pretty.SubValue(depth+1, "KMID", "", &s.KMID, opts...)...)
   445  	// ManifestFieldType is endValue
   446  	lines = append(lines, pretty.SubValue(depth+1, "Pub Key Hash Alg", "", &s.PubKeyHashAlg, opts...)...)
   447  	// ManifestFieldType is list
   448  	lines = append(lines, pretty.Header(depth+1, fmt.Sprintf("Hash: Array of \"Key Manifest\" of length %d", len(s.Hash)), s.Hash))
   449  	for i := 0; i < len(s.Hash); i++ {
   450  		lines = append(lines, fmt.Sprintf("%sitem #%d: ", strings.Repeat("  ", int(depth+2)), i)+strings.TrimSpace(s.Hash[i].PrettyString(depth+2, true)))
   451  	}
   452  	if depth < 1 {
   453  		lines = append(lines, "")
   454  	}
   455  	// ManifestFieldType is subStruct
   456  	lines = append(lines, pretty.SubValue(depth+1, "Key And Signature", "", &s.KeyAndSignature, opts...)...)
   457  	if depth < 2 {
   458  		lines = append(lines, "")
   459  	}
   460  	return strings.Join(lines, "\n")
   461  }