|
| 1 | +"""A EventLog module. |
| 2 | +
|
| 3 | +This module provides functions related to event log processing. The event log can be fetched by |
| 4 | +Confidential Cloud-Native Primitives (CCNP), and is defined according to several tcg supported event |
| 5 | +log formats defined in TCG_PCClient Spec, Canonical Eventlog Spec, etc. |
| 6 | +
|
| 7 | +CCNP: https://github.com/intel/confidential-cloud-native-primitives |
| 8 | +TCG_PCClient Spec: |
| 9 | + https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf |
| 10 | +Canonical Eventlog Spec: |
| 11 | + https://trustedcomputinggroup.org/wp-content/uploads/TCG_IWG_CEL_v1_r0p41_pub.pdf |
| 12 | +
|
| 13 | +Functions: |
| 14 | + replay_event_log: Replay event logs by IMR index. |
| 15 | + verify_event_log: Verify event log by comparing IMR value from CCNP fetching and replayed from |
| 16 | + event log. |
| 17 | +""" |
| 18 | +import logging |
| 19 | +import base64 |
| 20 | +from hashlib import sha1, sha256, sha384, sha512 |
| 21 | + |
| 22 | +from ccnp.eventlog.eventlog_sdk import CCEventLogEntry, CCAlgorithms |
| 23 | +from ccnp import Measurement, MeasurementType |
| 24 | + |
| 25 | +LOG = logging.getLogger(__name__) |
| 26 | + |
| 27 | +IMR_VERIFY_COUNT = 3 |
| 28 | + |
| 29 | +def replay_event_log(event_logs: list[CCEventLogEntry]) -> dict: |
| 30 | + """Replay event logs by Integrated Measurement Register (IMR) index. |
| 31 | +
|
| 32 | + Args: |
| 33 | + event_logs (list[CCEventLogEntry]): Event logs fetched by CCNP. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + dict: A dictionary containing the replay result displayed by IMR index and hash algorithm. |
| 37 | + Layer 1 key of the dict is the IMR index, the value is another dict which using the hash |
| 38 | + algorithm as the key and the replayed measurement as value. |
| 39 | + Sample value: |
| 40 | + { 0: { 12: <measurement_replayed>}} |
| 41 | + """ |
| 42 | + measurement_dict = {} |
| 43 | + for event_log in event_logs: |
| 44 | + # pylint: disable-next=consider-iterating-dictionary |
| 45 | + if event_log.reg_idx not in measurement_dict.keys(): |
| 46 | + measurement_dict[event_log.reg_idx] = {} |
| 47 | + |
| 48 | + alg_id = event_log.alg_id.algo_id |
| 49 | + # Check algorithm type and prepare for replay |
| 50 | + if alg_id == CCAlgorithms.ALG_SHA1: |
| 51 | + algo = sha1() |
| 52 | + elif alg_id == CCAlgorithms.ALG_SHA384: |
| 53 | + algo = sha384() |
| 54 | + elif alg_id == CCAlgorithms.ALG_SHA256: |
| 55 | + algo = sha256() |
| 56 | + elif alg_id == CCAlgorithms.ALG_SHA512: |
| 57 | + algo = sha512() |
| 58 | + else: |
| 59 | + LOG.error("Unsupported hash algorithm %d", alg_id) |
| 60 | + continue |
| 61 | + |
| 62 | + # Initialize value if alg_id not found in dict |
| 63 | + if alg_id not in measurement_dict[event_log.reg_idx].keys(): |
| 64 | + measurement_dict[event_log.reg_idx][alg_id] = bytearray( |
| 65 | + event_log.alg_id.digest_size) |
| 66 | + |
| 67 | + # Do replay and update the result into dict |
| 68 | + digest = list(map(int, event_log.digest.strip('[]').split(' '))) |
| 69 | + # pylint: disable-next=consider-using-f-string |
| 70 | + digest_hex = ''.join('{:02x}'.format(i) for i in digest) |
| 71 | + algo.update(measurement_dict[event_log.reg_idx][alg_id] + bytes.fromhex(digest_hex)) |
| 72 | + measurement_dict[event_log.reg_idx][alg_id] = algo.digest() |
| 73 | + |
| 74 | + return measurement_dict |
| 75 | + |
| 76 | +def verify_event_log(measurement_dict: dict) -> bool: |
| 77 | + """Verify event log by comparing IMR value from CCNP fetching and replayed via event log. |
| 78 | +
|
| 79 | + IMR: Integrated Measurement Register. |
| 80 | +
|
| 81 | + Args: |
| 82 | + measurement_dict (dict): The event logs replay result displayed by IMR index and hash |
| 83 | + algorithm. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + bool: True if event log verify success, False if event log verify failed. |
| 87 | + """ |
| 88 | + LOG.info("Verify IMR measurement value and replayed value from event logs") |
| 89 | + for index in range(IMR_VERIFY_COUNT): |
| 90 | + # Fectch IMR measurement |
| 91 | + LOG.info("Fetch measurements in IMR[%d]", index) |
| 92 | + imr_measurement = base64.b64decode(Measurement.get_platform_measurement( |
| 93 | + MeasurementType.TYPE_TDX_RTMR, None, index)) |
| 94 | + LOG.info("IMR[%d](measurement): %s", index, imr_measurement.hex()) |
| 95 | + |
| 96 | + # Get IMR value from replayed event log |
| 97 | + if index not in measurement_dict or measurement_dict[index] == {}: |
| 98 | + LOG.error("IMR[%d] verify failed, the replayed value from event logs doesn't exist", |
| 99 | + index) |
| 100 | + return False |
| 101 | + for value in measurement_dict[index].values(): |
| 102 | + imr_replayed = value |
| 103 | + break |
| 104 | + |
| 105 | + LOG.info("IMR[%d](replayed): %s", index, imr_replayed.hex()) |
| 106 | + if imr_measurement == imr_replayed: |
| 107 | + LOG.info("IMR[%d] passed the verification.", index) |
| 108 | + else: |
| 109 | + LOG.error("IMR[%d] did not pass the verification.", index) |
| 110 | + return False |
| 111 | + |
| 112 | + return True |
0 commit comments