core_crypto_keystore/entities/
proteus.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use 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}