Skip to content

Commit fadd05c

Browse files
leongrossembediver
authored andcommitted
fix NEGOTIATE_ALGORITHMS size
Signed-off-by: leongross <leon.gross@9elements.com>
1 parent bd7b43f commit fadd05c

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

examples/spdm_requester.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn full_flow(stream: TcpStream, config: &RequesterConfig) -> IoResult<()> {
323323
&mut message_buffer,
324324
Some(&ext_asym),
325325
Some(&ext_hash),
326-
alg_structure,
326+
Some(alg_structure),
327327
Some(&alg_external),
328328
)
329329
.unwrap();

src/commands/algorithms/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ impl NegotiateAlgorithmsReq {
150150
other_param_support: OtherParamSupport,
151151
base_asym_algo: BaseAsymAlgo,
152152
base_hash_algo: BaseHashAlgo,
153-
ext_asyn_count: u8,
153+
ext_asym_count: u8,
154154
ext_hash_count: u8,
155155
mel_specification: MelSpecification,
156+
alg_ext_count: u8,
156157
) -> SpdmResult<NegotiateAlgorithmsReq> {
157158
let mut req = NegotiateAlgorithmsReq {
158159
num_alg_struct_tables,
@@ -163,13 +164,14 @@ impl NegotiateAlgorithmsReq {
163164
base_asym_algo,
164165
base_hash_algo,
165166
reserved_1: [0u8; 12],
166-
ext_asym_count: ext_asyn_count,
167+
ext_asym_count,
167168
ext_hash_count,
168169
reserved_2: 0,
169170
mel_specification,
170171
};
171172

172-
req.length = req.min_req_len();
173+
req.length = req.min_req_len()
174+
+ (size_of::<ExtendedAlgo>() * alg_ext_count as usize) as u16;
173175

174176
if req.length > MAX_SPDM_REQUEST_LENGTH {
175177
return Err(SpdmError::InvalidParam);

src/commands/algorithms/request.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn generate_negotiate_algorithms_request<'a>(
169169
req_buf: &mut MessageBuf<'a>,
170170
ext_asym: Option<&'a [ExtendedAlgo]>,
171171
ext_hash: Option<&'a [ExtendedAlgo]>,
172-
req_alg_struct: AlgStructure,
172+
req_alg_struct: Option<AlgStructure>,
173173
alg_external: Option<&'a [ExtendedAlgo]>, // req_alg_struct.AlgCount.ExtAlgCount many
174174
) -> CommandResult<()> {
175175
let local_algorithms = &ctx.local_algorithms.device_algorithms;
@@ -184,9 +184,12 @@ pub fn generate_negotiate_algorithms_request<'a>(
184184
None => 0,
185185
};
186186

187+
let num_alg_struct_tables = req_alg_struct.is_some() as u8;
188+
let alg_ext_count = req_alg_struct.map_or(0, |s| s.ext_alg_count());
189+
187190
// Generate base structure **without** the variable length structures
188191
let negotiate_algorithms_req = NegotiateAlgorithmsReq::new(
189-
req_alg_struct.ext_alg_count(),
192+
num_alg_struct_tables,
190193
0, // param2
191194
local_algorithms.measurement_spec,
192195
local_algorithms.other_param_support,
@@ -195,6 +198,7 @@ pub fn generate_negotiate_algorithms_request<'a>(
195198
ext_asym_count,
196199
ext_hash_count,
197200
local_algorithms.mel_specification,
201+
alg_ext_count,
198202
)
199203
.map_err(|_| (false, CommandError::UnsupportedRequest))?;
200204

@@ -253,27 +257,23 @@ pub fn generate_negotiate_algorithms_request<'a>(
253257
}
254258
}
255259

256-
// 3.2
257-
if req_alg_struct.fixed_alg_count() != 0 && !req_alg_struct.is_multiple() {
258-
return Err((false, CommandError::UnsupportedRequest));
259-
}
260+
// 3.3 + 3.4: encode AlgStructure and its extended algorithms if present
261+
if let Some(alg_struct) = req_alg_struct {
262+
if alg_struct.fixed_alg_count() != 0 && !alg_struct.is_multiple() {
263+
return Err((false, CommandError::UnsupportedRequest));
264+
}
260265

261-
// If this is 1, we have an additional extended algorithm structure to add.
262-
if negotiate_algorithms_req.num_alg_struct_tables > 0 {
263-
// 3.3
264-
req_alg_struct
266+
alg_struct
265267
.encode(req_buf)
266268
.map_err(|e| (false, CommandError::Codec(e)))?;
267269

268-
// 3.4
269-
if let Some(extended_algos) = alg_external {
270+
// If ext_alg_count > 0, we must have the extended algorithm structures.
271+
if alg_struct.ext_alg_count() > 0 {
272+
let extended_algos = alg_external.ok_or((false, CommandError::UnsupportedRequest))?;
270273
for ext in extended_algos {
271274
ext.encode(req_buf)
272275
.map_err(|e| (false, CommandError::Codec(e)))?;
273276
}
274-
} else {
275-
// If ext_alg_count > 0, we must have the extended algorithm structure.
276-
return Err((false, CommandError::UnsupportedRequest));
277277
}
278278
}
279279

0 commit comments

Comments
 (0)