Skip to content

Commit

Permalink
get_measurement: complete initial draft (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Ken <[email protected]>
  • Loading branch information
kenplusplus authored Dec 11, 2023
1 parent 70d331d commit bb28afe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
21 changes: 17 additions & 4 deletions common/python/cctrusted_base/imr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Integrated Measurement Register packages.
"""

from abc import ABC, abstractmethod
from cctrusted_base.tcg import TcgDigest
from cctrusted_base.tcg import TcgDigest, TcgAlgorithmRegistry

class TcgIMR(ABC):
"""
Expand All @@ -11,9 +12,10 @@ class TcgIMR(ABC):

_INVALID_IMR_INDEX = -1

def __init__(self):
self._index = -1
self._digests:dict[int, TcgDigest] = {}
def __init__(self, index, default_alg_id, default_digest_hash):
self._index = index
self._digests:dict[int, TcgDigest] = \
{default_alg_id:TcgDigest(default_alg_id, default_digest_hash)}

@property
def index(self) -> int:
Expand All @@ -22,6 +24,13 @@ def index(self) -> int:
"""
return self._index

@property
def digests(self) -> dict:
"""
Digests dict
"""
return self._digests

def digest(self, alg_id):
"""
The digest value of IMR
Expand Down Expand Up @@ -54,6 +63,10 @@ class TdxRTMR(TcgIMR):
def max_index(self):
return 3

def __init__(self, index, digest_hash):
super().__init__(index, TcgAlgorithmRegistry.TPM_ALG_SHA384,
digest_hash)

class TpmPCR(TcgIMR):
"""
PCR class defined for TPM
Expand Down
5 changes: 3 additions & 2 deletions common/python/cctrusted_base/tcg.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ class TcgDigest:
TCG Digest
"""

def __init__(self, alg_id=TcgAlgorithmRegistry.TPM_ALG_SHA384):
self._hash: list = []
def __init__(self, alg_id=TcgAlgorithmRegistry.TPM_ALG_SHA384,
digest_hash=None):
self._hash: list = digest_hash
self._alg_id = alg_id

@property
Expand Down
12 changes: 10 additions & 2 deletions vmsdk/python/cc_imr_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

logging.basicConfig(level=logging.NOTSET, format='%(name)s %(levelname)-8s %(message)s')

imr_inst = cctrusted.get_measurement([2, None])
count = cctrusted.get_measurement_count()
for index in range(cctrusted.get_measurement_count()):
alg = cctrusted.get_default_algorithms()
digest_obj = cctrusted.get_measurement([index, alg.alg_id])

# TODO: print IMR
hash_str = ""
for hash_item in digest_obj.hash:
hash_str += "".join([f"{hash_item:02x}", " "])

LOG.info("Algorithms: %s", str(alg))
LOG.info("HASH: %s", hash_str)
14 changes: 14 additions & 0 deletions vmsdk/python/cctrusted/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@

LOG = logging.getLogger(__name__)

def get_default_algorithms() -> TcgAlgorithmRegistry:
"""
Get default algorithms ID supported by platform
"""
cvm_inst = ConfidentialVM.inst()
return TcgAlgorithmRegistry(cvm_inst.default_algo_id)

def get_measurement_count() -> int:
"""
Get IMR register value according to given index
"""
cvm_inst = ConfidentialVM.inst()
return len(cvm_inst.imrs)

def get_measurement(imr_select:[int, int]) -> TcgIMR:
"""
Get IMR register value according to given index
Expand Down
7 changes: 6 additions & 1 deletion vmsdk/python/cctrusted/cvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import struct
import fcntl
from abc import ABC, abstractmethod
from cctrusted_base.imr import TcgIMR
from cctrusted_base.imr import TdxRTMR,TcgIMR
from cctrusted_base.tcg import TcgAlgorithmRegistry
from cctrusted_base.tdx.common import TDX_VERSION_1_0, TDX_VERSION_1_5
from cctrusted_base.tdx.report import TdxReportReq10, TdxReportReq15
Expand Down Expand Up @@ -233,6 +233,11 @@ def process_cc_report(self) -> bool:

# process IMR
self._tdreport = tdreport
self._imrs[0] = TdxRTMR(0, tdreport.td_info.rtmr_0)
self._imrs[1] = TdxRTMR(1, tdreport.td_info.rtmr_1)
self._imrs[2] = TdxRTMR(2, tdreport.td_info.rtmr_2)
self._imrs[3] = TdxRTMR(3, tdreport.td_info.rtmr_3)

return True

def process_eventlog(self) -> bool:
Expand Down

0 comments on commit bb28afe

Please sign in to comment.