solana_zk_sdk/encryption/pod/
auth_encryption.rs

1//! Plain Old Data types for the AES128-GCM-SIV authenticated encryption scheme.
2
3#[cfg(not(target_os = "solana"))]
4use crate::{encryption::auth_encryption::AeCiphertext, errors::AuthenticatedEncryptionError};
5#[cfg(target_arch = "wasm32")]
6use wasm_bindgen::prelude::*;
7use {
8    crate::{
9        encryption::AE_CIPHERTEXT_LEN,
10        pod::{impl_from_bytes, impl_from_str, impl_wasm_bindings},
11    },
12    base64::{prelude::BASE64_STANDARD, Engine},
13    bytemuck::{Pod, Zeroable},
14    std::fmt,
15};
16
17/// Maximum length of a base64 encoded authenticated encryption ciphertext
18const AE_CIPHERTEXT_MAX_BASE64_LEN: usize = 48;
19
20/// The `AeCiphertext` type as a `Pod`.
21#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
22#[derive(Clone, Copy, PartialEq, Eq)]
23#[repr(transparent)]
24pub struct PodAeCiphertext(pub(crate) [u8; AE_CIPHERTEXT_LEN]);
25
26impl_wasm_bindings!(POD_TYPE = PodAeCiphertext, DECODED_TYPE = AeCiphertext);
27
28// `PodAeCiphertext` is a wrapper type for a byte array, which is both `Pod` and `Zeroable`. However,
29// the marker traits `bytemuck::Pod` and `bytemuck::Zeroable` can only be derived for power-of-two
30// length byte arrays. Directly implement these traits for `PodAeCiphertext`.
31unsafe impl Zeroable for PodAeCiphertext {}
32unsafe impl Pod for PodAeCiphertext {}
33
34impl fmt::Debug for PodAeCiphertext {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        write!(f, "{:?}", self.0)
37    }
38}
39
40impl fmt::Display for PodAeCiphertext {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "{}", BASE64_STANDARD.encode(self.0))
43    }
44}
45
46impl_from_str!(
47    TYPE = PodAeCiphertext,
48    BYTES_LEN = AE_CIPHERTEXT_LEN,
49    BASE64_LEN = AE_CIPHERTEXT_MAX_BASE64_LEN
50);
51
52impl_from_bytes!(TYPE = PodAeCiphertext, BYTES_LEN = AE_CIPHERTEXT_LEN);
53
54impl Default for PodAeCiphertext {
55    fn default() -> Self {
56        Self::zeroed()
57    }
58}
59
60#[cfg(not(target_os = "solana"))]
61impl From<AeCiphertext> for PodAeCiphertext {
62    fn from(decoded_ciphertext: AeCiphertext) -> Self {
63        Self(decoded_ciphertext.to_bytes())
64    }
65}
66
67#[cfg(not(target_os = "solana"))]
68impl TryFrom<PodAeCiphertext> for AeCiphertext {
69    type Error = AuthenticatedEncryptionError;
70
71    fn try_from(pod_ciphertext: PodAeCiphertext) -> Result<Self, Self::Error> {
72        Self::from_bytes(&pod_ciphertext.0).ok_or(AuthenticatedEncryptionError::Deserialization)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use {super::*, crate::encryption::auth_encryption::AeKey, std::str::FromStr};
79
80    #[test]
81    fn ae_ciphertext_fromstr() {
82        let ae_key = AeKey::new_rand();
83        let expected_ae_ciphertext: PodAeCiphertext = ae_key.encrypt(0_u64).into();
84
85        let ae_ciphertext_base64_str = format!("{}", expected_ae_ciphertext);
86        let computed_ae_ciphertext = PodAeCiphertext::from_str(&ae_ciphertext_base64_str).unwrap();
87
88        assert_eq!(expected_ae_ciphertext, computed_ae_ciphertext);
89    }
90}