Skip to content

Commit 01d2513

Browse files
author
Bennett Hardwick
committed
Salt sk with pk
1 parent 4d5c7ae commit 01d2513

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/crypto/mod.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ mod unsealed;
55
use crate::traits::{Encryptable, ReadConversionError, Searchable, WriteConversionError};
66
use cipherstash_client::{
77
credentials::{vitur_credentials::ViturToken, Credentials},
8-
encryption::{Encryption, EncryptionError, Plaintext, TypeParseError},
9-
schema::column::Index,
8+
encryption::{
9+
compound_indexer::{CompoundIndex, ExactIndex},
10+
Encryption, EncryptionError, Plaintext, TypeParseError,
11+
},
1012
};
1113
use thiserror::Error;
1214

@@ -56,15 +58,26 @@ pub(crate) fn all_index_keys<E: Searchable + Encryptable>(sort_key: &str) -> Vec
5658
.collect()
5759
}
5860

59-
pub(crate) fn hmac<C>(value: &str, cipher: &Encryption<C>) -> Result<String, EncryptionError>
61+
pub(crate) fn hmac<C>(
62+
field: &str,
63+
value: &str,
64+
salt: Option<&str>,
65+
cipher: &Encryption<C>,
66+
) -> Result<String, EncryptionError>
6067
where
6168
C: Credentials<Token = ViturToken>,
6269
{
6370
let plaintext = Plaintext::Utf8Str(Some(value.to_string()));
64-
let index_type = Index::new_unique().index_type;
71+
let index = CompoundIndex::new(ExactIndex::new(field, vec![]));
6572

6673
cipher
67-
.index(&plaintext, &index_type)?
74+
.compound_index(
75+
&index,
76+
plaintext,
77+
// passing None here results in no terms so pass an empty string
78+
Some(salt.unwrap_or("")),
79+
16,
80+
)?
6881
.as_binary()
6982
.map(hex::encode)
7083
.ok_or(EncryptionError::IndexingError(

src/crypto/sealer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = hmac(&pk, cipher)?;
67+
pk = hmac("pk", &pk, None, cipher)?;
6868
}
6969

7070
if T::is_sort_key_encrypted() {
71-
sk = hmac(&sk, cipher)?;
71+
sk = hmac("sk", &sk, Some(pk.as_str()), cipher)?;
7272
}
7373

7474
let mut table_entry = TableEntry::new_with_attributes(
@@ -142,7 +142,12 @@ impl<T> Sealer<T> {
142142
.clone()
143143
.set_term(hex::encode(term))
144144
// TODO: HMAC the sort key, too (users#index_name#pk)
145-
.set_sk(hmac(&format!("{}#{}#{}", &sk, index_name, i), cipher)?),
145+
.set_sk(hmac(
146+
"sk",
147+
&format!("{}#{}#{}", &sk, index_name, i),
148+
Some(pk.as_str()),
149+
cipher,
150+
)?),
146151
))
147152
})
148153
.chain(once(Ok(Sealed(table_entry.clone()))))

src/encrypted_table/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ impl EncryptedTable {
125125
let PrimaryKeyParts { mut pk, mut sk } = k.into().into_parts::<T>();
126126

127127
if T::is_partition_key_encrypted() {
128-
pk = hmac(&pk, &self.cipher)?;
128+
pk = hmac("pk", &pk, None, &self.cipher)?;
129129
}
130130

131131
if T::is_sort_key_encrypted() {
132-
sk = hmac(&sk, &self.cipher)?;
132+
sk = hmac("sk", &sk, Some(pk.as_str()), &self.cipher)?;
133133
}
134134

135135
Ok(PrimaryKeyParts { pk, sk })
@@ -168,7 +168,7 @@ impl EncryptedTable {
168168

169169
let sk_to_delete = all_index_keys::<E>(&sk)
170170
.into_iter()
171-
.map(|x| hmac(&x, &self.cipher))
171+
.map(|x| hmac("sk", &x, Some(pk.as_str()), &self.cipher))
172172
.chain([Ok(sk)])
173173
.collect::<Result<Vec<_>, _>>()?;
174174

@@ -226,7 +226,7 @@ impl EncryptedTable {
226226
}
227227

228228
for index_sk in all_index_keys::<T>(&sk) {
229-
let index_sk = hmac(&index_sk, &self.cipher)?;
229+
let index_sk = hmac("sk", &index_sk, Some(pk.as_str()), &self.cipher)?;
230230

231231
if seen_sk.contains(&index_sk) {
232232
continue;

0 commit comments

Comments
 (0)