Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/spdm_requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions src/commands/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NegotiateAlgorithmsReq> {
let mut req = NegotiateAlgorithmsReq {
num_alg_struct_tables,
Expand All @@ -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::<ExtendedAlgo>() * alg_ext_count as usize) as u16;

if req.length > MAX_SPDM_REQUEST_LENGTH {
return Err(SpdmError::InvalidParam);
Expand All @@ -188,8 +190,7 @@ impl NegotiateAlgorithmsReq {
+ size_of::<NegotiateAlgorithmsReq>()
+ 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.
Expand Down
30 changes: 15 additions & 15 deletions src/commands/algorithms/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlgStructure>,
alg_external: Option<&'a [ExtendedAlgo]>, // req_alg_struct.AlgCount.ExtAlgCount many
) -> CommandResult<()> {
let local_algorithms = &ctx.local_algorithms.device_algorithms;
Expand All @@ -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,
Expand All @@ -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))?;

Expand Down Expand Up @@ -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));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/commands/algorithms/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading