Debian Codegraph @

internal/public.go

raw
1 package internal
2
3 import (
4 "crypto"
5 "fmt"
6
7 "github.com/google/go-tpm/legacy/tpm2"
8 )
9
10 // GetSigningHashAlg returns the hash algorithm used for a signing key. Returns
11 // an error if an algorithm isn't supported, or the key is not a signing key.
12 func GetSigningHashAlg(pubArea tpm2.Public) (tpm2.Algorithm, error) {
13 if pubArea.Attributes&tpm2.FlagSign == 0 {
14 return tpm2.AlgNull, fmt.Errorf("non-signing key used with signing operation")
15 }
16
17 var sigScheme *tpm2.SigScheme
18 switch pubArea.Type {
19 case tpm2.AlgRSA:
20 sigScheme = pubArea.RSAParameters.Sign
21 case tpm2.AlgECC:
22 sigScheme = pubArea.ECCParameters.Sign
23 default:
24 return tpm2.AlgNull, fmt.Errorf("unsupported key type: %v", pubArea.Type)
25 }
26
27 if sigScheme == nil {
28 return tpm2.AlgNull, fmt.Errorf("unsupported null signing scheme")
29 }
30 switch sigScheme.Alg {
31 case tpm2.AlgRSAPSS, tpm2.AlgRSASSA, tpm2.AlgECDSA:
32 return sigScheme.Hash, nil
33 default:
34 return tpm2.AlgNull, fmt.Errorf("unsupported signing algorithm: %v", sigScheme.Alg)
35 }
36 }
37
38 // PubKeysEqual returns whether the two public keys are equal.
39 func PubKeysEqual(k1 crypto.PublicKey, k2 crypto.PublicKey) bool {
40 // Common interface for all the standard public key types, see:
41 // https://pkg.go.dev/[email protected]#PublicKey
42 type publicKey interface {
43 Equal(crypto.PublicKey) bool
44 }
45 if key, ok := k1.(publicKey); ok {
46 return key.Equal(k2)
47 }
48 return false
49 }