| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
|
| 6 |
"github.com/google/go-tpm/legacy/tpm2" |
| 7 |
"github.com/google/go-tpm/tpmutil" |
| 8 |
) |
| 9 |
|
| 10 |
// Calculations from Credential_Profile_EK_V2.0, section 2.1.5.3 - authPolicy |
| 11 |
func defaultEKAuthPolicy() []byte { |
| 12 |
buf, err := tpmutil.Pack(tpm2.CmdPolicySecret, tpm2.HandleEndorsement) |
| 13 |
if err != nil { |
| 14 |
panic(err) |
| 15 |
} |
| 16 |
digest1 := sha256.Sum256(append(make([]byte, 32), buf...)) |
| 17 |
// We would normally append the policy buffer to digest1, but the |
| 18 |
// policy buffer is empty for the default Auth Policy. |
| 19 |
digest2 := sha256.Sum256(digest1[:]) |
| 20 |
return digest2[:] |
| 21 |
} |
| 22 |
|
| 23 |
func defaultEKAttributes() tpm2.KeyProp { |
| 24 |
// The EK is a storage key that must use session-based authorization. |
| 25 |
return (tpm2.FlagStorageDefault | tpm2.FlagAdminWithPolicy) & ^tpm2.FlagUserWithAuth |
| 26 |
} |
| 27 |
|
| 28 |
func defaultSRKAttributes() tpm2.KeyProp { |
| 29 |
// FlagNoDA doesn't do anything (as the AuthPolicy is nil). However, this is |
| 30 |
// what Windows does, and we don't want to conflict. |
| 31 |
return tpm2.FlagStorageDefault | tpm2.FlagNoDA |
| 32 |
} |
| 33 |
|
| 34 |
func defaultSymScheme() *tpm2.SymScheme { |
| 35 |
return &tpm2.SymScheme{ |
| 36 |
Alg: tpm2.AlgAES, |
| 37 |
KeyBits: 128, |
| 38 |
Mode: tpm2.AlgCFB, |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func defaultRSAParams() *tpm2.RSAParams { |
| 43 |
return &tpm2.RSAParams{ |
| 44 |
Symmetric: defaultSymScheme(), |
| 45 |
KeyBits: 2048, |
| 46 |
ModulusRaw: make([]byte, 256), // public.unique must be all zeros |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func defaultECCParams() *tpm2.ECCParams { |
| 51 |
return &tpm2.ECCParams{ |
| 52 |
Symmetric: defaultSymScheme(), |
| 53 |
CurveID: tpm2.CurveNISTP256, |
| 54 |
Point: tpm2.ECPoint{ |
| 55 |
XRaw: make([]byte, 32), |
| 56 |
YRaw: make([]byte, 32), |
| 57 |
}, |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// DefaultEKTemplateRSA returns the default Endorsement Key (EK) template as |
| 62 |
// specified in Credential_Profile_EK_V2.0, section 2.1.5.1 - authPolicy. |
| 63 |
// https://trustedcomputinggroup.org/wp-content/uploads/Credential_Profile_EK_V2.0_R14_published.pdf |
| 64 |
func DefaultEKTemplateRSA() tpm2.Public { |
| 65 |
return tpm2.Public{ |
| 66 |
Type: tpm2.AlgRSA, |
| 67 |
NameAlg: tpm2.AlgSHA256, |
| 68 |
Attributes: defaultEKAttributes(), |
| 69 |
AuthPolicy: defaultEKAuthPolicy(), |
| 70 |
RSAParameters: defaultRSAParams(), |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// DefaultEKTemplateECC returns the default Endorsement Key (EK) template as |
| 75 |
// specified in Credential_Profile_EK_V2.0, section 2.1.5.2 - authPolicy. |
| 76 |
// https://trustedcomputinggroup.org/wp-content/uploads/Credential_Profile_EK_V2.0_R14_published.pdf |
| 77 |
func DefaultEKTemplateECC() tpm2.Public { |
| 78 |
return tpm2.Public{ |
| 79 |
Type: tpm2.AlgECC, |
| 80 |
NameAlg: tpm2.AlgSHA256, |
| 81 |
Attributes: defaultEKAttributes(), |
| 82 |
AuthPolicy: defaultEKAuthPolicy(), |
| 83 |
ECCParameters: defaultECCParams(), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// AKTemplateRSA returns a potential Attestation Key (AK) template. |
| 88 |
// This is very similar to DefaultEKTemplateRSA, except that this will be a |
| 89 |
// signing key instead of an encrypting key. |
| 90 |
func AKTemplateRSA() tpm2.Public { |
| 91 |
return tpm2.Public{ |
| 92 |
Type: tpm2.AlgRSA, |
| 93 |
NameAlg: tpm2.AlgSHA256, |
| 94 |
Attributes: tpm2.FlagSignerDefault, |
| 95 |
RSAParameters: &tpm2.RSAParams{ |
| 96 |
Sign: &tpm2.SigScheme{ |
| 97 |
Alg: tpm2.AlgRSASSA, |
| 98 |
Hash: tpm2.AlgSHA256, |
| 99 |
}, |
| 100 |
KeyBits: 2048, |
| 101 |
}, |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// AKTemplateECC returns a potential Attestation Key (AK) template. |
| 106 |
// This is very similar to DefaultEKTemplateECC, except that this will be a |
| 107 |
// signing key instead of an encrypting key. |
| 108 |
func AKTemplateECC() tpm2.Public { |
| 109 |
params := defaultECCParams() |
| 110 |
params.Symmetric = nil |
| 111 |
params.Sign = &tpm2.SigScheme{ |
| 112 |
Alg: tpm2.AlgECDSA, |
| 113 |
Hash: tpm2.AlgSHA256, |
| 114 |
} |
| 115 |
return tpm2.Public{ |
| 116 |
Type: tpm2.AlgECC, |
| 117 |
NameAlg: tpm2.AlgSHA256, |
| 118 |
Attributes: tpm2.FlagSignerDefault, |
| 119 |
ECCParameters: params, |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// SRKTemplateRSA returns a standard Storage Root Key (SRK) template. |
| 124 |
// This is based upon the advice in the TCG's TPM v2.0 Provisioning Guidance. |
| 125 |
func SRKTemplateRSA() tpm2.Public { |
| 126 |
return tpm2.Public{ |
| 127 |
Type: tpm2.AlgRSA, |
| 128 |
NameAlg: tpm2.AlgSHA256, |
| 129 |
Attributes: defaultSRKAttributes(), |
| 130 |
RSAParameters: defaultRSAParams(), |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// SRKTemplateECC returns a standard Storage Root Key (SRK) template. |
| 135 |
// This is based upon the advice in the TCG's TPM v2.0 Provisioning Guidance. |
| 136 |
func SRKTemplateECC() tpm2.Public { |
| 137 |
return tpm2.Public{ |
| 138 |
Type: tpm2.AlgECC, |
| 139 |
NameAlg: tpm2.AlgSHA256, |
| 140 |
Attributes: defaultSRKAttributes(), |
| 141 |
ECCParameters: defaultECCParams(), |
| 142 |
} |
| 143 |
} |