Skip to content

Commit 6fec90b

Browse files
author
Bennett Hardwick
committed
Hmac all sort keys
1 parent 74abffe commit 6fec90b

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/crypto/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn all_index_keys<E: Searchable + Encryptable>(sort_key: &str) -> Vec
5656
.collect()
5757
}
5858

59-
pub(crate) fn encrypt_partition_key<C>(
59+
pub(crate) fn hmac<C>(
6060
value: &str,
6161
cipher: &Encryption<C>,
6262
) -> Result<String, EncryptionError>

src/crypto/sealer.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{encrypt_partition_key, SealError, Sealed, Unsealed, MAX_TERMS_PER_INDEX};
1+
use super::{hmac, SealError, Sealed, Unsealed, MAX_TERMS_PER_INDEX};
22
use crate::{
33
encrypted_table::{TableAttribute, TableEntry},
44
traits::PrimaryKeyParts,
@@ -64,11 +64,11 @@ impl<T> Sealer<T> {
6464
let mut sk = self.inner.sort_key();
6565

6666
if T::is_partition_key_encrypted() {
67-
pk = encrypt_partition_key(&pk, cipher)?;
67+
pk = hmac(&pk, cipher)?;
6868
}
6969

7070
if T::is_sort_key_encrypted() {
71-
sk = encrypt_partition_key(&sk, cipher)?;
71+
sk = hmac(&sk, cipher)?;
7272
}
7373

7474
let mut table_entry = TableEntry::new_with_attributes(
@@ -112,15 +112,14 @@ impl<T> Sealer<T> {
112112
})
113113
.ok_or(SealError::MissingAttribute(index_name.to_string()))
114114
.and_then(|(attr, index, index_name)| {
115-
cipher
116-
.compound_index(
117-
&CompoundIndex::new(index),
118-
attr,
119-
Some(format!("{}#{}", T::type_name(), index_name)),
120-
term_length,
121-
)
122-
.map_err(SealError::CryptoError)
123-
.map(|result| (index_name, result))
115+
let term = cipher.compound_index(
116+
&CompoundIndex::new(index),
117+
attr,
118+
Some(format!("{}#{}", T::type_name(), index_name)),
119+
term_length,
120+
)?;
121+
122+
Ok::<_, SealError>((index_name, term))
124123
})
125124
})
126125
.map(|index_term| match index_term {
@@ -138,16 +137,16 @@ impl<T> Sealer<T> {
138137
.enumerate()
139138
.take(MAX_TERMS_PER_INDEX)
140139
.map(|(i, (index_name, term))| {
141-
Sealed(
140+
Ok(Sealed(
142141
table_entry
143142
.clone()
144143
.set_term(hex::encode(term))
145144
// TODO: HMAC the sort key, too (users#index_name#pk)
146-
.set_sk(format!("{}#{}#{}", &sk, index_name, i)),
147-
)
145+
.set_sk(hmac(&format!("{}#{}#{}", &sk, index_name, i), &cipher)?),
146+
))
148147
})
149-
.chain(once(Sealed(table_entry.clone())))
150-
.collect();
148+
.chain(once(Ok(Sealed(table_entry.clone()))))
149+
.collect::<Result<_, SealError>>()?;
151150

152151
Ok((PrimaryKeyParts { pk, sk }, table_entries))
153152
}

src/encrypted_table/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum PutError {
4545
Seal(#[from] SealError),
4646
#[error("CryptoError: {0}")]
4747
Crypto(#[from] CryptoError),
48+
#[error("Encryption Error: {0}")]
49+
Encryption(#[from] EncryptionError),
4850
}
4951

5052
#[derive(Error, Debug)]
@@ -123,11 +125,11 @@ impl EncryptedTable {
123125
let PrimaryKeyParts { mut pk, mut sk } = k.into().into_parts::<T>();
124126

125127
if T::is_partition_key_encrypted() {
126-
pk = encrypt_partition_key(&pk, &self.cipher)?;
128+
pk = hmac(&pk, &self.cipher)?;
127129
}
128130

129131
if T::is_sort_key_encrypted() {
130-
sk = encrypt_partition_key(&sk, &self.cipher)?;
132+
sk = hmac(&sk, &self.cipher)?;
131133
}
132134

133135
Ok(PrimaryKeyParts { pk, sk })
@@ -164,9 +166,13 @@ impl EncryptedTable {
164166
) -> Result<(), DeleteError> {
165167
let PrimaryKeyParts { pk, sk } = self.get_primary_key_parts::<E>(k)?;
166168

167-
let sk_to_delete = all_index_keys::<E>(&sk).into_iter().chain([sk]);
169+
let sk_to_delete = all_index_keys::<E>(&sk)
170+
.into_iter()
171+
.map(|x| hmac(&x, &self.cipher))
172+
.chain([Ok(sk)])
173+
.collect::<Result<Vec<_>, _>>()?;
168174

169-
let transact_items = sk_to_delete.map(|sk| {
175+
let transact_items = sk_to_delete.into_iter().map(|sk| {
170176
TransactWriteItem::builder()
171177
.delete(
172178
Delete::builder()
@@ -220,6 +226,8 @@ impl EncryptedTable {
220226
}
221227

222228
for index_sk in all_index_keys::<T>(&sk) {
229+
let index_sk = hmac(&index_sk, &self.cipher)?;
230+
223231
if seen_sk.contains(&index_sk) {
224232
continue;
225233
}

0 commit comments

Comments
 (0)