1 Star 0 Fork 0

mysnapcore / mygo-tpm2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
templates.go 30.49 KB
一键复制 编辑 原始数据 按行查看 历史
tupelo-shen 提交于 2022-11-10 09:54 . fix: modified reference modules
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
/*
Package template contains helpers for constructing templates to create objects with go-tpm2.
*/
package templates
import tpm2 "gitee.com/mysnapcore/mygo-tpm2"
type KeyUsage int
const (
KeyUsageSign KeyUsage = 1 << iota
KeyUsageDecrypt
KeyUsageEncrypt = KeyUsageSign
)
// NewRSAStorageKey returns a template for a RSA storage parent with the specified
// name algorithm, symmetric cipher, symmetric key size and RSA key size. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If algorithm is
// SymObjectAlgorithmNull, then SymObjectAlgorithmAES is used. If symKeyBits is zero,
// then 128 is used. If asymKeyBits is zero, then 2048 is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRSAStorageKey(nameAlg tpm2.HashAlgorithmId, algorithm tpm2.SymObjectAlgorithmId, symKeyBits, asymKeyBits uint16) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if algorithm == tpm2.SymObjectAlgorithmNull {
algorithm = tpm2.SymObjectAlgorithmAES
}
if symKeyBits == 0 {
symKeyBits = 128
}
if asymKeyBits == 0 {
asymKeyBits = 2048
}
return &tpm2.Public{
Type: tpm2.ObjectTypeRSA,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrRestricted | tpm2.AttrDecrypt,
Params: &tpm2.PublicParamsU{
RSADetail: &tpm2.RSAParams{
Symmetric: tpm2.SymDefObject{
Algorithm: algorithm,
KeyBits: &tpm2.SymKeyBitsU{Sym: symKeyBits},
Mode: &tpm2.SymModeU{Sym: tpm2.SymModeCFB}},
Scheme: tpm2.RSAScheme{Scheme: tpm2.RSASchemeNull},
KeyBits: asymKeyBits,
Exponent: 0}}}
}
// NewRSAStorageKeyWithDefaults returns a template for a RSA storage parent with
// SHA256 as the name algorithm, AES-128 as the symmetric cipher and 2048 bits as
// the RSA key size.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRSAStorageKeyWithDefaults() *tpm2.Public {
return NewRSAStorageKey(tpm2.HashAlgorithmNull, tpm2.SymObjectAlgorithmNull, 0, 0)
}
// NewRestrictedRSASigningKey returns a template for a restricted RSA signing
// key with the specified name algorithm, RSA scheme and RSA key size. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If scheme is nil, then
// RSASSA is used with the digest algorithm set to the same as the name
// algorithm. If keyBits is zero, then 2048 is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRestrictedRSASigningKey(nameAlg tpm2.HashAlgorithmId, scheme *tpm2.RSAScheme, keyBits uint16) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if scheme == nil {
scheme = &tpm2.RSAScheme{
Scheme: tpm2.RSASchemeRSASSA,
Details: &tpm2.AsymSchemeU{
RSASSA: &tpm2.SigSchemeRSASSA{HashAlg: nameAlg}}}
}
if keyBits == 0 {
keyBits = 2048
}
return &tpm2.Public{
Type: tpm2.ObjectTypeRSA,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrSign | tpm2.AttrRestricted,
Params: &tpm2.PublicParamsU{
RSADetail: &tpm2.RSAParams{
Symmetric: tpm2.SymDefObject{Algorithm: tpm2.SymObjectAlgorithmNull},
Scheme: *scheme,
KeyBits: keyBits,
Exponent: 0}}}
}
// NewRestrictedRSASigningKeyWithDefaults returns a template for a restricted RSA
// signing key with SHA256 as the name algorithm, RSA-SSA with SHA256 as the scheme
// and 2048 bits as the key size.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRestrictedRSASigningKeyWithDefaults() *tpm2.Public {
return NewRestrictedRSASigningKey(tpm2.HashAlgorithmNull, nil, 0)
}
// NewRSAKey returns a template for a general purpose RSA key for the specified
// usage, with the specified name algorithm, RSA scheme and RSA key size. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If keyBits is zero, then
// 2048 is used. If no usage is specified, the template will include both sign and
// decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRSAKey(nameAlg tpm2.HashAlgorithmId, usage KeyUsage, scheme *tpm2.RSAScheme, keyBits uint16) *tpm2.Public {
attrs := tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth
if usage == 0 {
usage = KeyUsageSign | KeyUsageDecrypt
}
if usage&KeyUsageSign != 0 {
attrs |= tpm2.AttrSign
}
if usage&KeyUsageDecrypt != 0 {
attrs |= tpm2.AttrDecrypt
}
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if scheme == nil {
scheme = &tpm2.RSAScheme{Scheme: tpm2.RSASchemeNull}
}
if keyBits == 0 {
keyBits = 2048
}
return &tpm2.Public{
Type: tpm2.ObjectTypeRSA,
NameAlg: nameAlg,
Attrs: attrs,
Params: &tpm2.PublicParamsU{
RSADetail: &tpm2.RSAParams{
Symmetric: tpm2.SymDefObject{Algorithm: tpm2.SymObjectAlgorithmNull},
Scheme: *scheme,
KeyBits: keyBits,
Exponent: 0}}}
}
// NewRSAKeyWithDefaults returns a template for a general purpose RSA key for the
// specified usage, with SHA256 as the name algorithm, the scheme unset and 2048 bits
// as the key size. If no usage is specified, the template will include both sign and
// decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRSAKeyWithDefaults(usage KeyUsage) *tpm2.Public {
return NewRSAKey(tpm2.HashAlgorithmNull, usage, nil, 0)
}
// NewSealedObject returns a template for a sealed object with the specified name
// algorithm. If nameAlg is HashAlgorithmNull, then HashAlgorithmSHA256 is used.
//
// The template cannot be used to create an object in a duplication group. In order to
// create an object in a duplication group, remove the AttrFixedTPM attribute. In
// order to create an object that can be moved to a new parent, remove both the
// AttrFixedTPM and AttrFixedParent attributes. In this case, an authorization policy
// that permits duplication must be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewSealedObject(nameAlg tpm2.HashAlgorithmId) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
return &tpm2.Public{
Type: tpm2.ObjectTypeKeyedHash,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrUserWithAuth,
Params: &tpm2.PublicParamsU{
KeyedHashDetail: &tpm2.KeyedHashParams{
Scheme: tpm2.KeyedHashScheme{Scheme: tpm2.KeyedHashSchemeNull}}}}
}
// NewECCStorageKey returns a template for a ECC storage parent with the specified
// name algorithm, symmetric cipher, symmetric key size and elliptic curve. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If algorithm is
// SymObjectAlgorithmNull, then SymObjectAlgorithmAES is used. If keyBits is zero,
// then 128 is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewECCStorageKey(nameAlg tpm2.HashAlgorithmId, algorithm tpm2.SymObjectAlgorithmId, keyBits uint16, curve tpm2.ECCCurve) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if algorithm == tpm2.SymObjectAlgorithmNull {
algorithm = tpm2.SymObjectAlgorithmAES
}
if keyBits == 0 {
keyBits = 128
}
return &tpm2.Public{
Type: tpm2.ObjectTypeECC,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrRestricted | tpm2.AttrDecrypt,
Params: &tpm2.PublicParamsU{
ECCDetail: &tpm2.ECCParams{
Symmetric: tpm2.SymDefObject{
Algorithm: algorithm,
KeyBits: &tpm2.SymKeyBitsU{Sym: keyBits},
Mode: &tpm2.SymModeU{Sym: tpm2.SymModeCFB}},
Scheme: tpm2.ECCScheme{Scheme: tpm2.ECCSchemeNull},
CurveID: curve,
KDF: tpm2.KDFScheme{Scheme: tpm2.KDFAlgorithmNull}}}}
}
// NewECCStorageKeyWithDefaults returns a template for a ECC storage parent with
// SHA256 as the name algorithm, AES-128 as the symmetric cipher and the NIST-P256
// curve.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewECCStorageKeyWithDefaults() *tpm2.Public {
return NewECCStorageKey(tpm2.HashAlgorithmNull, tpm2.SymObjectAlgorithmNull, 0, tpm2.ECCCurveNIST_P256)
}
// NewRestrictedECCSigningKey returns a template for a restricted ECC signing
// key with the specified name algorithm, ECC scheme and elliptic curve. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If scheme is nil, then
// ECDSA is used with the digest algorithm set to the same as the name algorithm.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRestrictedECCSigningKey(nameAlg tpm2.HashAlgorithmId, scheme *tpm2.ECCScheme, curve tpm2.ECCCurve) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if scheme == nil {
scheme = &tpm2.ECCScheme{
Scheme: tpm2.ECCSchemeECDSA,
Details: &tpm2.AsymSchemeU{
ECDSA: &tpm2.SigSchemeECDSA{HashAlg: nameAlg}}}
}
return &tpm2.Public{
Type: tpm2.ObjectTypeECC,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrSign | tpm2.AttrRestricted,
Params: &tpm2.PublicParamsU{
ECCDetail: &tpm2.ECCParams{
Symmetric: tpm2.SymDefObject{Algorithm: tpm2.SymObjectAlgorithmNull},
Scheme: *scheme,
CurveID: curve,
KDF: tpm2.KDFScheme{Scheme: tpm2.KDFAlgorithmNull}}}}
}
// NewRestrictedECCSigningKeyWithDefaults returns a template for a restricted ECC
// signing key with SHA256 as the name algorithm, ECDSA with SHA256 as the scheme and
// NIST-P256 as the curve.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewRestrictedECCSigningKeyWithDefaults() *tpm2.Public {
return NewRestrictedECCSigningKey(tpm2.HashAlgorithmNull, nil, tpm2.ECCCurveNIST_P256)
}
// NewECCKey returns a template for a general purpose ECC key for the specified
// usage, with the specified name algorithm, ECC scheme and elliptic curve. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If no usage is specified,
// the template will include both sign and decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewECCKey(nameAlg tpm2.HashAlgorithmId, usage KeyUsage, scheme *tpm2.ECCScheme, curve tpm2.ECCCurve) *tpm2.Public {
attrs := tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth
if usage == 0 {
usage = KeyUsageSign | KeyUsageDecrypt
}
if usage&KeyUsageSign != 0 {
attrs |= tpm2.AttrSign
}
if usage&KeyUsageDecrypt != 0 {
attrs |= tpm2.AttrDecrypt
}
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if scheme == nil {
scheme = &tpm2.ECCScheme{Scheme: tpm2.ECCSchemeNull}
}
return &tpm2.Public{
Type: tpm2.ObjectTypeECC,
NameAlg: nameAlg,
Attrs: attrs,
Params: &tpm2.PublicParamsU{
ECCDetail: &tpm2.ECCParams{
Symmetric: tpm2.SymDefObject{Algorithm: tpm2.SymObjectAlgorithmNull},
Scheme: *scheme,
CurveID: curve,
KDF: tpm2.KDFScheme{Scheme: tpm2.KDFAlgorithmNull}}}}
}
// NewECCKeyWithDefaults returns a template for a general purpose ECC key for the
// specified usage, with SHA256 as the name algorithm, the scheme unset and NIST-P256
// as the curve. If no usage is specified, the template will include both sign and
// decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewECCKeyWithDefaults(usage KeyUsage) *tpm2.Public {
return NewECCKey(tpm2.HashAlgorithmNull, usage, nil, tpm2.ECCCurveNIST_P256)
}
// NewSymmetricStorageKey returns a template for a symmetric storage parent with the
// specified name algorithm, symmetric cipher and symmetric key size. If nameAlg
// is HashAlgorithmNull, then HashAlgorithmSHA256 is used. If algorithm is
// SymObjectAlgorithmNull, then SymObjectAlgorithmAES is used. If keyBits is zero,
// then 128 is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewSymmetricStorageKey(nameAlg tpm2.HashAlgorithmId, algorithm tpm2.SymObjectAlgorithmId, keyBits uint16) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if algorithm == tpm2.SymObjectAlgorithmNull {
algorithm = tpm2.SymObjectAlgorithmAES
}
if keyBits == 0 {
keyBits = 128
}
return &tpm2.Public{
Type: tpm2.ObjectTypeSymCipher,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrRestricted | tpm2.AttrDecrypt,
Params: &tpm2.PublicParamsU{
SymDetail: &tpm2.SymCipherParams{
Sym: tpm2.SymDefObject{
Algorithm: algorithm,
KeyBits: &tpm2.SymKeyBitsU{Sym: keyBits},
Mode: &tpm2.SymModeU{Sym: tpm2.SymModeCFB}}}}}
}
// NewSymmetricStorageKeyWithDefaults returns a template for a symmetric storage
// parent with SHA256 as the name algorithm and AES-128 as the symmetric cipher.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewSymmetricStorageKeyWithDefaults() *tpm2.Public {
return NewSymmetricStorageKey(tpm2.HashAlgorithmNull, tpm2.SymObjectAlgorithmNull, 0)
}
// NewSymmetricKey returns a template for a general purpose symmetric key with
// the specified name algorithm, key usage, symmetic algorithm, symmetric key size
// and symmetric mode. If nameAlg is HashAlgorithmNull, then HashAlgorithmSHA256
// is used. If algorithm is SymObjectAlgorithmNull, then SymObjectAlgorithmAES is
// used. If keyBits is zero, then 128 is used. If no usage is specified, the template
// will include both sign and decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewSymmetricKey(nameAlg tpm2.HashAlgorithmId, usage KeyUsage, algorithm tpm2.SymObjectAlgorithmId, keyBits uint16, mode tpm2.SymModeId) *tpm2.Public {
attrs := tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth
if usage == 0 {
usage = KeyUsageEncrypt | KeyUsageDecrypt
}
if usage&KeyUsageEncrypt != 0 {
attrs |= tpm2.AttrSign
}
if usage&KeyUsageDecrypt != 0 {
attrs |= tpm2.AttrDecrypt
}
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if algorithm == tpm2.SymObjectAlgorithmNull {
algorithm = tpm2.SymObjectAlgorithmAES
}
if keyBits == 0 {
keyBits = 128
}
return &tpm2.Public{
Type: tpm2.ObjectTypeSymCipher,
NameAlg: nameAlg,
Attrs: attrs,
Params: &tpm2.PublicParamsU{
SymDetail: &tpm2.SymCipherParams{
Sym: tpm2.SymDefObject{
Algorithm: algorithm,
KeyBits: &tpm2.SymKeyBitsU{Sym: keyBits},
Mode: &tpm2.SymModeU{Sym: mode}}}}}
}
// NewSymmetricKeyWithDefaults returns a template for a general purpose symmetric
// key for the specified usage with SHA256 as the name algorithm, AES-128 as the
// cipher and CFB as the cipher mode. If no usage is specified, the template will
// include both sign and decrypt attributes.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewSymmetricKeyWithDefaults(usage KeyUsage) *tpm2.Public {
return NewSymmetricKey(tpm2.HashAlgorithmNull, usage, tpm2.SymObjectAlgorithmNull, 0, tpm2.SymModeCFB)
}
// NewHMACKey returns a template for a HMAC key with the specified name algorithm
// and HMAC digest algorithm. If nameAlg is HashAlgorithmNull, then HashAlgorithmSHA256
// is used. If schemeAlg is HashAlgorithmNull, then nameAlg is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewHMACKey(nameAlg, schemeAlg tpm2.HashAlgorithmId) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if schemeAlg == tpm2.HashAlgorithmNull {
schemeAlg = nameAlg
}
return &tpm2.Public{
Type: tpm2.ObjectTypeKeyedHash,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrSign,
Params: &tpm2.PublicParamsU{
KeyedHashDetail: &tpm2.KeyedHashParams{
Scheme: tpm2.KeyedHashScheme{
Scheme: tpm2.KeyedHashSchemeHMAC,
Details: &tpm2.SchemeKeyedHashU{
HMAC: &tpm2.SchemeHMAC{
HashAlg: schemeAlg}}}}}}
}
// NewHMACKeyWithDefaults returns a template for a HMAC key with SHA256 as the
// name algorithm and the HMAC digest algorithm.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a TPM generated key. In order to supply the key, remove
// the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewHMACKeyWithDefaults() *tpm2.Public {
return NewHMACKey(tpm2.HashAlgorithmNull, tpm2.HashAlgorithmNull)
}
// NewDerivationParentKey returns a template for derivation parent key with the
// specified name algorithm and KDF digest algorithm. If nameAlg is HashAlgorithmNull,
// then HashAlgorithmSHA256 is used. If schemeAlg is HashAlgorithmNull, then nameAlg
// is used.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a key with a TPM generated seed. In order to supply the
// seed, remove the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewDerivationParentKey(nameAlg, schemeAlg tpm2.HashAlgorithmId) *tpm2.Public {
if nameAlg == tpm2.HashAlgorithmNull {
nameAlg = tpm2.HashAlgorithmSHA256
}
if schemeAlg == tpm2.HashAlgorithmNull {
schemeAlg = nameAlg
}
return &tpm2.Public{
Type: tpm2.ObjectTypeKeyedHash,
NameAlg: nameAlg,
Attrs: tpm2.AttrFixedTPM | tpm2.AttrFixedParent | tpm2.AttrSensitiveDataOrigin | tpm2.AttrUserWithAuth | tpm2.AttrDecrypt | tpm2.AttrRestricted,
Params: &tpm2.PublicParamsU{
KeyedHashDetail: &tpm2.KeyedHashParams{
Scheme: tpm2.KeyedHashScheme{
Scheme: tpm2.KeyedHashSchemeXOR,
Details: &tpm2.SchemeKeyedHashU{
XOR: &tpm2.SchemeXOR{
HashAlg: schemeAlg,
KDF: tpm2.KDFAlgorithmKDF1_SP800_108}}}}}}
}
// NewDerivationParentKeyWithDefaults returns a template for derivation parent key
// with SHA256 as the name algorithm and KDF digest algorithm.
//
// The template cannot be used to create a key in a duplication group. In order to create
// a key in a duplication group, remove the AttrFixedTPM attribute. In order to create
// a key that is a duplication root, remove both the AttrFixedTPM and AttrFixedParent
// attributes. In this case, an authorization policy that permits duplication must
// be added.
//
// The template will create a key with a TPM generated seed. In order to supply the
// seed, remove the AttrSensitiveDataOrigin attribute.
//
// The template has the AttrUserWithAuth set in order to permit authentication for
// the user auth role using the created object's authorization value. In order to
// require authentication for the user auth role using an authorization policy,
// remove the AttrUserWithAuth attribute.
func NewDerivationParentKeyWithDefaults() *tpm2.Public {
return NewDerivationParentKey(tpm2.HashAlgorithmNull, tpm2.HashAlgorithmNull)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mysnapcore/mygo-tpm2.git
git@gitee.com:mysnapcore/mygo-tpm2.git
mysnapcore
mygo-tpm2
mygo-tpm2
v0.0.6

搜索帮助

344bd9b3 5694891 D2dac590 5694891