~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/tpm2/tpm2.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/testing/selftests/tpm2/tpm2.py (Version linux-6.12-rc7) and /tools/testing/selftests/tpm2/tpm2.py (Version linux-5.9.16)


  1 # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-C      1 # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2                                                     2 
  3 import hashlib                                      3 import hashlib
  4 import os                                           4 import os
  5 import socket                                       5 import socket
  6 import struct                                       6 import struct
  7 import sys                                          7 import sys
  8 import unittest                                     8 import unittest
  9 import fcntl                                        9 import fcntl
 10 import select                                      10 import select
 11                                                    11 
 12 TPM2_ST_NO_SESSIONS = 0x8001                       12 TPM2_ST_NO_SESSIONS = 0x8001
 13 TPM2_ST_SESSIONS = 0x8002                          13 TPM2_ST_SESSIONS = 0x8002
 14                                                    14 
 15 TPM2_CC_FIRST = 0x01FF                             15 TPM2_CC_FIRST = 0x01FF
 16                                                    16 
 17 TPM2_CC_CREATE_PRIMARY = 0x0131                    17 TPM2_CC_CREATE_PRIMARY = 0x0131
 18 TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET = 0x0139      18 TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET = 0x0139
 19 TPM2_CC_CREATE = 0x0153                            19 TPM2_CC_CREATE = 0x0153
 20 TPM2_CC_LOAD = 0x0157                              20 TPM2_CC_LOAD = 0x0157
 21 TPM2_CC_UNSEAL = 0x015E                            21 TPM2_CC_UNSEAL = 0x015E
 22 TPM2_CC_FLUSH_CONTEXT = 0x0165                     22 TPM2_CC_FLUSH_CONTEXT = 0x0165
 23 TPM2_CC_START_AUTH_SESSION = 0x0176                23 TPM2_CC_START_AUTH_SESSION = 0x0176
 24 TPM2_CC_GET_CAPABILITY  = 0x017A                   24 TPM2_CC_GET_CAPABILITY  = 0x017A
 25 TPM2_CC_GET_RANDOM = 0x017B                        25 TPM2_CC_GET_RANDOM = 0x017B
 26 TPM2_CC_PCR_READ = 0x017E                          26 TPM2_CC_PCR_READ = 0x017E
 27 TPM2_CC_POLICY_PCR = 0x017F                        27 TPM2_CC_POLICY_PCR = 0x017F
 28 TPM2_CC_PCR_EXTEND = 0x0182                        28 TPM2_CC_PCR_EXTEND = 0x0182
 29 TPM2_CC_POLICY_PASSWORD = 0x018C                   29 TPM2_CC_POLICY_PASSWORD = 0x018C
 30 TPM2_CC_POLICY_GET_DIGEST = 0x0189                 30 TPM2_CC_POLICY_GET_DIGEST = 0x0189
 31                                                    31 
 32 TPM2_SE_POLICY = 0x01                              32 TPM2_SE_POLICY = 0x01
 33 TPM2_SE_TRIAL = 0x03                               33 TPM2_SE_TRIAL = 0x03
 34                                                    34 
 35 TPM2_ALG_RSA = 0x0001                              35 TPM2_ALG_RSA = 0x0001
 36 TPM2_ALG_SHA1 = 0x0004                             36 TPM2_ALG_SHA1 = 0x0004
 37 TPM2_ALG_AES = 0x0006                              37 TPM2_ALG_AES = 0x0006
 38 TPM2_ALG_KEYEDHASH = 0x0008                        38 TPM2_ALG_KEYEDHASH = 0x0008
 39 TPM2_ALG_SHA256 = 0x000B                           39 TPM2_ALG_SHA256 = 0x000B
 40 TPM2_ALG_NULL = 0x0010                             40 TPM2_ALG_NULL = 0x0010
 41 TPM2_ALG_CBC = 0x0042                              41 TPM2_ALG_CBC = 0x0042
 42 TPM2_ALG_CFB = 0x0043                              42 TPM2_ALG_CFB = 0x0043
 43                                                    43 
 44 TPM2_RH_OWNER = 0x40000001                         44 TPM2_RH_OWNER = 0x40000001
 45 TPM2_RH_NULL = 0x40000007                          45 TPM2_RH_NULL = 0x40000007
 46 TPM2_RH_LOCKOUT = 0x4000000A                       46 TPM2_RH_LOCKOUT = 0x4000000A
 47 TPM2_RS_PW = 0x40000009                            47 TPM2_RS_PW = 0x40000009
 48                                                    48 
 49 TPM2_RC_SIZE            = 0x01D5                   49 TPM2_RC_SIZE            = 0x01D5
 50 TPM2_RC_AUTH_FAIL       = 0x098E                   50 TPM2_RC_AUTH_FAIL       = 0x098E
 51 TPM2_RC_POLICY_FAIL     = 0x099D                   51 TPM2_RC_POLICY_FAIL     = 0x099D
 52 TPM2_RC_COMMAND_CODE    = 0x0143                   52 TPM2_RC_COMMAND_CODE    = 0x0143
 53                                                    53 
 54 TSS2_RC_LAYER_SHIFT = 16                           54 TSS2_RC_LAYER_SHIFT = 16
 55 TSS2_RESMGR_TPM_RC_LAYER = (11 << TSS2_RC_LAYE     55 TSS2_RESMGR_TPM_RC_LAYER = (11 << TSS2_RC_LAYER_SHIFT)
 56                                                    56 
 57 TPM2_CAP_HANDLES = 0x00000001                      57 TPM2_CAP_HANDLES = 0x00000001
 58 TPM2_CAP_COMMANDS = 0x00000002                     58 TPM2_CAP_COMMANDS = 0x00000002
 59 TPM2_CAP_PCRS = 0x00000005                     << 
 60 TPM2_CAP_TPM_PROPERTIES = 0x00000006               59 TPM2_CAP_TPM_PROPERTIES = 0x00000006
 61                                                    60 
 62 TPM2_PT_FIXED = 0x100                              61 TPM2_PT_FIXED = 0x100
 63 TPM2_PT_TOTAL_COMMANDS = TPM2_PT_FIXED + 41        62 TPM2_PT_TOTAL_COMMANDS = TPM2_PT_FIXED + 41
 64                                                    63 
 65 HR_SHIFT = 24                                      64 HR_SHIFT = 24
 66 HR_LOADED_SESSION = 0x02000000                     65 HR_LOADED_SESSION = 0x02000000
 67 HR_TRANSIENT = 0x80000000                          66 HR_TRANSIENT = 0x80000000
 68                                                    67 
 69 SHA1_DIGEST_SIZE = 20                              68 SHA1_DIGEST_SIZE = 20
 70 SHA256_DIGEST_SIZE = 32                            69 SHA256_DIGEST_SIZE = 32
 71                                                    70 
 72 TPM2_VER0_ERRORS = {                               71 TPM2_VER0_ERRORS = {
 73     0x000: "TPM_RC_SUCCESS",                       72     0x000: "TPM_RC_SUCCESS",
 74     0x030: "TPM_RC_BAD_TAG",                       73     0x030: "TPM_RC_BAD_TAG",
 75 }                                                  74 }
 76                                                    75 
 77 TPM2_VER1_ERRORS = {                               76 TPM2_VER1_ERRORS = {
 78     0x000: "TPM_RC_FAILURE",                       77     0x000: "TPM_RC_FAILURE",
 79     0x001: "TPM_RC_FAILURE",                       78     0x001: "TPM_RC_FAILURE",
 80     0x003: "TPM_RC_SEQUENCE",                      79     0x003: "TPM_RC_SEQUENCE",
 81     0x00B: "TPM_RC_PRIVATE",                       80     0x00B: "TPM_RC_PRIVATE",
 82     0x019: "TPM_RC_HMAC",                          81     0x019: "TPM_RC_HMAC",
 83     0x020: "TPM_RC_DISABLED",                      82     0x020: "TPM_RC_DISABLED",
 84     0x021: "TPM_RC_EXCLUSIVE",                     83     0x021: "TPM_RC_EXCLUSIVE",
 85     0x024: "TPM_RC_AUTH_TYPE",                     84     0x024: "TPM_RC_AUTH_TYPE",
 86     0x025: "TPM_RC_AUTH_MISSING",                  85     0x025: "TPM_RC_AUTH_MISSING",
 87     0x026: "TPM_RC_POLICY",                        86     0x026: "TPM_RC_POLICY",
 88     0x027: "TPM_RC_PCR",                           87     0x027: "TPM_RC_PCR",
 89     0x028: "TPM_RC_PCR_CHANGED",                   88     0x028: "TPM_RC_PCR_CHANGED",
 90     0x02D: "TPM_RC_UPGRADE",                       89     0x02D: "TPM_RC_UPGRADE",
 91     0x02E: "TPM_RC_TOO_MANY_CONTEXTS",             90     0x02E: "TPM_RC_TOO_MANY_CONTEXTS",
 92     0x02F: "TPM_RC_AUTH_UNAVAILABLE",              91     0x02F: "TPM_RC_AUTH_UNAVAILABLE",
 93     0x030: "TPM_RC_REBOOT",                        92     0x030: "TPM_RC_REBOOT",
 94     0x031: "TPM_RC_UNBALANCED",                    93     0x031: "TPM_RC_UNBALANCED",
 95     0x042: "TPM_RC_COMMAND_SIZE",                  94     0x042: "TPM_RC_COMMAND_SIZE",
 96     0x043: "TPM_RC_COMMAND_CODE",                  95     0x043: "TPM_RC_COMMAND_CODE",
 97     0x044: "TPM_RC_AUTHSIZE",                      96     0x044: "TPM_RC_AUTHSIZE",
 98     0x045: "TPM_RC_AUTH_CONTEXT",                  97     0x045: "TPM_RC_AUTH_CONTEXT",
 99     0x046: "TPM_RC_NV_RANGE",                      98     0x046: "TPM_RC_NV_RANGE",
100     0x047: "TPM_RC_NV_SIZE",                       99     0x047: "TPM_RC_NV_SIZE",
101     0x048: "TPM_RC_NV_LOCKED",                    100     0x048: "TPM_RC_NV_LOCKED",
102     0x049: "TPM_RC_NV_AUTHORIZATION",             101     0x049: "TPM_RC_NV_AUTHORIZATION",
103     0x04A: "TPM_RC_NV_UNINITIALIZED",             102     0x04A: "TPM_RC_NV_UNINITIALIZED",
104     0x04B: "TPM_RC_NV_SPACE",                     103     0x04B: "TPM_RC_NV_SPACE",
105     0x04C: "TPM_RC_NV_DEFINED",                   104     0x04C: "TPM_RC_NV_DEFINED",
106     0x050: "TPM_RC_BAD_CONTEXT",                  105     0x050: "TPM_RC_BAD_CONTEXT",
107     0x051: "TPM_RC_CPHASH",                       106     0x051: "TPM_RC_CPHASH",
108     0x052: "TPM_RC_PARENT",                       107     0x052: "TPM_RC_PARENT",
109     0x053: "TPM_RC_NEEDS_TEST",                   108     0x053: "TPM_RC_NEEDS_TEST",
110     0x054: "TPM_RC_NO_RESULT",                    109     0x054: "TPM_RC_NO_RESULT",
111     0x055: "TPM_RC_SENSITIVE",                    110     0x055: "TPM_RC_SENSITIVE",
112     0x07F: "RC_MAX_FM0",                          111     0x07F: "RC_MAX_FM0",
113 }                                                 112 }
114                                                   113 
115 TPM2_FMT1_ERRORS = {                              114 TPM2_FMT1_ERRORS = {
116     0x001: "TPM_RC_ASYMMETRIC",                   115     0x001: "TPM_RC_ASYMMETRIC",
117     0x002: "TPM_RC_ATTRIBUTES",                   116     0x002: "TPM_RC_ATTRIBUTES",
118     0x003: "TPM_RC_HASH",                         117     0x003: "TPM_RC_HASH",
119     0x004: "TPM_RC_VALUE",                        118     0x004: "TPM_RC_VALUE",
120     0x005: "TPM_RC_HIERARCHY",                    119     0x005: "TPM_RC_HIERARCHY",
121     0x007: "TPM_RC_KEY_SIZE",                     120     0x007: "TPM_RC_KEY_SIZE",
122     0x008: "TPM_RC_MGF",                          121     0x008: "TPM_RC_MGF",
123     0x009: "TPM_RC_MODE",                         122     0x009: "TPM_RC_MODE",
124     0x00A: "TPM_RC_TYPE",                         123     0x00A: "TPM_RC_TYPE",
125     0x00B: "TPM_RC_HANDLE",                       124     0x00B: "TPM_RC_HANDLE",
126     0x00C: "TPM_RC_KDF",                          125     0x00C: "TPM_RC_KDF",
127     0x00D: "TPM_RC_RANGE",                        126     0x00D: "TPM_RC_RANGE",
128     0x00E: "TPM_RC_AUTH_FAIL",                    127     0x00E: "TPM_RC_AUTH_FAIL",
129     0x00F: "TPM_RC_NONCE",                        128     0x00F: "TPM_RC_NONCE",
130     0x010: "TPM_RC_PP",                           129     0x010: "TPM_RC_PP",
131     0x012: "TPM_RC_SCHEME",                       130     0x012: "TPM_RC_SCHEME",
132     0x015: "TPM_RC_SIZE",                         131     0x015: "TPM_RC_SIZE",
133     0x016: "TPM_RC_SYMMETRIC",                    132     0x016: "TPM_RC_SYMMETRIC",
134     0x017: "TPM_RC_TAG",                          133     0x017: "TPM_RC_TAG",
135     0x018: "TPM_RC_SELECTOR",                     134     0x018: "TPM_RC_SELECTOR",
136     0x01A: "TPM_RC_INSUFFICIENT",                 135     0x01A: "TPM_RC_INSUFFICIENT",
137     0x01B: "TPM_RC_SIGNATURE",                    136     0x01B: "TPM_RC_SIGNATURE",
138     0x01C: "TPM_RC_KEY",                          137     0x01C: "TPM_RC_KEY",
139     0x01D: "TPM_RC_POLICY_FAIL",                  138     0x01D: "TPM_RC_POLICY_FAIL",
140     0x01F: "TPM_RC_INTEGRITY",                    139     0x01F: "TPM_RC_INTEGRITY",
141     0x020: "TPM_RC_TICKET",                       140     0x020: "TPM_RC_TICKET",
142     0x021: "TPM_RC_RESERVED_BITS",                141     0x021: "TPM_RC_RESERVED_BITS",
143     0x022: "TPM_RC_BAD_AUTH",                     142     0x022: "TPM_RC_BAD_AUTH",
144     0x023: "TPM_RC_EXPIRED",                      143     0x023: "TPM_RC_EXPIRED",
145     0x024: "TPM_RC_POLICY_CC",                    144     0x024: "TPM_RC_POLICY_CC",
146     0x025: "TPM_RC_BINDING",                      145     0x025: "TPM_RC_BINDING",
147     0x026: "TPM_RC_CURVE",                        146     0x026: "TPM_RC_CURVE",
148     0x027: "TPM_RC_ECC_POINT",                    147     0x027: "TPM_RC_ECC_POINT",
149 }                                                 148 }
150                                                   149 
151 TPM2_WARN_ERRORS = {                              150 TPM2_WARN_ERRORS = {
152     0x001: "TPM_RC_CONTEXT_GAP",                  151     0x001: "TPM_RC_CONTEXT_GAP",
153     0x002: "TPM_RC_OBJECT_MEMORY",                152     0x002: "TPM_RC_OBJECT_MEMORY",
154     0x003: "TPM_RC_SESSION_MEMORY",               153     0x003: "TPM_RC_SESSION_MEMORY",
155     0x004: "TPM_RC_MEMORY",                       154     0x004: "TPM_RC_MEMORY",
156     0x005: "TPM_RC_SESSION_HANDLES",              155     0x005: "TPM_RC_SESSION_HANDLES",
157     0x006: "TPM_RC_OBJECT_HANDLES",               156     0x006: "TPM_RC_OBJECT_HANDLES",
158     0x007: "TPM_RC_LOCALITY",                     157     0x007: "TPM_RC_LOCALITY",
159     0x008: "TPM_RC_YIELDED",                      158     0x008: "TPM_RC_YIELDED",
160     0x009: "TPM_RC_CANCELED",                     159     0x009: "TPM_RC_CANCELED",
161     0x00A: "TPM_RC_TESTING",                      160     0x00A: "TPM_RC_TESTING",
162     0x010: "TPM_RC_REFERENCE_H0",                 161     0x010: "TPM_RC_REFERENCE_H0",
163     0x011: "TPM_RC_REFERENCE_H1",                 162     0x011: "TPM_RC_REFERENCE_H1",
164     0x012: "TPM_RC_REFERENCE_H2",                 163     0x012: "TPM_RC_REFERENCE_H2",
165     0x013: "TPM_RC_REFERENCE_H3",                 164     0x013: "TPM_RC_REFERENCE_H3",
166     0x014: "TPM_RC_REFERENCE_H4",                 165     0x014: "TPM_RC_REFERENCE_H4",
167     0x015: "TPM_RC_REFERENCE_H5",                 166     0x015: "TPM_RC_REFERENCE_H5",
168     0x016: "TPM_RC_REFERENCE_H6",                 167     0x016: "TPM_RC_REFERENCE_H6",
169     0x018: "TPM_RC_REFERENCE_S0",                 168     0x018: "TPM_RC_REFERENCE_S0",
170     0x019: "TPM_RC_REFERENCE_S1",                 169     0x019: "TPM_RC_REFERENCE_S1",
171     0x01A: "TPM_RC_REFERENCE_S2",                 170     0x01A: "TPM_RC_REFERENCE_S2",
172     0x01B: "TPM_RC_REFERENCE_S3",                 171     0x01B: "TPM_RC_REFERENCE_S3",
173     0x01C: "TPM_RC_REFERENCE_S4",                 172     0x01C: "TPM_RC_REFERENCE_S4",
174     0x01D: "TPM_RC_REFERENCE_S5",                 173     0x01D: "TPM_RC_REFERENCE_S5",
175     0x01E: "TPM_RC_REFERENCE_S6",                 174     0x01E: "TPM_RC_REFERENCE_S6",
176     0x020: "TPM_RC_NV_RATE",                      175     0x020: "TPM_RC_NV_RATE",
177     0x021: "TPM_RC_LOCKOUT",                      176     0x021: "TPM_RC_LOCKOUT",
178     0x022: "TPM_RC_RETRY",                        177     0x022: "TPM_RC_RETRY",
179     0x023: "TPM_RC_NV_UNAVAILABLE",               178     0x023: "TPM_RC_NV_UNAVAILABLE",
180     0x7F: "TPM_RC_NOT_USED",                      179     0x7F: "TPM_RC_NOT_USED",
181 }                                                 180 }
182                                                   181 
183 RC_VER1 = 0x100                                   182 RC_VER1 = 0x100
184 RC_FMT1 = 0x080                                   183 RC_FMT1 = 0x080
185 RC_WARN = 0x900                                   184 RC_WARN = 0x900
186                                                   185 
187 ALG_DIGEST_SIZE_MAP = {                           186 ALG_DIGEST_SIZE_MAP = {
188     TPM2_ALG_SHA1: SHA1_DIGEST_SIZE,              187     TPM2_ALG_SHA1: SHA1_DIGEST_SIZE,
189     TPM2_ALG_SHA256: SHA256_DIGEST_SIZE,          188     TPM2_ALG_SHA256: SHA256_DIGEST_SIZE,
190 }                                                 189 }
191                                                   190 
192 ALG_HASH_FUNCTION_MAP = {                         191 ALG_HASH_FUNCTION_MAP = {
193     TPM2_ALG_SHA1: hashlib.sha1,                  192     TPM2_ALG_SHA1: hashlib.sha1,
194     TPM2_ALG_SHA256: hashlib.sha256               193     TPM2_ALG_SHA256: hashlib.sha256
195 }                                                 194 }
196                                                   195 
197 NAME_ALG_MAP = {                                  196 NAME_ALG_MAP = {
198     "sha1": TPM2_ALG_SHA1,                        197     "sha1": TPM2_ALG_SHA1,
199     "sha256": TPM2_ALG_SHA256,                    198     "sha256": TPM2_ALG_SHA256,
200 }                                                 199 }
201                                                   200 
202                                                   201 
203 class UnknownAlgorithmIdError(Exception):         202 class UnknownAlgorithmIdError(Exception):
204     def __init__(self, alg):                      203     def __init__(self, alg):
205         self.alg = alg                            204         self.alg = alg
206                                                   205 
207     def __str__(self):                            206     def __str__(self):
208         return '0x%0x' % (alg)                    207         return '0x%0x' % (alg)
209                                                   208 
210                                                   209 
211 class UnknownAlgorithmNameError(Exception):       210 class UnknownAlgorithmNameError(Exception):
212     def __init__(self, name):                     211     def __init__(self, name):
213         self.name = name                          212         self.name = name
214                                                   213 
215     def __str__(self):                            214     def __str__(self):
216         return name                               215         return name
217                                                   216 
218                                                   217 
219 class UnknownPCRBankError(Exception):             218 class UnknownPCRBankError(Exception):
220     def __init__(self, alg):                      219     def __init__(self, alg):
221         self.alg = alg                            220         self.alg = alg
222                                                   221 
223     def __str__(self):                            222     def __str__(self):
224         return '0x%0x' % (alg)                    223         return '0x%0x' % (alg)
225                                                   224 
226                                                   225 
227 class ProtocolError(Exception):                   226 class ProtocolError(Exception):
228     def __init__(self, cc, rc):                   227     def __init__(self, cc, rc):
229         self.cc = cc                              228         self.cc = cc
230         self.rc = rc                              229         self.rc = rc
231                                                   230 
232         if (rc & RC_FMT1) == RC_FMT1:             231         if (rc & RC_FMT1) == RC_FMT1:
233             self.name = TPM2_FMT1_ERRORS.get(r    232             self.name = TPM2_FMT1_ERRORS.get(rc & 0x3f, "TPM_RC_UNKNOWN")
234         elif (rc & RC_WARN) == RC_WARN:           233         elif (rc & RC_WARN) == RC_WARN:
235             self.name = TPM2_WARN_ERRORS.get(r    234             self.name = TPM2_WARN_ERRORS.get(rc & 0x7f, "TPM_RC_UNKNOWN")
236         elif (rc & RC_VER1) == RC_VER1:           235         elif (rc & RC_VER1) == RC_VER1:
237             self.name = TPM2_VER1_ERRORS.get(r    236             self.name = TPM2_VER1_ERRORS.get(rc & 0x7f, "TPM_RC_UNKNOWN")
238         else:                                     237         else:
239             self.name = TPM2_VER0_ERRORS.get(r    238             self.name = TPM2_VER0_ERRORS.get(rc & 0x7f, "TPM_RC_UNKNOWN")
240                                                   239 
241     def __str__(self):                            240     def __str__(self):
242         if self.cc:                               241         if self.cc:
243             return '%s: cc=0x%08x, rc=0x%08x'     242             return '%s: cc=0x%08x, rc=0x%08x' % (self.name, self.cc, self.rc)
244         else:                                     243         else:
245             return '%s: rc=0x%08x' % (self.nam    244             return '%s: rc=0x%08x' % (self.name, self.rc)
246                                                   245 
247                                                   246 
248 class AuthCommand(object):                        247 class AuthCommand(object):
249     """TPMS_AUTH_COMMAND"""                       248     """TPMS_AUTH_COMMAND"""
250                                                   249 
251     def __init__(self, session_handle=TPM2_RS_    250     def __init__(self, session_handle=TPM2_RS_PW, nonce=bytes(),
252                  session_attributes=0, hmac=by    251                  session_attributes=0, hmac=bytes()):
253         self.session_handle = session_handle      252         self.session_handle = session_handle
254         self.nonce = nonce                        253         self.nonce = nonce
255         self.session_attributes = session_attr    254         self.session_attributes = session_attributes
256         self.hmac = hmac                          255         self.hmac = hmac
257                                                   256 
258     def __bytes__(self):                          257     def __bytes__(self):
259         fmt = '>I H%us B H%us' % (len(self.non    258         fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
260         return struct.pack(fmt, self.session_h    259         return struct.pack(fmt, self.session_handle, len(self.nonce),
261                            self.nonce, self.se    260                            self.nonce, self.session_attributes, len(self.hmac),
262                            self.hmac)             261                            self.hmac)
263                                                   262 
264     def __len__(self):                            263     def __len__(self):
265         fmt = '>I H%us B H%us' % (len(self.non    264         fmt = '>I H%us B H%us' % (len(self.nonce), len(self.hmac))
266         return struct.calcsize(fmt)               265         return struct.calcsize(fmt)
267                                                   266 
268                                                   267 
269 class SensitiveCreate(object):                    268 class SensitiveCreate(object):
270     """TPMS_SENSITIVE_CREATE"""                   269     """TPMS_SENSITIVE_CREATE"""
271                                                   270 
272     def __init__(self, user_auth=bytes(), data    271     def __init__(self, user_auth=bytes(), data=bytes()):
273         self.user_auth = user_auth                272         self.user_auth = user_auth
274         self.data = data                          273         self.data = data
275                                                   274 
276     def __bytes__(self):                          275     def __bytes__(self):
277         fmt = '>H%us H%us' % (len(self.user_au    276         fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
278         return struct.pack(fmt, len(self.user_    277         return struct.pack(fmt, len(self.user_auth), self.user_auth,
279                            len(self.data), sel    278                            len(self.data), self.data)
280                                                   279 
281     def __len__(self):                            280     def __len__(self):
282         fmt = '>H%us H%us' % (len(self.user_au    281         fmt = '>H%us H%us' % (len(self.user_auth), len(self.data))
283         return struct.calcsize(fmt)               282         return struct.calcsize(fmt)
284                                                   283 
285                                                   284 
286 class Public(object):                             285 class Public(object):
287     """TPMT_PUBLIC"""                             286     """TPMT_PUBLIC"""
288                                                   287 
289     FIXED_TPM = (1 << 1)                          288     FIXED_TPM = (1 << 1)
290     FIXED_PARENT = (1 << 4)                       289     FIXED_PARENT = (1 << 4)
291     SENSITIVE_DATA_ORIGIN = (1 << 5)              290     SENSITIVE_DATA_ORIGIN = (1 << 5)
292     USER_WITH_AUTH = (1 << 6)                     291     USER_WITH_AUTH = (1 << 6)
293     RESTRICTED = (1 << 16)                        292     RESTRICTED = (1 << 16)
294     DECRYPT = (1 << 17)                           293     DECRYPT = (1 << 17)
295                                                   294 
296     def __fmt(self):                              295     def __fmt(self):
297         return '>HHIH%us%usH%us' % \              296         return '>HHIH%us%usH%us' % \
298             (len(self.auth_policy), len(self.p    297             (len(self.auth_policy), len(self.parameters), len(self.unique))
299                                                   298 
300     def __init__(self, object_type, name_alg,     299     def __init__(self, object_type, name_alg, object_attributes,
301                  auth_policy=bytes(), paramete    300                  auth_policy=bytes(), parameters=bytes(),
302                  unique=bytes()):                 301                  unique=bytes()):
303         self.object_type = object_type            302         self.object_type = object_type
304         self.name_alg = name_alg                  303         self.name_alg = name_alg
305         self.object_attributes = object_attrib    304         self.object_attributes = object_attributes
306         self.auth_policy = auth_policy            305         self.auth_policy = auth_policy
307         self.parameters = parameters              306         self.parameters = parameters
308         self.unique = unique                      307         self.unique = unique
309                                                   308 
310     def __bytes__(self):                          309     def __bytes__(self):
311         return struct.pack(self.__fmt(),          310         return struct.pack(self.__fmt(),
312                            self.object_type,      311                            self.object_type,
313                            self.name_alg,         312                            self.name_alg,
314                            self.object_attribu    313                            self.object_attributes,
315                            len(self.auth_polic    314                            len(self.auth_policy),
316                            self.auth_policy,      315                            self.auth_policy,
317                            self.parameters,       316                            self.parameters,
318                            len(self.unique),      317                            len(self.unique),
319                            self.unique)           318                            self.unique)
320                                                   319 
321     def __len__(self):                            320     def __len__(self):
322         return struct.calcsize(self.__fmt())      321         return struct.calcsize(self.__fmt())
323                                                   322 
324                                                   323 
325 def get_digest_size(alg):                         324 def get_digest_size(alg):
326     ds = ALG_DIGEST_SIZE_MAP.get(alg)             325     ds = ALG_DIGEST_SIZE_MAP.get(alg)
327     if not ds:                                    326     if not ds:
328         raise UnknownAlgorithmIdError(alg)        327         raise UnknownAlgorithmIdError(alg)
329     return ds                                     328     return ds
330                                                   329 
331                                                   330 
332 def get_hash_function(alg):                       331 def get_hash_function(alg):
333     f = ALG_HASH_FUNCTION_MAP.get(alg)            332     f = ALG_HASH_FUNCTION_MAP.get(alg)
334     if not f:                                     333     if not f:
335         raise UnknownAlgorithmIdError(alg)        334         raise UnknownAlgorithmIdError(alg)
336     return f                                      335     return f
337                                                   336 
338                                                   337 
339 def get_algorithm(name):                          338 def get_algorithm(name):
340     alg = NAME_ALG_MAP.get(name)                  339     alg = NAME_ALG_MAP.get(name)
341     if not alg:                                   340     if not alg:
342         raise UnknownAlgorithmNameError(name)     341         raise UnknownAlgorithmNameError(name)
343     return alg                                    342     return alg
344                                                   343 
345                                                   344 
346 def hex_dump(d):                                  345 def hex_dump(d):
347     d = [format(x, '02x') for x in d]          !! 346     d = [format(ord(x), '02x') for x in d]
348     d = [d[i: i + 16] for i in range(0, len(d)    347     d = [d[i: i + 16] for i in range(0, len(d), 16)]
349     d = [' '.join(x) for x in d]                  348     d = [' '.join(x) for x in d]
350     d = os.linesep.join(d)                        349     d = os.linesep.join(d)
351                                                   350 
352     return d                                      351     return d
353                                                   352 
354 class Client:                                     353 class Client:
355     FLAG_DEBUG = 0x01                             354     FLAG_DEBUG = 0x01
356     FLAG_SPACE = 0x02                             355     FLAG_SPACE = 0x02
357     FLAG_NONBLOCK = 0x04                          356     FLAG_NONBLOCK = 0x04
358     TPM_IOC_NEW_SPACE = 0xa200                    357     TPM_IOC_NEW_SPACE = 0xa200
359                                                   358 
360     def __init__(self, flags = 0):                359     def __init__(self, flags = 0):
361         self.flags = flags                        360         self.flags = flags
362                                                   361 
363         if (self.flags & Client.FLAG_SPACE) ==    362         if (self.flags & Client.FLAG_SPACE) == 0:
364             self.tpm = open('/dev/tpm0', 'r+b'    363             self.tpm = open('/dev/tpm0', 'r+b', buffering=0)
365         else:                                     364         else:
366             self.tpm = open('/dev/tpmrm0', 'r+    365             self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
367                                                   366 
368         if (self.flags & Client.FLAG_NONBLOCK)    367         if (self.flags & Client.FLAG_NONBLOCK):
369             flags = fcntl.fcntl(self.tpm, fcnt    368             flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL)
370             flags |= os.O_NONBLOCK                369             flags |= os.O_NONBLOCK
371             fcntl.fcntl(self.tpm, fcntl.F_SETF    370             fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
372             self.tpm_poll = select.poll()         371             self.tpm_poll = select.poll()
373                                                   372 
374     def __del__(self):                         << 
375         if self.tpm:                           << 
376             self.tpm.close()                   << 
377                                                << 
378     def close(self):                              373     def close(self):
379         self.tpm.close()                          374         self.tpm.close()
380                                                   375 
381     def send_cmd(self, cmd):                      376     def send_cmd(self, cmd):
382         self.tpm.write(cmd)                       377         self.tpm.write(cmd)
383                                                   378 
384         if (self.flags & Client.FLAG_NONBLOCK)    379         if (self.flags & Client.FLAG_NONBLOCK):
385             self.tpm_poll.register(self.tpm, s    380             self.tpm_poll.register(self.tpm, select.POLLIN)
386             self.tpm_poll.poll(10000)             381             self.tpm_poll.poll(10000)
387                                                   382 
388         rsp = self.tpm.read()                     383         rsp = self.tpm.read()
389                                                   384 
390         if (self.flags & Client.FLAG_NONBLOCK)    385         if (self.flags & Client.FLAG_NONBLOCK):
391             self.tpm_poll.unregister(self.tpm)    386             self.tpm_poll.unregister(self.tpm)
392                                                   387 
393         if (self.flags & Client.FLAG_DEBUG) !=    388         if (self.flags & Client.FLAG_DEBUG) != 0:
394             sys.stderr.write('cmd' + os.linese    389             sys.stderr.write('cmd' + os.linesep)
395             sys.stderr.write(hex_dump(cmd) + o    390             sys.stderr.write(hex_dump(cmd) + os.linesep)
396             sys.stderr.write('rsp' + os.linese    391             sys.stderr.write('rsp' + os.linesep)
397             sys.stderr.write(hex_dump(rsp) + o    392             sys.stderr.write(hex_dump(rsp) + os.linesep)
398                                                   393 
399         rc = struct.unpack('>I', rsp[6:10])[0]    394         rc = struct.unpack('>I', rsp[6:10])[0]
400         if rc != 0:                               395         if rc != 0:
401             cc = struct.unpack('>I', cmd[6:10]    396             cc = struct.unpack('>I', cmd[6:10])[0]
402             raise ProtocolError(cc, rc)           397             raise ProtocolError(cc, rc)
403                                                   398 
404         return rsp                                399         return rsp
405                                                   400 
406     def read_pcr(self, i, bank_alg = TPM2_ALG_    401     def read_pcr(self, i, bank_alg = TPM2_ALG_SHA1):
407         pcrsel_len = max((i >> 3) + 1, 3)         402         pcrsel_len = max((i >> 3) + 1, 3)
408         pcrsel = [0] * pcrsel_len                 403         pcrsel = [0] * pcrsel_len
409         pcrsel[i >> 3] = 1 << (i & 7)             404         pcrsel[i >> 3] = 1 << (i & 7)
410         pcrsel = ''.join(map(chr, pcrsel)).enc    405         pcrsel = ''.join(map(chr, pcrsel)).encode()
411                                                   406 
412         fmt = '>HII IHB%us' % (pcrsel_len)        407         fmt = '>HII IHB%us' % (pcrsel_len)
413         cmd = struct.pack(fmt,                    408         cmd = struct.pack(fmt,
414                           TPM2_ST_NO_SESSIONS,    409                           TPM2_ST_NO_SESSIONS,
415                           struct.calcsize(fmt)    410                           struct.calcsize(fmt),
416                           TPM2_CC_PCR_READ,       411                           TPM2_CC_PCR_READ,
417                           1,                      412                           1,
418                           bank_alg,               413                           bank_alg,
419                           pcrsel_len, pcrsel)     414                           pcrsel_len, pcrsel)
420                                                   415 
421         rsp = self.send_cmd(cmd)                  416         rsp = self.send_cmd(cmd)
422                                                   417 
423         pcr_update_cnt, pcr_select_cnt = struc    418         pcr_update_cnt, pcr_select_cnt = struct.unpack('>II', rsp[10:18])
424         assert pcr_select_cnt == 1                419         assert pcr_select_cnt == 1
425         rsp = rsp[18:]                            420         rsp = rsp[18:]
426                                                   421 
427         alg2, pcrsel_len2 = struct.unpack('>HB    422         alg2, pcrsel_len2 = struct.unpack('>HB', rsp[:3])
428         assert bank_alg == alg2 and pcrsel_len    423         assert bank_alg == alg2 and pcrsel_len == pcrsel_len2
429         rsp = rsp[3 + pcrsel_len:]                424         rsp = rsp[3 + pcrsel_len:]
430                                                   425 
431         digest_cnt = struct.unpack('>I', rsp[:    426         digest_cnt = struct.unpack('>I', rsp[:4])[0]
432         if digest_cnt == 0:                       427         if digest_cnt == 0:
433             return None                           428             return None
434         rsp = rsp[6:]                             429         rsp = rsp[6:]
435                                                   430 
436         return rsp                                431         return rsp
437                                                   432 
438     def extend_pcr(self, i, dig, bank_alg = TP    433     def extend_pcr(self, i, dig, bank_alg = TPM2_ALG_SHA1):
439         ds = get_digest_size(bank_alg)            434         ds = get_digest_size(bank_alg)
440         assert(ds == len(dig))                    435         assert(ds == len(dig))
441                                                   436 
442         auth_cmd = AuthCommand()                  437         auth_cmd = AuthCommand()
443                                                   438 
444         fmt = '>HII I I%us IH%us' % (len(auth_    439         fmt = '>HII I I%us IH%us' % (len(auth_cmd), ds)
445         cmd = struct.pack(                        440         cmd = struct.pack(
446             fmt,                                  441             fmt,
447             TPM2_ST_SESSIONS,                     442             TPM2_ST_SESSIONS,
448             struct.calcsize(fmt),                 443             struct.calcsize(fmt),
449             TPM2_CC_PCR_EXTEND,                   444             TPM2_CC_PCR_EXTEND,
450             i,                                    445             i,
451             len(auth_cmd),                        446             len(auth_cmd),
452             bytes(auth_cmd),                      447             bytes(auth_cmd),
453             1, bank_alg, dig)                     448             1, bank_alg, dig)
454                                                   449 
455         self.send_cmd(cmd)                        450         self.send_cmd(cmd)
456                                                   451 
457     def start_auth_session(self, session_type,    452     def start_auth_session(self, session_type, name_alg = TPM2_ALG_SHA1):
458         fmt = '>HII IIH16sHBHH'                   453         fmt = '>HII IIH16sHBHH'
459         cmd = struct.pack(fmt,                    454         cmd = struct.pack(fmt,
460                           TPM2_ST_NO_SESSIONS,    455                           TPM2_ST_NO_SESSIONS,
461                           struct.calcsize(fmt)    456                           struct.calcsize(fmt),
462                           TPM2_CC_START_AUTH_S    457                           TPM2_CC_START_AUTH_SESSION,
463                           TPM2_RH_NULL,           458                           TPM2_RH_NULL,
464                           TPM2_RH_NULL,           459                           TPM2_RH_NULL,
465                           16,                     460                           16,
466                           ('\0' * 16).encode()    461                           ('\0' * 16).encode(),
467                           0,                      462                           0,
468                           session_type,           463                           session_type,
469                           TPM2_ALG_NULL,          464                           TPM2_ALG_NULL,
470                           name_alg)               465                           name_alg)
471                                                   466 
472         return struct.unpack('>I', self.send_c    467         return struct.unpack('>I', self.send_cmd(cmd)[10:14])[0]
473                                                   468 
474     def __calc_pcr_digest(self, pcrs, bank_alg    469     def __calc_pcr_digest(self, pcrs, bank_alg = TPM2_ALG_SHA1,
475                           digest_alg = TPM2_AL    470                           digest_alg = TPM2_ALG_SHA1):
476         x = []                                    471         x = []
477         f = get_hash_function(digest_alg)         472         f = get_hash_function(digest_alg)
478                                                   473 
479         for i in pcrs:                            474         for i in pcrs:
480             pcr = self.read_pcr(i, bank_alg)      475             pcr = self.read_pcr(i, bank_alg)
481             if pcr is None:                       476             if pcr is None:
482                 return None                       477                 return None
483             x += pcr                              478             x += pcr
484                                                   479 
485         return f(bytearray(x)).digest()           480         return f(bytearray(x)).digest()
486                                                   481 
487     def policy_pcr(self, handle, pcrs, bank_al    482     def policy_pcr(self, handle, pcrs, bank_alg = TPM2_ALG_SHA1,
488                    name_alg = TPM2_ALG_SHA1):     483                    name_alg = TPM2_ALG_SHA1):
489         ds = get_digest_size(name_alg)            484         ds = get_digest_size(name_alg)
490         dig = self.__calc_pcr_digest(pcrs, ban    485         dig = self.__calc_pcr_digest(pcrs, bank_alg, name_alg)
491         if not dig:                               486         if not dig:
492             raise UnknownPCRBankError(bank_alg    487             raise UnknownPCRBankError(bank_alg)
493                                                   488 
494         pcrsel_len = max((max(pcrs) >> 3) + 1,    489         pcrsel_len = max((max(pcrs) >> 3) + 1, 3)
495         pcrsel = [0] * pcrsel_len                 490         pcrsel = [0] * pcrsel_len
496         for i in pcrs:                            491         for i in pcrs:
497             pcrsel[i >> 3] |= 1 << (i & 7)        492             pcrsel[i >> 3] |= 1 << (i & 7)
498         pcrsel = ''.join(map(chr, pcrsel)).enc    493         pcrsel = ''.join(map(chr, pcrsel)).encode()
499                                                   494 
500         fmt = '>HII IH%usIHB3s' % ds              495         fmt = '>HII IH%usIHB3s' % ds
501         cmd = struct.pack(fmt,                    496         cmd = struct.pack(fmt,
502                           TPM2_ST_NO_SESSIONS,    497                           TPM2_ST_NO_SESSIONS,
503                           struct.calcsize(fmt)    498                           struct.calcsize(fmt),
504                           TPM2_CC_POLICY_PCR,     499                           TPM2_CC_POLICY_PCR,
505                           handle,                 500                           handle,
506                           len(dig),               501                           len(dig),
507                           bytes(dig),             502                           bytes(dig),
508                           1,                      503                           1,
509                           bank_alg,               504                           bank_alg,
510                           pcrsel_len, pcrsel)     505                           pcrsel_len, pcrsel)
511                                                   506 
512         self.send_cmd(cmd)                        507         self.send_cmd(cmd)
513                                                   508 
514     def policy_password(self, handle):            509     def policy_password(self, handle):
515         fmt = '>HII I'                            510         fmt = '>HII I'
516         cmd = struct.pack(fmt,                    511         cmd = struct.pack(fmt,
517                           TPM2_ST_NO_SESSIONS,    512                           TPM2_ST_NO_SESSIONS,
518                           struct.calcsize(fmt)    513                           struct.calcsize(fmt),
519                           TPM2_CC_POLICY_PASSW    514                           TPM2_CC_POLICY_PASSWORD,
520                           handle)                 515                           handle)
521                                                   516 
522         self.send_cmd(cmd)                        517         self.send_cmd(cmd)
523                                                   518 
524     def get_policy_digest(self, handle):          519     def get_policy_digest(self, handle):
525         fmt = '>HII I'                            520         fmt = '>HII I'
526         cmd = struct.pack(fmt,                    521         cmd = struct.pack(fmt,
527                           TPM2_ST_NO_SESSIONS,    522                           TPM2_ST_NO_SESSIONS,
528                           struct.calcsize(fmt)    523                           struct.calcsize(fmt),
529                           TPM2_CC_POLICY_GET_D    524                           TPM2_CC_POLICY_GET_DIGEST,
530                           handle)                 525                           handle)
531                                                   526 
532         return self.send_cmd(cmd)[12:]            527         return self.send_cmd(cmd)[12:]
533                                                   528 
534     def flush_context(self, handle):              529     def flush_context(self, handle):
535         fmt = '>HIII'                             530         fmt = '>HIII'
536         cmd = struct.pack(fmt,                    531         cmd = struct.pack(fmt,
537                           TPM2_ST_NO_SESSIONS,    532                           TPM2_ST_NO_SESSIONS,
538                           struct.calcsize(fmt)    533                           struct.calcsize(fmt),
539                           TPM2_CC_FLUSH_CONTEX    534                           TPM2_CC_FLUSH_CONTEXT,
540                           handle)                 535                           handle)
541                                                   536 
542         self.send_cmd(cmd)                        537         self.send_cmd(cmd)
543                                                   538 
544     def create_root_key(self, auth_value = byt    539     def create_root_key(self, auth_value = bytes()):
545         attributes = \                            540         attributes = \
546             Public.FIXED_TPM | \                  541             Public.FIXED_TPM | \
547             Public.FIXED_PARENT | \               542             Public.FIXED_PARENT | \
548             Public.SENSITIVE_DATA_ORIGIN | \      543             Public.SENSITIVE_DATA_ORIGIN | \
549             Public.USER_WITH_AUTH | \             544             Public.USER_WITH_AUTH | \
550             Public.RESTRICTED | \                 545             Public.RESTRICTED | \
551             Public.DECRYPT                        546             Public.DECRYPT
552                                                   547 
553         auth_cmd = AuthCommand()                  548         auth_cmd = AuthCommand()
554         sensitive = SensitiveCreate(user_auth=    549         sensitive = SensitiveCreate(user_auth=auth_value)
555                                                   550 
556         public_parms = struct.pack(               551         public_parms = struct.pack(
557             '>HHHHHI',                            552             '>HHHHHI',
558             TPM2_ALG_AES,                         553             TPM2_ALG_AES,
559             128,                                  554             128,
560             TPM2_ALG_CFB,                         555             TPM2_ALG_CFB,
561             TPM2_ALG_NULL,                        556             TPM2_ALG_NULL,
562             2048,                                 557             2048,
563             0)                                    558             0)
564                                                   559 
565         public = Public(                          560         public = Public(
566             object_type=TPM2_ALG_RSA,             561             object_type=TPM2_ALG_RSA,
567             name_alg=TPM2_ALG_SHA1,               562             name_alg=TPM2_ALG_SHA1,
568             object_attributes=attributes,         563             object_attributes=attributes,
569             parameters=public_parms)              564             parameters=public_parms)
570                                                   565 
571         fmt = '>HIII I%us H%us H%us HI' % \       566         fmt = '>HIII I%us H%us H%us HI' % \
572             (len(auth_cmd), len(sensitive), le    567             (len(auth_cmd), len(sensitive), len(public))
573         cmd = struct.pack(                        568         cmd = struct.pack(
574             fmt,                                  569             fmt,
575             TPM2_ST_SESSIONS,                     570             TPM2_ST_SESSIONS,
576             struct.calcsize(fmt),                 571             struct.calcsize(fmt),
577             TPM2_CC_CREATE_PRIMARY,               572             TPM2_CC_CREATE_PRIMARY,
578             TPM2_RH_OWNER,                        573             TPM2_RH_OWNER,
579             len(auth_cmd),                        574             len(auth_cmd),
580             bytes(auth_cmd),                      575             bytes(auth_cmd),
581             len(sensitive),                       576             len(sensitive),
582             bytes(sensitive),                     577             bytes(sensitive),
583             len(public),                          578             len(public),
584             bytes(public),                        579             bytes(public),
585             0, 0)                                 580             0, 0)
586                                                   581 
587         return struct.unpack('>I', self.send_c    582         return struct.unpack('>I', self.send_cmd(cmd)[10:14])[0]
588                                                   583 
589     def seal(self, parent_key, data, auth_valu    584     def seal(self, parent_key, data, auth_value, policy_dig,
590              name_alg = TPM2_ALG_SHA1):           585              name_alg = TPM2_ALG_SHA1):
591         ds = get_digest_size(name_alg)            586         ds = get_digest_size(name_alg)
592         assert(not policy_dig or ds == len(pol    587         assert(not policy_dig or ds == len(policy_dig))
593                                                   588 
594         attributes = 0                            589         attributes = 0
595         if not policy_dig:                        590         if not policy_dig:
596             attributes |= Public.USER_WITH_AUT    591             attributes |= Public.USER_WITH_AUTH
597             policy_dig = bytes()                  592             policy_dig = bytes()
598                                                   593 
599         auth_cmd =  AuthCommand()                 594         auth_cmd =  AuthCommand()
600         sensitive = SensitiveCreate(user_auth=    595         sensitive = SensitiveCreate(user_auth=auth_value, data=data)
601                                                   596 
602         public = Public(                          597         public = Public(
603             object_type=TPM2_ALG_KEYEDHASH,       598             object_type=TPM2_ALG_KEYEDHASH,
604             name_alg=name_alg,                    599             name_alg=name_alg,
605             object_attributes=attributes,         600             object_attributes=attributes,
606             auth_policy=policy_dig,               601             auth_policy=policy_dig,
607             parameters=struct.pack('>H', TPM2_    602             parameters=struct.pack('>H', TPM2_ALG_NULL))
608                                                   603 
609         fmt = '>HIII I%us H%us H%us HI' % \       604         fmt = '>HIII I%us H%us H%us HI' % \
610             (len(auth_cmd), len(sensitive), le    605             (len(auth_cmd), len(sensitive), len(public))
611         cmd = struct.pack(                        606         cmd = struct.pack(
612             fmt,                                  607             fmt,
613             TPM2_ST_SESSIONS,                     608             TPM2_ST_SESSIONS,
614             struct.calcsize(fmt),                 609             struct.calcsize(fmt),
615             TPM2_CC_CREATE,                       610             TPM2_CC_CREATE,
616             parent_key,                           611             parent_key,
617             len(auth_cmd),                        612             len(auth_cmd),
618             bytes(auth_cmd),                      613             bytes(auth_cmd),
619             len(sensitive),                       614             len(sensitive),
620             bytes(sensitive),                     615             bytes(sensitive),
621             len(public),                          616             len(public),
622             bytes(public),                        617             bytes(public),
623             0, 0)                                 618             0, 0)
624                                                   619 
625         rsp = self.send_cmd(cmd)                  620         rsp = self.send_cmd(cmd)
626                                                   621 
627         return rsp[14:]                           622         return rsp[14:]
628                                                   623 
629     def unseal(self, parent_key, blob, auth_va    624     def unseal(self, parent_key, blob, auth_value, policy_handle):
630         private_len = struct.unpack('>H', blob    625         private_len = struct.unpack('>H', blob[0:2])[0]
631         public_start = private_len + 2            626         public_start = private_len + 2
632         public_len = struct.unpack('>H', blob[    627         public_len = struct.unpack('>H', blob[public_start:public_start + 2])[0]
633         blob = blob[:private_len + public_len     628         blob = blob[:private_len + public_len + 4]
634                                                   629 
635         auth_cmd = AuthCommand()                  630         auth_cmd = AuthCommand()
636                                                   631 
637         fmt = '>HII I I%us %us' % (len(auth_cm    632         fmt = '>HII I I%us %us' % (len(auth_cmd), len(blob))
638         cmd = struct.pack(                        633         cmd = struct.pack(
639             fmt,                                  634             fmt,
640             TPM2_ST_SESSIONS,                     635             TPM2_ST_SESSIONS,
641             struct.calcsize(fmt),                 636             struct.calcsize(fmt),
642             TPM2_CC_LOAD,                         637             TPM2_CC_LOAD,
643             parent_key,                           638             parent_key,
644             len(auth_cmd),                        639             len(auth_cmd),
645             bytes(auth_cmd),                      640             bytes(auth_cmd),
646             blob)                                 641             blob)
647                                                   642 
648         data_handle = struct.unpack('>I', self    643         data_handle = struct.unpack('>I', self.send_cmd(cmd)[10:14])[0]
649                                                   644 
650         if policy_handle:                         645         if policy_handle:
651             auth_cmd = AuthCommand(session_han    646             auth_cmd = AuthCommand(session_handle=policy_handle, hmac=auth_value)
652         else:                                     647         else:
653             auth_cmd = AuthCommand(hmac=auth_v    648             auth_cmd = AuthCommand(hmac=auth_value)
654                                                   649 
655         fmt = '>HII I I%us' % (len(auth_cmd))     650         fmt = '>HII I I%us' % (len(auth_cmd))
656         cmd = struct.pack(                        651         cmd = struct.pack(
657             fmt,                                  652             fmt,
658             TPM2_ST_SESSIONS,                     653             TPM2_ST_SESSIONS,
659             struct.calcsize(fmt),                 654             struct.calcsize(fmt),
660             TPM2_CC_UNSEAL,                       655             TPM2_CC_UNSEAL,
661             data_handle,                          656             data_handle,
662             len(auth_cmd),                        657             len(auth_cmd),
663             bytes(auth_cmd))                      658             bytes(auth_cmd))
664                                                   659 
665         try:                                      660         try:
666             rsp = self.send_cmd(cmd)              661             rsp = self.send_cmd(cmd)
667         finally:                                  662         finally:
668             self.flush_context(data_handle)       663             self.flush_context(data_handle)
669                                                   664 
670         data_len = struct.unpack('>I', rsp[10:    665         data_len = struct.unpack('>I', rsp[10:14])[0] - 2
671                                                   666 
672         return rsp[16:16 + data_len]              667         return rsp[16:16 + data_len]
673                                                   668 
674     def reset_da_lock(self):                      669     def reset_da_lock(self):
675         auth_cmd = AuthCommand()                  670         auth_cmd = AuthCommand()
676                                                   671 
677         fmt = '>HII I I%us' % (len(auth_cmd))     672         fmt = '>HII I I%us' % (len(auth_cmd))
678         cmd = struct.pack(                        673         cmd = struct.pack(
679             fmt,                                  674             fmt,
680             TPM2_ST_SESSIONS,                     675             TPM2_ST_SESSIONS,
681             struct.calcsize(fmt),                 676             struct.calcsize(fmt),
682             TPM2_CC_DICTIONARY_ATTACK_LOCK_RES    677             TPM2_CC_DICTIONARY_ATTACK_LOCK_RESET,
683             TPM2_RH_LOCKOUT,                      678             TPM2_RH_LOCKOUT,
684             len(auth_cmd),                        679             len(auth_cmd),
685             bytes(auth_cmd))                      680             bytes(auth_cmd))
686                                                   681 
687         self.send_cmd(cmd)                        682         self.send_cmd(cmd)
688                                                   683 
689     def __get_cap_cnt(self, cap, pt, cnt):        684     def __get_cap_cnt(self, cap, pt, cnt):
690         handles = []                              685         handles = []
691         fmt = '>HII III'                          686         fmt = '>HII III'
692                                                   687 
693         cmd = struct.pack(fmt,                    688         cmd = struct.pack(fmt,
694                           TPM2_ST_NO_SESSIONS,    689                           TPM2_ST_NO_SESSIONS,
695                           struct.calcsize(fmt)    690                           struct.calcsize(fmt),
696                           TPM2_CC_GET_CAPABILI    691                           TPM2_CC_GET_CAPABILITY,
697                           cap, pt, cnt)           692                           cap, pt, cnt)
698                                                   693 
699         rsp = self.send_cmd(cmd)[10:]             694         rsp = self.send_cmd(cmd)[10:]
700         more_data, cap, cnt = struct.unpack('>    695         more_data, cap, cnt = struct.unpack('>BII', rsp[:9])
701         rsp = rsp[9:]                             696         rsp = rsp[9:]
702                                                   697 
703         for i in range(0, cnt):                   698         for i in range(0, cnt):
704             handle = struct.unpack('>I', rsp[:    699             handle = struct.unpack('>I', rsp[:4])[0]
705             handles.append(handle)                700             handles.append(handle)
706             rsp = rsp[4:]                         701             rsp = rsp[4:]
707                                                   702 
708         return handles, more_data                 703         return handles, more_data
709                                                   704 
710     def get_cap(self, cap, pt):                   705     def get_cap(self, cap, pt):
711         handles = []                              706         handles = []
712                                                   707 
713         more_data = True                          708         more_data = True
714         while more_data:                          709         while more_data:
715             next_handles, more_data = self.__g    710             next_handles, more_data = self.__get_cap_cnt(cap, pt, 1)
716             handles += next_handles               711             handles += next_handles
717             pt += 1                               712             pt += 1
718                                                   713 
719         return handles                            714         return handles
720                                                << 
721     def get_cap_pcrs(self):                    << 
722         pcr_banks = {}                         << 
723                                                << 
724         fmt = '>HII III'                       << 
725                                                << 
726         cmd = struct.pack(fmt,                 << 
727                           TPM2_ST_NO_SESSIONS, << 
728                           struct.calcsize(fmt) << 
729                           TPM2_CC_GET_CAPABILI << 
730                           TPM2_CAP_PCRS, 0, 1) << 
731         rsp = self.send_cmd(cmd)[10:]          << 
732         _, _, cnt = struct.unpack('>BII', rsp[ << 
733         rsp = rsp[9:]                          << 
734                                                << 
735         # items are TPMS_PCR_SELECTION's       << 
736         for i in range(0, cnt):                << 
737               hash, sizeOfSelect = struct.unpa << 
738               rsp = rsp[3:]                    << 
739                                                << 
740               pcrSelect = 0                    << 
741               if sizeOfSelect > 0:             << 
742                   pcrSelect, = struct.unpack(' << 
743                                              r << 
744                   rsp = rsp[sizeOfSelect:]     << 
745                   pcrSelect = int.from_bytes(p << 
746                                                << 
747               pcr_banks[hash] = pcrSelect      << 
748                                                << 
749         return pcr_banks                       << 
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php