Skip to content

Commit d7da4e2

Browse files
committed
WIP
1 parent 4611888 commit d7da4e2

File tree

7 files changed

+36
-34
lines changed

7 files changed

+36
-34
lines changed

src/crypto/attrs/flattened_encrypted_attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
crypto::{attrs::flattened_protected_attributes::FlattenedAttrName, SealError},
3-
encrypted_table::{ScopedCipherWithCreds, TableAttributes},
3+
encrypted_table::{ScopedZeroKmsCipher, TableAttributes},
44
traits::TableAttribute,
55
};
66
use cipherstash_client::{
@@ -34,7 +34,7 @@ impl FlattenedEncryptedAttributes {
3434
/// Decrypt self, returning a [FlattenedProtectedAttributes].
3535
pub(crate) async fn decrypt_all(
3636
self,
37-
cipher: &ScopedCipherWithCreds,
37+
cipher: &ScopedZeroKmsCipher,
3838
) -> Result<FlattenedProtectedAttributes, SealError> {
3939
let descriptors = self
4040
.attrs

src/crypto/attrs/flattened_protected_attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
flattened_encrypted_attributes::FlattenedEncryptedAttributes,
33
normalized_protected_attributes::NormalizedKey,
44
};
5-
use crate::{crypto::SealError, encrypted_table::{AttributeName, ScopedCipherWithCreds}};
5+
use crate::{crypto::SealError, encrypted_table::{AttributeName, ScopedZeroKmsCipher}};
66
use cipherstash_client::{
77
encryption::{BytesWithDescriptor, Plaintext}, zerokms::EncryptPayload,
88
};
@@ -31,7 +31,7 @@ impl FlattenedProtectedAttributes {
3131
/// The output is a vec of `chunk_into` [FlattenedEncryptedAttributes] objects.
3232
pub(crate) async fn encrypt_all(
3333
self,
34-
cipher: &ScopedCipherWithCreds,
34+
cipher: &ScopedZeroKmsCipher,
3535
chunk_into: usize,
3636
) -> Result<Vec<FlattenedEncryptedAttributes>, SealError> {
3737
let chunk_size = self.0.len() / chunk_into;

src/crypto/sealed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
crypto::attrs::FlattenedEncryptedAttributes,
3-
encrypted_table::{ScopedCipherWithCreds, TableEntry},
3+
encrypted_table::{ScopedZeroKmsCipher, TableEntry},
44
traits::{ReadConversionError, WriteConversionError},
55
Decryptable, Identifiable,
66
};
@@ -68,7 +68,7 @@ impl SealedTableEntry {
6868
pub(crate) async fn unseal_all(
6969
items: Vec<Self>,
7070
spec: UnsealSpec<'_>,
71-
cipher: &ScopedCipherWithCreds,
71+
cipher: &ScopedZeroKmsCipher,
7272
) -> Result<Vec<Unsealed>, SealError> {
7373
let UnsealSpec {
7474
protected_attributes,
@@ -130,7 +130,7 @@ impl SealedTableEntry {
130130
pub(crate) async fn unseal(
131131
self,
132132
spec: UnsealSpec<'_>,
133-
cipher: &ScopedCipherWithCreds,
133+
cipher: &ScopedZeroKmsCipher,
134134
) -> Result<Unsealed, SealError> {
135135
let mut vec = Self::unseal_all(vec![self], spec, cipher).await?;
136136

@@ -203,7 +203,7 @@ impl TryFrom<SealedTableEntry> for HashMap<String, AttributeValue> {
203203

204204
#[cfg(test)]
205205
mod tests {
206-
use crate::encrypted_table::{Cipher, ScopedCipherWithCreds};
206+
use crate::encrypted_table::{ZeroKmsCipher, ScopedZeroKmsCipher};
207207

208208
use super::SealedTableEntry;
209209
use cipherstash_client::{
@@ -214,7 +214,7 @@ mod tests {
214214
use std::{borrow::Cow, sync::Arc};
215215

216216
// FIXME: Use the test cipher from CipherStash Client when that's ready
217-
async fn get_cipher() -> Result<Arc<Cipher>, Box<dyn std::error::Error>> {
217+
async fn get_cipher() -> Result<Arc<ZeroKmsCipher>, Box<dyn std::error::Error>> {
218218
let console_config = ConsoleConfig::builder().with_env().build().into_diagnostic()?;
219219
let zero_kms_config = ZeroKMSConfig::builder()
220220
.decryption_log(true)
@@ -242,7 +242,7 @@ mod tests {
242242
let cipher = get_cipher().await?;
243243
// TODO: Temporary obvs
244244
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
245-
let scoped_cipher = ScopedCipherWithCreds::init(cipher, dataset_id).await.unwrap();
245+
let scoped_cipher = ScopedZeroKmsCipher::init(cipher, dataset_id).await.unwrap();
246246

247247
let results = SealedTableEntry::unseal_all(vec![], spec, &scoped_cipher)
248248
.await

src/crypto/sealer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
attrs::FlattenedProtectedAttributes, b64_encode, format_term_key, SealError, SealedTableEntry, Unsealed, MAX_TERMS_PER_INDEX
33
};
44
use crate::{
5-
encrypted_table::{AttributeName, ScopedCipherWithCreds, TableAttribute, TableAttributes, TableEntry},
5+
encrypted_table::{AttributeName, ScopedZeroKmsCipher, TableAttribute, TableAttributes, TableEntry},
66
traits::PrimaryKeyParts,
77
IndexType,
88
};
@@ -53,7 +53,7 @@ impl RecordsWithTerms {
5353

5454
async fn encrypt(
5555
self,
56-
cipher: &ScopedCipherWithCreds,
56+
cipher: &ScopedZeroKmsCipher,
5757
) -> Result<Vec<Sealed>, SealError> {
5858
let num_records = self.records.len();
5959
let mut pksks = Vec::with_capacity(num_records);
@@ -134,7 +134,7 @@ impl Sealer {
134134
fn index_all_terms<'a>(
135135
records: impl IntoIterator<Item = Sealer>,
136136
protected_attributes: impl AsRef<[Cow<'a, str>]>,
137-
cipher: &ScopedCipherWithCreds,
137+
cipher: &ScopedZeroKmsCipher,
138138
// FIXME: This might need to be a const generic
139139
term_length: usize,
140140
) -> Result<RecordsWithTerms, SealError> {
@@ -210,7 +210,7 @@ impl Sealer {
210210
pub(crate) async fn seal_all<'a>(
211211
records: impl IntoIterator<Item = Sealer>,
212212
protected_attributes: impl AsRef<[Cow<'a, str>]>,
213-
cipher: &ScopedCipherWithCreds,
213+
cipher: &ScopedZeroKmsCipher,
214214
term_length: usize,
215215
) -> Result<Vec<Sealed>, SealError> {
216216
Self::index_all_terms(records, protected_attributes, &cipher, term_length)?
@@ -221,7 +221,7 @@ impl Sealer {
221221
pub(crate) async fn seal<'a>(
222222
self,
223223
protected_attributes: impl AsRef<[Cow<'a, str>]>,
224-
cipher: &ScopedCipherWithCreds,
224+
cipher: &ScopedZeroKmsCipher,
225225
term_length: usize,
226226
) -> Result<Sealed, SealError> {
227227
let mut vec = Self::seal_all([self], protected_attributes, cipher, term_length).await?;

src/encrypted_table/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ impl Deref for Dynamo {
4949
}
5050
}
5151

52-
pub type Cipher = ZeroKMSWithClientKey<AutoRefresh<ServiceCredentials>>;
53-
pub type ScopedCipherWithCreds = ScopedCipher<AutoRefresh<ServiceCredentials>>;
52+
pub type ZeroKmsCipher = ZeroKMSWithClientKey<AutoRefresh<ServiceCredentials>>;
53+
pub type ScopedZeroKmsCipher = ScopedCipher<AutoRefresh<ServiceCredentials>>;
5454

5555
pub struct EncryptedTable<D = Dynamo> {
5656
db: D,
57-
cipher: Arc<Cipher>,
57+
cipher: Arc<ZeroKmsCipher>,
5858
}
5959

6060
impl<D> EncryptedTable<D> {
61-
pub fn cipher(&self) -> Arc<Cipher> {
61+
pub fn cipher(&self) -> Arc<ZeroKmsCipher> {
6262
self.cipher.clone()
6363
}
6464
}
@@ -279,9 +279,10 @@ impl<D> EncryptedTable<D> {
279279
) -> Result<Vec<T>, DecryptError>
280280
where T: Decryptable + Identifiable,
281281
{
282+
// TODO: Decryption _may_ not need to be scoped
282283
// TODO: Temporary obvs
283284
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
284-
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await.unwrap();
285+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id).await.unwrap();
285286

286287
decrypt_all(&scoped_cipher, items).await
287288
}
@@ -292,7 +293,7 @@ impl<D> EncryptedTable<D> {
292293
) -> Result<DynamoRecordPatch, DeleteError> {
293294
// TODO: Temporary obvs
294295
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
295-
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await.unwrap();
296+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id).await.unwrap();
296297

297298
let PrimaryKeyParts { pk, sk } = encrypt_primary_key_parts(&scoped_cipher, delete.primary_key)?;
298299

@@ -323,14 +324,15 @@ impl<D> EncryptedTable<D> {
323324
pub async fn create_put_patch(
324325
&self,
325326
record: PreparedRecord,
327+
dataset_id: Uuid,
326328
// TODO: Make sure the index_predicate is used correctly
327329
index_predicate: impl FnMut(&AttributeName, &TableAttribute) -> bool,
328330
) -> Result<DynamoRecordPatch, PutError> {
329331
let mut seen_sk = HashSet::new();
330332

331333
// TODO: Temporary obvs
332334
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
333-
let indexable_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await.unwrap();
335+
let indexable_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id).await.unwrap();
334336

335337
let PreparedRecord {
336338
protected_attributes,
@@ -405,7 +407,7 @@ impl EncryptedTable<Dynamo> {
405407
{
406408
// TODO: Temporary obvs
407409
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
408-
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await.unwrap();
410+
let scoped_cipher = ScopedZeroKmsCipher::init(self.cipher.clone(), dataset_id).await.unwrap();
409411

410412
let PrimaryKeyParts { pk, sk } =
411413
encrypt_primary_key_parts(&scoped_cipher, PreparedPrimaryKey::new::<T>(k))?;
@@ -484,7 +486,7 @@ impl EncryptedTable<Dynamo> {
484486
/// Take a prepared primary key and encrypt it to get the [`PrimaryKeyParts`] which can be used
485487
/// for retrieval.
486488
fn encrypt_primary_key_parts(
487-
scoped_cipher: &ScopedCipherWithCreds,
489+
scoped_cipher: &ScopedZeroKmsCipher,
488490
prepared_primary_key: PreparedPrimaryKey,
489491
) -> Result<PrimaryKeyParts, PrimaryKeyError> {
490492
let PrimaryKeyParts { mut pk, mut sk } = prepared_primary_key.primary_key_parts;
@@ -500,7 +502,7 @@ fn encrypt_primary_key_parts(
500502
Ok(PrimaryKeyParts { pk, sk })
501503
}
502504

503-
async fn decrypt<T>(scoped_cipher: &ScopedCipherWithCreds, item: HashMap<String, AttributeValue>) -> Result<T, DecryptError>
505+
async fn decrypt<T>(scoped_cipher: &ScopedZeroKmsCipher, item: HashMap<String, AttributeValue>) -> Result<T, DecryptError>
504506
where
505507
T: Decryptable + Identifiable,
506508
{
@@ -512,7 +514,7 @@ where
512514
}
513515

514516
async fn decrypt_all<T>(
515-
scoped_cipher: &ScopedCipherWithCreds,
517+
scoped_cipher: &ScopedZeroKmsCipher,
516518
items: impl IntoIterator<Item = HashMap<String, AttributeValue>>,
517519
) -> Result<Vec<T>, DecryptError>
518520
where

src/encrypted_table/query.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
};
1616
use cipherstash_client::encryption::IndexTerm;
1717

18-
use super::{Dynamo, EncryptedTable, ScopedCipherWithCreds, QueryError, SealError};
18+
use super::{Dynamo, EncryptedTable, ScopedZeroKmsCipher, QueryError, SealError};
1919

2020
/// A builder for a query operation which returns records of type `S`.
2121
/// `B` is the storage backend used to store the data.
@@ -35,7 +35,7 @@ pub struct PreparedQuery {
3535
impl PreparedQuery {
3636
pub async fn encrypt(
3737
self,
38-
scoped_cipher: &ScopedCipherWithCreds,
38+
scoped_cipher: &ScopedZeroKmsCipher,
3939
) -> Result<AttributeValue, QueryError> {
4040
let PreparedQuery {
4141
index_name,
@@ -62,7 +62,7 @@ impl PreparedQuery {
6262
pub async fn send(
6363
self,
6464
table: &EncryptedTable<Dynamo>,
65-
scoped_cipher: &ScopedCipherWithCreds,
65+
scoped_cipher: &ScopedZeroKmsCipher,
6666
) -> Result<Vec<HashMap<String, AttributeValue>>, QueryError> {
6767
let term = self.encrypt(scoped_cipher).await?;
6868

@@ -134,7 +134,7 @@ where
134134
{
135135
// TODO: Temporary obvs
136136
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
137-
let scoped_cipher = ScopedCipherWithCreds::init(self.storage.cipher.clone(), dataset_id).await.unwrap();
137+
let scoped_cipher = ScopedZeroKmsCipher::init(self.storage.cipher.clone(), dataset_id).await.unwrap();
138138

139139
let storage = self.storage;
140140
let query = self.build()?;

tests/query_builder_direct.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cipherstash_dynamodb::{
2-
encrypted_table::ScopedCipherWithCreds, Decryptable, Encryptable, EncryptedTable, Identifiable, QueryBuilder, Searchable
2+
encrypted_table::ScopedZeroKmsCipher, Decryptable, Encryptable, EncryptedTable, Identifiable, QueryBuilder, Searchable
33
};
44
use itertools::Itertools;
55
use serial_test::serial;
@@ -90,7 +90,7 @@ async fn test_query_single_exact() {
9090
.expect("failed to build query");
9191

9292
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
93-
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await.unwrap();
93+
let scoped_cipher = ScopedZeroKmsCipher::init(table.cipher(), dataset_id).await.unwrap();
9494

9595
let term = query
9696
.encrypt(&scoped_cipher)
@@ -133,7 +133,7 @@ async fn test_query_single_prefix() {
133133
.expect("failed to init table");
134134

135135
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
136-
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await.unwrap();
136+
let scoped_cipher = ScopedZeroKmsCipher::init(table.cipher(), dataset_id).await.unwrap();
137137

138138
let query = QueryBuilder::<User>::new()
139139
.starts_with("name", "Dan")
@@ -190,7 +190,7 @@ async fn test_query_compound() {
190190
.expect("failed to build query");
191191

192192
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
193-
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await.unwrap();
193+
let scoped_cipher = ScopedZeroKmsCipher::init(table.cipher(), dataset_id).await.unwrap();
194194

195195
let term = query
196196
.encrypt(&scoped_cipher)

0 commit comments

Comments
 (0)