PqcBinaryFormat

Struct PqcBinaryFormat 

Source
pub struct PqcBinaryFormat {
    pub magic: [u8; 4],
    pub version: u8,
    pub algorithm: Algorithm,
    pub flags: u8,
    pub metadata: PqcMetadata,
    pub data: Vec<u8>,
    pub checksum: [u8; 32],
}
Expand description

PQC Binary Format v1.0 specification

This structure represents encrypted data in a standardized, self-describing format compatible across all post-quantum cryptographic algorithms.

§Example

use pqc_binary_format::{PqcBinaryFormat, Algorithm, PqcMetadata, EncParameters};
use std::collections::HashMap;

let metadata = PqcMetadata {
    enc_params: EncParameters {
        iv: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        tag: vec![1; 16],
        params: HashMap::new(),
    },
    ..Default::default()
};

let format = PqcBinaryFormat::new(
    Algorithm::Hybrid,
    metadata,
    vec![1, 2, 3, 4, 5],
);

// Serialize to bytes
let bytes = format.to_bytes().unwrap();

// Deserialize and verify
let recovered = PqcBinaryFormat::from_bytes(&bytes).unwrap();
assert_eq!(format, recovered);

Fields§

§magic: [u8; 4]

Magic bytes - always “PQC\x01”

§version: u8

Format version - currently 0x01

§algorithm: Algorithm

Algorithm identifier

§flags: u8

Feature flags

§metadata: PqcMetadata

Algorithm-specific metadata

§data: Vec<u8>

Encrypted data payload

§checksum: [u8; 32]

SHA-256 checksum of the entire structure (excluding this field)

Implementations§

Source§

impl PqcBinaryFormat

Source

pub fn new(algorithm: Algorithm, metadata: PqcMetadata, data: Vec<u8>) -> Self

Create a new PQC binary format structure with default flags

The checksum is automatically calculated and set.

§Example
use pqc_binary_format::{PqcBinaryFormat, Algorithm, PqcMetadata, EncParameters};
use std::collections::HashMap;

let metadata = PqcMetadata {
    enc_params: EncParameters {
        iv: vec![1; 12],
        tag: vec![1; 16],
        params: HashMap::new(),
    },
    ..Default::default()
};

let format = PqcBinaryFormat::new(
    Algorithm::PostQuantum,
    metadata,
    vec![1, 2, 3],
);
Source

pub fn with_flags( algorithm: Algorithm, flags: FormatFlags, metadata: PqcMetadata, data: Vec<u8>, ) -> Self

Create with specific flags

§Example
use pqc_binary_format::{PqcBinaryFormat, Algorithm, PqcMetadata, FormatFlags, EncParameters};
use std::collections::HashMap;

let metadata = PqcMetadata {
    enc_params: EncParameters {
        iv: vec![1; 12],
        tag: vec![1; 16],
        params: HashMap::new(),
    },
    ..Default::default()
};

let flags = FormatFlags::new().with_compression().with_streaming();

let format = PqcBinaryFormat::with_flags(
    Algorithm::Hybrid,
    flags,
    metadata,
    vec![1, 2, 3],
);

assert!(format.flags().has_compression());
assert!(format.flags().has_streaming());
Source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Serialize to binary format

§Errors

Returns CryptoError::BinaryFormatError if:

  • Format validation fails
  • Binary serialization fails
§Example
use pqc_binary_format::{PqcBinaryFormat, Algorithm, PqcMetadata, EncParameters};
use std::collections::HashMap;

let bytes = format.to_bytes().unwrap();
// Send bytes over network or save to file
Source

pub fn from_bytes(data: &[u8]) -> Result<Self>

Deserialize from binary format with checksum verification

§Errors

Returns CryptoError::BinaryFormatError if:

  • Binary deserialization fails
  • Format validation fails after deserialization
  • Checksum verification fails
§Example
use pqc_binary_format::PqcBinaryFormat;

let recovered = PqcBinaryFormat::from_bytes(&bytes).unwrap();
Source

pub fn validate(&self) -> Result<()>

Validate the binary format structure

§Errors

Returns CryptoError if:

  • Magic bytes are invalid
  • Version is unsupported
  • Algorithm ID is invalid
  • Metadata validation fails
Source

pub fn update_checksum(&mut self)

Update the checksum field with the calculated checksum

Call this after modifying any fields to maintain integrity.

Source

pub fn flags(&self) -> FormatFlags

Get format flags

Source

pub const fn algorithm(&self) -> Algorithm

Get algorithm

Source

pub fn data(&self) -> &[u8]

Get encrypted data

Source

pub const fn metadata(&self) -> &PqcMetadata

Get metadata

Source

pub fn total_size(&self) -> usize

Get total size of the binary format when serialized

Trait Implementations§

Source§

impl Clone for PqcBinaryFormat

Source§

fn clone(&self) -> PqcBinaryFormat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PqcBinaryFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for PqcBinaryFormat

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for PqcBinaryFormat

Source§

fn eq(&self, other: &PqcBinaryFormat) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for PqcBinaryFormat

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for PqcBinaryFormat

Source§

impl StructuralPartialEq for PqcBinaryFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,