Skip to content

Commit f9c3189

Browse files
committed
Switching to custom parser for binary.
- Refactoring some misplaced code - Adding custom parsing functionality - Implemented new and updated existing unit tests to match new functionality - Implemented parsing library in place of bincode where possible (eventually will completely remove it) Signed-off-by: Larry Dewey <[email protected]>
1 parent 8ec281d commit f9c3189

File tree

22 files changed

+1699
-521
lines changed

22 files changed

+1699
-521
lines changed

Cargo.lock

Lines changed: 177 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/certs/sev/sev/cert/v1/body/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::*;
77
#[repr(C, packed)]
88
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
99
pub struct Data {
10-
pub firmware: crate::Version,
10+
pub firmware: crate::firmware::host::Version,
1111
pub reserved: u16,
1212
pub key: key::PubKey,
1313
}

src/certs/snp/ecdsa/mod.rs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(any(feature = "openssl", feature = "crypto_nossl"))]
44
use super::*;
55

6-
use crate::util::hexdump;
6+
use crate::{firmware::parser::ByteParser, util::hexdump};
77

88
#[cfg(feature = "openssl")]
99
use crate::certs::snp::{AsLeBytes, FromLe};
@@ -17,8 +17,8 @@ use serde_big_array::BigArray;
1717
#[cfg(feature = "openssl")]
1818
use openssl::{bn, ecdsa};
1919

20-
const SIG_PIECE_SIZE: usize = std::mem::size_of::<[u8; 72]>();
21-
const R_S_SIZE: usize = SIG_PIECE_SIZE * 2usize;
20+
pub(crate) const SIG_PIECE_SIZE: usize = std::mem::size_of::<[u8; 72]>();
21+
pub(crate) const R_S_SIZE: usize = SIG_PIECE_SIZE * 2usize;
2222

2323
#[repr(C)]
2424
#[derive(Copy, Clone, Deserialize, Serialize, PartialOrd, Ord)]
@@ -33,6 +33,14 @@ pub struct Signature {
3333
}
3434

3535
impl Signature {
36+
/// Creates a new signature from the values specified
37+
pub fn new(r: [u8; 72], s: [u8; 72]) -> Self {
38+
Self {
39+
r,
40+
s,
41+
..Default::default()
42+
}
43+
}
3644
/// Returns the signatures `r` component
3745
pub fn r(&self) -> &[u8; 72] {
3846
&self.r
@@ -55,23 +63,46 @@ impl std::fmt::Debug for Signature {
5563
}
5664
}
5765

58-
impl Eq for Signature {}
59-
impl PartialEq for Signature {
60-
fn eq(&self, other: &Signature) -> bool {
61-
self.r[..] == other.r[..] && self.s[..] == other.s[..]
66+
impl Default for Signature {
67+
fn default() -> Self {
68+
ByteParser::default()
6269
}
6370
}
6471

65-
impl Default for Signature {
72+
impl ByteParser for Signature {
73+
type Bytes = [u8; 512];
74+
#[inline(always)]
75+
fn from_bytes(bytes: Self::Bytes) -> Self {
76+
let mut r = [0; 72];
77+
let mut s = [0; 72];
78+
r.copy_from_slice(&bytes[0..72]);
79+
s.copy_from_slice(&bytes[72..144]);
80+
Self::new(r, s)
81+
}
82+
#[inline(always)]
83+
fn to_bytes(&self) -> Self::Bytes {
84+
let mut bytes = [0u8; 512];
85+
bytes[0..72].copy_from_slice(&self.r);
86+
bytes[72..144].copy_from_slice(&self.s);
87+
bytes
88+
}
89+
#[inline(always)]
6690
fn default() -> Self {
67-
Signature {
68-
r: [0u8; 72],
69-
s: [0u8; 72],
70-
_reserved: [0u8; (512 - (SIG_PIECE_SIZE * 2))],
91+
Self {
92+
r: [0; 72],
93+
s: [0; 72],
94+
_reserved: [0; 512 - R_S_SIZE],
7195
}
7296
}
7397
}
7498

99+
impl Eq for Signature {}
100+
impl PartialEq for Signature {
101+
fn eq(&self, other: &Signature) -> bool {
102+
self.r[..] == other.r[..] && self.s[..] == other.s[..]
103+
}
104+
}
105+
75106
impl std::fmt::Display for Signature {
76107
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77108
write!(
@@ -159,7 +190,7 @@ mod tests {
159190

160191
#[test]
161192
fn test_signature_default() {
162-
let sig: Signature = Signature::default();
193+
let sig: Signature = Default::default();
163194
assert_eq!(sig.r(), &[0u8; 72]);
164195
assert_eq!(sig.s(), &[0u8; 72]);
165196
}
@@ -177,8 +208,8 @@ mod tests {
177208

178209
#[test]
179210
fn test_signature_eq() {
180-
let sig1: Signature = Signature::default();
181-
let sig2: Signature = Signature::default();
211+
let sig1: Signature = Default::default();
212+
let sig2: Signature = Default::default();
182213
let sig3: Signature = Signature {
183214
r: [1u8; 72],
184215
s: [0u8; 72],
@@ -191,7 +222,7 @@ mod tests {
191222

192223
#[test]
193224
fn test_signature_ord() {
194-
let sig1: Signature = Signature::default();
225+
let sig1: Signature = Default::default();
195226
let sig2: Signature = Signature {
196227
r: [1u8; 72],
197228
s: [0u8; 72],
@@ -203,15 +234,15 @@ mod tests {
203234

204235
#[test]
205236
fn test_signature_debug() {
206-
let sig: Signature = Signature::default();
237+
let sig: Signature = Default::default();
207238
let debug_str: String = format!("{:?}", sig);
208239
assert!(debug_str.starts_with("Signature { r: "));
209240
assert!(debug_str.contains(", s: "));
210241
}
211242

212243
#[test]
213244
fn test_signature_display() {
214-
let sig: Signature = Signature::default();
245+
let sig: Signature = Default::default();
215246
let display_str: String = format!("{}", sig);
216247
assert!(display_str.contains("Signature:"));
217248
assert!(display_str.contains("R:"));
@@ -247,15 +278,15 @@ mod tests {
247278

248279
#[test]
249280
fn test_try_into_ecdsa_sig() {
250-
let sig = Signature::default();
281+
let sig: Signature = Default::default();
251282
let ecdsa_sig: ecdsa::EcdsaSig = (&sig).try_into().unwrap();
252283
assert_eq!(ecdsa_sig.r().to_vec(), vec![]);
253284
assert_eq!(ecdsa_sig.s().to_vec(), vec![]);
254285
}
255286

256287
#[test]
257288
fn test_try_into_vec() {
258-
let sig = Signature::default();
289+
let sig: Signature = Default::default();
259290
let der: Vec<u8> = (&sig).try_into().unwrap();
260291
assert!(!der.is_empty());
261292
}
@@ -269,7 +300,7 @@ mod tests {
269300
#[test]
270301
#[should_panic]
271302
fn test_try_into_p384_signature_failure() {
272-
let signature: Signature = Signature::default();
303+
let signature: Signature = Default::default();
273304

274305
let _p384_sig: p384::ecdsa::Signature = (&signature).try_into().unwrap();
275306
}
@@ -290,7 +321,7 @@ mod tests {
290321

291322
#[test]
292323
fn test_signature_serde() {
293-
let sig: Signature = Signature::default();
324+
let sig: Signature = Default::default();
294325
let serialized: Vec<u8> = bincode::serialize(&sig).unwrap();
295326
let deserialized: Signature = bincode::deserialize(&serialized).unwrap();
296327
assert_eq!(sig, deserialized);

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Display for VmmError {
117117

118118
/// The raw firmware error.
119119
#[derive(Debug)]
120-
pub(crate) struct RawFwError(u64);
120+
pub(crate) struct RawFwError(pub(crate) u64);
121121

122122
impl std::error::Error for RawFwError {}
123123

src/firmware/guest/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn map_fw_err(raw_error: RawFwError) -> UserApiError {
4343

4444
/// A handle to the SEV-SNP guest device.
4545
#[cfg(target_os = "linux")]
46+
#[derive(Debug)]
4647
pub struct Firmware(File);
4748

4849
#[cfg(target_os = "linux")]
@@ -234,3 +235,26 @@ impl Firmware {
234235
Ok(ffi_derived_key_response.key)
235236
}
236237
}
238+
239+
#[cfg(test)]
240+
mod tests {
241+
use super::*;
242+
243+
#[test]
244+
fn test_firmware_error_mapping() {
245+
let raw_error = RawFwError(1); // Lower byte error
246+
let error = map_fw_err(raw_error);
247+
assert!(matches!(error, UserApiError::FirmwareError(_)));
248+
249+
let raw_error = RawFwError(0x100000000u64); // Upper byte error
250+
let error = map_fw_err(raw_error);
251+
assert!(matches!(error, UserApiError::VmmError(_)));
252+
253+
let raw_error = RawFwError(0x0u64); // lower byte error
254+
let error = map_fw_err(raw_error);
255+
assert!(matches!(
256+
error,
257+
UserApiError::FirmwareError(FirmwareError::UnknownSevError(0))
258+
));
259+
}
260+
}

0 commit comments

Comments
 (0)