tor-proto: Simplify ct::lookup API.

This commit is contained in:
Nick Mathewson 2020-05-10 12:10:16 -04:00
parent 8d40caf8be
commit 9c62d15846
2 changed files with 7 additions and 10 deletions

View File

@ -35,7 +35,7 @@ pub struct NtorSecretKey {
use subtle::{Choice, ConstantTimeEq};
impl NtorSecretKey {
fn matches_pk(&self, pk: PublicKey) -> Choice {
fn matches_pk(&self, pk: &PublicKey) -> Choice {
self.pk.pk.as_bytes().ct_eq(pk.as_bytes())
}
}
@ -222,7 +222,7 @@ where
let my_key: PublicKey = cur.extract()?;
let their_pk: PublicKey = cur.extract()?;
let keypair = ct::lookup(&my_key, keys, |a, b| b.matches_pk(*a));
let keypair = ct::lookup(keys, |key| key.matches_pk(&my_key));
let keypair = match keypair {
Some(k) => k,
None => return Err(Error::MissingKey),

View File

@ -4,8 +4,8 @@ use subtle::*;
/// Try to find an item in a slice without leaking where and whether the
/// item was found.
///
/// If there is any item `x` in the `array` for which `matches(item,
/// x)` is true, this function will return a reference to one such
/// If there is any item `x` in the `array` for which `matches(x)`
/// is true, this function will return a reference to one such
/// item. (We don't specify which.)
///
/// Otherwise, this function returns none.
@ -15,12 +15,9 @@ use subtle::*;
///
/// Note that this doesn't necessarily do a constant-time comparison,
/// and that it is not constant-time for found/not-found case.
///
/// TODO: the item 'item' should really be part of the 'matches' closure.
pub fn lookup<'a, T, U, F>(item: &T, array: &'a [U], matches: F) -> Option<&'a U>
pub fn lookup<T, F>(array: &[T], matches: F) -> Option<&T>
where
F: Fn(&T, &U) -> Choice,
T: ?Sized,
F: Fn(&T) -> Choice,
{
// ConditionallySelectable isn't implemented for usize, so we need
// to use u64.
@ -28,7 +25,7 @@ where
let mut found: Choice = 0.into();
for (i, x) in array.iter().enumerate() {
let equal = matches(item, x);
let equal = matches(x);
idx.conditional_assign(&(i as u64), equal);
found.conditional_assign(&equal, equal)
}