Skip to content

Commit 9d755a3

Browse files
Make errors more consistent (#3467)
1 parent ebdaf86 commit 9d755a3

File tree

106 files changed

+612
-617
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+612
-617
lines changed

experimental/offline_attestation/client/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl RequestHelper {
129129
let response_public_key_handle = self
130130
.private_key_handle
131131
.public()
132-
.map_err(|error| anyhow!("Couldn't get public key: {}", error))?;
132+
.map_err(|error| anyhow!("couldn't get public key: {}", error))?;
133133
let response_public_key = serialize_public_key(&response_public_key_handle)?;
134134

135135
Ok(EncryptedRequest {

experimental/offline_attestation/server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async fn main() -> anyhow::Result<()> {
5656
let private_key_handle = Arc::new(generate_private_key()?);
5757
let public_key_handle = private_key_handle
5858
.public()
59-
.map_err(|error| anyhow!("Couldn't get public key: {}", error))?;
59+
.map_err(|error| anyhow!("couldn't get public key: {}", error))?;
6060

6161
let attestation_report = generate_attestation_report(&public_key_handle)?;
6262

experimental/offline_attestation/shared/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ impl PublicKeyInfo {
110110
/// public key.
111111
pub fn encrypt(public_key_handle: &Handle, data: &[u8]) -> anyhow::Result<Vec<u8>> {
112112
let encryptor = tink_hybrid::new_encrypt(public_key_handle)
113-
.map_err(|error| anyhow!("Couldn't create hybrid encryptor: {}", error))?;
113+
.map_err(|error| anyhow!("couldn't create hybrid encryptor: {}", error))?;
114114
encryptor
115115
.encrypt(data, ENCRYPTION_CONTEXT)
116-
.map_err(|error| anyhow!("Couldn't encrypt data: {}", error))
116+
.map_err(|error| anyhow!("couldn't encrypt data: {}", error))
117117
}
118118

119119
/// Decrypts the provides `cyphertext` using the private key.
@@ -122,17 +122,17 @@ pub fn encrypt(public_key_handle: &Handle, data: &[u8]) -> anyhow::Result<Vec<u8
122122
/// decryption will only succeed if the ciphertext was created using the corresponding public key.
123123
pub fn decrypt(private_key_handle: &Handle, ciphertext: &[u8]) -> anyhow::Result<Vec<u8>> {
124124
let decryptor = tink_hybrid::new_decrypt(private_key_handle)
125-
.map_err(|error| anyhow!("Couldn't create hybrid decryptor: {}", error))?;
125+
.map_err(|error| anyhow!("couldn't create hybrid decryptor: {}", error))?;
126126
decryptor
127127
.decrypt(ciphertext, ENCRYPTION_CONTEXT)
128-
.map_err(|error| anyhow!("Couldn't decrypt ciphertext: {}", error))
128+
.map_err(|error| anyhow!("couldn't decrypt ciphertext: {}", error))
129129
}
130130

131131
/// Generates a new private key suitable for hybrid encryption and returns a handle to the
132132
/// containing keyset.
133133
pub fn generate_private_key() -> anyhow::Result<Handle> {
134134
tink_core::keyset::Handle::new(&tink_hybrid::ecies_hkdf_aes128_gcm_key_template())
135-
.map_err(|error| anyhow!("Couldn't create private key: {}", error))
135+
.map_err(|error| anyhow!("couldn't create private key: {}", error))
136136
}
137137

138138
/// Serialises the handle's underlying keyset containing the public key to a binary representation.
@@ -143,7 +143,7 @@ pub fn serialize_public_key(public_key_handle: &Handle) -> anyhow::Result<Vec<u8
143143
let mut writer = tink_core::keyset::BinaryWriter::new(&mut result);
144144
public_key_handle
145145
.write_with_no_secrets(&mut writer)
146-
.map_err(|error| anyhow!("Couldn't deserialise public key: {}", error))?;
146+
.map_err(|error| anyhow!("couldn't deserialise public key: {}", error))?;
147147
Ok(result)
148148
}
149149

@@ -152,7 +152,7 @@ pub fn serialize_public_key(public_key_handle: &Handle) -> anyhow::Result<Vec<u8
152152
pub fn deserialize_public_key(data: &[u8]) -> anyhow::Result<Handle> {
153153
let mut reader = tink_core::keyset::BinaryReader::new(data);
154154
Handle::read_with_no_secrets(&mut reader)
155-
.map_err(|error| anyhow!("Couldn't deserialise public key: {}", error))
155+
.map_err(|error| anyhow!("couldn't deserialise public key: {}", error))
156156
}
157157

158158
/// Serialises the attestation report to a binary representation.

experimental/sev_guest/src/cpuid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ impl CpuidPage {
9494
/// all zero.
9595
pub fn validate(&self) -> Result<(), &'static str> {
9696
if self.count as usize > CPUID_COUNT_MAX {
97-
return Err("Invalid count");
97+
return Err("invalid count");
9898
}
9999
if self._reserved.iter().any(|&value| value != 0) {
100-
return Err("Nonzero value in _reserved");
100+
return Err("nonzero value in _reserved");
101101
}
102102
Ok(())
103103
}

experimental/sev_guest/src/crypto.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl GuestMessageEncryptor {
6363
initial_sequence_number: u64,
6464
) -> Result<Self, &'static str> {
6565
Ok(Self {
66-
cipher: Aes256Gcm::new_from_slice(key).map_err(|_| "Invalid key length")?,
66+
cipher: Aes256Gcm::new_from_slice(key).map_err(|_| "invalid key length")?,
6767
sequence_number: initial_sequence_number,
6868
})
6969
}
@@ -94,7 +94,7 @@ impl GuestMessageEncryptor {
9494
let auth_tag = self
9595
.cipher
9696
.encrypt_in_place_detached(nonce, associated_data, buffer)
97-
.map_err(|_| "Message encryption failed")?;
97+
.map_err(|aes_gcm::Error| "message encryption failed")?;
9898
// Only write the payload once we are sure the encryption succeeded.
9999
destination.payload[0..message_size].copy_from_slice(buffer);
100100
destination.header.auth_tag[0..auth_tag.len()].copy_from_slice(auth_tag.as_slice());
@@ -113,19 +113,19 @@ impl GuestMessageEncryptor {
113113
let mut result = M::new_zeroed();
114114
source.validate()?;
115115
if M::get_message_type() as u8 != source.header.auth_header.message_type {
116-
return Err("Invalid message type");
116+
return Err("invalid message type");
117117
}
118118
let sequence_number = source.header.sequence_number;
119119
if sequence_number != self.sequence_number + 1 {
120-
return Err("Unexpected sequence numer");
120+
return Err("unexpected sequence numer");
121121
}
122122
let mut iv_bytes = [0u8; IV_SIZE];
123123
iv_bytes[0..size_of::<u64>()].copy_from_slice(sequence_number.as_bytes());
124124
let nonce = Nonce::from_slice(&iv_bytes[..]);
125125
let associated_data = source.header.auth_header.as_bytes();
126126
let buffer = result.as_bytes_mut();
127127
if buffer.len() != source.header.auth_header.message_size as usize {
128-
return Err("Invalid message length");
128+
return Err("invalid message length");
129129
}
130130
// The source message is in memory that is shared with the hypervisor, so we must not
131131
// decrypt the payload in place. Copy the encrypted payload into the buffer and
@@ -134,7 +134,7 @@ impl GuestMessageEncryptor {
134134
let tag = Tag::from_slice(&source.header.auth_tag[0..size_of::<Tag>()]);
135135
self.cipher
136136
.decrypt_in_place_detached(nonce, associated_data, buffer, tag)
137-
.map_err(|_| "Couldn't decrypt message")?;
137+
.map_err(|aes_gcm::Error| "couldn't decrypt message")?;
138138
self.sequence_number += 1;
139139
Ok(result)
140140
}

experimental/sev_guest/src/ghcb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ where
283283
let virtual_address = VirtAddr::from_ptr(ghcb.as_ref() as *const Ghcb);
284284
// Crashing is OK if we cannot find the physical address for the GHCB.
285285
let gpa = translate(virtual_address)
286-
.expect("Could not translate the GHCB virtual address to a physical address.");
286+
.expect("couldn't translate the GHCB virtual address to a physical address");
287287
Self { ghcb, gpa }
288288
}
289289

@@ -429,7 +429,7 @@ where
429429
Ok(())
430430
} else {
431431
// For now we treat all non-zero return values as unrecoverable errors.
432-
Err("Guest message response indicates an error.")
432+
Err("guest message response indicates an error")
433433
}
434434
}
435435

@@ -451,7 +451,7 @@ where
451451
Ok(())
452452
} else {
453453
// For now we treat all non-zero return values as unrecoverable errors.
454-
Err("VMGEXIT call returned an error.")
454+
Err("VMGEXIT call returned an error")
455455
}
456456
}
457457
}

experimental/sev_guest/src/guest.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,28 @@ impl GuestMessageHeader {
156156
if self.get_algorithm().is_none()
157157
|| self.auth_header.algorithm == AeadAlgorithm::Invalid as u8
158158
{
159-
return Err("Invalid AEAD algorithm");
159+
return Err("invalid AEAD algorithm");
160160
}
161161
if self.get_message_type().is_none()
162162
|| self.auth_header.message_type == MessageType::Invalid as u8
163163
{
164-
return Err("Invalid message type");
164+
return Err("invalid message type");
165165
}
166166
if self.auth_header.header_version != CURRENT_HEADER_VERSION {
167-
return Err("Invalid header version");
167+
return Err("invalid header version");
168168
}
169169
if self.auth_header.message_version != CURRENT_MESSAGE_VERSION {
170-
return Err("Invalid message version");
170+
return Err("invalid message version");
171171
}
172172
// For now we always assume we use VMPCK_0 to encrypt all messages.
173173
if self.auth_header.message_vmpck != 0 {
174-
return Err("Invalid message VMPCK");
174+
return Err("invalid message VMPCK");
175175
}
176176
if self.auth_header.header_size != size_of::<Self>() as u16 {
177-
return Err("Invalid header size");
177+
return Err("invalid header size");
178178
}
179179
if self.auth_header.message_size as usize > MAX_PAYLOAD_SIZE {
180-
return Err("Invalid message size");
180+
return Err("invalid message size");
181181
}
182182
Ok(())
183183
}
@@ -311,13 +311,13 @@ impl AttestationResponse {
311311
/// report format are all valid.
312312
pub fn validate(&self) -> Result<(), &'static str> {
313313
if self._reserved.iter().any(|&value| value != 0) {
314-
return Err("Nonzero value in _reserved");
314+
return Err("nonzero value in _reserved");
315315
}
316316
if self.get_status().is_none() {
317-
return Err("Invalid status");
317+
return Err("invalid status");
318318
}
319319
if self.report_size != size_of::<AttestationReport>() as u32 {
320-
return Err("Invalid report size");
320+
return Err("invalid report size");
321321
}
322322
self.report.validate()
323323
}
@@ -459,25 +459,25 @@ impl AttestationReportData {
459459
self.reported_tcb.validate()?;
460460
self.committed_tcb.validate()?;
461461
if self._reserved_0.iter().any(|&value| value != 0) {
462-
return Err("Nonzero value in _reserved_0");
462+
return Err("nonzero value in _reserved_0");
463463
}
464464
if self._reserved_1 != 0 {
465-
return Err("Nonzero value in _reserved_1");
465+
return Err("nonzero value in _reserved_1");
466466
}
467467
if self._reserved_2 != 0 {
468-
return Err("Nonzero value in _reserved_2");
468+
return Err("nonzero value in _reserved_2");
469469
}
470470
if self._reserved_3.iter().any(|&value| value != 0) {
471-
return Err("Nonzero value in _reserved_3");
471+
return Err("nonzero value in _reserved_3");
472472
}
473473
if self.signature_algo != SigningAlgorithm::EcdsaP384Sha384 as u32 {
474-
return Err("Invalid signature algorithm");
474+
return Err("invalid signature algorithm");
475475
}
476476
if self.get_platform_info().is_none() {
477-
return Err("Invalid platform info");
477+
return Err("invalid platform info");
478478
}
479479
if self.get_author_key_en().is_none() {
480-
return Err("Invalid value for author_key_en");
480+
return Err("invalid value for author_key_en");
481481
}
482482
Ok(())
483483
}
@@ -523,10 +523,10 @@ impl GuestPolicy {
523523
/// Checks that the flags are valid and the reserved bytes are all zero.
524524
pub fn validate(&self) -> Result<(), &'static str> {
525525
if self._reserved != 0 {
526-
return Err("Nonzero value in _reserved");
526+
return Err("nonzero value in _reserved");
527527
}
528528
if self.get_flags().is_none() {
529-
return Err("Invalid flags");
529+
return Err("invalid flags");
530530
}
531531
Ok(())
532532
}
@@ -556,7 +556,7 @@ impl TcbVersion {
556556
/// Checks that the reserved bytes are all zero.
557557
pub fn validate(&self) -> Result<(), &'static str> {
558558
if self._reserved.iter().any(|&value| value != 0) {
559-
return Err("Nonzero value in _reserved");
559+
return Err("nonzero value in _reserved");
560560
}
561561
Ok(())
562562
}
@@ -619,7 +619,7 @@ impl EcdsaSignature {
619619
/// Checks that the reserved bytes are all zero.
620620
pub fn validate_format(&self) -> Result<(), &'static str> {
621621
if self._reserved.iter().any(|&value| value != 0) {
622-
return Err("Nonzero value in _reserved");
622+
return Err("nonzero value in _reserved");
623623
}
624624
Ok(())
625625
}

experimental/sev_guest/src/instructions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn pvalidate(
9191
}
9292
} else {
9393
Err(InstructionError::from_repr(result)
94-
.expect("Invalid return value from PVALIDATE instruction."))
94+
.expect("invalid return value from PVALIDATE instruction"))
9595
}
9696
}
9797

@@ -175,7 +175,7 @@ pub fn rmpadjust(
175175
Ok(())
176176
} else {
177177
Err(InstructionError::from_repr(result as u32)
178-
.expect("Invalid return value from RMPADJUST instruction."))
178+
.expect("invalid return value from RMPADJUST instruction"))
179179
}
180180
}
181181

experimental/sev_guest/src/msr.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl TryFrom<u64> for SevInfoResponse {
109109
fn try_from(msr_value: u64) -> Result<Self, &'static str> {
110110
const SEV_INFO_RESPONSE_INFO: u64 = 0x001;
111111
if msr_value & GHCB_INFO_MASK != SEV_INFO_RESPONSE_INFO {
112-
return Err("Value is not a valid SEV information response");
112+
return Err("value is not a valid SEV information response");
113113
}
114114
let max_protocol_version = (msr_value >> 48) as u16;
115115
let min_protocol_version = (msr_value >> 32) as u16;
@@ -179,12 +179,12 @@ impl TryFrom<u64> for CpuidResponse {
179179
const SEV_INFO_RESPONSE_INFO: u64 = 0x005;
180180
const RESERVED_MASK: u64 = 0x3FFFF000;
181181
if msr_value & GHCB_INFO_MASK != SEV_INFO_RESPONSE_INFO || msr_value & RESERVED_MASK != 0 {
182-
return Err("Value is not a valid CPUID response");
182+
return Err("value is not a valid CPUID response");
183183
}
184184
let value = (msr_value >> 32) as u32;
185185
const REGISTER_MASK: u64 = 0xC0000000;
186186
let register = CpuidRegister::from_repr(((msr_value & REGISTER_MASK) >> 30) as u8)
187-
.ok_or("Invalid register")?;
187+
.ok_or("invalid register")?;
188188
Ok(Self { value, register })
189189
}
190190
}
@@ -248,7 +248,7 @@ impl TryFrom<u64> for PreferredGhcbGpaResponse {
248248
fn try_from(msr_value: u64) -> Result<Self, &'static str> {
249249
const PREFERRED_GPA_RESPONSE_INFO: u64 = 0x011;
250250
if msr_value & GHCB_INFO_MASK != PREFERRED_GPA_RESPONSE_INFO {
251-
return Err("Value is not a valid preferred GHCP GPA response");
251+
return Err("value is not a valid preferred GHCP GPA response");
252252
}
253253
let ghcb_gpa = (msr_value & GCHP_DATA_MASK) as usize;
254254
Ok(Self { ghcb_gpa })
@@ -355,12 +355,12 @@ impl SnpPageStateChangeRequest {
355355
pub fn new(page_gpa: usize, assignment: PageAssignment) -> Result<Self, &'static str> {
356356
let page_gpa = page_gpa as u64;
357357
if page_gpa & GHCB_INFO_MASK != 0 {
358-
return Err("Page must be 4KiB-aligned");
358+
return Err("page must be 4KiB-aligned");
359359
}
360360
// Only 52 bits can be use for an address.
361361
const ADDRESS_MAX: u64 = (1 << 52) - 1;
362362
if page_gpa > ADDRESS_MAX {
363-
return Err("Page address is too high");
363+
return Err("page address is too high");
364364
}
365365
Ok(Self {
366366
page_gpa,
@@ -393,7 +393,7 @@ impl TryFrom<u64> for SnpPageStateChangeResponse {
393393
if msr_value & GHCB_INFO_MASK != SNP_PAGE_STATE_CHANGE_RESPONSE_INFO
394394
|| msr_value & RESERVED_MASK != 0
395395
{
396-
return Err("Value is not a valid SNP Page State Change response");
396+
return Err("value is not a valid SNP Page State Change response");
397397
}
398398
let error_code = (msr_value >> 32) as u32;
399399
Ok(Self { error_code })
@@ -410,7 +410,7 @@ pub fn change_snp_page_state(request: SnpPageStateChangeRequest) -> Result<(), &
410410
let response: SnpPageStateChangeResponse = read_protocol_msr().try_into()?;
411411
// Ensure that the page state change was successful.
412412
if response.error_code != 0 {
413-
return Err("Page state change failed");
413+
return Err("page state change failed");
414414
}
415415
Ok(())
416416
}
@@ -460,10 +460,10 @@ impl TryFrom<u64> for HypervisorFeatureSupportResponse {
460460
fn try_from(msr_value: u64) -> Result<Self, &'static str> {
461461
const HYPERVISOR_FEATURE_SUPPORT_RESPONSE_INFO: u64 = 0x081;
462462
if msr_value & GHCB_INFO_MASK != HYPERVISOR_FEATURE_SUPPORT_RESPONSE_INFO {
463-
return Err("Value is not a valid Hypervisor Feature Support response");
463+
return Err("value is not a valid Hypervisor Feature Support response");
464464
}
465465
HypervisorFeatureSupportResponse::from_bits(msr_value >> 12)
466-
.ok_or("Invalid Hypervisor Feature Support bitmap")
466+
.ok_or("invalid Hypervisor Feature Support bitmap")
467467
}
468468
}
469469

experimental/sev_guest/src/secrets.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ impl SecretsPage {
9393
/// the reserved bytes are all zero.
9494
pub fn validate(&self) -> Result<(), &'static str> {
9595
if !(SECRETS_PAGE_MIN_VERSION..=SECRETS_PAGE_MAX_VERSION).contains(&self.version) {
96-
return Err("Invalid version");
96+
return Err("invalid version");
9797
}
9898
if self.get_imi_en().is_none() {
99-
return Err("Invalid value for imi_en");
99+
return Err("invalid value for imi_en");
100100
}
101101
if self._reserved != 0 {
102-
return Err("Nonzero value in _reserved");
102+
return Err("nonzero value in _reserved");
103103
}
104104
Ok(())
105105
}

0 commit comments

Comments
 (0)