1use crate::error::CryptoResult;
35use crate::namespace::KeyNamespace;
36use crate::sealed::{
37 seal_json, seal_string, try_unseal_json, try_unseal_string, unseal_json, unseal_string,
38};
39use crate::session::SessionKey;
40use serde::{de::DeserializeOwned, Serialize};
41
42pub trait EncryptedEntity {
50 const NAMESPACE: KeyNamespace;
52
53 fn entity_id(&self) -> i64;
57}
58
59impl SessionKey {
60 pub fn entity_key<E: EncryptedEntity>(&self, entity: &E) -> CryptoResult<[u8; 32]> {
62 self.derive(E::NAMESPACE, entity.entity_id())
63 }
64}
65
66pub fn seal_entity_string<E: EncryptedEntity>(
69 session: &SessionKey,
70 entity: &E,
71 plaintext: &str,
72) -> CryptoResult<String> {
73 let key = session.entity_key(entity)?;
74 seal_string(&key, plaintext)
75}
76
77pub fn unseal_entity_string<E: EncryptedEntity>(
79 session: &SessionKey,
80 entity: &E,
81 stored: &str,
82) -> CryptoResult<String> {
83 let key = session.entity_key(entity)?;
84 unseal_string(&key, stored)
85}
86
87pub fn try_unseal_entity_string<E: EncryptedEntity>(
89 session: &SessionKey,
90 entity: &E,
91 stored: &str,
92) -> CryptoResult<String> {
93 let key = session.entity_key(entity)?;
94 try_unseal_string(&key, stored)
95}
96
97pub fn seal_entity_json<E: EncryptedEntity, T: Serialize>(
100 session: &SessionKey,
101 entity: &E,
102 value: &T,
103) -> CryptoResult<String> {
104 let key = session.entity_key(entity)?;
105 seal_json(&key, value)
106}
107
108pub fn unseal_entity_json<E: EncryptedEntity, T: DeserializeOwned>(
110 session: &SessionKey,
111 entity: &E,
112 stored: &str,
113) -> CryptoResult<T> {
114 let key = session.entity_key(entity)?;
115 unseal_json(&key, stored)
116}
117
118pub fn try_unseal_entity_json<E: EncryptedEntity, T: DeserializeOwned>(
120 session: &SessionKey,
121 entity: &E,
122 stored: &str,
123) -> CryptoResult<T> {
124 let key = session.entity_key(entity)?;
125 try_unseal_json(&key, stored)
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 struct Dummy {
133 id: i64,
134 }
135
136 impl EncryptedEntity for Dummy {
137 const NAMESPACE: KeyNamespace = KeyNamespace::Profile;
138 fn entity_id(&self) -> i64 {
139 self.id
140 }
141 }
142
143 fn session() -> SessionKey {
144 SessionKey::new([42u8; 32])
145 }
146
147 #[test]
148 fn roundtrip_string() {
149 let s = session();
150 let e = Dummy { id: 7 };
151 let sealed = seal_entity_string(&s, &e, "hello").unwrap();
152 assert_eq!(try_unseal_entity_string(&s, &e, &sealed).unwrap(), "hello");
153 }
154
155 #[test]
156 fn different_ids_cannot_cross_decrypt() {
157 let s = session();
158 let a = Dummy { id: 1 };
159 let b = Dummy { id: 2 };
160 let sealed = seal_entity_string(&s, &a, "secret").unwrap();
161 assert!(try_unseal_entity_string(&s, &b, &sealed).is_err());
162 }
163
164 #[test]
165 fn plaintext_passes_through_try_variant() {
166 let s = session();
167 let e = Dummy { id: 1 };
168 assert_eq!(
169 try_unseal_entity_string(&s, &e, "legacy-plaintext").unwrap(),
170 "legacy-plaintext"
171 );
172 }
173
174 #[test]
175 fn plaintext_rejected_by_strict_variant() {
176 let s = session();
177 let e = Dummy { id: 1 };
178 assert!(unseal_entity_string(&s, &e, "legacy-plaintext").is_err());
179 }
180
181 struct OtherEntity {
185 id: i64,
186 }
187 impl EncryptedEntity for OtherEntity {
188 const NAMESPACE: KeyNamespace = KeyNamespace::Proxy;
189 fn entity_id(&self) -> i64 {
190 self.id
191 }
192 }
193
194 #[test]
198 fn same_id_different_namespace_cannot_cross_decrypt() {
199 let s = session();
200 let profile = Dummy { id: 1 };
201 let proxy = OtherEntity { id: 1 };
202
203 let sealed = seal_entity_string(&s, &profile, "profile-secret").unwrap();
204 assert!(
205 try_unseal_entity_string(&s, &proxy, &sealed).is_err(),
206 "a proxy key must not decrypt a profile blob sharing the same id"
207 );
208 }
209
210 #[test]
213 fn different_session_key_cannot_decrypt() {
214 let e = Dummy { id: 7 };
215 let sealed = seal_entity_string(&session(), &e, "secret").unwrap();
216
217 let other_session = SessionKey::new([99u8; 32]);
218 assert!(unseal_entity_string(&other_session, &e, &sealed).is_err());
219 }
220
221 #[test]
222 fn roundtrip_json() {
223 use serde_json::json;
224 let s = session();
225 let e = Dummy { id: 3 };
226 let value = json!({ "host": "1.2.3.4", "port": 1080 });
227
228 let sealed = seal_entity_json(&s, &e, &value).unwrap();
229 let back: serde_json::Value = try_unseal_entity_json(&s, &e, &sealed).unwrap();
230 assert_eq!(back, value);
231 }
232}