github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/bg/crypto_routines.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  import (
    10  	"crypto"
    11  	"fmt"
    12  	"hash"
    13  	"strings"
    14  
    15  	// Required for hash.Hash return in hashInfo struct
    16  	_ "crypto/sha1"
    17  	_ "crypto/sha256"
    18  	_ "crypto/sha512"
    19  )
    20  
    21  // Algorithm represents a crypto algorithm value.
    22  type Algorithm uint16
    23  
    24  const (
    25  	AlgUnknown Algorithm = 0x0000
    26  	AlgRSA     Algorithm = 0x0001
    27  	AlgSHA1    Algorithm = 0x0004
    28  	AlgSHA256  Algorithm = 0x000B
    29  	AlgNull    Algorithm = 0x0010
    30  	AlgRSASSA  Algorithm = 0x0014
    31  )
    32  
    33  var hashInfo = []struct {
    34  	alg  Algorithm
    35  	hash hash.Hash
    36  }{
    37  	{AlgSHA1, crypto.SHA1.New()},
    38  	{AlgSHA256, crypto.SHA256.New()},
    39  }
    40  
    41  // IsNull returns true if a is AlgNull or zero (unset).
    42  func (a Algorithm) IsNull() bool {
    43  	return a == AlgNull || a == AlgUnknown
    44  }
    45  
    46  // Hash returns a crypto.Hash based on the given id.
    47  // An error is returned if the given algorithm is not a hash algorithm or is not available.
    48  func (a Algorithm) Hash() (hash.Hash, error) {
    49  	for _, info := range hashInfo {
    50  		if info.alg == a {
    51  			if info.hash == nil {
    52  				return nil, fmt.Errorf("go hash algorithm #%snot available", info.alg.String())
    53  			}
    54  			return info.hash, nil
    55  		}
    56  	}
    57  	return nil, fmt.Errorf("hash algorithm not supported: %s", a.String())
    58  }
    59  
    60  func (a Algorithm) String() string {
    61  	var s strings.Builder
    62  	var err error
    63  	switch a {
    64  	case AlgUnknown:
    65  		_, err = s.WriteString("AlgUnknown")
    66  	case AlgRSA:
    67  		_, err = s.WriteString("RSA")
    68  	case AlgSHA1:
    69  		_, err = s.WriteString("SHA1")
    70  	case AlgSHA256:
    71  		_, err = s.WriteString("SHA256")
    72  	case AlgNull:
    73  		_, err = s.WriteString("AlgNull")
    74  	case AlgRSASSA:
    75  		_, err = s.WriteString("RSASSA")
    76  	default:
    77  		return fmt.Sprintf("Alg?<%d>", int(a))
    78  	}
    79  	if err != nil {
    80  		return fmt.Sprintf("Writing to string builder failed: %v", err)
    81  	}
    82  	return s.String()
    83  }
    84  
    85  func GetAlgFromString(name string) (Algorithm, error) {
    86  	n := strings.ToUpper(name)
    87  	switch n {
    88  	case "ALGUNKNOWN":
    89  		return AlgUnknown, nil
    90  	case "RSA":
    91  		return AlgRSA, nil
    92  	case "SHA1":
    93  		return AlgSHA1, nil
    94  	case "SHA256":
    95  		return AlgSHA256, nil
    96  	case "ALGNULL":
    97  		return AlgNull, nil
    98  	case "RSASSA":
    99  		return AlgRSASSA, nil
   100  	default:
   101  		return AlgNull, fmt.Errorf("algorithm name provided unknown")
   102  	}
   103  }