github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/cbnt/crypto_routines.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 cbnt 8 9 import ( 10 "crypto" 11 // Required for hash.Hash return in hashInfo struct 12 _ "crypto/sha1" 13 _ "crypto/sha256" 14 _ "crypto/sha512" 15 "fmt" 16 "hash" 17 "strings" 18 19 "github.com/tjfoc/gmsm/sm3" 20 ) 21 22 // Algorithm represents a crypto algorithm value. 23 type Algorithm uint16 24 25 // Supported algorithms 26 const ( 27 AlgUnknown Algorithm = 0x0000 28 AlgRSA Algorithm = 0x0001 29 AlgSHA1 Algorithm = 0x0004 30 AlgSHA256 Algorithm = 0x000B 31 AlgSHA384 Algorithm = 0x000C 32 AlgSHA512 Algorithm = 0x000D 33 AlgNull Algorithm = 0x0010 34 AlgSM3 Algorithm = 0x0012 35 AlgRSASSA Algorithm = 0x0014 36 AlgRSAPSS Algorithm = 0x0016 37 AlgECDSA Algorithm = 0x0018 38 AlgSM2 Algorithm = 0x001b 39 AlgECC Algorithm = 0x0023 40 ) 41 42 var hashInfo = []struct { 43 alg Algorithm 44 hashFactory func() hash.Hash 45 }{ 46 {AlgSHA1, crypto.SHA1.New}, 47 {AlgSHA256, crypto.SHA256.New}, 48 {AlgSHA384, crypto.SHA384.New}, 49 {AlgSHA512, crypto.SHA512.New}, 50 {AlgSM3, sm3.New}, 51 } 52 53 // IsNull returns true if a is AlgNull or zero (unset). 54 func (a Algorithm) IsNull() bool { 55 return a == AlgNull || a == AlgUnknown 56 } 57 58 // Hash returns a crypto.Hash based on the given id. 59 // An error is returned if the given algorithm is not a hash algorithm or is not available. 60 func (a Algorithm) Hash() (hash.Hash, error) { 61 for _, info := range hashInfo { 62 if info.alg == a { 63 if info.hashFactory == nil { 64 return nil, fmt.Errorf("go hash algorithm #%snot available", info.alg.String()) 65 } 66 return info.hashFactory(), nil 67 } 68 } 69 return nil, fmt.Errorf("hash algorithm not supported: %s", a.String()) 70 } 71 72 func (a Algorithm) String() string { 73 var s strings.Builder 74 var err error 75 switch a { 76 case AlgUnknown: 77 _, err = s.WriteString("AlgUnknown") 78 case AlgRSA: 79 _, err = s.WriteString("RSA") 80 case AlgSHA1: 81 _, err = s.WriteString("SHA1") 82 case AlgSHA256: 83 _, err = s.WriteString("SHA256") 84 case AlgSHA384: 85 _, err = s.WriteString("SHA384") 86 case AlgSHA512: 87 _, err = s.WriteString("SHA512") 88 case AlgSM3: 89 _, err = s.WriteString("SM3_256") 90 case AlgNull: 91 _, err = s.WriteString("AlgNull") 92 case AlgRSASSA: 93 _, err = s.WriteString("RSASSA") 94 case AlgRSAPSS: 95 _, err = s.WriteString("RSAPSS") 96 case AlgECDSA: 97 _, err = s.WriteString("ECDSA") 98 case AlgECC: 99 _, err = s.WriteString("ECC") 100 case AlgSM2: 101 _, err = s.WriteString("SM2") 102 default: 103 return fmt.Sprintf("Alg?<%d>", int(a)) 104 } 105 if err != nil { 106 return fmt.Sprintf("Writing to string builder failed: %v", err) 107 } 108 return s.String() 109 } 110 111 func GetAlgFromString(name string) (Algorithm, error) { 112 n := strings.ToUpper(name) 113 switch n { 114 case "ALGUNKNOWN": 115 return AlgUnknown, nil 116 case "RSA": 117 return AlgRSA, nil 118 case "SHA1": 119 return AlgSHA1, nil 120 case "SHA256": 121 return AlgSHA256, nil 122 case "SHA384": 123 return AlgSHA384, nil 124 case "SM3": 125 return AlgSM3, nil 126 case "ALGNULL": 127 return AlgNull, nil 128 case "RSASSA": 129 return AlgRSASSA, nil 130 case "RSAPSS": 131 return AlgRSAPSS, nil 132 case "ECDSA": 133 return AlgECDSA, nil 134 case "ECC": 135 return AlgECC, nil 136 case "SM2": 137 return AlgSM2, nil 138 default: 139 return AlgNull, fmt.Errorf("algorithm name provided unknown") 140 } 141 }