Comments (2)
Let's add a Go1.18 type constraint for this:
There's tons of types, so we can divide them by category:
type TPMA interface {
TPMAAlgorithm | TPMAObject | TPMASession | TPMALocality | ...
}
type Marshallable interface {
TPMA | TPMT | TPMS | ...
}
While we're taking a dependency on Go1.18, let's also consider using type constraints for the unions:
// TPMUCapabilities represents a TPMU_CAPABILITIES.
// See definition in Part 2: Structures, section 10.10.1.
type TPMUCapabilities struct {
Algorithms *TPMLAlgProperty `gotpm:"selector=0x00000000"` // TPM_CAP_ALGS
Handles *TPMLHandle `gotpm:"selector=0x00000001"` // TPM_CAP_HANDLES
Command *TPMLCCA `gotpm:"selector=0x00000002"` // TPM_CAP_COMMANDS
PPCommands *TPMLCC `gotpm:"selector=0x00000003"` // TPM_CAP_PP_COMMANDS
AuditCommands *TPMLCC `gotpm:"selector=0x00000004"` // TPM_CAP_AUDIT_COMMANDS
AssignedPCR *TPMLPCRSelection `gotpm:"selector=0x00000005"` // TPM_CAP_PCRS
TPMProperties *TPMLTaggedTPMProperty `gotpm:"selector=0x00000006"` // TPM_CAP_TPM_PROPERTIES
PCRProperties *TPMLTaggedPCRProperty `gotpm:"selector=0x00000007"` // TPM_CAP_PCR_PROPERTIES
ECCCurves *TPMLECCCurve `gotpm:"selector=0x00000008"` // TPM_CAP_ECC_CURVES
AuthPolicies *TPMLTaggedPolicy `gotpm:"selector=0x00000009"` // TPM_CAP_AUTH_POLICIES
ACTData *TPMLACTData `gotpm:"selector=0x0000000A"` // TPM_CAP_ACT
}
// TPMSCapabilityData represents a TPMS_CAPABILITY_DATA.
// See definition in Part 2: Structures, section 10.10.2.
type TPMSCapabilityData struct {
// the capability
Capability TPMCap
// the capability data
Data TPMUCapabilities `gotpm:"tag=Capability"`
}
We can make marshalling trivial with a type constraint:
// TPMUCapabilities represents a TPMU_CAPABILITIES.
// See definition in Part 2: Structures, section 10.10.1.
type TPMUCapabilities interface {
TPMLAlgProperty | TPMLHandle | TPMLCCA | TPMLCC | TPMLPCRSelection |TPMLTaggedTPMProperty | TPMLTaggedPCRProperty |TPMLECCCurve | TPMLTaggedPolicy | TPMLACTData
}
// TPMSCapabilityData represents a TPMS_CAPABILITY_DATA.
// See definition in Part 2: Structures, section 10.10.2.
type TPMSCapabilityData struct {
// the capability
Capability TPMCap
// the capability data
Data TPMUCapabilities
}
This has the added benefit of deleting a level of nesting:
parms := tpmu.PublicParms{
ECCDetail: &tpms.ECCParms{
Scheme: tpmt.ECCScheme{
Scheme: tpm.AlgECDSA,
Details: tpmu.AsymScheme{
ECDSA: &tpms.SigSchemeECDSA{
HashAlg: tpm.AlgSHA256,
},
},
},
CurveID: tpm.ECCNistP256,
},
}
to
parms := tpmu.PublicParms{
ECCDetail: &tpms.ECCParms{
Scheme: tpmt.ECCScheme{
Scheme: tpm.AlgECDSA,
Details: tpms.SigSchemeECDSA{
HashAlg: tpm.AlgSHA256,
},
},
CurveID: tpm.ECCNistP256,
},
}
However we have to do an extra step on the library side to make unmarshalling work. For each of the TPMU
we have to define a "custom" unmarshalling function that checks the value of the selector and unmarshals a value of the correct corresponding type. This is not a big deal, given that there are about 18 or so TPMU
structures - not too many to do by hand.
The Unmarshal
function can check if a type implements customUnmarshallable
and invoke the custom unmarshaller, and we can hide the details of this from the callers. Also, removing the TPMU
handler type annotations from the library will simplify it at least enough to offset the complexity of adding an interface.
from go-tpm.
Similar to what we saw in #307, TPMU types can't be based on type constraints since they have to be types.
from go-tpm.
Related Issues (20)
- Add helper for tpmDirect ObjectAttributes
- tpmDirect Design Feedback HOT 2
- delete the struct aliases & consider merging all of structures and commands into one 'tpm2' package HOT 4
- reduce repetitive, nested structs by proving a defaults package HOT 1
- support passing []byte as TPM2B HOT 2
- Add a Compare function
- Load TSS2 Private Key generated with tpm2tss-genkey HOT 2
- Inconsistency between written and read values when accessing PCR HOT 3
- reduce unnecessary overhead of Tbsi_Get_TCG_Log HOT 1
- Missing ExtraData in Quote
- Use `crypto/ecdh` for tpmdirect HOT 5
- Darwin ? HOT 1
- EvictControl missing from new tpm2 HOT 10
- Consider an UnmarshalReader API for types
- HMAC Session not working HOT 2
- Support As(TPMRC) for format-1 errors.
- Support serialization of persistent handles HOT 1
- Support for RSAEncrypt and RSADecrypt commands HOT 1
- TPM Simulator reporting unrecognised command over socket
- Implement TPM2_HMAC_Start
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-tpm.