cipherstash-client 0.42.0

The official CipherStash SDK
Documentation
//! Custom serialization formats for EQL types.

/// Base85 encoding/decoding for raw bytes — a v3 SteVec entry's `c`.
///
/// Under the envelope wire format an entry's ciphertext is the raw AEAD
/// output only (the key material lives once in the document's `h` header),
/// so the wire form is a bare base85 string, not a self-describing
/// MessagePack record.
pub(super) mod base85 {
    use serde::Deserialize;

    pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&base85::encode(bytes))
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        base85::decode(&s).map_err(serde::de::Error::custom)
    }
}

/// MessagePack Base85 encoding/decoding for a required `KeyHeader` — the v3
/// SteVec envelope's `h` field.
pub(super) mod key_header_mp_base85 {
    use crate::zerokms::KeyHeader;
    use serde::Deserialize;

    pub fn serialize<S>(header: &KeyHeader, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = header.to_mp_base85().map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&s)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<KeyHeader, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        KeyHeader::from_mp_base85(&s).map_err(serde::de::Error::custom)
    }
}

/// MessagePack Base85 encoding/decoding for a required `EncryptedRecord`.
///
/// Used with `#[serde(with = "formats::mp_base85")]` on non-optional ciphertext
/// fields. The v2.3 schema requires `c` on every encrypted payload (root scalar
/// and STE vector elements), so this module models the field as non-optional.
pub(super) mod mp_base85 {
    use super::super::*;
    use serde::Deserialize;

    /// Serializes an `EncryptedRecord` to MessagePack Base85 format.
    pub fn serialize<S>(ciphertext: &EncryptedRecord, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = ciphertext
            .to_mp_base85()
            .map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&s)
    }

    /// Deserializes an `EncryptedRecord` from MessagePack Base85 format.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<EncryptedRecord, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        EncryptedRecord::from_mp_base85(&s).map_err(serde::de::Error::custom)
    }
}