Skip to content

Commit aeb558d

Browse files
committed
WIP
1 parent 590c399 commit aeb558d

File tree

9 files changed

+60
-52
lines changed

9 files changed

+60
-52
lines changed

Cargo.lock

Lines changed: 2 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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::{ScopedCipher, TableAttributes},
3+
encrypted_table::{ScopedCipherWithCreds, 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: &ScopedCipher,
37+
cipher: &ScopedCipherWithCreds,
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, ScopedCipher}};
5+
use crate::{crypto::SealError, encrypted_table::{AttributeName, ScopedCipherWithCreds}};
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: &ScopedCipher,
34+
cipher: &ScopedCipherWithCreds,
3535
chunk_into: usize,
3636
) -> Result<Vec<FlattenedEncryptedAttributes>, SealError> {
3737
let chunk_size = self.0.len() / chunk_into;

src/crypto/sealed.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
crypto::attrs::FlattenedEncryptedAttributes,
3-
encrypted_table::{ScopedCipher, TableEntry},
3+
encrypted_table::{ScopedCipherWithCreds, 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: &ScopedCipher,
71+
cipher: &ScopedCipherWithCreds,
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: &ScopedCipher,
133+
cipher: &ScopedCipherWithCreds,
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, ScopedCipher};
206+
use crate::encrypted_table::{Cipher, ScopedCipherWithCreds};
207207

208208
use super::SealedTableEntry;
209209
use cipherstash_client::{
@@ -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 = ScopedCipher::init(cipher, dataset_id).await;
245+
let scoped_cipher = ScopedCipherWithCreds::init(cipher, dataset_id).await;
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, ScopedCipher, TableAttribute, TableAttributes, TableEntry},
5+
encrypted_table::{AttributeName, ScopedCipherWithCreds, TableAttribute, TableAttributes, TableEntry},
66
traits::PrimaryKeyParts,
77
IndexType,
88
};
@@ -53,7 +53,7 @@ impl RecordsWithTerms {
5353

5454
async fn encrypt(
5555
self,
56-
cipher: &ScopedCipher,
56+
cipher: &ScopedCipherWithCreds,
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: &ScopedCipher,
137+
cipher: &ScopedCipherWithCreds,
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: &ScopedCipher,
213+
cipher: &ScopedCipherWithCreds,
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: &ScopedCipher,
224+
cipher: &ScopedCipherWithCreds,
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: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use cipherstash_client::{
2121
config::{console_config::ConsoleConfig, cts_config::CtsConfig, zero_kms_config::ZeroKMSConfig}, credentials::{
2222
auto_refresh::AutoRefresh,
2323
service_credentials::ServiceCredentials,
24-
}, encryption::ScopedZeroKMSCipher, zerokms::{ZeroKMS, ZeroKMSWithClientKey}
24+
}, encryption::ScopedCipher, zerokms::{ZeroKMS, ZeroKMSWithClientKey}
2525
};
2626
use log::info;
2727
use uuid::Uuid;
@@ -50,13 +50,11 @@ impl Deref for Dynamo {
5050
}
5151

5252
pub type Cipher = ZeroKMSWithClientKey<AutoRefresh<ServiceCredentials>>;
53-
pub type ScopedCipher = ScopedZeroKMSCipher<AutoRefresh<ServiceCredentials>>;
53+
pub type ScopedCipherWithCreds = ScopedCipher<AutoRefresh<ServiceCredentials>>;
5454

5555
pub struct EncryptedTable<D = Dynamo> {
5656
db: D,
5757
cipher: Arc<Cipher>,
58-
// FIXME: This is temporary
59-
dataset_root_key: [u8; 32],
6058
}
6159

6260
impl<D> EncryptedTable<D> {
@@ -92,7 +90,6 @@ impl EncryptedTable<Headless> {
9290
Ok(Self {
9391
db: Headless,
9492
cipher: Arc::new(cipher),
95-
dataset_root_key: [0; 32], // TODO
9693
})
9794
}
9895
}
@@ -284,7 +281,7 @@ impl<D> EncryptedTable<D> {
284281
{
285282
// TODO: Temporary obvs
286283
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
287-
let scoped_cipher = ScopedCipher::init(self.cipher.clone(), dataset_id).await;
284+
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await?;
288285

289286
decrypt_all(&scoped_cipher, items).await
290287
}
@@ -295,7 +292,7 @@ impl<D> EncryptedTable<D> {
295292
) -> Result<DynamoRecordPatch, DeleteError> {
296293
// TODO: Temporary obvs
297294
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
298-
let scoped_cipher = ScopedCipher::init(self.cipher.clone(), dataset_id).await;
295+
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await;
299296

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

@@ -333,7 +330,7 @@ impl<D> EncryptedTable<D> {
333330

334331
// TODO: Temporary obvs
335332
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
336-
let indexable_cipher = ScopedCipher::init(self.cipher.clone(), dataset_id).await;
333+
let indexable_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await;
337334

338335
let PreparedRecord {
339336
protected_attributes,
@@ -354,8 +351,6 @@ impl<D> EncryptedTable<D> {
354351

355352
let PrimaryKeyParts { pk, sk } = sealed.primary_key();
356353

357-
println!("IN CREATE_PUT_PATCH {:?} {:?}", pk, sk);
358-
359354
let (root, index_entries) = sealed.into_table_entries(index_predicate);
360355

361356
seen_sk.insert(root.inner().sk.clone());
@@ -401,8 +396,6 @@ impl EncryptedTable<Dynamo> {
401396
db,
402397
},
403398
cipher: table.cipher,
404-
// FIXME: This is temporary
405-
dataset_root_key: table.dataset_root_key,
406399
})
407400
}
408401

@@ -412,14 +405,12 @@ impl EncryptedTable<Dynamo> {
412405
{
413406
// TODO: Temporary obvs
414407
let dataset_id = Uuid::parse_str("93e10481-2692-4d65-a619-37e36a496e64").unwrap();
415-
let scoped_cipher = ScopedCipher::init(self.cipher.clone(), dataset_id).await;
408+
let scoped_cipher = ScopedCipherWithCreds::init(self.cipher.clone(), dataset_id).await;
416409

417410
let PrimaryKeyParts { pk, sk } =
418411
encrypt_primary_key_parts(&scoped_cipher, PreparedPrimaryKey::new::<T>(k))?;
419412

420413

421-
println!("IN GET {:?} {:?}", pk, sk);
422-
423414
let result = self
424415
.db
425416
.get_item()
@@ -493,7 +484,7 @@ impl EncryptedTable<Dynamo> {
493484
/// Take a prepared primary key and encrypt it to get the [`PrimaryKeyParts`] which can be used
494485
/// for retrieval.
495486
fn encrypt_primary_key_parts(
496-
scoped_cipher: &ScopedCipher,
487+
scoped_cipher: &ScopedCipherWithCreds,
497488
prepared_primary_key: PreparedPrimaryKey,
498489
) -> Result<PrimaryKeyParts, PrimaryKeyError> {
499490
let PrimaryKeyParts { mut pk, mut sk } = prepared_primary_key.primary_key_parts;
@@ -509,7 +500,7 @@ fn encrypt_primary_key_parts(
509500
Ok(PrimaryKeyParts { pk, sk })
510501
}
511502

512-
async fn decrypt<T>(scoped_cipher: &ScopedCipher, item: HashMap<String, AttributeValue>) -> Result<T, DecryptError>
503+
async fn decrypt<T>(scoped_cipher: &ScopedCipherWithCreds, item: HashMap<String, AttributeValue>) -> Result<T, DecryptError>
513504
where
514505
T: Decryptable + Identifiable,
515506
{
@@ -521,7 +512,7 @@ where
521512
}
522513

523514
async fn decrypt_all<T>(
524-
scoped_cipher: &ScopedCipher,
515+
scoped_cipher: &ScopedCipherWithCreds,
525516
items: impl IntoIterator<Item = HashMap<String, AttributeValue>>,
526517
) -> Result<Vec<T>, DecryptError>
527518
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, ScopedCipher, QueryError, SealError};
18+
use super::{Dynamo, EncryptedTable, ScopedCipherWithCreds, 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: &ScopedCipher,
38+
scoped_cipher: &ScopedCipherWithCreds,
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: &ScopedCipher,
65+
scoped_cipher: &ScopedCipherWithCreds,
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 = ScopedCipher::init(self.storage.cipher.clone(), dataset_id).await;
137+
let scoped_cipher = ScopedCipherWithCreds::init(self.storage.cipher.clone(), dataset_id).await;
138138

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

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
pub mod crypto;
77
pub mod encrypted_table;
88
pub mod traits;
9+
use encrypted_table::DynamoRecordPatch;
910
pub use encrypted_table::{EncryptedTable, QueryBuilder};
1011
pub use traits::{
1112
Decryptable, Encryptable, Identifiable, IndexType, Pk, PkSk, PrimaryKey, Searchable,
@@ -22,3 +23,29 @@ pub use cipherstash_dynamodb_derive::{Decryptable, Encryptable, Identifiable, Se
2223
pub use cipherstash_client::encryption;
2324

2425
pub type Key = [u8; 32];
26+
27+
28+
pub struct Put<T> {
29+
record: T,
30+
}
31+
32+
impl<T> Put<T> where T: Encryptable + Identifiable {
33+
pub fn new(record: T) -> Self {
34+
Self { record }
35+
}
36+
}
37+
38+
pub struct Cipher {}
39+
40+
impl Cipher {
41+
}
42+
43+
pub trait IntoPatch<Op> {
44+
fn seal(&self, op: Op) -> Result<DynamoRecordPatch, Error>;
45+
}
46+
47+
impl<T> IntoPatch<Put<T>> for Cipher {
48+
fn seal(&self, op: Put<T>) -> Result<DynamoRecordPatch, Error> {
49+
unimplemented!()
50+
}
51+
}

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::ScopedCipher, Decryptable, Encryptable, EncryptedTable, Identifiable, QueryBuilder, Searchable
2+
encrypted_table::ScopedCipherWithCreds, 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 = ScopedCipher::init(table.cipher(), dataset_id).await;
93+
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await;
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 = ScopedCipher::init(table.cipher(), dataset_id).await;
136+
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await;
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 = ScopedCipher::init(table.cipher(), dataset_id).await;
193+
let scoped_cipher = ScopedCipherWithCreds::init(table.cipher(), dataset_id).await;
194194

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

0 commit comments

Comments
 (0)