You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pubarea.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package googletpm
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math/big"
  6. )
  7. // DecodePublic decodes a TPMT_PUBLIC message. No error is returned if
  8. // the input has extra trailing data.
  9. func DecodePublic(buf []byte) (Public, error) {
  10. in := bytes.NewBuffer(buf)
  11. var pub Public
  12. var err error
  13. if err = UnpackBuf(in, &pub.Type, &pub.NameAlg, &pub.Attributes, &pub.AuthPolicy); err != nil {
  14. return pub, fmt.Errorf("decoding TPMT_PUBLIC: %v", err)
  15. }
  16. switch pub.Type {
  17. case AlgRSA:
  18. pub.RSAParameters, err = decodeRSAParams(in)
  19. case AlgECC:
  20. pub.ECCParameters, err = decodeECCParams(in)
  21. default:
  22. err = fmt.Errorf("unsupported type in TPMT_PUBLIC: %v", pub.Type)
  23. }
  24. return pub, err
  25. }
  26. // Public contains the public area of an object.
  27. type Public struct {
  28. Type Algorithm
  29. NameAlg Algorithm
  30. Attributes KeyProp
  31. AuthPolicy []byte
  32. // If Type is AlgKeyedHash, then do not set these.
  33. // Otherwise, only one of the Parameters fields should be set. When encoding/decoding,
  34. // one will be picked based on Type.
  35. RSAParameters *RSAParams
  36. ECCParameters *ECCParams
  37. }
  38. // Algorithm represents a TPM_ALG_ID value.
  39. type Algorithm uint16
  40. // KeyProp is a bitmask used in Attributes field of key templates. Individual
  41. // flags should be OR-ed to form a full mask.
  42. type KeyProp uint32
  43. // Key properties.
  44. const (
  45. FlagFixedTPM KeyProp = 0x00000002
  46. FlagFixedParent KeyProp = 0x00000010
  47. FlagSensitiveDataOrigin KeyProp = 0x00000020
  48. FlagUserWithAuth KeyProp = 0x00000040
  49. FlagAdminWithPolicy KeyProp = 0x00000080
  50. FlagNoDA KeyProp = 0x00000400
  51. FlagRestricted KeyProp = 0x00010000
  52. FlagDecrypt KeyProp = 0x00020000
  53. FlagSign KeyProp = 0x00040000
  54. FlagSealDefault = FlagFixedTPM | FlagFixedParent
  55. FlagSignerDefault = FlagSign | FlagRestricted | FlagFixedTPM |
  56. FlagFixedParent | FlagSensitiveDataOrigin | FlagUserWithAuth
  57. FlagStorageDefault = FlagDecrypt | FlagRestricted | FlagFixedTPM |
  58. FlagFixedParent | FlagSensitiveDataOrigin | FlagUserWithAuth
  59. )
  60. func decodeRSAParams(in *bytes.Buffer) (*RSAParams, error) {
  61. var params RSAParams
  62. var err error
  63. if params.Symmetric, err = decodeSymScheme(in); err != nil {
  64. return nil, fmt.Errorf("decoding Symmetric: %v", err)
  65. }
  66. if params.Sign, err = decodeSigScheme(in); err != nil {
  67. return nil, fmt.Errorf("decoding Sign: %v", err)
  68. }
  69. var modBytes []byte
  70. if err := UnpackBuf(in, &params.KeyBits, &params.Exponent, &modBytes); err != nil {
  71. return nil, fmt.Errorf("decoding KeyBits, Exponent, Modulus: %v", err)
  72. }
  73. if params.Exponent == 0 {
  74. params.encodeDefaultExponentAsZero = true
  75. params.Exponent = defaultRSAExponent
  76. }
  77. params.Modulus = new(big.Int).SetBytes(modBytes)
  78. return &params, nil
  79. }
  80. const defaultRSAExponent = 1<<16 + 1
  81. // RSAParams represents parameters of an RSA key pair.
  82. //
  83. // Symmetric and Sign may be nil, depending on key Attributes in Public.
  84. //
  85. // One of Modulus and ModulusRaw must always be non-nil. Modulus takes
  86. // precedence. ModulusRaw is used for key templates where the field named
  87. // "unique" must be a byte array of all zeroes.
  88. type RSAParams struct {
  89. Symmetric *SymScheme
  90. Sign *SigScheme
  91. KeyBits uint16
  92. // The default Exponent (65537) has two representations; the
  93. // 0 value, and the value 65537.
  94. // If encodeDefaultExponentAsZero is set, an exponent of 65537
  95. // will be encoded as zero. This is necessary to produce an identical
  96. // encoded bitstream, so Name digest calculations will be correct.
  97. encodeDefaultExponentAsZero bool
  98. Exponent uint32
  99. ModulusRaw []byte
  100. Modulus *big.Int
  101. }
  102. // SymScheme represents a symmetric encryption scheme.
  103. type SymScheme struct {
  104. Alg Algorithm
  105. KeyBits uint16
  106. Mode Algorithm
  107. } // SigScheme represents a signing scheme.
  108. type SigScheme struct {
  109. Alg Algorithm
  110. Hash Algorithm
  111. Count uint32
  112. }
  113. func decodeSigScheme(in *bytes.Buffer) (*SigScheme, error) {
  114. var scheme SigScheme
  115. if err := UnpackBuf(in, &scheme.Alg); err != nil {
  116. return nil, fmt.Errorf("decoding Alg: %v", err)
  117. }
  118. if scheme.Alg == AlgNull {
  119. return nil, nil
  120. }
  121. if err := UnpackBuf(in, &scheme.Hash); err != nil {
  122. return nil, fmt.Errorf("decoding Hash: %v", err)
  123. }
  124. if scheme.Alg.UsesCount() {
  125. if err := UnpackBuf(in, &scheme.Count); err != nil {
  126. return nil, fmt.Errorf("decoding Count: %v", err)
  127. }
  128. }
  129. return &scheme, nil
  130. }
  131. // UsesCount returns true if a signature algorithm uses count value.
  132. func (a Algorithm) UsesCount() bool {
  133. return a == AlgECDAA
  134. }
  135. func decodeKDFScheme(in *bytes.Buffer) (*KDFScheme, error) {
  136. var scheme KDFScheme
  137. if err := UnpackBuf(in, &scheme.Alg); err != nil {
  138. return nil, fmt.Errorf("decoding Alg: %v", err)
  139. }
  140. if scheme.Alg == AlgNull {
  141. return nil, nil
  142. }
  143. if err := UnpackBuf(in, &scheme.Hash); err != nil {
  144. return nil, fmt.Errorf("decoding Hash: %v", err)
  145. }
  146. return &scheme, nil
  147. }
  148. func decodeSymScheme(in *bytes.Buffer) (*SymScheme, error) {
  149. var scheme SymScheme
  150. if err := UnpackBuf(in, &scheme.Alg); err != nil {
  151. return nil, fmt.Errorf("decoding Alg: %v", err)
  152. }
  153. if scheme.Alg == AlgNull {
  154. return nil, nil
  155. }
  156. if err := UnpackBuf(in, &scheme.KeyBits, &scheme.Mode); err != nil {
  157. return nil, fmt.Errorf("decoding KeyBits, Mode: %v", err)
  158. }
  159. return &scheme, nil
  160. }
  161. func decodeECCParams(in *bytes.Buffer) (*ECCParams, error) {
  162. var params ECCParams
  163. var err error
  164. if params.Symmetric, err = decodeSymScheme(in); err != nil {
  165. return nil, fmt.Errorf("decoding Symmetric: %v", err)
  166. }
  167. if params.Sign, err = decodeSigScheme(in); err != nil {
  168. return nil, fmt.Errorf("decoding Sign: %v", err)
  169. }
  170. if err := UnpackBuf(in, &params.CurveID); err != nil {
  171. return nil, fmt.Errorf("decoding CurveID: %v", err)
  172. }
  173. if params.KDF, err = decodeKDFScheme(in); err != nil {
  174. return nil, fmt.Errorf("decoding KDF: %v", err)
  175. }
  176. var x, y []byte
  177. if err := UnpackBuf(in, &x, &y); err != nil {
  178. return nil, fmt.Errorf("decoding Point: %v", err)
  179. }
  180. params.Point.X = new(big.Int).SetBytes(x)
  181. params.Point.Y = new(big.Int).SetBytes(y)
  182. return &params, nil
  183. }
  184. // ECCParams represents parameters of an ECC key pair.
  185. //
  186. // Symmetric, Sign and KDF may be nil, depending on key Attributes in Public.
  187. type ECCParams struct {
  188. Symmetric *SymScheme
  189. Sign *SigScheme
  190. CurveID EllipticCurve
  191. KDF *KDFScheme
  192. Point ECPoint
  193. }
  194. // EllipticCurve identifies specific EC curves.
  195. type EllipticCurve uint16
  196. // ECC curves supported by TPM 2.0 spec.
  197. const (
  198. CurveNISTP192 = EllipticCurve(iota + 1)
  199. CurveNISTP224
  200. CurveNISTP256
  201. CurveNISTP384
  202. CurveNISTP521
  203. CurveBNP256 = EllipticCurve(iota + 10)
  204. CurveBNP638
  205. CurveSM2P256 = EllipticCurve(0x0020)
  206. )
  207. // ECPoint represents a ECC coordinates for a point.
  208. type ECPoint struct {
  209. X, Y *big.Int
  210. }
  211. // KDFScheme represents a KDF (Key Derivation Function) scheme.
  212. type KDFScheme struct {
  213. Alg Algorithm
  214. Hash Algorithm
  215. }