Debian Codegraph @

legacy/tpm2/constants.go

raw
1 // Copyright (c) 2018, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tpm2
16
17 import (
18 "crypto"
19 "crypto/elliptic"
20 "fmt"
21 "strings"
22
23 // Register the relevant hash implementations to prevent a runtime failure.
24 _ "crypto/sha1"
25 _ "crypto/sha256"
26 _ "crypto/sha512"
27
28 "github.com/google/go-tpm/tpmutil"
29 )
30
31 var hashInfo = []struct {
32 alg Algorithm
33 hash crypto.Hash
34 }{
35 {AlgSHA1, crypto.SHA1},
36 {AlgSHA256, crypto.SHA256},
37 {AlgSHA384, crypto.SHA384},
38 {AlgSHA512, crypto.SHA512},
39 {AlgSHA3_256, crypto.SHA3_256},
40 {AlgSHA3_384, crypto.SHA3_384},
41 {AlgSHA3_512, crypto.SHA3_512},
42 }
43
44 // MAX_DIGEST_BUFFER is the maximum size of []byte request or response fields.
45 // Typically used for chunking of big blobs of data (such as for hashing or
46 // encryption).
47 const maxDigestBuffer = 1024
48
49 // Algorithm represents a TPM_ALG_ID value.
50 type Algorithm uint16
51
52 // HashToAlgorithm looks up the TPM2 algorithm corresponding to the provided crypto.Hash
53 func HashToAlgorithm(hash crypto.Hash) (Algorithm, error) {
54 for _, info := range hashInfo {
55 if info.hash == hash {
56 return info.alg, nil
57 }
58 }
59 return AlgUnknown, fmt.Errorf("go hash algorithm #%d has no TPM2 algorithm", hash)
60 }
61
62 // IsNull returns true if a is AlgNull or zero (unset).
63 func (a Algorithm) IsNull() bool {
64 return a == AlgNull || a == AlgUnknown
65 }
66
67 // UsesCount returns true if a signature algorithm uses count value.
68 func (a Algorithm) UsesCount() bool {
69 return a == AlgECDAA
70 }
71
72 // UsesHash returns true if the algorithm requires the use of a hash.
73 func (a Algorithm) UsesHash() bool {
74 return a == AlgOAEP
75 }
76
77 // Hash returns a crypto.Hash based on the given TPM_ALG_ID.
78 // An error is returned if the given algorithm is not a hash algorithm or is not available.
79 func (a Algorithm) Hash() (crypto.Hash, error) {
80 for _, info := range hashInfo {
81 if info.alg == a {
82 if !info.hash.Available() {
83 return crypto.Hash(0), fmt.Errorf("go hash algorithm #%d not available", info.hash)
84 }
85 return info.hash, nil
86 }
87 }
88 return crypto.Hash(0), fmt.Errorf("hash algorithm not supported: 0x%x", a)
89 }
90
91 func (a Algorithm) String() string {
92 var s strings.Builder
93 var err error
94 switch a {
95 case AlgUnknown:
96 _, err = s.WriteString("AlgUnknown")
97 case AlgRSA:
98 _, err = s.WriteString("RSA")
99 case AlgSHA1:
100 _, err = s.WriteString("SHA1")
101 case AlgHMAC:
102 _, err = s.WriteString("HMAC")
103 case AlgAES:
104 _, err = s.WriteString("AES")
105 case AlgKeyedHash:
106 _, err = s.WriteString("KeyedHash")
107 case AlgXOR:
108 _, err = s.WriteString("XOR")
109 case AlgSHA256:
110 _, err = s.WriteString("SHA256")
111 case AlgSHA384:
112 _, err = s.WriteString("SHA384")
113 case AlgSHA512:
114 _, err = s.WriteString("SHA512")
115 case AlgNull:
116 _, err = s.WriteString("AlgNull")
117 case AlgRSASSA:
118 _, err = s.WriteString("RSASSA")
119 case AlgRSAES:
120 _, err = s.WriteString("RSAES")
121 case AlgRSAPSS:
122 _, err = s.WriteString("RSAPSS")
123 case AlgOAEP:
124 _, err = s.WriteString("OAEP")
125 case AlgECDSA:
126 _, err = s.WriteString("ECDSA")
127 case AlgECDH:
128 _, err = s.WriteString("ECDH")
129 case AlgECDAA:
130 _, err = s.WriteString("ECDAA")
131 case AlgKDF2:
132 _, err = s.WriteString("KDF2")
133 case AlgECC:
134 _, err = s.WriteString("ECC")
135 case AlgSymCipher:
136 _, err = s.WriteString("SymCipher")
137 case AlgSHA3_256:
138 _, err = s.WriteString("SHA3_256")
139 case AlgSHA3_384:
140 _, err = s.WriteString("SHA3_384")
141 case AlgSHA3_512:
142 _, err = s.WriteString("SHA3_512")
143 case AlgCTR:
144 _, err = s.WriteString("CTR")
145 case AlgOFB:
146 _, err = s.WriteString("OFB")
147 case AlgCBC:
148 _, err = s.WriteString("CBC")
149 case AlgCFB:
150 _, err = s.WriteString("CFB")
151 case AlgECB:
152 _, err = s.WriteString("ECB")
153 default:
154 return fmt.Sprintf("Alg?<%d>", int(a))
155 }
156 if err != nil {
157 return fmt.Sprintf("Writing to string builder failed: %v", err)
158 }
159 return s.String()
160 }
161
162 // Supported Algorithms.
163 const (
164 AlgUnknown Algorithm = 0x0000
165 AlgRSA Algorithm = 0x0001
166 AlgSHA1 Algorithm = 0x0004
167 AlgHMAC Algorithm = 0x0005
168 AlgAES Algorithm = 0x0006
169 AlgKeyedHash Algorithm = 0x0008
170 AlgXOR Algorithm = 0x000A
171 AlgSHA256 Algorithm = 0x000B
172 AlgSHA384 Algorithm = 0x000C
173 AlgSHA512 Algorithm = 0x000D
174 AlgNull Algorithm = 0x0010
175 AlgRSASSA Algorithm = 0x0014
176 AlgRSAES Algorithm = 0x0015
177 AlgRSAPSS Algorithm = 0x0016
178 AlgOAEP Algorithm = 0x0017
179 AlgECDSA Algorithm = 0x0018
180 AlgECDH Algorithm = 0x0019
181 AlgECDAA Algorithm = 0x001A
182 AlgKDF2 Algorithm = 0x0021
183 AlgECC Algorithm = 0x0023
184 AlgSymCipher Algorithm = 0x0025
185 AlgSHA3_256 Algorithm = 0x0027
186 AlgSHA3_384 Algorithm = 0x0028
187 AlgSHA3_512 Algorithm = 0x0029
188 AlgCTR Algorithm = 0x0040
189 AlgOFB Algorithm = 0x0041
190 AlgCBC Algorithm = 0x0042
191 AlgCFB Algorithm = 0x0043
192 AlgECB Algorithm = 0x0044
193 )
194
195 // HandleType defines a type of handle.
196 type HandleType uint8
197
198 // Supported handle types
199 const (
200 HandleTypePCR HandleType = 0x00
201 HandleTypeNVIndex HandleType = 0x01
202 HandleTypeHMACSession HandleType = 0x02
203 HandleTypeLoadedSession HandleType = 0x02
204 HandleTypePolicySession HandleType = 0x03
205 HandleTypeSavedSession HandleType = 0x03
206 HandleTypePermanent HandleType = 0x40
207 HandleTypeTransient HandleType = 0x80
208 HandleTypePersistent HandleType = 0x81
209 )
210
211 // SessionType defines the type of session created in StartAuthSession.
212 type SessionType uint8
213
214 // Supported session types.
215 const (
216 SessionHMAC SessionType = 0x00
217 SessionPolicy SessionType = 0x01
218 SessionTrial SessionType = 0x03
219 )
220
221 // SessionAttributes represents an attribute of a session.
222 type SessionAttributes byte
223
224 // Session Attributes (Structures 8.4 TPMA_SESSION)
225 const (
226 AttrContinueSession SessionAttributes = 1 << iota
227 AttrAuditExclusive
228 AttrAuditReset
229 _ // bit 3 reserved
230 _ // bit 4 reserved
231 AttrDecrypt
232 AttrEcrypt
233 AttrAudit
234 )
235
236 // EmptyAuth represents the empty authorization value.
237 var EmptyAuth []byte
238
239 // KeyProp is a bitmask used in Attributes field of key templates. Individual
240 // flags should be OR-ed to form a full mask.
241 type KeyProp uint32
242
243 // Key properties.
244 const (
245 FlagFixedTPM KeyProp = 0x00000002
246 FlagStClear KeyProp = 0x00000004
247 FlagFixedParent KeyProp = 0x00000010
248 FlagSensitiveDataOrigin KeyProp = 0x00000020
249 FlagUserWithAuth KeyProp = 0x00000040
250 FlagAdminWithPolicy KeyProp = 0x00000080
251 FlagNoDA KeyProp = 0x00000400
252 FlagRestricted KeyProp = 0x00010000
253 FlagDecrypt KeyProp = 0x00020000
254 FlagSign KeyProp = 0x00040000
255
256 FlagSealDefault = FlagFixedTPM | FlagFixedParent
257 FlagSignerDefault = FlagSign | FlagRestricted | FlagFixedTPM |
258 FlagFixedParent | FlagSensitiveDataOrigin | FlagUserWithAuth
259 FlagStorageDefault = FlagDecrypt | FlagRestricted | FlagFixedTPM |
260 FlagFixedParent | FlagSensitiveDataOrigin | FlagUserWithAuth
261 )
262
263 // TPMProp represents a Property Tag (TPM_PT) used with calls to GetCapability(CapabilityTPMProperties).
264 type TPMProp uint32
265
266 // TPM Capability Properties, see TPM 2.0 Spec, Rev 1.38, Table 23.
267 // Fixed TPM Properties (PT_FIXED)
268 const (
269 FamilyIndicator TPMProp = 0x100 + iota
270 SpecLevel
271 SpecRevision
272 SpecDayOfYear
273 SpecYear
274 Manufacturer
275 VendorString1
276 VendorString2
277 VendorString3
278 VendorString4
279 VendorTPMType
280 FirmwareVersion1
281 FirmwareVersion2
282 InputMaxBufferSize
283 TransientObjectsMin
284 PersistentObjectsMin
285 LoadedObjectsMin
286 ActiveSessionsMax
287 PCRCount
288 PCRSelectMin
289 ContextGapMax
290 _ // (PT_FIXED + 21) is skipped
291 NVCountersMax
292 NVIndexMax
293 MemoryMethod
294 ClockUpdate
295 ContextHash
296 ContextSym
297 ContextSymSize
298 OrderlyCount
299 CommandMaxSize
300 ResponseMaxSize
301 DigestMaxSize
302 ObjectContextMaxSize
303 SessionContextMaxSize
304 PSFamilyIndicator
305 PSSpecLevel
306 PSSpecRevision
307 PSSpecDayOfYear
308 PSSpecYear
309 SplitSigningMax
310 TotalCommands
311 LibraryCommands
312 VendorCommands
313 NVMaxBufferSize
314 TPMModes
315 CapabilityMaxBufferSize
316 )
317
318 // Variable TPM Properties (PT_VAR)
319 const (
320 TPMAPermanent TPMProp = 0x200 + iota
321 TPMAStartupClear
322 HRNVIndex
323 HRLoaded
324 HRLoadedAvail
325 HRActive
326 HRActiveAvail
327 HRTransientAvail
328 CurrentPersistent
329 AvailPersistent
330 NVCounters
331 NVCountersAvail
332 AlgorithmSet
333 LoadedCurves
334 LockoutCounter
335 MaxAuthFail
336 LockoutInterval
337 LockoutRecovery
338 NVWriteRecovery
339 AuditCounter0
340 AuditCounter1
341 )
342
343 // Allowed ranges of different kinds of Handles (TPM_HANDLE)
344 // These constants have type TPMProp for backwards compatibility.
345 const (
346 PCRFirst TPMProp = 0x00000000
347 HMACSessionFirst TPMProp = 0x02000000
348 LoadedSessionFirst TPMProp = 0x02000000
349 PolicySessionFirst TPMProp = 0x03000000
350 ActiveSessionFirst TPMProp = 0x03000000
351 TransientFirst TPMProp = 0x80000000
352 PersistentFirst TPMProp = 0x81000000
353 PersistentLast TPMProp = 0x81FFFFFF
354 PlatformPersistent TPMProp = 0x81800000
355 NVIndexFirst TPMProp = 0x01000000
356 NVIndexLast TPMProp = 0x01FFFFFF
357 PermanentFirst TPMProp = 0x40000000
358 PermanentLast TPMProp = 0x4000010F
359 )
360
361 // Reserved Handles.
362 const (
363 HandleOwner tpmutil.Handle = 0x40000001 + iota
364 HandleRevoke
365 HandleTransport
366 HandleOperator
367 HandleAdmin
368 HandleEK
369 HandleNull
370 HandleUnassigned
371 HandlePasswordSession
372 HandleLockout
373 HandleEndorsement
374 HandlePlatform
375 )
376
377 // Capability identifies some TPM property or state type.
378 type Capability uint32
379
380 // TPM Capabilities.
381 const (
382 CapabilityAlgs Capability = iota
383 CapabilityHandles
384 CapabilityCommands
385 CapabilityPPCommands
386 CapabilityAuditCommands
387 CapabilityPCRs
388 CapabilityTPMProperties
389 CapabilityPCRProperties
390 CapabilityECCCurves
391 CapabilityAuthPolicies
392 )
393
394 // TPM Structure Tags. Tags are used to disambiguate structures, similar to Alg
395 // values: tag value defines what kind of data lives in a nested field.
396 const (
397 TagNull tpmutil.Tag = 0x8000
398 TagNoSessions tpmutil.Tag = 0x8001
399 TagSessions tpmutil.Tag = 0x8002
400 TagAttestCertify tpmutil.Tag = 0x8017
401 TagAttestQuote tpmutil.Tag = 0x8018
402 TagAttestCreation tpmutil.Tag = 0x801a
403 TagAuthSecret tpmutil.Tag = 0x8023
404 TagHashCheck tpmutil.Tag = 0x8024
405 TagAuthSigned tpmutil.Tag = 0x8025
406 )
407
408 // StartupType instructs the TPM on how to handle its state during Shutdown or
409 // Startup.
410 type StartupType uint16
411
412 // Startup types
413 const (
414 StartupClear StartupType = iota
415 StartupState
416 )
417
418 // EllipticCurve identifies specific EC curves.
419 type EllipticCurve uint16
420
421 // ECC curves supported by TPM 2.0 spec.
422 const (
423 CurveNISTP192 = EllipticCurve(iota + 1)
424 CurveNISTP224
425 CurveNISTP256
426 CurveNISTP384
427 CurveNISTP521
428
429 CurveBNP256 = EllipticCurve(iota + 10)
430 CurveBNP638
431
432 CurveSM2P256 = EllipticCurve(0x0020)
433 )
434
435 var toGoCurve = map[EllipticCurve]elliptic.Curve{
436 CurveNISTP224: elliptic.P224(),
437 CurveNISTP256: elliptic.P256(),
438 CurveNISTP384: elliptic.P384(),
439 CurveNISTP521: elliptic.P521(),
440 }
441
442 // Supported TPM operations.
443 const (
444 CmdNVUndefineSpaceSpecial tpmutil.Command = 0x0000011F
445 CmdEvictControl tpmutil.Command = 0x00000120
446 CmdUndefineSpace tpmutil.Command = 0x00000122
447 CmdClear tpmutil.Command = 0x00000126
448 CmdHierarchyChangeAuth tpmutil.Command = 0x00000129
449 CmdDefineSpace tpmutil.Command = 0x0000012A
450 CmdPCRAllocate tpmutil.Command = 0x0000012B
451 CmdCreatePrimary tpmutil.Command = 0x00000131
452 CmdIncrementNVCounter tpmutil.Command = 0x00000134
453 CmdWriteNV tpmutil.Command = 0x00000137
454 CmdWriteLockNV tpmutil.Command = 0x00000138
455 CmdDictionaryAttackLockReset tpmutil.Command = 0x00000139
456 CmdDictionaryAttackParameters tpmutil.Command = 0x0000013A
457 CmdPCREvent tpmutil.Command = 0x0000013C
458 CmdPCRReset tpmutil.Command = 0x0000013D
459 CmdSequenceComplete tpmutil.Command = 0x0000013E
460 CmdStartup tpmutil.Command = 0x00000144
461 CmdShutdown tpmutil.Command = 0x00000145
462 CmdActivateCredential tpmutil.Command = 0x00000147
463 CmdCertify tpmutil.Command = 0x00000148
464 CmdCertifyCreation tpmutil.Command = 0x0000014A
465 CmdReadNV tpmutil.Command = 0x0000014E
466 CmdReadLockNV tpmutil.Command = 0x0000014F
467 CmdPolicySecret tpmutil.Command = 0x00000151
468 CmdCreate tpmutil.Command = 0x00000153
469 CmdECDHZGen tpmutil.Command = 0x00000154
470 CmdImport tpmutil.Command = 0x00000156
471 CmdLoad tpmutil.Command = 0x00000157
472 CmdQuote tpmutil.Command = 0x00000158
473 CmdRSADecrypt tpmutil.Command = 0x00000159
474 CmdSequenceUpdate tpmutil.Command = 0x0000015C
475 CmdSign tpmutil.Command = 0x0000015D
476 CmdUnseal tpmutil.Command = 0x0000015E
477 CmdPolicySigned tpmutil.Command = 0x00000160
478 CmdContextLoad tpmutil.Command = 0x00000161
479 CmdContextSave tpmutil.Command = 0x00000162
480 CmdECDHKeyGen tpmutil.Command = 0x00000163
481 CmdEncryptDecrypt tpmutil.Command = 0x00000164
482 CmdFlushContext tpmutil.Command = 0x00000165
483 CmdLoadExternal tpmutil.Command = 0x00000167
484 CmdMakeCredential tpmutil.Command = 0x00000168
485 CmdReadPublicNV tpmutil.Command = 0x00000169
486 CmdPolicyCommandCode tpmutil.Command = 0x0000016C
487 CmdPolicyOr tpmutil.Command = 0x00000171
488 CmdReadPublic tpmutil.Command = 0x00000173
489 CmdRSAEncrypt tpmutil.Command = 0x00000174
490 CmdStartAuthSession tpmutil.Command = 0x00000176
491 CmdGetCapability tpmutil.Command = 0x0000017A
492 CmdGetRandom tpmutil.Command = 0x0000017B
493 CmdHash tpmutil.Command = 0x0000017D
494 CmdPCRRead tpmutil.Command = 0x0000017E
495 CmdPolicyPCR tpmutil.Command = 0x0000017F
496 CmdReadClock tpmutil.Command = 0x00000181
497 CmdPCRExtend tpmutil.Command = 0x00000182
498 CmdEventSequenceComplete tpmutil.Command = 0x00000185
499 CmdHashSequenceStart tpmutil.Command = 0x00000186
500 CmdPolicyGetDigest tpmutil.Command = 0x00000189
501 CmdPolicyPassword tpmutil.Command = 0x0000018C
502 CmdEncryptDecrypt2 tpmutil.Command = 0x00000193
503 )
504
505 // Regular TPM 2.0 devices use 24-bit mask (3 bytes) for PCR selection.
506 const sizeOfPCRSelect = 3
507
508 const defaultRSAExponent = 1<<16 + 1
509
510 // NVAttr is a bitmask used in Attributes field of NV indexes. Individual
511 // flags should be OR-ed to form a full mask.
512 type NVAttr uint32
513
514 // NV Attributes
515 const (
516 AttrPPWrite NVAttr = 0x00000001
517 AttrOwnerWrite NVAttr = 0x00000002
518 AttrAuthWrite NVAttr = 0x00000004
519 AttrPolicyWrite NVAttr = 0x00000008
520 AttrPolicyDelete NVAttr = 0x00000400
521 AttrWriteLocked NVAttr = 0x00000800
522 AttrWriteAll NVAttr = 0x00001000
523 AttrWriteDefine NVAttr = 0x00002000
524 AttrWriteSTClear NVAttr = 0x00004000
525 AttrGlobalLock NVAttr = 0x00008000
526 AttrPPRead NVAttr = 0x00010000
527 AttrOwnerRead NVAttr = 0x00020000
528 AttrAuthRead NVAttr = 0x00040000
529 AttrPolicyRead NVAttr = 0x00080000
530 AttrNoDA NVAttr = 0x02000000
531 AttrOrderly NVAttr = 0x04000000
532 AttrClearSTClear NVAttr = 0x08000000
533 AttrReadLocked NVAttr = 0x10000000
534 AttrWritten NVAttr = 0x20000000
535 AttrPlatformCreate NVAttr = 0x40000000
536 AttrReadSTClear NVAttr = 0x80000000
537 )
538
539 var permMap = map[NVAttr]string{
540 AttrPPWrite: "PPWrite",
541 AttrOwnerWrite: "OwnerWrite",
542 AttrAuthWrite: "AuthWrite",
543 AttrPolicyWrite: "PolicyWrite",
544 AttrPolicyDelete: "PolicyDelete",
545 AttrWriteLocked: "WriteLocked",
546 AttrWriteAll: "WriteAll",
547 AttrWriteDefine: "WriteDefine",
548 AttrWriteSTClear: "WriteSTClear",
549 AttrGlobalLock: "GlobalLock",
550 AttrPPRead: "PPRead",
551 AttrOwnerRead: "OwnerRead",
552 AttrAuthRead: "AuthRead",
553 AttrPolicyRead: "PolicyRead",
554 AttrNoDA: "No Do",
555 AttrOrderly: "Oderly",
556 AttrClearSTClear: "ClearSTClear",
557 AttrReadLocked: "ReadLocked",
558 AttrWritten: "Writte",
559 AttrPlatformCreate: "PlatformCreate",
560 AttrReadSTClear: "ReadSTClear",
561 }
562
563 // String returns a textual representation of the set of NVAttr
564 func (p NVAttr) String() string {
565 var retString strings.Builder
566 for iterator, item := range permMap {
567 if (p & iterator) != 0 {
568 retString.WriteString(item + " + ")
569 }
570 }
571 if retString.String() == "" {
572 return "Permission/s not found"
573 }
574 return strings.TrimSuffix(retString.String(), " + ")
575
576 }