core_crypto_keystore/entities/
proteus.rs1use zeroize::Zeroize;
18
19use crate::connection::FetchFromDatabase;
20
21#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
22#[zeroize(drop)]
23#[cfg_attr(
24 any(target_family = "wasm", feature = "serde"),
25 derive(serde::Serialize, serde::Deserialize)
26)]
27pub struct ProteusIdentity {
28 pub sk: Vec<u8>,
29 pub pk: Vec<u8>,
30}
31
32impl ProteusIdentity {
33 pub const SK_KEY_SIZE: usize = 64;
34 pub const PK_KEY_SIZE: usize = 32;
35
36 pub fn sk_raw(&self) -> zeroize::Zeroizing<[u8; Self::SK_KEY_SIZE]> {
37 let mut slice = zeroize::Zeroizing::new([0u8; Self::SK_KEY_SIZE]);
38 debug_assert_eq!(self.sk.len(), Self::SK_KEY_SIZE);
39 slice.copy_from_slice(&self.sk[..Self::SK_KEY_SIZE]);
40 slice
41 }
42
43 pub fn pk_raw(&self) -> zeroize::Zeroizing<[u8; Self::PK_KEY_SIZE]> {
44 let mut slice = zeroize::Zeroizing::new([0u8; Self::PK_KEY_SIZE]);
45 debug_assert_eq!(self.pk.len(), Self::PK_KEY_SIZE);
46 slice.copy_from_slice(&self.pk[..Self::PK_KEY_SIZE]);
47 slice
48 }
49}
50
51#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
52#[zeroize(drop)]
53#[cfg_attr(
54 any(target_family = "wasm", feature = "serde"),
55 derive(serde::Serialize, serde::Deserialize)
56)]
57pub struct ProteusPrekey {
58 pub id: u16,
59 id_bytes: Vec<u8>,
60 pub prekey: Vec<u8>,
61}
62
63impl ProteusPrekey {
64 pub fn from_raw(id: u16, prekey: Vec<u8>) -> Self {
65 Self {
66 id_bytes: id.to_le_bytes().into(),
67 id,
68 prekey,
69 }
70 }
71
72 pub fn id_bytes(&self) -> &[u8] {
73 &self.id_bytes
74 }
75
76 pub fn id_from_slice(slice: &[u8]) -> u16 {
77 if slice.len() < 2 {
78 panic!("Oops, Proteus Prekey id slice is too small!");
79 }
80
81 let mut id_buf = [0u8; 2];
82 id_buf.copy_from_slice(&slice[..2]);
83 let id: u16 = u16::from_le_bytes(id_buf);
84 id
85 }
86
87 pub fn set_id(&mut self, id: u16) {
88 self.id = id;
89 self.id_bytes = self.id.to_le_bytes().into();
90 }
91
92 pub async fn get_free_id(conn: &crate::Connection) -> crate::CryptoKeystoreResult<u16> {
93 let mut id = 1u16;
94 let limit = u16::MAX;
95 while id <= limit {
96 if id == limit {
97 return Err(crate::CryptoKeystoreError::NoFreePrekeyId);
98 }
99 if conn.find::<Self>(&id.to_le_bytes()).await?.is_none() {
100 break;
101 }
102 id += 1;
103 }
104
105 Ok(id)
106 }
107}
108
109#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
110#[zeroize(drop)]
111#[cfg_attr(
112 any(target_family = "wasm", feature = "serde"),
113 derive(serde::Serialize, serde::Deserialize)
114)]
115pub struct ProteusSession {
116 pub id: String,
117 pub session: Vec<u8>,
118}