Skip to content

Commit 7642db9

Browse files
committed
Clean up and refactoring
1 parent 0cb8faf commit 7642db9

2 files changed

Lines changed: 4 additions & 204 deletions

File tree

src/crypto/sealed.rs

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::{attrs::NormalizedProtectedAttributes, SealError, Unsealed};
1717
/// Wrapped to indicate that the value is encrypted
1818
pub struct SealedTableEntry(pub(super) TableEntry);
1919

20+
// TODO: Remove this
2021
pub struct UnsealSpec<'a> {
2122
pub protected_attributes: Cow<'a, [Cow<'a, str>]>,
2223
pub plaintext_attributes: Cow<'a, [Cow<'a, str>]>,
@@ -95,7 +96,7 @@ impl SealedTableEntry {
9596
} else {
9697
let chunk_size = protected_items.len() / unprotected_items.len();
9798

98-
let r1 = protected_items
99+
protected_items
99100
.decrypt_all(cipher)
100101
.await?
101102
.into_iter()
@@ -104,82 +105,13 @@ impl SealedTableEntry {
104105
.chunks(chunk_size)
105106
.into_iter()
106107
.map(|fpa| fpa.into_iter().collect::<NormalizedProtectedAttributes>())
107-
.collect_vec();
108-
109-
dbg!(&r1);
110-
111-
let r = r1
112108
.into_iter()
113109
.zip_eq(unprotected_items.into_iter())
114110
.map(|(fpa, unprotected)| {
115-
Unsealed::new_from_parts(fpa, unprotected)
111+
Ok(Unsealed::new_from_parts(fpa, unprotected))
116112
})
117-
.collect();
118-
119-
Ok(r)
120-
}
121-
122-
/*
123-
let unprotected = unprotected
124-
.into_iter()
125-
.map(|(name, attribute)| {
126-
/*let attr = match name.deref() {
127-
"sk" => "__sk",
128-
_ => name,
129-
};
130-
131-
attributes.get(attr)*/
132-
attribute
133-
})
134-
.collect::<Vec<TableAttribute>>();
135-
136-
unprotected_items.push(unprotected);
113+
.collect()
137114
}
138-
139-
//let decrypted = async_map_somes(decryptable_items, |items| cipher.decrypt(items)).await?;
140-
let decrypted = cipher.decrypt(protected_items).await?;
141-
//let mut default_iter =
142-
// std::iter::repeat_with::<Plaintext, _>(|| &[]).take(plaintext_items.len());
143-
//std::iter::repeat_with::<&[Option<Plaintext>], _>(|| &[]).take(plaintext_items.len());
144-
145-
/*let mut chunks_exact;
146-
//let decrypted_iter: &mut dyn Iterator<Item = &[Option<Plaintext>]> =
147-
let decrypted_iter: &mut dyn Iterator<Item = Plaintext> =
148-
if protected_attributes.len() > 0 {
149-
chunks_exact = decrypted.chunks_exact(protected_attributes.len());
150-
&mut chunks_exact
151-
} else {
152-
&mut default_iter
153-
};*/
154-
155-
// TODO: Handle if protected_attributes is empty
156-
let unsealed = decrypted
157-
.into_iter()
158-
.chunks(protected_attributes.len())
159-
.into_iter()
160-
.zip(unprotected_items.into_iter())
161-
.map(|(decrypted, plaintext_items)| {
162-
let mut unsealed = Unsealed::new();
163-
164-
//println!("----->> EEEE {:?} {:?}", protected_attributes, decrypted);
165-
166-
for (name, plaintext) in decryptable_names.iter().zip(decrypted) {
167-
println!("DECRYPT: Inserting protected attribute: {}", name);
168-
unsealed.add_protected(name.to_string(), plaintext);
169-
}
170-
171-
for (name, plaintext) in
172-
plaintext_attributes.iter().zip(plaintext_items.into_iter())
173-
{
174-
unsealed.add_unprotected(name.to_string(), plaintext.clone());
175-
}
176-
177-
unsealed
178-
})
179-
.collect::<Vec<_>>();
180-
181-
dbg!(&unsealed);
182-
Ok(unsealed)*/
183115
}
184116

185117
/// Unseal the current value and return it's plaintext representation
@@ -208,7 +140,6 @@ impl TryFrom<HashMap<String, AttributeValue>> for SealedTableEntry {
208140
type Error = ReadConversionError;
209141

210142
fn try_from(item: HashMap<String, AttributeValue>) -> Result<Self, Self::Error> {
211-
// FIXME: pk and sk should be AttributeValue and term
212143
let pk = item
213144
.get("pk")
214145
.ok_or(ReadConversionError::NoSuchAttribute("pk".to_string()))?
@@ -250,12 +181,7 @@ impl TryFrom<SealedTableEntry> for HashMap<String, AttributeValue> {
250181
}
251182

252183
item.0.attributes.into_iter().for_each(|(k, v)| {
253-
// FIXME: Why would "sk" ever be in the attributes?
254184
map.insert(
255-
/*match k.as_str() {
256-
"sk" => "__sk".to_string(),
257-
_ => k,
258-
},*/
259185
k,
260186
v.into(),
261187
);

src/crypto/sealer.rs

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct RecordsWithTerms<'a> {
4949

5050
impl<'a> RecordsWithTerms<'a> {
5151
fn new(records: Vec<RecordWithTerms<'a>>, num_protected_attributes: usize) -> Self {
52-
println!("Creating RecordsWithTerms with {} protected attrs", num_protected_attributes);
5352
Self {
5453
num_protected_attributes,
5554
records,
@@ -93,14 +92,8 @@ impl<'a> RecordsWithTerms<'a> {
9392
.collect()
9493

9594
} else {
96-
println!("Protecteds before encryption: {:?}", protected);
9795
let encrypted = protected.encrypt_all(cipher, num_records).await?;
9896

99-
println!("Encrypted len = {}", encrypted.len());
100-
println!("Unprotected len = {}", unprotecteds.len());
101-
println!("Encrypted: {:?}", encrypted);
102-
println!("Unprotected: {:?}", unprotecteds);
103-
10497
encrypted
10598
.into_iter()
10699
.zip_eq(unprotecteds.into_iter())
@@ -235,124 +228,6 @@ impl Sealer {
235228
Self::index_all_terms(records, protected_attributes, cipher, term_length)?
236229
.encrypt(cipher)
237230
.await
238-
/*let records = records.into_iter();
239-
let records_len = records.size_hint().1.unwrap_or(1);
240-
241-
let protected_attributes = protected_attributes.as_ref();
242-
243-
let mut protected = Vec::with_capacity(records_len * protected_attributes.len());
244-
let mut table_entries = Vec::with_capacity(records_len);
245-
246-
for mut record in records {
247-
let mut pk = record.pk;
248-
let mut sk = record.sk;
249-
250-
if record.is_pk_encrypted {
251-
pk = b64_encode(hmac(&pk, None, cipher)?);
252-
}
253-
254-
if record.is_sk_encrypted {
255-
sk = b64_encode(hmac(&sk, Some(pk.as_str()), cipher)?);
256-
}
257-
258-
let type_name = &record.type_name;
259-
260-
for attr in protected_attributes.iter() {
261-
protected.extend(record.unsealed.remove_protected_with_descriptor(attr)?);
262-
}
263-
264-
let terms: Vec<(Cow<'_, str>, IndexType, Vec<u8>)> = record
265-
.unsealed_indexes
266-
.into_iter()
267-
.map(|(attr, index, index_name, index_type)| {
268-
let term = cipher.compound_index(
269-
&CompoundIndex::new(index),
270-
attr,
271-
Some(format!("{}#{}", type_name, index_name)),
272-
term_length,
273-
)?;
274-
275-
Ok::<_, SealError>((index_name, index_type, term))
276-
})
277-
.map(|index_term| match index_term {
278-
Ok((index_name, index_type, IndexTerm::Binary(x))) => {
279-
Ok(vec![(index_name, index_type, x)])
280-
}
281-
Ok((index_name, index_type, IndexTerm::BinaryVec(x))) => Ok(x
282-
.into_iter()
283-
.take(MAX_TERMS_PER_INDEX)
284-
.map(|x| (index_name.clone(), index_type, x))
285-
.collect()),
286-
_ => Err(SealError::InvalidCiphertext("Invalid index term".into())),
287-
})
288-
.flatten_ok()
289-
.try_collect()?;
290-
291-
table_entries.push((
292-
PrimaryKeyParts { pk, sk },
293-
record.unsealed.unprotected().clone(),
294-
terms,
295-
));
296-
}
297-
298-
// Only encrypt if there are actually protected attributes
299-
if !protected_attributes.is_empty() {
300-
let encrypted = cipher
301-
.encrypt(protected.iter().map(|(a, b)| (a, b.as_str())))
302-
.await?;
303-
304-
for (encrypted, (_, attributes, _)) in encrypted
305-
.chunks_exact(protected.len())
306-
.zip(table_entries.iter_mut())
307-
{
308-
for (enc, name) in encrypted.iter().zip(protected.iter()) {
309-
let name: &str = name.1.deref();
310-
311-
println!("Inserting protected attribute: {}", name);
312-
313-
attributes.insert(
314-
String::from(match name {
315-
// TODO: Possibly reinstate or move this
316-
//"pk" => "__pk",
317-
//"sk" => "__sk",
318-
_ => name,
319-
}),
320-
TableAttribute::Bytes(enc.to_vec().map_err(|_| {
321-
SealError::InvalidCiphertext(
322-
"Failed to serialize encrypted record as bytes".into(),
323-
)
324-
})?),
325-
);
326-
}
327-
}
328-
}
329-
330-
let mut output = Vec::with_capacity(table_entries.len());
331-
332-
for (PrimaryKeyParts { pk, sk }, attributes, terms) in table_entries.into_iter() {
333-
let terms = terms
334-
.into_iter()
335-
.enumerate()
336-
.map(|(i, (index_name, index_type, term))| {
337-
let sk = b64_encode(hmac(
338-
&format_term_key(sk.as_str(), &index_name, index_type, i),
339-
Some(pk.as_str()),
340-
cipher,
341-
)?);
342-
343-
Ok::<_, SealError>(Term { sk, value: term })
344-
})
345-
.collect::<Result<_, SealError>>()?;
346-
347-
output.push(dbg!(Sealed {
348-
pk,
349-
sk,
350-
attributes,
351-
terms,
352-
}));
353-
}
354-
355-
Ok(output)*/
356231
}
357232

358233
pub(crate) async fn seal<'a>(
@@ -411,7 +286,6 @@ impl Sealed {
411286
self,
412287
mut index_predicate: impl FnMut(&str, &TableAttribute) -> bool,
413288
) -> (SealedTableEntry, Vec<SealedTableEntry>) {
414-
// TODO: Is this normalization necessary? - try removing it
415289
let root_attributes = self.attributes;
416290

417291
let index_attributes: TableAttributes = root_attributes

0 commit comments

Comments
 (0)