-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vmsdk/python/tests: add tests for TDX
Signed-off-by: zhongjie <zhongjie.shi@intel.com>
- Loading branch information
1 parent
904333a
commit 6913295
Showing
13 changed files
with
223 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
RTMR (Runtime Measurement Register). | ||
""" | ||
|
||
from cctrusted_base.imr import TcgIMR | ||
from cctrusted_base.tcg import TcgAlgorithmRegistry | ||
|
||
class TdxRTMR(TcgIMR): | ||
"""RTMR class defined for Intel TDX.""" | ||
|
||
RTMR_COUNT = 4 | ||
"""Intel TDX TDREPORT provides the 4 measurement registers by default.""" | ||
|
||
RTMR_LENGTH_BY_BYTES = 48 | ||
"""RTMR length by bytes.""" | ||
|
||
@property | ||
def max_index(self): | ||
return 3 | ||
|
||
def __init__(self, index, digest_hash): | ||
super().__init__(index, TcgAlgorithmRegistry.TPM_ALG_SHA384, | ||
digest_hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
""" | ||
PCR (Platform Configuration Register). | ||
""" | ||
|
||
from cctrusted_base.imr import TcgIMR | ||
|
||
class TpmPCR(TcgIMR): | ||
"""PCR class defined for TPM""" | ||
|
||
@property | ||
def max_index(self): | ||
return 23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[pytest] | ||
markers = | ||
basic: Select the test functions for basic testing | ||
tdx: Select the test functions for TDX testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
echo Run basic tests ... | ||
python3 -m pytest -v . -m basic | ||
|
||
cc_type=$(python3 -c ' | ||
from cctrusted_vm.cvm import ConfidentialVM | ||
cc_type = ConfidentialVM.detect_cc_type() | ||
print(ConfidentialVM.TYPE_CC_STRING[cc_type]) | ||
') | ||
echo CC type is ${cc_type} | ||
|
||
if [[ ${cc_type} == "TDX" ]]; then | ||
echo Run TDX specific tests ... | ||
python3 -m pytest -v . -m tdx | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""TDX specific test.""" | ||
|
||
from hashlib import sha384 | ||
import pytest | ||
from cctrusted_base.tcg import TcgAlgorithmRegistry, TcgImrEvent | ||
from cctrusted_base.tdx.quote import TdxQuote, TdxQuoteBody | ||
from cctrusted_base.tdx.rtmr import TdxRTMR | ||
from cctrusted_vm.sdk import CCTrustedVmSdk | ||
|
||
@pytest.mark.tdx | ||
def test_tdx_get_default_algorithms(): | ||
"""Test default algorithm is supported.""" | ||
algo = CCTrustedVmSdk.inst().get_default_algorithms() | ||
assert algo is not None | ||
assert algo.alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA384 | ||
|
||
@pytest.mark.tdx | ||
def test_tdx_get_measurement_count(): | ||
"""Test measurement count is 4 (RTMR count).""" | ||
count = CCTrustedVmSdk.inst().get_measurement_count() | ||
assert count == TdxRTMR.RTMR_COUNT | ||
|
||
def _replay_eventlog(): | ||
"""Get RTMRs from event log by replay.""" | ||
rtmr_len = TdxRTMR.RTMR_LENGTH_BY_BYTES | ||
rtmr_cnt = TdxRTMR.RTMR_COUNT | ||
rtmrs = [bytearray(rtmr_len)] * rtmr_cnt | ||
event_logs = CCTrustedVmSdk.inst().get_eventlog().event_logs | ||
assert event_logs is not None | ||
for event in event_logs: | ||
if isinstance(event, TcgImrEvent): | ||
sha384_algo = sha384() | ||
sha384_algo.update(rtmrs[event.imr_index] + event.digests[0].hash) | ||
rtmrs[event.imr_index] = sha384_algo.digest() | ||
return rtmrs | ||
|
||
def _check_imr(imr_index: int, alg_id: int, rtmr: bytes): | ||
"""Check individual IMR. | ||
Compare the 4 IMR hash with the hash derived by replay event log. They are | ||
expected to be same. | ||
Args: | ||
imr_index: an integer specified the IMR index. | ||
alg_id: an integer specified the hash algorithm. | ||
rtmr: bytes of RTMR data for comparison. | ||
""" | ||
assert 0 <= imr_index < TdxRTMR.RTMR_COUNT | ||
assert rtmr is not None | ||
assert alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA384 | ||
imr = CCTrustedVmSdk.inst().get_measurement([imr_index, alg_id]) | ||
assert imr is not None | ||
digest_obj = imr.digest(alg_id) | ||
assert digest_obj is not None | ||
digest_alg_id = digest_obj.alg.alg_id | ||
assert digest_alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA384 | ||
digest_hash = digest_obj.hash | ||
assert digest_hash is not None | ||
assert digest_hash == rtmr, \ | ||
f"rtmr {rtmr.hex()} doesn't equal digest {digest_hash.hex()}" | ||
|
||
@pytest.mark.tdx | ||
def test_tdx_get_measurement_imrs(): | ||
"""Test measurement result. | ||
The test is done by compare the measurement register against the value | ||
derived by replay eventlog. | ||
""" | ||
alg = CCTrustedVmSdk.inst().get_default_algorithms() | ||
rtmrs = _replay_eventlog() | ||
_check_imr(0, alg.alg_id, rtmrs[0]) | ||
_check_imr(1, alg.alg_id, rtmrs[1]) | ||
_check_imr(2, alg.alg_id, rtmrs[2]) | ||
_check_imr(3, alg.alg_id, rtmrs[3]) | ||
|
||
@pytest.mark.tdx | ||
def test_tdx_get_quote_rtmrs(): | ||
"""Test quote result. | ||
The test is done by compare the RTMRs in quote body against the value | ||
derived by replay eventlog. | ||
""" | ||
quote = CCTrustedVmSdk.inst().get_quote() | ||
assert quote is not None | ||
assert isinstance(quote, TdxQuote) | ||
body = quote.body | ||
assert body is not None | ||
assert isinstance(body, TdxQuoteBody) | ||
rtmrs = _replay_eventlog() | ||
assert body.rtmr0 == rtmrs[0], \ | ||
"RTMR0 doesn't equal the replay from event log!" | ||
assert body.rtmr1 == rtmrs[1], \ | ||
"RTMR1 doesn't equal the replay from event log!" | ||
assert body.rtmr2 == rtmrs[2], \ | ||
"RTMR2 doesn't equal the replay from event log!" | ||
assert body.rtmr3 == rtmrs[3], \ | ||
"RTMR3 doesn't equal the replay from event log!" |