Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit c7eb278

Browse files
author
Ruomeng Hao
authored
Update code and docs about CCNP (#189)
CCNP SDK has been updated, update related code and docs accordingly Signed-off-by: Hao, Ruomeng <[email protected]>
1 parent c6e39c8 commit c7eb278

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

cnap/core/eventlog.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
import base64
2020
from hashlib import sha1, sha256, sha384, sha512
2121

22-
from ccnp.eventlog.eventlog_sdk import CCEventLogEntry, CCAlgorithms
23-
from ccnp import Measurement, MeasurementType
22+
from ccnp import CcnpSdk
23+
from cctrusted_base.tcg import TcgAlgorithmRegistry
2424

2525
LOG = logging.getLogger(__name__)
2626

2727
IMR_VERIFY_COUNT = 3
2828

29-
def replay_event_log(event_logs: list[CCEventLogEntry]) -> dict:
29+
def replay_event_log(event_logs) -> dict:
3030
"""Replay event logs by Integrated Measurement Register (IMR) index.
3131
3232
Args:
33-
event_logs (list[CCEventLogEntry]): Event logs fetched by CCNP.
33+
event_logs: Event logs fetched by CCNP.
3434
3535
Returns:
3636
dict: A dictionary containing the replay result displayed by IMR index and hash algorithm.
@@ -47,13 +47,13 @@ def replay_event_log(event_logs: list[CCEventLogEntry]) -> dict:
4747

4848
alg_id = event_log.alg_id.algo_id
4949
# Check algorithm type and prepare for replay
50-
if alg_id == CCAlgorithms.ALG_SHA1:
50+
if alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA1:
5151
algo = sha1()
52-
elif alg_id == CCAlgorithms.ALG_SHA384:
52+
elif alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA384:
5353
algo = sha384()
54-
elif alg_id == CCAlgorithms.ALG_SHA256:
54+
elif alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA256:
5555
algo = sha256()
56-
elif alg_id == CCAlgorithms.ALG_SHA512:
56+
elif alg_id == TcgAlgorithmRegistry.TPM_ALG_SHA512:
5757
algo = sha512()
5858
else:
5959
LOG.error("Unsupported hash algorithm %d", alg_id)
@@ -89,9 +89,9 @@ def verify_event_log(measurement_dict: dict) -> bool:
8989
for index in range(IMR_VERIFY_COUNT):
9090
# Fectch IMR measurement
9191
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())
92+
imr_measurement = base64.b64decode(CcnpSdk.inst().get_cc_measurement(
93+
[index, 12]))
94+
LOG.info("IMR[%d](measurement): %s", index, imr_measurement.hash.hex())
9595

9696
# Get IMR value from replayed event log
9797
if index not in measurement_dict or measurement_dict[index] == {}:
@@ -102,7 +102,7 @@ def verify_event_log(measurement_dict: dict) -> bool:
102102
imr_replayed = value
103103
break
104104

105-
LOG.info("IMR[%d](replayed): %s", index, imr_replayed.hex())
105+
LOG.info("IMR[%d](replayed): %s", index, imr_replayed.hash.hex())
106106
if imr_measurement == imr_replayed:
107107
LOG.info("IMR[%d] passed the verification.", index)
108108
else:

cnap/core/keybroker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
import struct
1616
import requests
1717

18-
from ccnp import Eventlog, Quote
18+
from ccnp import CcnpSdk
1919
from cryptography.hazmat.primitives import hashes
2020
from cryptography.hazmat.primitives import serialization
2121
from cryptography.hazmat.primitives.asymmetric import rsa
2222
from cryptography.hazmat.primitives.asymmetric import padding
2323
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
2424

25-
from core.eventlog import replay_event_log, verify_event_log
25+
from core.eventlog import verify_event_log
2626

2727
LOG = logging.getLogger(__name__)
2828

@@ -120,10 +120,10 @@ def get_key(self, server_url: str, key_id: str) -> bytes: # pylint: disable=too-
120120
# Get and verify event logs before get quote.
121121
# The exectuion environment judgment will be implemented by ccnp in the future.
122122
LOG.debug("Getting event log by CCNP")
123-
event_logs = Eventlog.get_platform_eventlog()
123+
event_logs = CcnpSdk.inst().get_cc_eventlog()
124124
if event_logs is None:
125125
raise RuntimeError("Get event log failed")
126-
measurement_dict = replay_event_log(event_logs)
126+
measurement_dict = CcnpSdk.inst().replay_cc_eventlog(event_logs)
127127
if verify_event_log(measurement_dict):
128128
LOG.info("Event log verify successfully.\n")
129129
else:
@@ -137,7 +137,7 @@ def get_key(self, server_url: str, key_id: str) -> bytes: # pylint: disable=too-
137137

138138
LOG.debug("Getting TDX Quote by CCNP")
139139
user_data = base64.b64encode(pubkey_der).decode('utf-8')
140-
quote = Quote.get_quote(user_data=user_data)
140+
quote = CcnpSdk.inst().get_cc_report(data=user_data).dump()
141141
if quote is None:
142142
raise RuntimeError("Get TDX Quote failed")
143143
quote = base64.b64encode(quote.quote).decode('utf-8')

cnap/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
ccnp==0.0.2
1+
ccnp
2+
cctrusted_base
23
Flask==3.0.0
34
Flask-Cors==4.0.1
45
kafka-python==2.0.2

docs/How_to_Protect_AI_Models_in_Cloud_Native_Environments.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ The service supports attestation, measurement fetching and event logs collecting
4040
CCNP is a good choice to fetch these evidences including measurements and event logs, which hides the complexity of the underlying platforms and increase the usability of the APIs. Here's the sample code using CCNP:
4141

4242
```Python
43-
from ccnp import Eventlog
44-
event_logs = Eventlog.get_platform_eventlog()
43+
from ccnp import Ccnpsdk
44+
event_logs = CcnpSdk.inst().get_cc_eventlog()
4545
```
4646

4747
To verify that the event logs have not been tampered with, we can compare the measurement replayed from event logs with the IMR (Integrated Measurement Register) values fetched using CCNP.
4848
Here's the sample code using CCNP to fetch IMR values (use Intel TDX RTMR as example):
4949

5050
```Python
51-
from ccnp import Measurement, MeasurementType
52-
imr_measurement = Measurement.get_platform_measurement(MeasurementType.TYPE_TDX_RTMR, None, 1)
51+
from ccnp import Ccnpsdk
52+
imr_measurement = CcnpSdk.inst().get_cc_measurement([index, 12])
5353
```
5454

55-
CCNP API detail documentation can be found [here](https://intel.github.io/confidential-cloud-native-primitives/).
55+
CCNP API detail documentation can be found [here](https://cc-api.github.io/confidential-cloud-native-primitives/).
5656

5757
### 1.3 Attestation by using Confidential Cloud-Native Primitives (CCNP)
5858

5959
To get the key to decrypt the model, we need provide the quote of TEE for attestation, CCNP is a good choice to get the quote and it hides the complexity and is easy to use, sample code from CCNP:
6060

6161
```Python
62-
from ccnp import Quote
63-
quote=Quote.get_quote()
62+
from ccnp import Ccnpsdk
63+
quote = CcnpSdk.inst().get_cc_report().dump()
6464
```
6565

6666
### 1.4 AI Model Decryption
@@ -221,20 +221,20 @@ We can fetch, replay and verify event logs before attestation, the sample code:
221221
```Python
222222
import logging
223223

224-
from ccnp import Eventlog, Measurement, MeasurementType
224+
from ccnp import Ccnpsdk
225225

226226
LOG = logging.getLogger(__name__)
227227
IMR_VERIFY_COUNT = 3
228228

229229
# Fetch event logs using CCNP and replay.
230-
event_logs = Eventlog.get_platform_eventlog()
231-
measurement_dict = replay(event_logs)
230+
event_logs = CcnpSdk.inst().get_cc_eventlog()
231+
measurement_dict = CcnpSdk.inst().replay_cc_eventlog(event_logs)
232232

233233
# Fetch IMR measurement (use Intel TDX RTMR as example) and verify with replayed value.
234234
for index in range(IMR_VERIFY_COUNT):
235235
# Fectch IMR measurement
236-
imr_measurement = base64.b64decode(Measurement.get_platform_measurement(
237-
MeasurementType.TYPE_TDX_RTMR, None, index))
236+
imr_measurement = base64.b64decode(CcnpSdk.inst().get_cc_measurement(
237+
[index, 12])
238238

239239
# Get IMR value from replayed event logs
240240
for value in measurement_dict[index].values():
@@ -261,15 +261,16 @@ The sample code to get the quote with user data:
261261
```Python
262262
import base64
263263

264-
from ccnp import Quote
264+
from ccnp import Ccnpsdk
265265
from cryptography.hazmat.primitives.asymmetric import rsa
266266

267267
private_key = rsa.generate_private_key(public_exponent=65537, key_size=3072)
268268
pubkey = private_key.public_key()
269269
pubkey_der = pubkey.public_bytes(encoding=serialization.Encoding.DER,
270270
format=serialization.PublicFormat.SubjectPublicKeyInfo)
271271
user_data = base64.b64encode(pubkey_der).decode('utf-8')
272-
quote = Quote.get_quote(user_data=user_data)
272+
quote = Ccnpsdk.get_cc_report(data=user_data).dump()
273+
quote = base64.b64encode(quote.quote).decode('utf-8')
273274
```
274275

275276
### 2.7 AI Model Decryption
@@ -286,8 +287,7 @@ uint32 IV length | uint32 tag length | uint32 data length
286287

287288
To decrypt the data, here are some sample code:
288289

289-
```
290-
290+
```Python
291291
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
292292

293293
def decrypt_data(encrypted_data, key) -> bytes:
@@ -317,4 +317,5 @@ Intel’s TDX technology can provide a TEE running environment, and CCNP can sim
317317
1. Model Provider: https://github.com/intel/cloud-native-ai-pipeline/blob/main/cnap/core/modelprovider.py
318318
2. Key Broker Client: https://github.com/intel/cloud-native-ai-pipeline/blob/main/cnap/core/keybroker.py
319319
3. CCNP: https://github.com/cc-api/confidential-cloud-native-primitives
320-
4. TCG_PCClient Spec: https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf
320+
4. CC Trusted API: https://github.com/cc-api/cc-trusted-api
321+
5. TCG_PCClient Spec: https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf

docs/secure-model-design.png

30 KB
Loading

0 commit comments

Comments
 (0)