cipherstash-client 0.12.5

The official CipherStash SDK
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Module for CipherStash encryption schemes and indexers
pub mod compound_indexer;
mod errors;
mod json_indexer;
mod match_indexer;
mod ore_indexer;
mod plaintext;
mod text;
mod unique_indexer;

use self::compound_indexer::{
    accumulator::Accumulator, composable_plaintext::ComposablePlaintext, ComposableIndex,
    CompoundIndex,
};
use crate::{
    credentials::{
        service_credentials::{ServiceCredentials, ServiceToken},
        Credentials,
    },
    zerokms::{EncryptPayload, EncryptedRecord, ZeroKMSWithClientKey},
};
use zerokms_protocol::cipherstash_config::{column::IndexType, operator::Operator, ColumnType};

// Re-exports
pub use self::{
    errors::{EncryptionError, TypeParseError},
    json_indexer::containment::JsonContainmentIndexer,
    match_indexer::MatchIndexer,
    ore_indexer::OreIndexer,
    plaintext::{
        BytesWithDescriptor, Plaintext, PlaintextNullVariant, PlaintextTarget, TryFromPlaintext,
    },
    unique_indexer::UniqueIndexer,
};

pub struct Encryption<C: Credentials<Token = ServiceToken> = ServiceCredentials> {
    // This field is public in order for the Driver to be able to cache
    // configuration and avoid a round trip to Vitur for every database
    // connection.
    pub root_key: [u8; 32],
    client: ZeroKMSWithClientKey<C>,
}

impl<Creds: Credentials<Token = ServiceToken>> Encryption<Creds> {
    pub fn new(root_key: [u8; 32], client: ZeroKMSWithClientKey<Creds>) -> Self {
        Self { root_key, client }
    }

    pub async fn encrypt<T: Into<BytesWithDescriptor>>(
        &self,
        items: impl IntoIterator<Item = T>,
    ) -> Result<Vec<EncryptedRecord>, EncryptionError> {
        let timer = cipherstash_stats::ENCRYPTION_DURATION.start_timer();

        let result = self.encrypt_impl(items).await;

        match result {
            Ok(ref output) => {
                timer.stop_and_record();
                cipherstash_stats::ENCRYPTIONS.inc_by(output.len() as u64);
            }
            Err(_) => {
                cipherstash_stats::ENCRYPTION_ERRORS.inc();
            }
        };

        result
    }

    #[inline(always)]
    pub async fn encrypt_impl<T: Into<BytesWithDescriptor>>(
        &self,
        items: impl IntoIterator<Item = T>,
    ) -> Result<Vec<EncryptedRecord>, EncryptionError> {
        let payloads: Vec<BytesWithDescriptor> = items.into_iter().map(Into::into).collect();

        Ok(self
            .client
            .encrypt(payloads.iter().map(EncryptPayload::from), None)
            .await?)
    }

    pub async fn encrypt_single(
        &self,
        target: PlaintextTarget,
    ) -> Result<EncryptedRecord, EncryptionError> {
        let timer = cipherstash_stats::ENCRYPTION_DURATION.start_timer();

        let result = self.encrypt_single_impl(target).await;

        match result {
            Ok(_) => {
                timer.stop_and_record();
                cipherstash_stats::ENCRYPTIONS.inc();
            }
            Err(_) => {
                cipherstash_stats::ENCRYPTION_ERRORS.inc();
            }
        };
        result
    }

    #[inline(always)]
    pub async fn encrypt_single_impl(
        &self,
        target: PlaintextTarget,
    ) -> Result<EncryptedRecord, EncryptionError> {
        let payload = target.payload();

        let ciphertext = self
            .client
            .encrypt_single(EncryptPayload::from(&payload), None)
            .await?;

        Ok(ciphertext)
    }

    pub async fn decrypt_single(
        &self,
        ciphertext: EncryptedRecord,
    ) -> Result<Plaintext, EncryptionError> {
        let timer = cipherstash_stats::DECRYPTION_DURATION.start_timer();

        let result = self.decrypt_single_impl(ciphertext).await;
        match result {
            Ok(_) => {
                timer.stop_and_record();
                cipherstash_stats::DECRYPTIONS.inc();
            }
            Err(_) => {
                cipherstash_stats::DECRYPTION_ERRORS.inc();
            }
        };
        result
    }

    #[inline(always)]
    pub async fn decrypt_single_impl(
        &self,
        ciphertext: EncryptedRecord,
    ) -> Result<Plaintext, EncryptionError> {
        let decrypted = self.client.decrypt_single(ciphertext).await?;
        Ok(Plaintext::from_slice(&decrypted)?)
    }

    /// Like `decrypt` but doesn't expect all values to be decryptable.
    /// This only means that a given input is `None` or the slice is not a
    /// serialized [`EncryptedRecord`].
    /// In the future this could also cover cases
    /// where the caller is not _authorized_ to decrypt a given value.
    ///
    /// As it stands, this function will return an Error if any valid ciphertexts
    /// fail to decrypt.
    ///
    /// Items in the returned vec wil be in the same order as the input
    /// but any values that are unable to be decrypted will be returned as `None`.
    ///
    /// Encrypted records must be encoded with hex.
    pub async fn maybe_decrypt_hex<C>(
        &self,
        ciphertexts: impl IntoIterator<Item = Option<C>>,
    ) -> Result<Vec<Option<Plaintext>>, EncryptionError>
    where
        C: AsRef<[u8]>,
    {
        let timer = cipherstash_stats::DECRYPTION_DURATION.start_timer();

        let result = self.maybe_decrypt_hex_impl(ciphertexts).await;

        match result {
            Ok(ref output) => {
                timer.stop_and_record();
                cipherstash_stats::DECRYPTIONS.inc_by(output.len() as u64);
            }
            Err(_) => {
                cipherstash_stats::DECRYPTION_ERRORS.inc();
            }
        };

        result
    }

    #[inline(always)]
    pub async fn maybe_decrypt_hex_impl<I, C>(
        &self,
        ciphertexts: I,
    ) -> Result<Vec<Option<Plaintext>>, EncryptionError>
    where
        I: IntoIterator<Item = Option<C>>,
        C: AsRef<[u8]>,
    {
        let records: (Vec<bool>, Vec<EncryptedRecord>) =
            ciphertexts
                .into_iter()
                .fold(Default::default(), |(mut all, mut target), hex_str| {
                    if let Some(rec) = hex_str
                        .map(hex::decode)
                        .transpose()
                        .unwrap_or(None)
                        .and_then(|bytes| EncryptedRecord::from_slice(&bytes).ok())
                    {
                        target.push(rec);
                        all.push(true);
                    } else {
                        all.push(false);
                    }
                    (all, target)
                });

        let mut results = self
            .client
            .decrypt(records.1)
            .await?
            .into_iter()
            .map(|bytes| Plaintext::from_slice(&bytes));

        Ok(records
            .0
            .iter()
            .map(|valid| {
                if *valid {
                    results.next().transpose()
                } else {
                    Ok(None)
                }
            })
            .collect::<Result<Vec<Option<Plaintext>>, _>>()?)
    }

    pub async fn decrypt(
        &self,
        ciphertexts: impl IntoIterator<Item = EncryptedRecord>,
    ) -> Result<Vec<Plaintext>, EncryptionError> {
        let timer = cipherstash_stats::DECRYPTION_DURATION.start_timer();

        let result = self.decrypt_impl(ciphertexts).await;

        match result {
            Ok(ref output) => {
                timer.stop_and_record();
                cipherstash_stats::DECRYPTIONS.inc_by(output.len() as u64);
            }
            Err(_) => {
                cipherstash_stats::DECRYPTION_ERRORS.inc();
            }
        };
        result
    }

    #[inline(always)]
    pub async fn decrypt_impl(
        &self,
        ciphertexts: impl IntoIterator<Item = EncryptedRecord>,
    ) -> Result<Vec<Plaintext>, EncryptionError> {
        Ok(self
            .client
            .decrypt(ciphertexts)
            .await?
            .iter()
            .map(|bytes| Plaintext::from_slice(bytes))
            .collect::<Result<Vec<Plaintext>, _>>()?)
    }

    pub fn index(
        &self,
        value: &Plaintext,
        index_type: &IndexType,
    ) -> Result<IndexTerm, EncryptionError> {
        match index_type {
            IndexType::Ore => OreIndexer::new(self.root_key)?.encrypt(value),
            IndexType::Unique { token_filters } => {
                UniqueIndexer::new(self.root_key, token_filters.clone()).encrypt(value)
            }
            IndexType::Match {
                tokenizer,
                token_filters,
                k,
                m,
                ..
            } => MatchIndexer::new(
                self.root_key,
                tokenizer.clone(),
                token_filters.to_vec(),
                *k,
                *m,
            )
            .encrypt(value),
            IndexType::SteVec { prefix } => {
                JsonContainmentIndexer::new(self.root_key, prefix.clone()).encrypt(
                    value.into_json().ok_or(EncryptionError::IndexingError(
                        "expected JSONB plaintext".into(),
                    ))?,
                )
            }
        }
    }

    pub fn index_all(&self, target: &PlaintextTarget) -> Result<Vec<IndexTerm>, EncryptionError> {
        let mut indexes = vec![];

        for index in target.config().indexes.iter() {
            indexes.push(self.index(&target.plaintext, &index.index_type)?);
        }

        Ok(indexes)
    }

    pub fn compound_index(
        &self,
        index: &CompoundIndex<impl ComposableIndex + Send>,
        input: impl Into<ComposablePlaintext>,
        salt: Option<impl AsRef<[u8]>>,
        term_length: usize,
    ) -> Result<IndexTerm, EncryptionError> {
        let accumulator = salt
            .map(|s| Accumulator::from_salt(s.as_ref()))
            .unwrap_or_else(Accumulator::empty);

        let term = index
            .compose_index(self.root_key, input.into(), accumulator)?
            .truncate(term_length)?;

        Ok(term.into())
    }

    pub fn compound_query(
        &self,
        index: &CompoundIndex<impl ComposableIndex + Send>,
        input: impl Into<ComposablePlaintext>,
        salt: Option<impl AsRef<[u8]>>,
        term_length: usize,
    ) -> Result<IndexTerm, EncryptionError> {
        let accumulator = salt
            .map(|s| Accumulator::from_salt(s.as_ref()))
            .unwrap_or_else(Accumulator::empty);

        let term = index
            .compose_query(self.root_key, input.into(), accumulator)?
            .exactly_one()?
            .truncate(term_length)?;

        Ok(term.try_into()?)
    }

    pub fn index_for_operator(
        &self,
        value: &Plaintext,
        index_type: &IndexType,
        operator: &Operator,
        cast_type: &ColumnType,
    ) -> Result<IndexTerm, EncryptionError> {
        // Check if index supports op
        if !index_type.supports(operator, cast_type) {
            return Err(EncryptionError::IndexingError(format!(
                "Unsupported operator ({}) for Index {:?}",
                operator.as_str(),
                index_type
            )));
        }
        match index_type {
            IndexType::Ore => OreIndexer::new(self.root_key)?.encrypt_for_query(value),
            // Unique and Match don't work any differently for queries
            IndexType::Unique { .. } => self.index(value, index_type),
            IndexType::Match { .. } => self.index(value, index_type),
            IndexType::SteVec { .. } => self.index(value, index_type),
        }
    }
}

#[derive(Debug, Eq, Clone, PartialEq)]
pub enum IndexTerm {
    Binary(Vec<u8>),
    BinaryVec(Vec<Vec<u8>>),
    BitMap(Vec<u16>),
    /// Represents a full ORE Ciphertext (both left and right)
    OreFull(Vec<u8>),
    /// Array of FullOre terms
    OreArray(Vec<Vec<u8>>),
    /// Represents a Left ORE Ciphertext
    OreLeft(Vec<u8>),
    /// NULL index field
    Null,
}

impl IndexTerm {
    pub fn as_binary(self) -> Option<Vec<u8>> {
        if let Self::Binary(x) = self {
            Some(x)
        } else {
            None
        }
    }

    /// Get the index term as a vector of binary terms.
    /// If the term is a single binary term, it will be wrapped in a vec.
    pub fn as_binary_vec(self) -> Option<Vec<Vec<u8>>> {
        match self {
            Self::BinaryVec(x) => Some(x),
            Self::Binary(x) => Some(vec![x]),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{config::zero_kms_config::ZeroKMSConfig, zerokms::ZeroKMS};
    use zerokms_protocol::cipherstash_config::ColumnConfig;

    fn create_test_encryption() -> Encryption {
        let root_key = [0; 32];

        let config = ZeroKMSConfig::builder()
            .with_env()
            .build_with_client_key()
            .expect("Unable to load Vitur config");

        let vitur_client = ZeroKMS::new_with_client_key(
            &config.base_url(),
            config.credentials(),
            config.decryption_log_path().as_deref(),
            config.client_key(),
        );

        Encryption::new(root_key, vitur_client)
    }

    // Ignore for now because these require a real Vitur instance running
    #[ignore]
    #[tokio::test]
    async fn test_round_trip_single() -> Result<(), Box<dyn std::error::Error>> {
        let encryption = create_test_encryption();
        let value = "hello cipher";
        let target = PlaintextTarget::new(value, ColumnConfig::build("name"), None);

        let ciphertext = encryption.encrypt_single(target).await?;
        assert_eq!(
            Plaintext::new(value),
            encryption.decrypt_single(ciphertext).await?
        );

        Ok(())
    }

    // Ignore for now because these require a real Vitur instance running
    /*#[ignore]
    #[tokio::test]
    async fn test_round_trip_bulk_decrypt() -> Result<(), Box<dyn std::error::Error>> {
        let encryption = create_test_encryption();

        let plaintexts = vec!["a".into(), "b".into(), "c".into()];

        let mut ciphertexts = vec![];

        for (i, plaintext) in plaintexts.iter().enumerate() {
            ciphertexts.push(
                encryption
                    .encrypt_single(plaintext, &format!("value-{i}"))
                    .await?,
            );
        }

        assert_eq!(plaintexts, encryption.decrypt(ciphertexts).await?);

        Ok(())
    }

    // Ignore for now because these require a real Vitur instance running
    #[ignore]
    #[tokio::test]
    async fn test_round_trip_bulk_maybe_decrypt() -> Result<(), Box<dyn std::error::Error>> {
        let encryption = create_test_encryption();

        let p1 = "a".into();
        let p2 = "b".into();
        let p3 = "c".into();

        let mut ciphertexts = vec![];

        for (i, plaintext) in vec![&p1, &p2, &p3].into_iter().enumerate() {
            ciphertexts.push(Some(hex::encode(
                encryption
                    .encrypt_single(plaintext, &format!("value-{i}"))
                    .await?
                    .to_vec()
                    .unwrap(),
            )));

            ciphertexts.push(Some(format!("not-encrypted-{i}")));
        }

        ciphertexts.push(None);

        assert_eq!(
            vec![Some(p1), None, Some(p2), None, Some(p3), None, None],
            encryption.maybe_decrypt_hex(ciphertexts).await?
        );

        Ok(())
    }*/

    // TODO: Test the other functions and indexers
}