[PATCH v5 17/23] lib: rspdm: Support SPDM negotiate_algorithms
From: alistair23
Date: Tue Oct 27 2026 - 01:31:20 EST
From: Alistair Francis <alistair.francis@xxxxxxx>
Support the NEGOTIATE_ALGORITHMS SPDM command.
Signed-off-by: Alistair Francis <alistair.francis@xxxxxxx>
---
lib/rspdm/consts.rs | 52 ++++++++++
lib/rspdm/lib.rs | 4 +
lib/rspdm/state.rs | 157 +++++++++++++++++++++++++++-
lib/rspdm/validator.rs | 226 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 438 insertions(+), 1 deletion(-)
diff --git a/lib/rspdm/consts.rs b/lib/rspdm/consts.rs
index fe5c79e0dd911..7a75bd1e4649c 100644
--- a/lib/rspdm/consts.rs
+++ b/lib/rspdm/consts.rs
@@ -45,6 +45,58 @@
pub(crate) const SPDM_CERT_CAP: u32 = bit_u32(1);
pub(crate) const SPDM_CHAL_CAP: u32 = bit_u32(2);
+pub(crate) const SPDM_KEY_EX_CAP: u32 = bit_u32(9);
+pub(crate) const SPDM_MEAS_RESP_CAP: u32 = bit_u32(3) | bit_u32(4);
pub(crate) const SPDM_REQ_CAPS: u32 = SPDM_CERT_CAP | SPDM_CHAL_CAP;
pub(crate) const SPDM_RSP_MIN_CAPS: u32 = SPDM_CERT_CAP | SPDM_CHAL_CAP;
+
+pub(crate) const SPDM_NEGOTIATE_ALGS: u8 = 0xe3;
+
+pub(crate) const SPDM_MEAS_SPEC_DMTF: u8 = bit_u8(0);
+
+pub(crate) const SPDM_ASYM_RSASSA_2048: u32 = bit_u32(0);
+pub(crate) const _SPDM_ASYM_RSAPSS_2048: u32 = bit_u32(1);
+pub(crate) const SPDM_ASYM_RSASSA_3072: u32 = bit_u32(2);
+pub(crate) const _SPDM_ASYM_RSAPSS_3072: u32 = bit_u32(3);
+pub(crate) const SPDM_ASYM_ECDSA_ECC_NIST_P256: u32 = bit_u32(4);
+pub(crate) const SPDM_ASYM_RSASSA_4096: u32 = bit_u32(5);
+pub(crate) const _SPDM_ASYM_RSAPSS_4096: u32 = bit_u32(6);
+pub(crate) const SPDM_ASYM_ECDSA_ECC_NIST_P384: u32 = bit_u32(7);
+pub(crate) const SPDM_ASYM_ECDSA_ECC_NIST_P521: u32 = bit_u32(8);
+pub(crate) const _SPDM_ASYM_SM2_ECC_SM2_P256: u32 = bit_u32(9);
+pub(crate) const _SPDM_ASYM_EDDSA_ED25519: u32 = bit_u32(10);
+pub(crate) const _SPDM_ASYM_EDDSA_ED448: u32 = bit_u32(11);
+
+pub(crate) const SPDM_HASH_SHA_256: u32 = bit_u32(0);
+pub(crate) const SPDM_HASH_SHA_384: u32 = bit_u32(1);
+pub(crate) const SPDM_HASH_SHA_512: u32 = bit_u32(2);
+
+// If the crypto support isn't enabled don't offer the algorithms
+// to the responder
+#[cfg(CONFIG_CRYPTO_RSA)]
+pub(crate) const SPDM_ASYM_RSA: u32 =
+ SPDM_ASYM_RSASSA_2048 | SPDM_ASYM_RSASSA_3072 | SPDM_ASYM_RSASSA_4096;
+#[cfg(not(CONFIG_CRYPTO_RSA))]
+pub(crate) const SPDM_ASYM_RSA: u32 = 0;
+
+#[cfg(CONFIG_CRYPTO_ECDSA)]
+pub(crate) const SPDM_ASYM_ECDSA: u32 =
+ SPDM_ASYM_ECDSA_ECC_NIST_P256 | SPDM_ASYM_ECDSA_ECC_NIST_P384 | SPDM_ASYM_ECDSA_ECC_NIST_P521;
+#[cfg(not(CONFIG_CRYPTO_ECDSA))]
+pub(crate) const SPDM_ASYM_ECDSA: u32 = 0;
+
+#[cfg(CONFIG_CRYPTO_SHA256)]
+pub(crate) const SPDM_HASH_SHA2_256: u32 = SPDM_HASH_SHA_256;
+#[cfg(not(CONFIG_CRYPTO_SHA256))]
+pub(crate) const SPDM_HASH_SHA2_256: u32 = 0;
+
+#[cfg(CONFIG_CRYPTO_SHA512)]
+pub(crate) const SPDM_HASH_SHA2_384_512: u32 = SPDM_HASH_SHA_384 | SPDM_HASH_SHA_512;
+#[cfg(not(CONFIG_CRYPTO_SHA512))]
+pub(crate) const SPDM_HASH_SHA2_384_512: u32 = 0;
+
+pub(crate) const SPDM_ASYM_ALGOS: u32 = SPDM_ASYM_RSA | SPDM_ASYM_ECDSA;
+pub(crate) const SPDM_HASH_ALGOS: u32 = SPDM_HASH_SHA2_256 | SPDM_HASH_SHA2_384_512;
+
+pub(crate) const SPDM_OPAQUE_DATA_FMT_GENERAL: u8 = bit_u8(1);
diff --git a/lib/rspdm/lib.rs b/lib/rspdm/lib.rs
index 728910273a2ce..4c219b32098ab 100644
--- a/lib/rspdm/lib.rs
+++ b/lib/rspdm/lib.rs
@@ -266,6 +266,10 @@ pub extern "C" fn spdm_authenticate(state_ptr: *mut spdm_state) -> c_int {
return e.to_errno() as c_int;
}
+ if let Err(e) = state.negotiate_algs() {
+ return e.to_errno() as c_int;
+ }
+
-(EPROTONOSUPPORT as i32)
}
diff --git a/lib/rspdm/state.rs b/lib/rspdm/state.rs
index 815f332fabad8..79819c492b07b 100644
--- a/lib/rspdm/state.rs
+++ b/lib/rspdm/state.rs
@@ -15,14 +15,30 @@
Device, //
},
error::Error,
+ hash::Shash,
+ str::CStr,
validate::Untrusted,
};
use crate::consts::{
+ SPDM_ASYM_ALGOS,
+ SPDM_ASYM_ECDSA_ECC_NIST_P256,
+ SPDM_ASYM_ECDSA_ECC_NIST_P384,
+ SPDM_ASYM_ECDSA_ECC_NIST_P521,
+ SPDM_ASYM_RSASSA_2048,
+ SPDM_ASYM_RSASSA_3072,
+ SPDM_ASYM_RSASSA_4096,
SPDM_ERROR,
SPDM_GET_VERSION_LEN,
+ SPDM_HASH_ALGOS,
+ SPDM_HASH_SHA_256,
+ SPDM_HASH_SHA_384,
+ SPDM_HASH_SHA_512,
+ SPDM_KEY_EX_CAP,
SPDM_MAX_VER,
+ SPDM_MEAS_RESP_CAP,
SPDM_MIN_VER,
+ SPDM_OPAQUE_DATA_FMT_GENERAL,
SPDM_REQ,
SPDM_RSP_MIN_CAPS,
SPDM_VER_10,
@@ -34,9 +50,12 @@
GetCapabilitiesRsp,
GetVersionReq,
GetVersionRsp,
+ NegotiateAlgsReq,
+ NegotiateAlgsRsp,
SpdmErrorRsp,
SpdmHeader,
- GET_CAPABILITIES_RSP_SZ, //
+ GET_CAPABILITIES_RSP_SZ,
+ NEGOTIATE_ALGS_RSP_SZ, //
};
/// Transport for exchanging SPDM messages with a responder device.
@@ -89,6 +108,27 @@ pub(crate) struct SpdmState<'a, T: Transport> {
/// Cached capabilities of responder. Received during GET_CAPABILITIES
/// exchange.
pub(crate) rsp_caps: u32,
+ /// Asymmetric key algorithm for signature verification of CHALLENGE_AUTH
+ /// and MEASUREMENTS messages. Selected by responder during
+ /// NEGOTIATE_ALGORITHMS exchange.
+ pub(crate) base_asym_alg: u32,
+ /// Hash algorithm for signature verification of CHALLENGE_AUTH and
+ /// MEASUREMENTS messages. Selected by responder during
+ /// NEGOTIATE_ALGORITHMS exchange.
+ pub(crate) base_hash_alg: u32,
+ /// Hash algorithm for measurement blocks. Selected by responder during
+ /// NEGOTIATE_ALGORITHMS exchange.
+ pub(crate) meas_hash_alg: u32,
+
+ /// Human-readable name of `base_asym_alg`'s signature encoding. Passed
+ /// to the crypto subsystem when calling `verify_signature()`.
+ base_asym_enc: &'static CStr,
+ /// Signature length of `base_asym_alg` (in bytes). S or SigLen in SPDM
+ /// specification.
+ sig_len: usize,
+
+ /// Hash transformation for `base_hash_alg` computation.
+ crypto: Option<Shash>,
}
impl<'a, T: Transport> SpdmState<'a, T> {
@@ -105,6 +145,12 @@ pub(crate) fn new(
validate,
version: SPDM_MIN_VER,
rsp_caps: 0,
+ base_asym_alg: 0,
+ base_hash_alg: 0,
+ meas_hash_alg: 0,
+ base_asym_enc: c"",
+ sig_len: 0,
+ crypto: None,
}
}
@@ -239,4 +285,113 @@ pub(crate) fn get_capabilities(&mut self) -> Result<(), Error> {
Ok(())
}
+
+ fn update_response_algs(&mut self) -> Result<(), Error> {
+ match self.base_asym_alg {
+ #[cfg(CONFIG_CRYPTO_RSA)]
+ SPDM_ASYM_RSASSA_2048 => {
+ self.sig_len = 256;
+ self.base_asym_enc = c"pkcs1";
+ }
+ #[cfg(CONFIG_CRYPTO_RSA)]
+ SPDM_ASYM_RSASSA_3072 => {
+ self.sig_len = 384;
+ self.base_asym_enc = c"pkcs1";
+ }
+ #[cfg(CONFIG_CRYPTO_RSA)]
+ SPDM_ASYM_RSASSA_4096 => {
+ self.sig_len = 512;
+ self.base_asym_enc = c"pkcs1";
+ }
+ #[cfg(CONFIG_CRYPTO_ECDSA)]
+ SPDM_ASYM_ECDSA_ECC_NIST_P256 => {
+ self.sig_len = 64;
+ self.base_asym_enc = c"p1363";
+ }
+ #[cfg(CONFIG_CRYPTO_ECDSA)]
+ SPDM_ASYM_ECDSA_ECC_NIST_P384 => {
+ self.sig_len = 96;
+ self.base_asym_enc = c"p1363";
+ }
+ #[cfg(CONFIG_CRYPTO_ECDSA)]
+ SPDM_ASYM_ECDSA_ECC_NIST_P521 => {
+ self.sig_len = 132;
+ self.base_asym_enc = c"p1363";
+ }
+ _ => {
+ pr_err!("Unknown asym algorithm\n");
+ return Err(EPROTO);
+ }
+ }
+
+ let hash_name = match self.base_hash_alg {
+ #[cfg(CONFIG_CRYPTO_SHA256)]
+ SPDM_HASH_SHA_256 => c"sha256",
+ #[cfg(CONFIG_CRYPTO_SHA512)]
+ SPDM_HASH_SHA_384 => c"sha384",
+ #[cfg(CONFIG_CRYPTO_SHA512)]
+ SPDM_HASH_SHA_512 => c"sha512",
+ _ => {
+ pr_err!("Unknown hash algorithm\n");
+ return Err(EPROTO);
+ }
+ };
+
+ self.crypto = Some(Shash::new(hash_name)?);
+
+ Ok(())
+ }
+
+ pub(crate) fn negotiate_algs(&mut self) -> Result<(), Error> {
+ let mut request = NegotiateAlgsReq::default();
+ request.header.version = self.version;
+
+ if self.version >= SPDM_VER_12 && (self.rsp_caps & SPDM_KEY_EX_CAP) == SPDM_KEY_EX_CAP {
+ request.other_params_support = SPDM_OPAQUE_DATA_FMT_GENERAL;
+ }
+
+ let rsp_sz =
+ (NEGOTIATE_ALGS_RSP_SZ as u32 + u32::from(u16::MAX)).min(self.transport_sz) as usize;
+
+ request.length = NegotiateAlgsReq::WIRE_SIZE as u16;
+
+ let mut request_buf = request.to_bytes()?;
+
+ let response_vec: KVec<u8> = KVec::from_elem(0u8, rsp_sz, GFP_KERNEL)?;
+
+ let response: NegotiateAlgsRsp = self
+ .spdm_exchange(request_buf.as_mut_slice(), response_vec)?
+ .validate(&*self)?;
+
+ self.base_asym_alg = response.base_asym_sel;
+ self.base_hash_alg = response.base_hash_sel;
+ self.meas_hash_alg = response.measurement_hash_algo;
+
+ if self.base_asym_alg & SPDM_ASYM_ALGOS == 0 || self.base_hash_alg & SPDM_HASH_ALGOS == 0 {
+ pr_err!("No common supported algorithms\n");
+ return Err(EPROTO);
+ }
+
+ let meas_hash_valid = if self.rsp_caps & SPDM_MEAS_RESP_CAP != 0
+ && response.measurement_specification_sel != 0
+ {
+ self.meas_hash_alg.count_ones() == 1
+ } else {
+ self.meas_hash_alg == 0
+ };
+
+ // /* Responder shall select exactly 1 alg (SPDM 1.0.0 table 14) */
+ if self.base_asym_alg.count_ones() != 1
+ || self.base_hash_alg.count_ones() != 1
+ || !meas_hash_valid
+ || response.other_params_sel != request.other_params_support
+ {
+ pr_err!("Malformed algorithms response\n");
+ return Err(EPROTO);
+ }
+
+ self.update_response_algs()?;
+
+ Ok(())
+ }
}
diff --git a/lib/rspdm/validator.rs b/lib/rspdm/validator.rs
index 080c2f2c49376..9afcb3f8bcb6c 100644
--- a/lib/rspdm/validator.rs
+++ b/lib/rspdm/validator.rs
@@ -8,12 +8,16 @@
//! <https://www.dmtf.org/dsp/DSP0274>
use crate::consts::{
+ SPDM_ASYM_ALGOS,
SPDM_CAP_SUPPORTED_ALGORITHMS,
SPDM_CTEXPONENT,
SPDM_GET_CAPABILITIES,
SPDM_GET_VERSION,
+ SPDM_HASH_ALGOS,
+ SPDM_MEAS_SPEC_DMTF,
SPDM_MIN_DATA_TRANSFER_SIZE,
SPDM_MIN_VER,
+ SPDM_NEGOTIATE_ALGS,
SPDM_REQ_CAPS,
SPDM_VER_10,
SPDM_VER_11,
@@ -493,3 +497,225 @@ fn validate(unvalidated: KVec<u8>, context: &'c SpdmState<'c, T>) -> Result<Self
})
}
}
+
+pub(crate) struct NegotiateAlgsReq {
+ pub(crate) header: SpdmHeader,
+
+ pub(crate) length: u16,
+ pub(crate) measurement_specification: u8,
+ pub(crate) other_params_support: u8,
+
+ pub(crate) base_asym_algo: u32,
+ pub(crate) base_hash_algo: u32,
+
+ pub(crate) ext_asym_count: u8,
+ pub(crate) ext_hash_count: u8,
+ pub(crate) mel_specification: u8,
+ // ext_asym
+ // ext_hash
+ // resp_alg_struct
+}
+
+impl NegotiateAlgsReq {
+ pub(crate) const WIRE_SIZE: usize = mem::size_of::<SpdmHeader>() + 28;
+
+ pub(crate) fn to_bytes(&self) -> Result<KVec<u8>> {
+ let mut out = self.header.to_bytes()?;
+
+ out.extend_from_slice(&self.length.to_le_bytes(), GFP_KERNEL)?;
+ out.push(self.measurement_specification, GFP_KERNEL)?;
+ out.push(self.other_params_support, GFP_KERNEL)?;
+
+ out.extend_from_slice(&self.base_asym_algo.to_le_bytes(), GFP_KERNEL)?;
+ out.extend_from_slice(&self.base_hash_algo.to_le_bytes(), GFP_KERNEL)?;
+
+ out.extend_from_slice(&[0u8; 12], GFP_KERNEL)?;
+
+ out.push(self.ext_asym_count, GFP_KERNEL)?;
+ out.push(self.ext_hash_count, GFP_KERNEL)?;
+ out.push(0u8, GFP_KERNEL)?;
+ out.push(self.mel_specification, GFP_KERNEL)?;
+
+ Ok(out)
+ }
+}
+
+impl Default for NegotiateAlgsReq {
+ fn default() -> Self {
+ NegotiateAlgsReq {
+ header: SpdmHeader::new(SPDM_NEGOTIATE_ALGS),
+
+ length: 32,
+ measurement_specification: SPDM_MEAS_SPEC_DMTF,
+ other_params_support: 0,
+ base_asym_algo: SPDM_ASYM_ALGOS,
+ base_hash_algo: SPDM_HASH_ALGOS,
+ ext_asym_count: 0,
+ ext_hash_count: 0,
+ mel_specification: 0,
+ }
+ }
+}
+
+/// Size of everything up to the variable-length algorithm arrays (ExtAsymSel).
+pub(crate) const NEGOTIATE_ALGS_RSP_SZ: usize = mem::size_of::<SpdmHeader>() + 32;
+
+#[expect(dead_code)]
+pub(crate) struct NegotiateAlgsRsp {
+ pub(crate) header: SpdmHeader,
+
+ pub(crate) measurement_specification_sel: u8,
+ pub(crate) other_params_sel: u8,
+
+ pub(crate) measurement_hash_algo: u32,
+ pub(crate) base_asym_sel: u32,
+ pub(crate) base_hash_sel: u32,
+
+ pub(crate) mel_specification_sel: u8,
+ pub(crate) ext_asym_sel_count: u8,
+ pub(crate) ext_hash_sel_count: u8,
+
+ pub(crate) ext_asym: KVec<u32>,
+ pub(crate) ext_hash: KVec<u32>,
+ pub(crate) resp_alg_struct: KVec<RespAlgStruct>,
+
+ /// The actual length of the SPDM message. `raw_bytes` might be longer
+ /// as it includes transport specific padding.
+ length: usize,
+
+ /// Entire response, used for the transcript hash.
+ raw_bytes: KVec<u8>,
+}
+
+impl NegotiateAlgsRsp {
+ #[expect(dead_code)]
+ pub(crate) fn as_bytes(&self) -> Result<&[u8], Error> {
+ self.raw_bytes.get(..self.length).ok_or(EPROTO)
+ }
+}
+
+impl<'c, T: Transport> Validate<'c, Untrusted<KVec<u8>>, &'c SpdmState<'c, T>>
+ for NegotiateAlgsRsp
+{
+ type Err = Error;
+
+ fn validate(unvalidated: KVec<u8>, context: &'c SpdmState<'c, T>) -> Result<Self, Self::Err> {
+ let header: SpdmHeader =
+ Untrusted::new(unvalidated.get(0..4).ok_or(EPROTO)?).validate(context)?;
+
+ if header.code != SPDM_NEGOTIATE_ALGS - 0x80 {
+ return Err(EPROTO);
+ }
+
+ if header.version != context.version {
+ pr_err!("Invalid version response\n");
+ return Err(EPROTO);
+ }
+
+ let resp_len = u16::from_le_bytes(
+ unvalidated
+ .get(4..4 + mem::size_of::<u16>())
+ .ok_or(EPROTO)?
+ .try_into()
+ .map_err(|_| EPROTO)?,
+ );
+
+ let measurement_specification_sel = *unvalidated.get(6).ok_or(EPROTO)?;
+ let other_params_sel = *unvalidated.get(7).ok_or(EPROTO)?;
+
+ // Helper to read a little-endian `u32`
+ let read_le32 = |offset: usize| -> Result<u32, Error> {
+ Ok(u32::from_le_bytes(
+ unvalidated
+ .get(offset..offset + mem::size_of::<u32>())
+ .ok_or(EPROTO)?
+ .try_into()
+ .map_err(|_| EPROTO)?,
+ ))
+ };
+
+ let measurement_hash_algo = read_le32(8)?;
+ let base_asym_sel = read_le32(12)?;
+ let base_hash_sel = read_le32(16)?;
+
+ let mel_specification_sel = *unvalidated.get(31).ok_or(EPROTO)?;
+ let ext_asym_sel_count = *unvalidated.get(32).ok_or(EPROTO)?;
+ let ext_hash_sel_count = *unvalidated.get(33).ok_or(EPROTO)?;
+
+ // We currently never request extended algorithms or algorithm structures
+ if header.param1 != 0 || ext_asym_sel_count != 0 || ext_hash_sel_count != 0 {
+ pr_err!("Responder selected unsupported extended algorithms\n");
+ return Err(EPROTO);
+ }
+
+ let mut offset = NEGOTIATE_ALGS_RSP_SZ;
+
+ let mut ext_asym = KVec::new();
+ for _ in 0..ext_asym_sel_count {
+ ext_asym.push(read_le32(offset)?, GFP_KERNEL)?;
+ offset += mem::size_of::<u32>();
+ }
+
+ let mut ext_hash = KVec::new();
+ for _ in 0..ext_hash_sel_count {
+ ext_hash.push(read_le32(offset)?, GFP_KERNEL)?;
+ offset += mem::size_of::<u32>();
+ }
+
+ let mut resp_alg_struct = KVec::new();
+ for _ in 0..header.param1 {
+ let alg_type = *unvalidated.get(offset).ok_or(EPROTO)?;
+ let alg_count = *unvalidated.get(offset + 1).ok_or(EPROTO)?;
+ let fixed_alg_count = (alg_count & 0xf) as usize;
+ let ext_alg_count = (alg_count >> 4) as usize;
+ offset += 2;
+
+ let mut alg_supported = KVec::new();
+ alg_supported.extend_from_slice(
+ unvalidated
+ .get(offset..offset + fixed_alg_count)
+ .ok_or(EPROTO)?,
+ GFP_KERNEL,
+ )?;
+ offset += fixed_alg_count;
+
+ let mut alg_external = KVec::new();
+ for _ in 0..ext_alg_count {
+ alg_external.push(read_le32(offset)?, GFP_KERNEL)?;
+ offset += mem::size_of::<u32>();
+ }
+
+ resp_alg_struct.push(
+ RespAlgStruct {
+ alg_type,
+ alg_count,
+ alg_supported,
+ alg_external,
+ },
+ GFP_KERNEL,
+ )?;
+ }
+
+ if resp_len != offset as u16 {
+ pr_err!("Incorrect response length reported\n");
+ return Err(EPROTO);
+ }
+
+ Ok(NegotiateAlgsRsp {
+ header,
+ measurement_specification_sel,
+ other_params_sel,
+ measurement_hash_algo,
+ base_asym_sel,
+ base_hash_sel,
+ mel_specification_sel,
+ ext_asym_sel_count,
+ ext_hash_sel_count,
+ ext_asym,
+ ext_hash,
+ resp_alg_struct,
+ length: offset,
+ raw_bytes: unvalidated,
+ })
+ }
+}
--
2.56.0