diff --git a/examples/spdm_requester.rs b/examples/spdm_requester.rs index a8d8440..335143f 100644 --- a/examples/spdm_requester.rs +++ b/examples/spdm_requester.rs @@ -323,7 +323,7 @@ fn full_flow(stream: TcpStream, config: &RequesterConfig) -> IoResult<()> { &mut message_buffer, Some(&ext_asym), Some(&ext_hash), - alg_structure, + Some(alg_structure), Some(&alg_external), ) .unwrap(); diff --git a/src/commands/algorithms/mod.rs b/src/commands/algorithms/mod.rs index 0c2a603..1a3e601 100644 --- a/src/commands/algorithms/mod.rs +++ b/src/commands/algorithms/mod.rs @@ -150,9 +150,10 @@ impl NegotiateAlgorithmsReq { other_param_support: OtherParamSupport, base_asym_algo: BaseAsymAlgo, base_hash_algo: BaseHashAlgo, - ext_asyn_count: u8, + ext_asym_count: u8, ext_hash_count: u8, mel_specification: MelSpecification, + alg_ext_count: u8, ) -> SpdmResult { let mut req = NegotiateAlgorithmsReq { num_alg_struct_tables, @@ -163,13 +164,14 @@ impl NegotiateAlgorithmsReq { base_asym_algo, base_hash_algo, reserved_1: [0u8; 12], - ext_asym_count: ext_asyn_count, + ext_asym_count, ext_hash_count, reserved_2: 0, mel_specification, }; - req.length = req.min_req_len(); + req.length = req.min_req_len() + + (size_of::() * alg_ext_count as usize) as u16; if req.length > MAX_SPDM_REQUEST_LENGTH { return Err(SpdmError::InvalidParam); @@ -188,8 +190,7 @@ impl NegotiateAlgorithmsReq { + size_of::() + total_alg_struct_len + total_ext_asym_len - + total_ext_hash_len - + 4) as u16 + + total_ext_hash_len) as u16 } /// Calculate the size of the extended algorithm structures in bytes. diff --git a/src/commands/algorithms/request.rs b/src/commands/algorithms/request.rs index 4a3255d..7f8d2c1 100644 --- a/src/commands/algorithms/request.rs +++ b/src/commands/algorithms/request.rs @@ -169,7 +169,7 @@ pub fn generate_negotiate_algorithms_request<'a>( req_buf: &mut MessageBuf<'a>, ext_asym: Option<&'a [ExtendedAlgo]>, ext_hash: Option<&'a [ExtendedAlgo]>, - req_alg_struct: AlgStructure, + req_alg_struct: Option, alg_external: Option<&'a [ExtendedAlgo]>, // req_alg_struct.AlgCount.ExtAlgCount many ) -> CommandResult<()> { let local_algorithms = &ctx.local_algorithms.device_algorithms; @@ -184,9 +184,12 @@ pub fn generate_negotiate_algorithms_request<'a>( None => 0, }; + let num_alg_struct_tables = req_alg_struct.is_some() as u8; + let alg_ext_count = req_alg_struct.map_or(0, |s| s.ext_alg_count()); + // Generate base structure **without** the variable length structures let negotiate_algorithms_req = NegotiateAlgorithmsReq::new( - req_alg_struct.ext_alg_count(), + num_alg_struct_tables, 0, // param2 local_algorithms.measurement_spec, local_algorithms.other_param_support, @@ -195,6 +198,7 @@ pub fn generate_negotiate_algorithms_request<'a>( ext_asym_count, ext_hash_count, local_algorithms.mel_specification, + alg_ext_count, ) .map_err(|_| (false, CommandError::UnsupportedRequest))?; @@ -253,27 +257,23 @@ pub fn generate_negotiate_algorithms_request<'a>( } } - // 3.2 - if req_alg_struct.fixed_alg_count() != 0 && !req_alg_struct.is_multiple() { - return Err((false, CommandError::UnsupportedRequest)); - } + // 3.3 + 3.4: encode AlgStructure and its extended algorithms if present + if let Some(alg_struct) = req_alg_struct { + if alg_struct.fixed_alg_count() != 0 && !alg_struct.is_multiple() { + return Err((false, CommandError::UnsupportedRequest)); + } - // If this is 1, we have an additional extended algorithm structure to add. - if negotiate_algorithms_req.num_alg_struct_tables > 0 { - // 3.3 - req_alg_struct + alg_struct .encode(req_buf) .map_err(|e| (false, CommandError::Codec(e)))?; - // 3.4 - if let Some(extended_algos) = alg_external { + // If ext_alg_count > 0, we must have the extended algorithm structures. + if alg_struct.ext_alg_count() > 0 { + let extended_algos = alg_external.ok_or((false, CommandError::UnsupportedRequest))?; for ext in extended_algos { ext.encode(req_buf) .map_err(|e| (false, CommandError::Codec(e)))?; } - } else { - // If ext_alg_count > 0, we must have the extended algorithm structure. - return Err((false, CommandError::UnsupportedRequest)); } } diff --git a/src/commands/algorithms/response.rs b/src/commands/algorithms/response.rs index ed9540b..adf0631 100644 --- a/src/commands/algorithms/response.rs +++ b/src/commands/algorithms/response.rs @@ -134,6 +134,13 @@ fn process_negotiate_algorithms_request<'a>( if fixed_alg_count != 2 { Err(ctx.generate_error_response(req_payload, ErrorCode::InvalidRequest, 0, None))?; } + + // Skip extended algorithm structures embedded in this AlgStructure + for _ in 0..ext_alg_count { + ExtendedAlgo::decode(req_payload).map_err(|_| { + ctx.generate_error_response(req_payload, ErrorCode::InvalidRequest, 0, None) + })?; + } } // Total number of extended algorithms check