core_crypto_keystore/entities/
proteus.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Wire
// Copyright (C) 2022 Wire Swiss GmbH

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

use zeroize::Zeroize;

use crate::connection::FetchFromDatabase;

#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
#[zeroize(drop)]
#[cfg_attr(
    any(target_family = "wasm", feature = "serde"),
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ProteusIdentity {
    pub sk: Vec<u8>,
    pub pk: Vec<u8>,
}

impl ProteusIdentity {
    pub const SK_KEY_SIZE: usize = 64;
    pub const PK_KEY_SIZE: usize = 32;

    pub fn sk_raw(&self) -> zeroize::Zeroizing<[u8; Self::SK_KEY_SIZE]> {
        let mut slice = zeroize::Zeroizing::new([0u8; Self::SK_KEY_SIZE]);
        debug_assert_eq!(self.sk.len(), Self::SK_KEY_SIZE);
        slice.copy_from_slice(&self.sk[..Self::SK_KEY_SIZE]);
        slice
    }

    pub fn pk_raw(&self) -> zeroize::Zeroizing<[u8; Self::PK_KEY_SIZE]> {
        let mut slice = zeroize::Zeroizing::new([0u8; Self::PK_KEY_SIZE]);
        debug_assert_eq!(self.pk.len(), Self::PK_KEY_SIZE);
        slice.copy_from_slice(&self.pk[..Self::PK_KEY_SIZE]);
        slice
    }
}

#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
#[zeroize(drop)]
#[cfg_attr(
    any(target_family = "wasm", feature = "serde"),
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ProteusPrekey {
    pub id: u16,
    id_bytes: Vec<u8>,
    pub prekey: Vec<u8>,
}

impl ProteusPrekey {
    pub fn from_raw(id: u16, prekey: Vec<u8>) -> Self {
        Self {
            id_bytes: id.to_le_bytes().into(),
            id,
            prekey,
        }
    }

    pub fn id_bytes(&self) -> &[u8] {
        &self.id_bytes
    }

    pub fn id_from_slice(slice: &[u8]) -> u16 {
        if slice.len() < 2 {
            panic!("Oops, Proteus Prekey id slice is too small!");
        }

        let mut id_buf = [0u8; 2];
        id_buf.copy_from_slice(&slice[..2]);
        let id: u16 = u16::from_le_bytes(id_buf);
        id
    }

    pub fn set_id(&mut self, id: u16) {
        self.id = id;
        self.id_bytes = self.id.to_le_bytes().into();
    }

    pub async fn get_free_id(conn: &crate::Connection) -> crate::CryptoKeystoreResult<u16> {
        let mut id = 1u16;
        let limit = u16::MAX;
        while id <= limit {
            if id == limit {
                return Err(crate::CryptoKeystoreError::NoFreePrekeyId);
            }
            if conn.find::<Self>(&id.to_le_bytes()).await?.is_none() {
                break;
            }
            id += 1;
        }

        Ok(id)
    }
}

#[derive(Debug, Clone, Zeroize, PartialEq, Eq)]
#[zeroize(drop)]
#[cfg_attr(
    any(target_family = "wasm", feature = "serde"),
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ProteusSession {
    pub id: String,
    pub session: Vec<u8>,
}