Skip to content

Commit

Permalink
Make find_record constant time (#3097)
Browse files Browse the repository at this point in the history
* Make find_record constant time

* Implement Chris's suggestion
  • Loading branch information
samdealy authored Feb 9, 2023
1 parent 4d85726 commit 2a83307
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions fog/view/enclave/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license = "GPL-3.0"
[dependencies]
aes-gcm = "0.10.1"
aligned-cmov = "2.2"
static_assertions = "1.1.0"

mc-attest-ake = { path = "../../../../attest/ake", default-features = false }
mc-attest-core = { path = "../../../../attest/core", default-features = false }
mc-attest-enclave-api = { path = "../../../../attest/enclave-api", default-features = false }
Expand Down
26 changes: 9 additions & 17 deletions fog/view/enclave/impl/src/e_tx_out_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,15 @@ impl<OSC: ORAMStorageCreator<StorageDataSize, StorageMetaSize>> ETxOutStore<OSC>
);
}

// TOOO: Per https://github.com/mobilecoinfoundation/mobilecoin/issues/2965, use a
// a constant time comparison function to always copy the same number of bytes.
// NOTE: As of right now, this code is not constant time and therefore
// blocks the v5 release.
// Code to implement:
// ```
// const LENGTH_TO_COPY: usize = core::cmp::min(FIXED_CIPHERTEXT_LENGTH,
// ValueSize::USIZE - 1);
// (&result.ciphertext[..LENGTH_TO_COPY]).copy_from_slice(&value[1..LENGTH_TO_COPY]);
// ```
let data_end = ValueSize::USIZE - value[0] as usize;
let payload = &value[1..data_end];
// Use this instead of payload.len() because the slice `len` method isn't
// guaranteed to be constant time.
let payload_length = data_end - 1;
result.ciphertext[0..payload_length].copy_from_slice(payload);
result.payload_length = payload_length as u32;
// To preserve constant time execution, we always copy `ValueSize::USIZE - 1`
// bytes. To ensure the copy doesn't panic, assert that the length to
// copy is less than the maximum length that ciphertext can be, which is
// `FIXED_CIPHERTEXT_LENGTH`.
const LENGTH_TO_COPY: usize = ValueSize::USIZE - 1;
static_assertions::const_assert!(LENGTH_TO_COPY < FIXED_CIPHERTEXT_LENGTH);

result.ciphertext[..LENGTH_TO_COPY].copy_from_slice(&value[1..(LENGTH_TO_COPY + 1)]);
result.payload_length = (ValueSize::USIZE - 1 - (value[0] as usize)) as u32;

result
}
Expand Down
1 change: 1 addition & 0 deletions fog/view/enclave/trusted/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a83307

Please sign in to comment.