From 628966a1f87ff323617cf713c5b83a71960a3280 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Date: Tue, 4 Mar 2025 16:02:10 -0700 Subject: [PATCH 1/3] add support for cc attestation generation, as well as cert chain support --- nvml-wrapper/src/device.rs | 76 ++++++++++++++++++++++++++++++ nvml-wrapper/src/structs/device.rs | 34 +++++++++++++ 2 files changed, 110 insertions(+) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index 2e46a24..ff60693 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -802,6 +802,82 @@ impl<'nvml> Device<'nvml> { } } + /** + Gets the confidential compute GPU certificate for this `Device`. + # Errors + * `Uninitialized` if the library has not been successfully initialized + * `InvalidArg` if device is invalid or memory is NULL + * `NotSupported` if this query is not supported by the device + * `Unknown` on any unexpected error + */ + pub fn confidential_compute_gpu_certificate( + &self, + ) -> Result { + let sym = nvml_sym( + self.nvml + .lib + .nvmlDeviceGetConfComputeGpuCertificate + .as_ref(), + )?; + + unsafe { + let mut certificate_chain: nvmlConfComputeGpuCertificate_t = mem::zeroed(); + nvml_try(sym(self.device, &mut certificate_chain))?; + + Ok(ConfidentialComputeGpuCertificate { + cert_chain_size: certificate_chain.certChainSize, + attestation_cert_chain_size: certificate_chain.attestationCertChainSize, + cert_chain: certificate_chain.certChain, + attestation_cert_chain: certificate_chain.attestationCertChain, + }) + } + } + + /** + Fetches the confidential compute attestation report for this [`Device`]. + This method retrieves a comprehensive attestation report from the device, which includes: + - A 32-byte nonce + - The attestation report size (as big-endian bytes) + - The attestation report data (up to 8192 bytes) + - A flag indicating if CEC attestation is present (as big-endian bytes) + - The CEC attestation report size (as big-endian bytes) + - The CEC attestation report data (up to 4096 bytes) + The returned vector contains all these components concatenated together in the order listed above. + # Errors + * `Uninitialized`, if the library has not been successfully initialized + * `InvalidArg`, if device is invalid or memory is NULL + * `NotSupported`, if this query is not supported by the device + * `Unknown`, on any unexpected error + */ + #[doc(alias = "nvmlDeviceGetAttestationReport")] + pub fn confidential_compute_gpu_attestation_report( + &self, + nonce: [u8; NVML_CC_GPU_CEC_NONCE_SIZE as usize], + ) -> Result { + let sym = nvml_sym( + self.nvml + .lib + .nvmlDeviceGetConfComputeGpuAttestationReport + .as_ref(), + )?; + + unsafe { + let mut report: nvmlConfComputeGpuAttestationReport_st = mem::zeroed(); + report.nonce = nonce; + + nvml_try(sym(self.device, &mut report))?; + + let is_cec_attestation_report_present = report.isCecAttestationReportPresent == 1; + Ok(ConfidentialComputeGpuAttestationReport { + attestation_report_size: report.attestationReportSize, + attestation_report: report.attestationReport, + is_cec_attestation_report_present, + cec_attestation_report_size: report.cecAttestationReportSize, + cec_attestation_report: report.cecAttestationReport, + }) + } + } + /** Gets the current utilization and sampling size (sampling size in μs) for the Decoder. diff --git a/nvml-wrapper/src/structs/device.rs b/nvml-wrapper/src/structs/device.rs index 2176607..59ab326 100644 --- a/nvml-wrapper/src/structs/device.rs +++ b/nvml-wrapper/src/structs/device.rs @@ -1,6 +1,10 @@ #[cfg(target_os = "windows")] use crate::enum_wrappers::device::DriverModel; use crate::enum_wrappers::device::OperationMode; +use ffi::bindings::{ + NVML_CC_GPU_ATTESTATION_REPORT_SIZE, NVML_CC_GPU_CEC_ATTESTATION_REPORT_SIZE, + NVML_GPU_ATTESTATION_CERT_CHAIN_SIZE, NVML_GPU_CERT_CHAIN_SIZE, +}; #[cfg(feature = "serde")] use serde_derive::{Deserialize, Serialize}; @@ -101,3 +105,33 @@ pub struct RetiredPage { #[derive(Debug, Clone, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FieldId(pub u32); + +/// Returned from `Device.confidential_compute_gpu_certificate()` +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct ConfidentialComputeGpuCertificate { + /// The size of the certificate chain. + pub cert_chain_size: u32, + /// The size of the attestation certificate chain. + pub attestation_cert_chain_size: u32, + /// The certificate chain. + pub cert_chain: [u8; NVML_GPU_CERT_CHAIN_SIZE as usize], + /// The attestation certificate chain. + pub attestation_cert_chain: [u8; NVML_GPU_ATTESTATION_CERT_CHAIN_SIZE as usize], +} + +/// Returned from `Device.confidential_compute_gpu_attestation_report_bytes()` +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct ConfidentialComputeGpuAttestationReport { + /// The size of the attestation report. + pub attestation_report_size: u32, + /// The attestation report. + pub attestation_report: [u8; NVML_CC_GPU_ATTESTATION_REPORT_SIZE as usize], + /// Whether the CEC attestation report is present. + pub is_cec_attestation_report_present: bool, + /// The size of the CEC attestation report. + pub cec_attestation_report_size: u32, + /// The CEC attestation report. + pub cec_attestation_report: [u8; NVML_CC_GPU_CEC_ATTESTATION_REPORT_SIZE as usize], +} From 757046ae39b7f0e7abd03e8bf046e6f735b8e2af Mon Sep 17 00:00:00 2001 From: Jorge Antonio Date: Mon, 10 Mar 2025 17:14:22 +0000 Subject: [PATCH 2/3] check for cc capabilities of the node, as well if node is in ready state for cc --- nvml-wrapper/src/device.rs | 72 ++++++++++++++++++++++++++++++ nvml-wrapper/src/structs/device.rs | 32 +++++++++++++ 2 files changed, 104 insertions(+) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index ff60693..736c6f3 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -802,6 +802,78 @@ impl<'nvml> Device<'nvml> { } } + /** + Checks simultaneously if confidential compute is enabled, if the device is in a production environment, + and if the device is accepting client requests. + # Errors + * `Uninitialized`, if the library has not been successfully initialized + * `NotSupported`, if this query is not supported by the device + * `InvalidArg`, if confidential compute state is invalid + */ + pub fn check_confidential_compute_status(&self) -> Result { + let cc_state_sym = nvml_sym(self.nvml.lib.nvmlSystemGetConfComputeState.as_ref())?; + let cc_gpus_ready_sym = nvml_sym( + self.nvml + .lib + .nvmlSystemGetConfComputeGpusReadyState + .as_ref(), + )?; + + unsafe { + let mut state: nvmlConfComputeSystemState_t = mem::zeroed(); + nvml_try(cc_state_sym(&mut state))?; + + let is_cc_enabled = state.ccFeature == NVML_CC_SYSTEM_FEATURE_ENABLED; + let is_prod_environment = state.environment == NVML_CC_SYSTEM_ENVIRONMENT_PROD; + + let mut cc_gpus_ready: std::os::raw::c_uint = 0; + nvml_try(cc_gpus_ready_sym(&mut cc_gpus_ready))?; + let is_accepting_client_requests = + cc_gpus_ready == NVML_CC_ACCEPTING_CLIENT_REQUESTS_TRUE; + + Ok(is_cc_enabled && is_prod_environment && is_accepting_client_requests) + } + } + + /** + Gets the confidential compute capabilities for this `Device`. + # Errors + * `Uninitialized`, if the library has not been successfully initialized + * `InvalidArg`, if device is invalid or memory is NULL + * `NotSupported`, if this query is not supported by the device + * `Unknown`, on any unexpected error + */ + pub fn get_confidential_compute_capabilities( + &self, + ) -> Result { + let sym = nvml_sym(self.nvml.lib.nvmlSystemGetConfComputeCapabilities.as_ref())?; + + unsafe { + let mut capabilities: nvmlConfComputeSystemCaps_t = mem::zeroed(); + nvml_try(sym(&mut capabilities))?; + + let cpu_caps = match capabilities.cpuCaps { + NVML_CC_SYSTEM_CPU_CAPS_NONE => ConfidentialComputeCpuCapabilities::None, + NVML_CC_SYSTEM_CPU_CAPS_AMD_SEV => ConfidentialComputeCpuCapabilities::AmdSev, + NVML_CC_SYSTEM_CPU_CAPS_INTEL_TDX => ConfidentialComputeCpuCapabilities::IntelTdx, + _ => return Err(NvmlError::Unknown), + }; + + let gpus_caps = match capabilities.gpusCaps { + NVML_CC_SYSTEM_GPUS_CC_CAPABLE => ConfidentialComputeGpuCapabilities::Capable, + NVML_CC_SYSTEM_GPUS_CC_NOT_CAPABLE => { + ConfidentialComputeGpuCapabilities::NotCapable + } + _ => return Err(NvmlError::Unknown), + }; + + Ok(ConfidentialComputeCapabilities { + cpu_caps, + gpus_caps, + }) + } + } + /** Gets the confidential compute GPU certificate for this `Device`. # Errors diff --git a/nvml-wrapper/src/structs/device.rs b/nvml-wrapper/src/structs/device.rs index 59ab326..d901ea7 100644 --- a/nvml-wrapper/src/structs/device.rs +++ b/nvml-wrapper/src/structs/device.rs @@ -106,6 +106,38 @@ pub struct RetiredPage { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FieldId(pub u32); +/// Returned from `Device.get_confidential_compute_capabilities()` +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct ConfidentialComputeCapabilities { + /// The CPU capabilities. + pub cpu_caps: ConfidentialComputeCpuCapabilities, + /// The GPU capabilities. + pub gpus_caps: ConfidentialComputeGpuCapabilities, +} + +/// The possible CPU capabilities for confidential compute (either None, AMD SEV or Intel TDX) +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum ConfidentialComputeCpuCapabilities { + /// No CPU capabilities. + None, + /// AMD SEV confidential compute capabilities. + AmdSev, + /// Intel TDX confidential compute capabilities. + IntelTdx, +} + +/// The possible GPU capabilities for confidential compute (either not capable or capable) +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum ConfidentialComputeGpuCapabilities { + /// Capable. + Capable, + /// Not capable. + NotCapable, +} + /// Returned from `Device.confidential_compute_gpu_certificate()` #[derive(Debug, Clone, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] From 98a368ba388f87f774b0ecf15cb33932aae398a1 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Date: Fri, 14 Mar 2025 08:31:39 +0000 Subject: [PATCH 3/3] address build failures --- nvml-wrapper/src/device.rs | 8 ++++---- nvml-wrapper/src/structs/device.rs | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nvml-wrapper/src/device.rs b/nvml-wrapper/src/device.rs index 736c6f3..f97f34a 100644 --- a/nvml-wrapper/src/device.rs +++ b/nvml-wrapper/src/device.rs @@ -899,8 +899,8 @@ impl<'nvml> Device<'nvml> { Ok(ConfidentialComputeGpuCertificate { cert_chain_size: certificate_chain.certChainSize, attestation_cert_chain_size: certificate_chain.attestationCertChainSize, - cert_chain: certificate_chain.certChain, - attestation_cert_chain: certificate_chain.attestationCertChain, + cert_chain: certificate_chain.certChain.to_vec(), + attestation_cert_chain: certificate_chain.attestationCertChain.to_vec(), }) } } @@ -942,10 +942,10 @@ impl<'nvml> Device<'nvml> { let is_cec_attestation_report_present = report.isCecAttestationReportPresent == 1; Ok(ConfidentialComputeGpuAttestationReport { attestation_report_size: report.attestationReportSize, - attestation_report: report.attestationReport, + attestation_report: report.attestationReport.to_vec(), is_cec_attestation_report_present, cec_attestation_report_size: report.cecAttestationReportSize, - cec_attestation_report: report.cecAttestationReport, + cec_attestation_report: report.cecAttestationReport.to_vec(), }) } } diff --git a/nvml-wrapper/src/structs/device.rs b/nvml-wrapper/src/structs/device.rs index d901ea7..4f888a6 100644 --- a/nvml-wrapper/src/structs/device.rs +++ b/nvml-wrapper/src/structs/device.rs @@ -1,10 +1,6 @@ #[cfg(target_os = "windows")] use crate::enum_wrappers::device::DriverModel; use crate::enum_wrappers::device::OperationMode; -use ffi::bindings::{ - NVML_CC_GPU_ATTESTATION_REPORT_SIZE, NVML_CC_GPU_CEC_ATTESTATION_REPORT_SIZE, - NVML_GPU_ATTESTATION_CERT_CHAIN_SIZE, NVML_GPU_CERT_CHAIN_SIZE, -}; #[cfg(feature = "serde")] use serde_derive::{Deserialize, Serialize}; @@ -146,10 +142,12 @@ pub struct ConfidentialComputeGpuCertificate { pub cert_chain_size: u32, /// The size of the attestation certificate chain. pub attestation_cert_chain_size: u32, - /// The certificate chain. - pub cert_chain: [u8; NVML_GPU_CERT_CHAIN_SIZE as usize], - /// The attestation certificate chain. - pub attestation_cert_chain: [u8; NVML_GPU_ATTESTATION_CERT_CHAIN_SIZE as usize], + /// The certificate chain, of size + /// `ffi::bindings::NVML_GPU_CERT_CHAIN_SIZE == 4096`. + pub cert_chain: Vec, + /// The attestation certificate chain, of size + /// `ffi::bindings::NVML_GPU_ATTESTATION_CERT_CHAIN_SIZE == 5120`. + pub attestation_cert_chain: Vec, } /// Returned from `Device.confidential_compute_gpu_attestation_report_bytes()` @@ -158,12 +156,14 @@ pub struct ConfidentialComputeGpuCertificate { pub struct ConfidentialComputeGpuAttestationReport { /// The size of the attestation report. pub attestation_report_size: u32, - /// The attestation report. - pub attestation_report: [u8; NVML_CC_GPU_ATTESTATION_REPORT_SIZE as usize], + /// The attestation report, of size + /// `ffi::bindings::NVML_CC_GPU_ATTESTATION_REPORT_SIZE == 8192`. + pub attestation_report: Vec, /// Whether the CEC attestation report is present. pub is_cec_attestation_report_present: bool, /// The size of the CEC attestation report. pub cec_attestation_report_size: u32, - /// The CEC attestation report. - pub cec_attestation_report: [u8; NVML_CC_GPU_CEC_ATTESTATION_REPORT_SIZE as usize], + /// The CEC attestation report, of size + /// `ffi::bindings::NVML_CC_GPU_CEC_ATTESTATION_REPORT_SIZE == 4096`. + pub cec_attestation_report: Vec, }