Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

remove Unknow(0) from SpdmVersion. #79

Merged
merged 1 commit into from
Sep 12, 2023
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
4 changes: 2 additions & 2 deletions spdmlib/src/common/key_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ impl SpdmKeySchedule {

let mut version = [0u8; 8];
version.copy_from_slice(SPDM_VERSION_VALUE);
version[SPDM_VERSION_VALUE_MAJOR_INDEX] = (spdm_version.get_u8() >> 4) + b'0';
version[SPDM_VERSION_VALUE_MINOR_INDEX] = (spdm_version.get_u8() & 0x0F) + b'0';
version[SPDM_VERSION_VALUE_MAJOR_INDEX] = (u8::from(spdm_version) >> 4) + b'0';
version[SPDM_VERSION_VALUE_MINOR_INDEX] = (u8::from(spdm_version) & 0x0F) + b'0';

let mut writer = Writer::init(buffer);
length.encode(&mut writer).ok()?;
Expand Down
6 changes: 2 additions & 4 deletions spdmlib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl SpdmContext {
return Err(SPDM_STATUS_CRYPTO_ERROR);
}

if spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if spdm_version_sel >= SpdmVersion::SpdmVersion12 {
crypto::hash::hash_ctx_update(
session.runtime_info.digest_context_l1l2.as_mut().unwrap(),
message_a.as_ref(),
Expand All @@ -443,9 +443,7 @@ impl SpdmContext {
return Err(SPDM_STATUS_CRYPTO_ERROR);
}

if self.negotiate_info.spdm_version_sel.get_u8()
>= SpdmVersion::SpdmVersion12.get_u8()
{
if self.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
crypto::hash::hash_ctx_update(
self.runtime_info.digest_context_l1l2.as_mut().unwrap(),
self.runtime_info.message_a.as_ref(),
Expand Down
4 changes: 2 additions & 2 deletions spdmlib/src/common/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl SpdmCodec for SecuredMessageGeneralOpaqueDataHeader {
bytes: &mut Writer,
) -> Result<usize, SpdmStatus> {
let mut cnt = 0usize;
if context.negotiate_info.spdm_version_sel.get_u8() < SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel < SpdmVersion::SpdmVersion12 {
cnt += DMTF_SPEC_ID
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;
Expand Down Expand Up @@ -302,7 +302,7 @@ impl SpdmCodec for SecuredMessageGeneralOpaqueDataHeader {
) -> Option<SecuredMessageGeneralOpaqueDataHeader> {
let total_elements: u8;

if context.negotiate_info.spdm_version_sel.get_u8() < SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel < SpdmVersion::SpdmVersion12 {
let spec_id = u32::read(r)?;
if spec_id != DMTF_SPEC_ID {
return None;
Expand Down
60 changes: 29 additions & 31 deletions spdmlib/src/message/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
) -> Result<usize, SpdmStatus> {
let mut cnt = 0usize;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
cnt += self
.alg_struct_count
.encode(bytes)
Expand All @@ -41,7 +41,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
cnt += 0u8.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // param2

let mut length: u16 = 32;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let alg_fixed_count = 2u8;
length += ((2 + alg_fixed_count) * self.alg_struct_count) as u16;
}
Expand All @@ -52,7 +52,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.other_params_support
.encode(bytes)
Expand All @@ -79,7 +79,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {

cnt += 0u16.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // reserved3

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
for algo in self.alg_struct.iter().take(self.alg_struct_count as usize) {
cnt += algo.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?;
}
Expand All @@ -92,7 +92,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
r: &mut Reader,
) -> Option<SpdmNegotiateAlgorithmsRequestPayload> {
let mut alg_struct_count = 0;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
alg_struct_count = u8::read(r)?; // param1
if alg_struct_count > 4 {
return None;
Expand All @@ -105,14 +105,13 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
let length = u16::read(r)?;
let measurement_specification = SpdmMeasurementSpecification::read(r)?;

let other_params_support = if context.negotiate_info.spdm_version_sel.get_u8()
>= SpdmVersion::SpdmVersion12.get_u8()
{
SpdmOpaqueSupport::read(r)?
} else {
u8::read(r)?;
SpdmOpaqueSupport::default()
};
let other_params_support =
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
SpdmOpaqueSupport::read(r)?
} else {
u8::read(r)?;
SpdmOpaqueSupport::default()
};

let base_asym_algo = SpdmBaseAsymAlgo::read(r)?;
let base_hash_algo = SpdmBaseHashAlgo::read(r)?;
Expand All @@ -134,7 +133,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
u16::read(r)?; // reserved3

let mut alg_struct = gen_array_clone(SpdmAlgStruct::default(), 4);
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let mut dhe_present = false;
let mut aead_present = false;
let mut req_asym_present = false;
Expand Down Expand Up @@ -183,7 +182,7 @@ impl SpdmCodec for SpdmNegotiateAlgorithmsRequestPayload {
// check length
//
let mut calc_length: u16 = 32;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let alg_fixed_count = 2u8;
calc_length += ((2 + alg_fixed_count) * alg_struct_count) as u16;
}
Expand Down Expand Up @@ -222,7 +221,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
) -> Result<usize, SpdmStatus> {
let mut cnt = 0usize;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
cnt += self
.alg_struct_count
.encode(bytes)
Expand All @@ -234,7 +233,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
cnt += 0u8.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // param2

let mut length: u16 = 36;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let alg_fixed_count = 2u8;
length += ((2 + alg_fixed_count) * self.alg_struct_count) as u16;
}
Expand All @@ -245,7 +244,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.other_params_selection
.encode(bytes)
Expand Down Expand Up @@ -276,7 +275,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {

cnt += 0u16.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // reserved3

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
for algo in self.alg_struct.iter().take(self.alg_struct_count as usize) {
cnt += algo.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?;
}
Expand All @@ -289,7 +288,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
r: &mut Reader,
) -> Option<SpdmAlgorithmsResponsePayload> {
let mut alg_struct_count = 0;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
alg_struct_count = u8::read(r)?; // param1
if alg_struct_count > 4 {
return None;
Expand Down Expand Up @@ -318,18 +317,17 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
return None;
}

let other_params_selection = if context.negotiate_info.spdm_version_sel.get_u8()
>= SpdmVersion::SpdmVersion12.get_u8()
{
SpdmOpaqueSupport::read(r)?
} else {
u8::read(r)?;
SpdmOpaqueSupport::default()
};
let other_params_selection =
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
SpdmOpaqueSupport::read(r)?
} else {
u8::read(r)?;
SpdmOpaqueSupport::default()
};
if !other_params_selection.is_no_more_than_one_selected() {
return None;
}
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8()
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12
&& (context
.negotiate_info
.rsp_capabilities_sel
Expand Down Expand Up @@ -451,7 +449,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
u16::read(r)?; // reserved3

let mut alg_struct = gen_array_clone(SpdmAlgStruct::default(), 4);
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let mut dhe_present = false;
let mut aead_present = false;
let mut req_asym_present = false;
Expand Down Expand Up @@ -581,7 +579,7 @@ impl SpdmCodec for SpdmAlgorithmsResponsePayload {
}

let mut calc_length: u16 = 36;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
let alg_fixed_count = 2u8;
calc_length += ((2 + alg_fixed_count) * alg_struct_count) as u16;
}
Expand Down
21 changes: 10 additions & 11 deletions spdmlib/src/message/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl SpdmCodec for SpdmGetCapabilitiesRequestPayload {
cnt += 0u8.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // param1
cnt += 0u8.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // param2

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
cnt += 0u8.encode(bytes).map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // reserved
cnt += self
.ct_exponent
Expand All @@ -40,7 +40,7 @@ impl SpdmCodec for SpdmGetCapabilitiesRequestPayload {
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;
}

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.data_transfer_size
.encode(bytes)
Expand All @@ -62,7 +62,7 @@ impl SpdmCodec for SpdmGetCapabilitiesRequestPayload {

let mut ct_exponent = 0;
let mut flags = SpdmRequestCapabilityFlags::default();
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion11 {
u8::read(r)?; // reserved
ct_exponent = u8::read(r)?;
u16::read(r)?; // reserved2
Expand Down Expand Up @@ -111,8 +111,7 @@ impl SpdmCodec for SpdmGetCapabilitiesRequestPayload {
return None;
}

if context.negotiate_info.spdm_version_sel.get_u8()
== SpdmVersion::SpdmVersion11.get_u8()
if context.negotiate_info.spdm_version_sel == SpdmVersion::SpdmVersion11
&& flags.contains(SpdmRequestCapabilityFlags::MUT_AUTH_CAP)
&& !flags.contains(SpdmRequestCapabilityFlags::ENCAP_CAP)
{
Expand All @@ -122,7 +121,7 @@ impl SpdmCodec for SpdmGetCapabilitiesRequestPayload {

let mut data_transfer_size = 0;
let mut max_spdm_msg_size = 0;
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
data_transfer_size = u32::read(r)?;
max_spdm_msg_size = u32::read(r)?;
if data_transfer_size < 42 || max_spdm_msg_size < data_transfer_size {
Expand Down Expand Up @@ -171,7 +170,7 @@ impl SpdmCodec for SpdmCapabilitiesResponsePayload {
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.data_transfer_size
.encode(bytes)
Expand Down Expand Up @@ -209,7 +208,7 @@ impl SpdmCodec for SpdmCapabilitiesResponsePayload {
{
return None;
}
if context.negotiate_info.spdm_version_sel.get_u8() < SpdmVersion::SpdmVersion11.get_u8() {
if context.negotiate_info.spdm_version_sel < SpdmVersion::SpdmVersion11 {
if !flags.contains(SpdmResponseCapabilityFlags::MEAS_CAP_SIG) {
if flags.contains(SpdmResponseCapabilityFlags::CERT_CAP)
!= flags.contains(SpdmResponseCapabilityFlags::CHAL_CAP)
Expand Down Expand Up @@ -269,13 +268,13 @@ impl SpdmCodec for SpdmCapabilitiesResponsePayload {
return None;
}
}
if context.negotiate_info.spdm_version_sel.get_u8() == SpdmVersion::SpdmVersion11.get_u8()
if context.negotiate_info.spdm_version_sel == SpdmVersion::SpdmVersion11
&& flags.contains(SpdmResponseCapabilityFlags::MUT_AUTH_CAP)
&& !flags.contains(SpdmResponseCapabilityFlags::ENCAP_CAP)
{
return None;
}
if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
if !flags.contains(SpdmResponseCapabilityFlags::CERT_CAP)
&& (flags.contains(SpdmResponseCapabilityFlags::ALIAS_CERT_CAP)
|| flags.contains(SpdmResponseCapabilityFlags::SET_CERT_CAP))
Expand All @@ -295,7 +294,7 @@ impl SpdmCodec for SpdmCapabilitiesResponsePayload {
}
}

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
let data_transfer_size = u32::read(r)?;
let max_spdm_msg_size = u32::read(r)?;
if data_transfer_size < 42 || max_spdm_msg_size < data_transfer_size {
Expand Down
4 changes: 2 additions & 2 deletions spdmlib/src/message/encapsulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl SpdmCodec for SpdmEncapsulatedResponseAckPayload {
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?; // param2

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.ack_request_id
.encode(bytes)
Expand All @@ -142,7 +142,7 @@ impl SpdmCodec for SpdmEncapsulatedResponseAckPayload {
let payload_type = SpdmEncapsulatedResponseAckPayloadType::read(r)?; // param2
let mut ack_request_id = 0;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
ack_request_id = u8::read(r)?;
let _ = u24::read(r)?; // reserved
}
Expand Down
2 changes: 1 addition & 1 deletion spdmlib/src/message/key_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl SpdmCodec for SpdmKeyExchangeRequestPayload {
.encode(bytes)
.map_err(|_| SPDM_STATUS_BUFFER_FULL)?;

if context.negotiate_info.spdm_version_sel.get_u8() >= SpdmVersion::SpdmVersion12.get_u8() {
if context.negotiate_info.spdm_version_sel >= SpdmVersion::SpdmVersion12 {
cnt += self
.session_policy
.encode(bytes)
Expand Down
8 changes: 4 additions & 4 deletions spdmlib/src/message/key_update_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn test_key_update_struct() {

// 1. Validate KeyUpdate request length is 4.
let u8_slice = &mut [
SpdmVersion::SpdmVersion11.get_u8(),
u8::from(SpdmVersion::SpdmVersion11),
SpdmRequestResponseCode::SpdmRequestKeyUpdate.get_u8(),
SpdmKeyUpdateOperation::SpdmUpdateSingleKey.get_u8(),
0u8,
Expand All @@ -30,7 +30,7 @@ fn test_key_update_struct() {

// 2. Validate KEY_UPDATE_ACK response length is 4.
let u8_slice = &mut [
SpdmVersion::SpdmVersion11.get_u8(),
u8::from(SpdmVersion::SpdmVersion11),
SpdmRequestResponseCode::SpdmResponseKeyUpdateAck.get_u8(),
SpdmKeyUpdateOperation::SpdmUpdateSingleKey.get_u8(),
0u8,
Expand All @@ -42,7 +42,7 @@ fn test_key_update_struct() {

// 3. Validate KEY_UPDATE operations equal to reserved value. Expactation, fail.
let u8_slice = &mut [
SpdmVersion::SpdmVersion11.get_u8(),
u8::from(SpdmVersion::SpdmVersion11),
SpdmRequestResponseCode::SpdmRequestKeyUpdate.get_u8(),
SpdmKeyUpdateOperation::SpdmUpdateSingleKey.get_u8(),
0u8,
Expand All @@ -54,7 +54,7 @@ fn test_key_update_struct() {

// 4. Validate KEY_UPDATE_ACK KEY_UPDATE operations equal to reserved value. Expectation, fail
let u8_slice = &mut [
SpdmVersion::SpdmVersion11.get_u8(),
u8::from(SpdmVersion::SpdmVersion11),
SpdmRequestResponseCode::SpdmResponseKeyUpdateAck.get_u8(),
SpdmKeyUpdateOperation::SpdmUpdateSingleKey.get_u8(),
0u8,
Expand Down
Loading
Loading