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
//! This module defines ScrambleDB transformations at the level of individual
//! pieces of data as defined in [`data_types`](crate::data_types).
//!
//! These transformations are:
//! - blinding identifiable and pseudonymous data
//! - pseudonymizing blinded identifiable data
//! - converting blinded pseudonymous data
//! - finalizing blinded pseudonymous data

use hacspec_lib::Randomness;
use oprf::coprf::{
    coprf_online::{blind, blind_convert, blind_evaluate, prepare_blind_convert},
    coprf_setup::{derive_key, BlindingPublicKey, CoPRFEvaluatorContext},
};

use crate::{
    data_types::*,
    error::Error,
    setup::{StoreContext, StoreEncryptionKey},
};

pub(crate) mod double_hpke;

/// CoPRF context string for domain separation of intial pseudonymization.
const PSEUDONYMIZATION_CONTEXT: &[u8] = b"CoPRF-Context-Pseudonymization";

/// Blind an identifiable datum as a first step in initial pseudonym
/// generation.
///
/// Inputs:
/// - `bpk`: Receiver's blinding public key
/// - `ek`: Receiver's public encryption key
/// - `datum`: Identifiable data
/// - `randomness`: Random bytes
///
/// Output:
/// [Blinded data](crate::data_types::BlindedIdentifiableData) such that the
/// datum's handle is blinded for CoPRF evaluation and the datum's value is
/// level-1 encrypted.
pub fn blind_identifiable_datum(
    bpk: &BlindingPublicKey,
    ek: &StoreEncryptionKey,
    datum: &IdentifiableData,
    randomness: &mut Randomness,
) -> Result<BlindedIdentifiableData, Error> {
    // Blind orthonym towards receiver.
    let blinded_handle = BlindedIdentifiableHandle(blind(
        *bpk,
        datum.handle.as_bytes(),
        PSEUDONYMIZATION_CONTEXT.to_vec(),
        randomness,
    )?);

    // Encrypt data value towards receiver.
    let encrypted_data_value = encrypt_data_value(&datum.data_value, ek, randomness)?;

    Ok(BlindedIdentifiableData {
        blinded_handle,
        encrypted_data_value,
    })
}

/// Encrypt a data value towards a data store.
///
/// Inputs:
/// - `data`: The data value to encrypt.
/// - `ek`: The receiver's public encryption key.
/// - `randomness`: Random bytes
///
/// Output:
/// A new [EncryptedDataValue], the encryption of `data`.
fn encrypt_data_value(
    data: &DataValue,
    ek: &StoreEncryptionKey,
    randomness: &mut Randomness,
) -> Result<EncryptedDataValue, Error> {
    let encrypted_data_value = double_hpke::hpke_seal_level_1(data, &ek.0, randomness)?;
    Ok(encrypted_data_value)
}

/// Blind a pseudonymous datum as a first step in pseudonym
/// conversion.
///
/// Inputs:
/// - `store_context`: The data store's long term private state including the pseudonym
///   hardening keys
/// - `bpk`: Receiver's blinding public key
/// - `ek`: Receiver's public encryption key
/// - `datum`: Pseudonymized data
/// - `randomness`: Random bytes
///
/// Output:
/// [Blinded pseudonymized data](BlindedPseudonymizedData) such that the
/// datum's handle is blinded for CoPRF conversion and the datum's value is
/// level-1 encrypted.
pub fn blind_pseudonymized_datum(
    store_context: &StoreContext,
    bpk: &BlindingPublicKey,
    ek: &StoreEncryptionKey,
    datum: &PseudonymizedData,
    randomness: &mut Randomness,
) -> Result<BlindedPseudonymizedData, Error> {
    // Blind recovered raw pseudonym towards receiver.
    let blinded_handle = BlindedPseudonymizedHandle(prepare_blind_convert(
        *bpk,
        store_context.recover_raw_pseudonym(datum.handle)?,
        randomness,
    )?);

    // Encrypt data value towards receiver.
    let encrypted_data_value = encrypt_data_value(&datum.data_value, ek, randomness)?;

    Ok(BlindedPseudonymizedData {
        blinded_handle,
        encrypted_data_value,
    })
}

/// Obliviously pseudonymmize a blinded identifiable datum.
///
/// Inputs:
/// - `coprf_context`: The converter's CoPRF evaluation context
/// - `bpk`: The receiver's blinding public key
/// - `ek`: The receiver's public encryption key
/// - `datum`: A blinded datum output by [`blind_identifiable_datum`]
/// - `randomness`: Random bytes
///
/// Output:
/// [Blinded pseudonymized data](BlindedPseudonymizedData) such that the
///  datum's blinded handle has been obliviously evaluated to a pseudonym and
///  the datum's value has been level-2 encrypted towards the receiver.
pub fn pseudonymize_blinded_datum(
    coprf_context: &CoPRFEvaluatorContext,
    bpk: &BlindingPublicKey,
    ek: &StoreEncryptionKey,
    datum: &BlindedIdentifiableData,
    randomness: &mut Randomness,
) -> Result<BlindedPseudonymizedData, Error> {
    let key = derive_key(
        coprf_context,
        datum.encrypted_data_value.attribute_name.as_bytes(),
    )?;

    // Obliviously generate raw pseudonym.
    let blinded_handle = BlindedPseudonymizedHandle(blind_evaluate(
        key,
        *bpk,
        datum.blinded_handle.0,
        randomness,
    )?);

    // Rerandomize encrypted data value towards receiver.
    let encrypted_data_value = rerandomize_encryption(&datum.encrypted_data_value, ek, randomness)?;

    Ok(BlindedPseudonymizedData {
        blinded_handle,
        encrypted_data_value,
    })
}

/// Rerandomize the encryption of an encrypted data value.
///
/// Inputs:
/// - `data`: The encrypted data value.
/// - `ek`: The receiver's public encryption key.
/// - `randomness`: Random bytes
///
/// Output:
/// A new, rerandomized [EncryptedDataValue].
fn rerandomize_encryption(
    data: &EncryptedDataValue,
    ek: &StoreEncryptionKey,
    randomness: &mut Randomness,
) -> Result<EncryptedDataValue, Error> {
    double_hpke::hpke_seal_level_2(data, &ek.0, randomness)
}

/// Obliviously convert a blinded pseudonymous datum to a given target pseudonym key.
///
/// Inputs:
/// - `coprf_context`: The Converters CoPRF evaluation context
/// - `bpk`: The receiver's blinding public key
/// - `ek`: The receiver's public encryption key
/// - `conversion_target`: Target pseudonym key identifier
/// - `randomness`: Random bytes
///
/// Output:
/// [Blinded pseudonymized data](BlindedPseudonymizedData)such that the
/// datum's pseudonymous handle is converted to the target pseudonym key and
/// the datum's value is level-2 encrypted towards the receiver.
pub fn convert_blinded_datum(
    coprf_context: &CoPRFEvaluatorContext,
    bpk: &BlindingPublicKey,
    ek: &StoreEncryptionKey,
    conversion_target: &[u8],
    datum: &BlindedPseudonymizedData,
    randomness: &mut Randomness,
) -> Result<BlindedPseudonymizedData, Error> {
    // Re-derive original pseudonymization key.
    let key_from = derive_key(
        coprf_context,
        datum.encrypted_data_value.attribute_name.as_bytes(),
    )?;

    // Derive target key.
    let key_to = derive_key(coprf_context, conversion_target)?;

    // Obliviously convert pseudonym.
    let blinded_handle = BlindedPseudonymizedHandle(blind_convert(
        *bpk,
        key_from,
        key_to,
        datum.blinded_handle.0,
        randomness,
    )?);

    // Rerandomize encrypted data value towards receiver.
    let encrypted_data_value = rerandomize_encryption(&datum.encrypted_data_value, ek, randomness)?;

    Ok(BlindedPseudonymizedData {
        blinded_handle,
        encrypted_data_value,
    })
}

/// Finalize a blinded pseudonymous datum for storage or analysis.
///
/// Inputs:
/// - `store_context`: The data store's long term private state including the
///   receiver's coPRF unblinding key, private decryption key, as well as
///   pseudonym hardening key
/// - `datum`: blinded pseudonymous datum output by [`convert_blinded_datum`] or
///   [`pseudonymize_blinded_datum`]
///
/// Output:
/// [Pseudonymized data](PseudonymizedData) such that the datum's pseudonymous
/// handle has been unblinded and hardened and the datum's value has been
/// decrypted.
pub fn finalize_blinded_datum(
    store_context: &StoreContext,
    datum: &BlindedPseudonymizedData,
) -> Result<PseudonymizedData, Error> {
    // Finalize pseudonym for storage.
    let handle = store_context.finalize_pseudonym(datum.blinded_handle)?;

    // Decrypt data value for storage.
    let data_value = decrypt_data_value(&datum.encrypted_data_value, store_context)?;

    Ok(PseudonymizedData { handle, data_value })
}

/// Decrypt an encrypted data value.
///
/// Inputs:
/// - `data`: The value to decrypt.
/// - `store_context`: The data store's long term private state, including in particular its decryption key.
///
/// Output:
/// The decrypted [DataValue] or an [Error] on decryption failure.
fn decrypt_data_value(
    data: &EncryptedDataValue,
    store_context: &StoreContext,
) -> Result<DataValue, Error> {
    double_hpke::hpke_open_level_2(data, &store_context.dk.0)
}