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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use alloc::boxed::Box;
use alloc::string::String;

use serde::{Deserialize, Serialize};
use umbral_pre::{decrypt_original, encrypt, serde_bytes, Capsule, PublicKey, SecretKey};

use crate::conditions::{Conditions, Context};
use crate::key_frag::DecryptionError;

use crate::versioning::{
    messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};

/// The ferveo variant to use for the decryption share derivation.
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Copy, Clone)]
pub enum FerveoVariant {
    /// the simple variant requires n/n shares to decrypt
    SIMPLE,
    /// the precomputed variant requires m/n shares to decrypt
    PRECOMPUTED,
}

/// A request for an Ursula to derive a decryption share.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdDecryptionRequest {
    /// The ID of the ritual.
    pub ritual_id: u16,
    /// The ciphertext to generate a decryption share for.
    #[serde(with = "serde_bytes::as_base64")]
    pub ciphertext: Box<[u8]>,
    /// A blob of bytes containing decryption conditions for this message.
    pub conditions: Option<Conditions>,
    /// A blob of bytes containing context required to evaluate conditions.
    pub context: Option<Context>,
    /// The ferveo variant to use for the decryption share derivation.
    pub variant: FerveoVariant,
}

impl ThresholdDecryptionRequest {
    /// Creates a new decryption request.
    pub fn new(
        ritual_id: u16,
        ciphertext: &[u8],
        conditions: Option<&Conditions>,
        context: Option<&Context>,
        variant: FerveoVariant,
    ) -> Self {
        Self {
            ritual_id,
            ciphertext: ciphertext.to_vec().into(),
            conditions: conditions.cloned(),
            context: context.cloned(),
            variant,
        }
    }

    /// Encrypts the decryption request.
    pub fn encrypt(
        &self,
        request_encrypting_key: &PublicKey,
        response_encrypting_key: &PublicKey,
    ) -> EncryptedThresholdDecryptionRequest {
        EncryptedThresholdDecryptionRequest::new(
            self,
            request_encrypting_key,
            response_encrypting_key,
        )
    }
}

impl<'a> ProtocolObjectInner<'a> for ThresholdDecryptionRequest {
    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn brand() -> [u8; 4] {
        *b"ThRq"
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for ThresholdDecryptionRequest {}

/// A request for an Ursula to derive a decryption share that specifies the key to encrypt Ursula's response.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct E2EThresholdDecryptionRequest {
    /// The decryption request.
    pub decryption_request: ThresholdDecryptionRequest,
    /// The key to encrypt the corresponding decryption response.
    pub response_encrypting_key: PublicKey,
}

impl E2EThresholdDecryptionRequest {
    /// Create E2E decryption request.
    pub fn new(
        decryption_request: &ThresholdDecryptionRequest,
        response_encrypting_key: &PublicKey,
    ) -> Self {
        Self {
            decryption_request: decryption_request.clone(),
            response_encrypting_key: *response_encrypting_key,
        }
    }
}

impl<'a> ProtocolObjectInner<'a> for E2EThresholdDecryptionRequest {
    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn brand() -> [u8; 4] {
        *b"E2eR"
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for E2EThresholdDecryptionRequest {}

/// An encrypted request for an Ursula to derive a decryption share.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedThresholdDecryptionRequest {
    /// ID of the ritual
    pub ritual_id: u16,
    /// TODO Umbral for now - but change
    capsule: Capsule,
    #[serde(with = "serde_bytes::as_base64")]
    ciphertext: Box<[u8]>,
}

impl EncryptedThresholdDecryptionRequest {
    fn new(
        request: &ThresholdDecryptionRequest,
        request_encrypting_key: &PublicKey,
        response_encrypting_key: &PublicKey,
    ) -> Self {
        let e2e_decryption_request =
            E2EThresholdDecryptionRequest::new(request, response_encrypting_key);
        // TODO: using Umbral for encryption to avoid introducing more crypto primitives.
        let (capsule, ciphertext) =
            encrypt(request_encrypting_key, &e2e_decryption_request.to_bytes())
                .expect("encryption failed - out of memory?");
        let ritual_id = request.ritual_id;
        Self {
            ritual_id,
            capsule,
            ciphertext,
        }
    }

    /// Decrypts the decryption request
    pub fn decrypt(
        &self,
        sk: &SecretKey,
    ) -> Result<E2EThresholdDecryptionRequest, DecryptionError> {
        let decryption_request_bytes = decrypt_original(sk, &self.capsule, &self.ciphertext)
            .map_err(DecryptionError::DecryptionFailed)?;
        let decryption_request =
            E2EThresholdDecryptionRequest::from_bytes(&decryption_request_bytes)
                .map_err(DecryptionError::DeserializationFailed)?;
        Ok(decryption_request)
    }
}

impl<'a> ProtocolObjectInner<'a> for EncryptedThresholdDecryptionRequest {
    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn brand() -> [u8; 4] {
        *b"ETRq"
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for EncryptedThresholdDecryptionRequest {}

/// A response from Ursula with a derived decryption share.
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
pub struct ThresholdDecryptionResponse {
    /// The decryption share to include in the response.
    #[serde(with = "serde_bytes::as_base64")]
    pub decryption_share: Box<[u8]>,
}

impl ThresholdDecryptionResponse {
    /// Creates and a new decryption response.
    pub fn new(decryption_share: &[u8]) -> Self {
        ThresholdDecryptionResponse {
            decryption_share: decryption_share.to_vec().into(),
        }
    }

    /// Encrypts the decryption response.
    pub fn encrypt(&self, encrypting_key: &PublicKey) -> EncryptedThresholdDecryptionResponse {
        EncryptedThresholdDecryptionResponse::new(encrypting_key, self)
    }
}

impl<'a> ProtocolObjectInner<'a> for ThresholdDecryptionResponse {
    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn brand() -> [u8; 4] {
        *b"ThRs"
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for ThresholdDecryptionResponse {}

/// An encrypted response from Ursula with a derived decryption share.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedThresholdDecryptionResponse {
    /// TODO Umbral for now - but change
    capsule: Capsule,
    #[serde(with = "serde_bytes::as_base64")]
    ciphertext: Box<[u8]>,
}

impl EncryptedThresholdDecryptionResponse {
    fn new(
        encrypting_key: &PublicKey,
        threshold_decryption_response: &ThresholdDecryptionResponse,
    ) -> Self {
        // TODO: using Umbral for encryption to avoid introducing more crypto primitives.
        let (capsule, ciphertext) =
            encrypt(encrypting_key, &threshold_decryption_response.to_bytes())
                .expect("encryption failed - out of memory?");
        Self {
            capsule,
            ciphertext,
        }
    }

    /// Decrypts the decryption request
    pub fn decrypt(&self, sk: &SecretKey) -> Result<ThresholdDecryptionResponse, DecryptionError> {
        let decryption_response_bytes = decrypt_original(sk, &self.capsule, &self.ciphertext)
            .map_err(DecryptionError::DecryptionFailed)?;
        let decryption_response =
            ThresholdDecryptionResponse::from_bytes(&decryption_response_bytes)
                .map_err(DecryptionError::DeserializationFailed)?;
        Ok(decryption_response)
    }
}

impl<'a> ProtocolObjectInner<'a> for EncryptedThresholdDecryptionResponse {
    fn version() -> (u16, u16) {
        (1, 0)
    }

    fn brand() -> [u8; 4] {
        *b"ETRs"
    }

    fn unversioned_to_bytes(&self) -> Box<[u8]> {
        messagepack_serialize(&self)
    }

    fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
        if minor_version == 0 {
            Some(messagepack_deserialize(bytes))
        } else {
            None
        }
    }
}

impl<'a> ProtocolObject<'a> for EncryptedThresholdDecryptionResponse {}

#[cfg(test)]
mod tests {
    use umbral_pre::encrypt;
    use umbral_pre::SecretKey;

    use crate::{
        Conditions, Context, EncryptedThresholdDecryptionRequest,
        EncryptedThresholdDecryptionResponse, FerveoVariant, ProtocolObject,
        ThresholdDecryptionRequest, ThresholdDecryptionResponse,
    };

    #[test]
    fn threshold_decryption_request() {
        let ritual_id = 0;

        let request_secret = SecretKey::random();
        let request_encrypting_key = request_secret.public_key();

        let response_secret = SecretKey::random();
        let response_encrypting_key = response_secret.public_key();

        let random_secret_key = SecretKey::random();

        let encryption_result = encrypt(&random_secret_key.public_key(), b"The Tyranny of Merit");

        let (_capsule, _ciphertext) = encryption_result.unwrap();

        let request = ThresholdDecryptionRequest::new(
            ritual_id,
            &_ciphertext,
            Some(&Conditions::new("abcd")),
            Some(&Context::new("efgh")),
            FerveoVariant::SIMPLE,
        );

        let encrypted_request = request.encrypt(&request_encrypting_key, &response_encrypting_key);

        let encrypted_request_bytes = encrypted_request.to_bytes();
        let encrypted_request_from_bytes =
            EncryptedThresholdDecryptionRequest::from_bytes(&encrypted_request_bytes).unwrap();

        assert_eq!(encrypted_request_from_bytes.ritual_id, ritual_id);

        let e2e_request = encrypted_request_from_bytes
            .decrypt(&request_secret)
            .unwrap();
        assert_eq!(response_encrypting_key, e2e_request.response_encrypting_key);
        assert_eq!(request, e2e_request.decryption_request);

        // wrong secret key used
        assert!(encrypted_request_from_bytes
            .decrypt(&response_secret)
            .is_err());

        assert!(encrypted_request_from_bytes
            .decrypt(&random_secret_key)
            .is_err());
    }

    #[test]
    fn threshold_decryption_response() {
        let response_secret = SecretKey::random();
        let response_encrypting_key = response_secret.public_key();

        let decryption_share = b"The Tyranny of Merit";

        let response = ThresholdDecryptionResponse::new(decryption_share);

        let encrypted_response = response.encrypt(&response_encrypting_key);

        let encrypted_response_bytes = encrypted_response.to_bytes();
        let encrypted_response_from_bytes =
            EncryptedThresholdDecryptionResponse::from_bytes(&encrypted_response_bytes).unwrap();

        let decrypted_response = encrypted_response_from_bytes
            .decrypt(&response_secret)
            .unwrap();
        assert_eq!(response, decrypted_response);
        assert_eq!(
            response.decryption_share,
            decrypted_response.decryption_share
        );

        // wrong secret key used
        let random_secret_key = SecretKey::random();
        assert!(encrypted_response_from_bytes
            .decrypt(&random_secret_key)
            .is_err());
    }
}