1use crate::error::IntoAnyError;
6#[cfg(mls_build_async)]
7use alloc::boxed::Box;
8use alloc::vec::Vec;
9use core::{
10 fmt::{self, Debug},
11 ops::Deref,
12};
13use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
14use zeroize::Zeroizing;
15
16#[derive(Clone, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct PreSharedKey(
20 #[mls_codec(with = "mls_rs_codec::byte_vec")]
21 #[cfg_attr(feature = "serde", serde(with = "crate::zeroizing_serde"))]
22 Zeroizing<Vec<u8>>,
23);
24
25impl Debug for PreSharedKey {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 crate::debug::pretty_bytes(&self.0)
28 .named("PreSharedKey")
29 .fmt(f)
30 }
31}
32
33impl PreSharedKey {
34 pub fn new(data: Vec<u8>) -> Self {
36 PreSharedKey(Zeroizing::new(data))
37 }
38
39 pub fn raw_value(&self) -> &[u8] {
41 &self.0
42 }
43}
44
45impl From<Vec<u8>> for PreSharedKey {
46 fn from(bytes: Vec<u8>) -> Self {
47 Self::new(bytes)
48 }
49}
50
51impl From<Zeroizing<Vec<u8>>> for PreSharedKey {
52 fn from(bytes: Zeroizing<Vec<u8>>) -> Self {
53 Self(bytes)
54 }
55}
56
57impl AsRef<[u8]> for PreSharedKey {
58 fn as_ref(&self) -> &[u8] {
59 self.raw_value()
60 }
61}
62
63impl Deref for PreSharedKey {
64 type Target = [u8];
65
66 fn deref(&self) -> &Self::Target {
67 self.raw_value()
68 }
69}
70
71#[derive(Clone, Eq, Hash, Ord, PartialOrd, PartialEq, MlsSize, MlsEncode, MlsDecode)]
72#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
73#[cfg_attr(
74 all(feature = "ffi", not(test)),
75 safer_ffi_gen::ffi_type(clone, opaque)
76)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub struct ExternalPskId(
80 #[mls_codec(with = "mls_rs_codec::byte_vec")]
81 #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
82 Vec<u8>,
83);
84
85impl Debug for ExternalPskId {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 crate::debug::pretty_bytes(&self.0)
88 .named("ExternalPskId")
89 .fmt(f)
90 }
91}
92
93#[cfg_attr(all(feature = "ffi", not(test)), safer_ffi_gen::safer_ffi_gen)]
94impl ExternalPskId {
95 pub fn new(id_data: Vec<u8>) -> Self {
96 Self(id_data)
97 }
98}
99
100impl AsRef<[u8]> for ExternalPskId {
101 fn as_ref(&self) -> &[u8] {
102 &self.0
103 }
104}
105
106impl Deref for ExternalPskId {
107 type Target = [u8];
108
109 fn deref(&self) -> &Self::Target {
110 &self.0
111 }
112}
113
114impl From<Vec<u8>> for ExternalPskId {
115 fn from(value: Vec<u8>) -> Self {
116 ExternalPskId(value)
117 }
118}
119
120#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
122#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
123pub trait PreSharedKeyStorage: Send + Sync {
124 type Error: IntoAnyError;
127
128 async fn get(&self, id: &ExternalPskId) -> Result<Option<PreSharedKey>, Self::Error>;
132
133 async fn contains(&self, id: &ExternalPskId) -> Result<bool, Self::Error> {
135 self.get(id).await.map(|key| key.is_some())
136 }
137}