Skip to content

Commit

Permalink
Switching to custom parser for binary.
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
larrydewey committed Feb 13, 2025
1 parent 8ec281d commit f9c3189
Show file tree
Hide file tree
Showing 22 changed files with 1,699 additions and 521 deletions.
319 changes: 177 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/certs/sev/sev/cert/v1/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::*;
#[repr(C, packed)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Data {
pub firmware: crate::Version,
pub firmware: crate::firmware::host::Version,
pub reserved: u16,
pub key: key::PubKey,
}
Expand Down
75 changes: 53 additions & 22 deletions src/certs/snp/ecdsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(any(feature = "openssl", feature = "crypto_nossl"))]
use super::*;

use crate::util::hexdump;
use crate::{firmware::parser::ByteParser, util::hexdump};

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

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

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

impl Signature {
/// Creates a new signature from the values specified
pub fn new(r: [u8; 72], s: [u8; 72]) -> Self {
Self {
r,
s,
..Default::default()
}
}
/// Returns the signatures `r` component
pub fn r(&self) -> &[u8; 72] {
&self.r
Expand All @@ -55,23 +63,46 @@ impl std::fmt::Debug for Signature {
}
}

impl Eq for Signature {}
impl PartialEq for Signature {
fn eq(&self, other: &Signature) -> bool {
self.r[..] == other.r[..] && self.s[..] == other.s[..]
impl Default for Signature {
fn default() -> Self {
ByteParser::default()
}
}

impl Default for Signature {
impl ByteParser for Signature {
type Bytes = [u8; 512];
#[inline(always)]
fn from_bytes(bytes: Self::Bytes) -> Self {
let mut r = [0; 72];
let mut s = [0; 72];
r.copy_from_slice(&bytes[0..72]);
s.copy_from_slice(&bytes[72..144]);
Self::new(r, s)
}
#[inline(always)]
fn to_bytes(&self) -> Self::Bytes {
let mut bytes = [0u8; 512];
bytes[0..72].copy_from_slice(&self.r);
bytes[72..144].copy_from_slice(&self.s);
bytes
}
#[inline(always)]
fn default() -> Self {
Signature {
r: [0u8; 72],
s: [0u8; 72],
_reserved: [0u8; (512 - (SIG_PIECE_SIZE * 2))],
Self {
r: [0; 72],
s: [0; 72],
_reserved: [0; 512 - R_S_SIZE],
}
}
}

impl Eq for Signature {}
impl PartialEq for Signature {
fn eq(&self, other: &Signature) -> bool {
self.r[..] == other.r[..] && self.s[..] == other.s[..]
}
}

impl std::fmt::Display for Signature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
Expand Down Expand Up @@ -159,7 +190,7 @@ mod tests {

#[test]
fn test_signature_default() {
let sig: Signature = Signature::default();
let sig: Signature = Default::default();
assert_eq!(sig.r(), &[0u8; 72]);
assert_eq!(sig.s(), &[0u8; 72]);
}
Expand All @@ -177,8 +208,8 @@ mod tests {

#[test]
fn test_signature_eq() {
let sig1: Signature = Signature::default();
let sig2: Signature = Signature::default();
let sig1: Signature = Default::default();
let sig2: Signature = Default::default();
let sig3: Signature = Signature {
r: [1u8; 72],
s: [0u8; 72],
Expand All @@ -191,7 +222,7 @@ mod tests {

#[test]
fn test_signature_ord() {
let sig1: Signature = Signature::default();
let sig1: Signature = Default::default();
let sig2: Signature = Signature {
r: [1u8; 72],
s: [0u8; 72],
Expand All @@ -203,15 +234,15 @@ mod tests {

#[test]
fn test_signature_debug() {
let sig: Signature = Signature::default();
let sig: Signature = Default::default();
let debug_str: String = format!("{:?}", sig);
assert!(debug_str.starts_with("Signature { r: "));
assert!(debug_str.contains(", s: "));
}

#[test]
fn test_signature_display() {
let sig: Signature = Signature::default();
let sig: Signature = Default::default();
let display_str: String = format!("{}", sig);
assert!(display_str.contains("Signature:"));
assert!(display_str.contains("R:"));
Expand Down Expand Up @@ -247,15 +278,15 @@ mod tests {

#[test]
fn test_try_into_ecdsa_sig() {
let sig = Signature::default();
let sig: Signature = Default::default();
let ecdsa_sig: ecdsa::EcdsaSig = (&sig).try_into().unwrap();
assert_eq!(ecdsa_sig.r().to_vec(), vec![]);
assert_eq!(ecdsa_sig.s().to_vec(), vec![]);
}

#[test]
fn test_try_into_vec() {
let sig = Signature::default();
let sig: Signature = Default::default();
let der: Vec<u8> = (&sig).try_into().unwrap();
assert!(!der.is_empty());
}
Expand All @@ -269,7 +300,7 @@ mod tests {
#[test]
#[should_panic]
fn test_try_into_p384_signature_failure() {
let signature: Signature = Signature::default();
let signature: Signature = Default::default();

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

#[test]
fn test_signature_serde() {
let sig: Signature = Signature::default();
let sig: Signature = Default::default();
let serialized: Vec<u8> = bincode::serialize(&sig).unwrap();
let deserialized: Signature = bincode::deserialize(&serialized).unwrap();
assert_eq!(sig, deserialized);
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Display for VmmError {

/// The raw firmware error.
#[derive(Debug)]
pub(crate) struct RawFwError(u64);
pub(crate) struct RawFwError(pub(crate) u64);

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

Expand Down
24 changes: 24 additions & 0 deletions src/firmware/guest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn map_fw_err(raw_error: RawFwError) -> UserApiError {

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

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -234,3 +235,26 @@ impl Firmware {
Ok(ffi_derived_key_response.key)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_firmware_error_mapping() {
let raw_error = RawFwError(1); // Lower byte error
let error = map_fw_err(raw_error);
assert!(matches!(error, UserApiError::FirmwareError(_)));

let raw_error = RawFwError(0x100000000u64); // Upper byte error
let error = map_fw_err(raw_error);
assert!(matches!(error, UserApiError::VmmError(_)));

let raw_error = RawFwError(0x0u64); // lower byte error
let error = map_fw_err(raw_error);
assert!(matches!(
error,
UserApiError::FirmwareError(FirmwareError::UnknownSevError(0))
));
}
}
Loading

0 comments on commit f9c3189

Please sign in to comment.