Skip to content

Commit 9a9355e

Browse files
authored
Merge pull request #403 from Superhepper/tpm-format-one-rc-display-impl
Cleans up TPM format one error messages.
2 parents bfcca27 + 3bd9121 commit 9a9355e

File tree

5 files changed

+321
-37
lines changed

5 files changed

+321
-37
lines changed

tss-esapi/src/error/return_code/tpm/format_one.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,39 @@ impl std::error::Error for TpmFormatOneResponseCode {}
5959
impl std::fmt::Display for TpmFormatOneResponseCode {
6060
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6161
match self.error_number {
62-
TpmFormatOneError::Asymmetric => write!(f, "asymmetric algorithm not supported or not correct ({})", self.argument_number()),
63-
TpmFormatOneError::Attributes => write!(f, "inconsistent attributes ({})", self.argument_number()),
64-
TpmFormatOneError::Hash => write!(f, "hash algorithm not supported or not appropriate ({})", self.argument_number()),
65-
TpmFormatOneError::Value => write!(f, "value is out of range or is not correct for the context ({})", self.argument_number()),
66-
TpmFormatOneError::Hierarchy => write!(f, "hierarchy is not enabled or is not correct for the use ({})", self.argument_number()),
67-
TpmFormatOneError::KeySize => write!(f, "key size is not supported ({})", self.argument_number()),
68-
TpmFormatOneError::Mgf => write!(f, "mask generation function not supported ({})", self.argument_number()),
69-
TpmFormatOneError::Mode => write!(f, "mode of operation not supported ({})", self.argument_number()),
70-
TpmFormatOneError::Type => write!(f, "the type of the value is not appropriate for the use ({})", self.argument_number()),
71-
TpmFormatOneError::Handle => write!(f, "the handle is not correct for the use ({})", self.argument_number()),
72-
TpmFormatOneError::Kdf => write!(f, "unsupported key derivation function or function not appropriate for use ({})", self.argument_number()),
73-
TpmFormatOneError::Range => write!(f, "value was out of allowed range ({})", self.argument_number()),
74-
TpmFormatOneError::AuthFail => write!(f, "the authorization HMAC check failed and DA counter incremented ({})", self.argument_number()),
75-
TpmFormatOneError::Nonce => write!(f, "invalid nonce size or nonce value mismatch ({})", self.argument_number()),
76-
TpmFormatOneError::Pp => write!(f, "authorization requires assertion of PP ({})", self.argument_number()),
77-
TpmFormatOneError::Scheme => write!(f, "unsupported or incompatible scheme ({})", self.argument_number()),
78-
TpmFormatOneError::Size => write!(f, "structure is the wrong size ({})", self.argument_number()),
79-
TpmFormatOneError::Symmetric => write!(f, "unsupported symmetric algorithm or key size, or not appropriate for instance ({})", self.argument_number()),
80-
TpmFormatOneError::Tag => write!(f, "incorrect structure tag ({})", self.argument_number()),
81-
TpmFormatOneError::Selector => write!(f, "union selector is incorrect ({})", self.argument_number()),
82-
TpmFormatOneError::Insufficient => write!(f, "the TPM was unable to unmarshal a value because there were not enough octets in the input buffer ({})", self.argument_number()),
83-
TpmFormatOneError::Signature => write!(f, "the signature is not valid ({})", self.argument_number()),
84-
TpmFormatOneError::Key => write!(f, "key fields are not compatible with the selected use ({})", self.argument_number()),
85-
TpmFormatOneError::PolicyFail => write!(f, "a policy check failed ({})", self.argument_number()),
86-
TpmFormatOneError::Integrity => write!(f, "integrity check failed ({})", self.argument_number()),
87-
TpmFormatOneError::Ticket => write!(f, "invalid ticket ({})", self.argument_number()),
88-
TpmFormatOneError::ReservedBits => write!(f, "reserved bits not set to zero as required ({})", self.argument_number()),
89-
TpmFormatOneError::BadAuth => write!(f, "authorization failure without DA implications ({})", self.argument_number()),
90-
TpmFormatOneError::Expired => write!(f, "the policy has expired ({})", self.argument_number()),
91-
TpmFormatOneError::PolicyCc => write!(f, "the command Code in the policy is not the command Code of the command or the command code in a policy command references a command that is not implemented ({})", self.argument_number()),
92-
TpmFormatOneError::Binding => write!(f, "public and sensitive portions of an object are not cryptographically bound ({})", self.argument_number()),
93-
TpmFormatOneError::Curve => write!(f, "curve not supported ({})", self.argument_number()),
94-
TpmFormatOneError::EccPoint => write!(f, "point is not on the required curve ({})", self.argument_number()),
62+
TpmFormatOneError::Asymmetric => write!(f, "Asymmetric algorithm not supported or not correct ({}).", self.argument_number()),
63+
TpmFormatOneError::Attributes => write!(f, "Inconsistent attributes ({}).", self.argument_number()),
64+
TpmFormatOneError::Hash => write!(f, "Hash algorithm not supported or not appropriate ({}).", self.argument_number()),
65+
TpmFormatOneError::Value => write!(f, "Value is out of range or is not correct for the context ({}).", self.argument_number()),
66+
TpmFormatOneError::Hierarchy => write!(f, "Hierarchy is not enabled or is not correct for the use ({}).", self.argument_number()),
67+
TpmFormatOneError::KeySize => write!(f, "Key size is not supported ({}).", self.argument_number()),
68+
TpmFormatOneError::Mgf => write!(f, "Mask generation function not supported ({}).", self.argument_number()),
69+
TpmFormatOneError::Mode => write!(f, "Mode of operation not supported ({}).", self.argument_number()),
70+
TpmFormatOneError::Type => write!(f, "The type of the value is not appropriate for the use ({}).", self.argument_number()),
71+
TpmFormatOneError::Handle => write!(f, "The handle is not correct for the use ({}).", self.argument_number()),
72+
TpmFormatOneError::Kdf => write!(f, "Unsupported key derivation function or function not appropriate for use ({}).", self.argument_number()),
73+
TpmFormatOneError::Range => write!(f, "Value was out of allowed range ({}).", self.argument_number()),
74+
TpmFormatOneError::AuthFail => write!(f, "The authorization HMAC check failed and DA counter incremented ({}).", self.argument_number()),
75+
TpmFormatOneError::Nonce => write!(f, "Invalid nonce size or nonce value mismatch ({}).", self.argument_number()),
76+
TpmFormatOneError::Pp => write!(f, "Authorization requires assertion of PP ({}).", self.argument_number()),
77+
TpmFormatOneError::Scheme => write!(f, "Unsupported or incompatible scheme ({}).", self.argument_number()),
78+
TpmFormatOneError::Size => write!(f, "Structure is the wrong size ({}).", self.argument_number()),
79+
TpmFormatOneError::Symmetric => write!(f, "Unsupported symmetric algorithm or key size, or not appropriate for instance ({}).", self.argument_number()),
80+
TpmFormatOneError::Tag => write!(f, "Incorrect structure tag ({}).", self.argument_number()),
81+
TpmFormatOneError::Selector => write!(f, "Union selector is incorrect ({}).", self.argument_number()),
82+
TpmFormatOneError::Insufficient => write!(f, "The TPM was unable to unmarshal a value because there were not enough octets in the input buffer ({}).", self.argument_number()),
83+
TpmFormatOneError::Signature => write!(f, "The signature is not valid ({}).", self.argument_number()),
84+
TpmFormatOneError::Key => write!(f, "Key fields are not compatible with the selected use ({}).", self.argument_number()),
85+
TpmFormatOneError::PolicyFail => write!(f, "A policy check failed ({}).", self.argument_number()),
86+
TpmFormatOneError::Integrity => write!(f, "Integrity check failed ({}).", self.argument_number()),
87+
TpmFormatOneError::Ticket => write!(f, "Invalid ticket ({}).", self.argument_number()),
88+
TpmFormatOneError::ReservedBits => write!(f, "Reserved bits not set to zero as required ({}).", self.argument_number()),
89+
TpmFormatOneError::BadAuth => write!(f, "Authorization failure without DA implications ({}).", self.argument_number()),
90+
TpmFormatOneError::Expired => write!(f, "The policy has expired ({}).", self.argument_number()),
91+
TpmFormatOneError::PolicyCc => write!(f, "The `commandCode` in the policy is not the `commandCode` of the command or the command code in a policy command references a command that is not implemented ({}).", self.argument_number()),
92+
TpmFormatOneError::Binding => write!(f, "Public and sensitive portions of an object are not cryptographically bound ({}).", self.argument_number()),
93+
TpmFormatOneError::Curve => write!(f, "Curve not supported ({}).", self.argument_number()),
94+
TpmFormatOneError::EccPoint => write!(f, "Point is not on the required curve ({}).", self.argument_number()),
9595
}
9696
}
9797
}

tss-esapi/src/error/return_code/tpm/format_one/argument_number.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@ impl From<ArgumentNumber> for u8 {
4747
}
4848
}
4949

50-
#[allow(clippy::uninlined_format_args)]
5150
impl std::fmt::Display for ArgumentNumber {
5251
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5352
match self {
5453
ArgumentNumber::Parameter(number) => {
55-
write!(f, "associated with TPM command parameter number {}", number)
54+
write!(f, "associated with parameter number {}", number)
5655
}
5756
ArgumentNumber::Session(number) => {
58-
write!(f, "associated with TPM session number {}", number)
57+
write!(f, "associated with session number {}", number)
5958
}
6059
ArgumentNumber::Handle(number) => {
61-
write!(f, "associated with TPM handle number {}", number)
60+
write!(f, "associated with handle number {}", number)
6261
}
6362
}
6463
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2022 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
mod tpm_format_one_argument_number_tests;
45
mod tpm_format_one_error_tests;
56
mod tpm_format_zero_error_tests;
67
mod tpm_format_zero_warning_tests;
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::convert::TryFrom;
5+
use tss_esapi::{
6+
constants::tss::{
7+
TPM2_RC_1, TPM2_RC_2, TPM2_RC_3, TPM2_RC_4, TPM2_RC_5, TPM2_RC_6, TPM2_RC_7, TPM2_RC_8,
8+
TPM2_RC_9, TPM2_RC_A, TPM2_RC_B, TPM2_RC_C, TPM2_RC_D, TPM2_RC_E, TPM2_RC_F, TPM2_RC_H,
9+
TPM2_RC_P, TPM2_RC_S,
10+
},
11+
error::ArgumentNumber,
12+
};
13+
14+
const ARGUMENT_NUMBERS: [u16; 15] = [
15+
TPM2_RC_1 as u16,
16+
TPM2_RC_2 as u16,
17+
TPM2_RC_3 as u16,
18+
TPM2_RC_4 as u16,
19+
TPM2_RC_5 as u16,
20+
TPM2_RC_6 as u16,
21+
TPM2_RC_7 as u16,
22+
TPM2_RC_8 as u16,
23+
TPM2_RC_9 as u16,
24+
TPM2_RC_A as u16,
25+
TPM2_RC_B as u16,
26+
TPM2_RC_C as u16,
27+
TPM2_RC_D as u16,
28+
TPM2_RC_E as u16,
29+
TPM2_RC_F as u16,
30+
];
31+
32+
#[test]
33+
fn test_valid_parameter_conversions() {
34+
let parameter_values = (1u8..16u8).collect::<Vec<u8>>();
35+
36+
// Test Parameters
37+
ARGUMENT_NUMBERS.iter().zip(parameter_values).for_each(|(&nr, val)| {
38+
// Ex:
39+
// TPM2_RC_1 || TPM_RC_P = 0x140
40+
// 0 0 0 1 0 1 |0 0 0 0 0 0
41+
// P |---Error nr---|
42+
// Only bits 6 to 11 is parsed by ArgumentNumber.
43+
// P bit is set, indicating parameter argument.
44+
let return_code_argument_info: u16 = nr | (TPM2_RC_P as u16);
45+
let actual = u8::try_from(
46+
return_code_argument_info
47+
.checked_shr(6).expect("Failed to extract argument information.")
48+
)
49+
.map (ArgumentNumber::from)
50+
.unwrap_or_else(|_| panic!("Failed to convert argument information {:#b} to u8.", return_code_argument_info));
51+
let expected = ArgumentNumber::Parameter(val);
52+
assert_eq!(
53+
expected,
54+
actual,
55+
"Performing `from` conversion return code {:#02X} did not produce the expected {} with value {}",
56+
return_code_argument_info,
57+
std::stringify!(ArgumentNumber::Parameter),
58+
val
59+
);
60+
})
61+
}
62+
63+
#[test]
64+
fn test_valid_session_conversions() {
65+
let session_values = (1u8..7u8).collect::<Vec<u8>>();
66+
67+
ARGUMENT_NUMBERS[0..7].iter().zip(session_values).for_each(|(&nr, val)| {
68+
// Ex:
69+
// TPM2_RC_1 || TPM_RC_S = 0x900
70+
// 1 0 0 1 0 0 |0 0 0 0 0 0
71+
// S P |---Error nr---|
72+
// Only bits 6 to 11 is parsed by ArgumentNumber.
73+
// Session bit is set and P bit is clear, indicating session argument.
74+
let return_code_argument_info: u16 = nr | (TPM2_RC_S as u16);
75+
let actual = u8::try_from(
76+
return_code_argument_info
77+
.checked_shr(6)
78+
.expect("Failed to extract argument information."),
79+
)
80+
.map(ArgumentNumber::from)
81+
.unwrap_or_else(|_| {
82+
panic!(
83+
"Failed to convert argument information {:#b} to u8.",
84+
return_code_argument_info
85+
)
86+
});
87+
let expected = ArgumentNumber::Session(val);
88+
assert_eq!(
89+
expected,
90+
actual,
91+
"Performing `from` conversion return code {:#02X} did not produce the expected {} with value {}",
92+
return_code_argument_info,
93+
std::stringify!(ArgumentNumber::Session),
94+
val
95+
);
96+
})
97+
}
98+
99+
#[test]
100+
fn test_valid_handle_conversions() {
101+
let handle_values = (1u8..7u8).collect::<Vec<u8>>();
102+
103+
ARGUMENT_NUMBERS[0..7].iter().zip(handle_values).for_each(|(&nr, val)| {
104+
// Ex:
105+
// TPM2_RC_1 || TPM_RC_H = 0x800
106+
// 0 0 0 1 0 0 |0 0 0 0 0 0
107+
// S P |---Error nr---|
108+
// Only bits 6 to 11 is parsed by ArgumentNumber.
109+
// Session bit is clear and P bit is clear, indicating handle argument.
110+
let return_code_argument_info: u16 = nr | (TPM2_RC_H as u16);
111+
let actual = u8::try_from(
112+
return_code_argument_info
113+
.checked_shr(6)
114+
.expect("Failed to extract argument information."),
115+
)
116+
.map(ArgumentNumber::from)
117+
.unwrap_or_else(|_| {
118+
panic!(
119+
"Failed to convert argument information {:#b} to u8.",
120+
return_code_argument_info
121+
)
122+
});
123+
let expected = ArgumentNumber::Handle(val);
124+
assert_eq!(
125+
expected,
126+
actual,
127+
"Performing `from` conversion return code {:#02X} did not produce the expected {} with value {}",
128+
return_code_argument_info, std::stringify!(ArgumentNumber::Handle),
129+
val
130+
);
131+
})
132+
}
133+
134+
#[test]
135+
fn test_display_trait_implementation_for_parameters() {
136+
for val in 1u8..16u8 {
137+
assert_eq!(
138+
format!("associated with parameter number {}", val),
139+
format!("{}", ArgumentNumber::Parameter(val)),
140+
"{} with value {} did not produce the expected error message",
141+
std::stringify!(ArgumentNumber::Parameter),
142+
val,
143+
);
144+
}
145+
}
146+
147+
#[test]
148+
fn test_display_trait_implementation_for_sessions() {
149+
for val in 1u8..7u8 {
150+
assert_eq!(
151+
format!("associated with session number {}", val),
152+
format!("{}", ArgumentNumber::Session(val)),
153+
"{} with value {} did not produce the expected error message",
154+
std::stringify!(ArgumentNumber::Session),
155+
val,
156+
);
157+
}
158+
}
159+
160+
#[test]
161+
fn test_display_trait_implementation_for_handles() {
162+
for val in 1u8..7u8 {
163+
assert_eq!(
164+
format!("associated with handle number {}", val),
165+
format!("{}", ArgumentNumber::Handle(val)),
166+
"{} with value {} did not produce the expected error message",
167+
std::stringify!(ArgumentNumber::Handle),
168+
val,
169+
);
170+
}
171+
}

0 commit comments

Comments
 (0)