Use constant-time compare for GAM auth tokens

GAM auth tokens are 128-bit capabilities bound to their owning
process. The previous `==` compare on `[u32; 4]` short-circuits
on the first mismatching element, leaking prefix-length information
through timing.

Switch to `subtle::ConstantTimeEq`, which is already present in
the dep graph via ed25519-dalek and used elsewhere in the crypto
stack.

Signed-off-by: tunnell <tunnell@users.noreply.github.com>
Assisted-by: Claude Code (Opus 4.7)
This commit is contained in:
tunnell 2026-04-20 08:15:32 -05:00
parent a642095c55
commit e19ba9973f
3 changed files with 7 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2688,6 +2688,7 @@ dependencies = [
"qrcode",
"rkyv 0.8.8",
"sha2",
"subtle",
"trng",
"tts-frontend",
"utralib 0.1.27",

View file

@ -46,6 +46,7 @@ miniz_oxide = "0.4.4"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
sha2 = { version = "0.10.8" }
digest = "0.9.0"
subtle = { version = "2.6", default-features = false }
utralib = { version = "0.1.27", optional = true, default-features = false }

View file

@ -3,6 +3,7 @@ use std::cell::RefCell;
#[cfg(feature = "bao1x")]
use bao1x_hal_service::trng;
use gam::{EXPECTED_APP_CONTEXTS, EXPECTED_BOOT_CONTEXTS};
use subtle::ConstantTimeEq;
/*
Authentication tokens to the GAM are created on a first-come, first-serve basis,
@ -117,7 +118,9 @@ impl<'a> TokenManager {
}
pub(crate) fn is_token_valid(&self, token: [u32; 4]) -> bool {
self.tokens.iter().find(|&namedtoken| namedtoken.token == token).is_some()
// Constant-time comparison: tokens are 128-bit capabilities; a
// non-CT compare leaks prefix information through timing.
self.tokens.iter().any(|namedtoken| bool::from(namedtoken.token.ct_eq(&token)))
}
pub(crate) fn find_token(&self, name: &str) -> Option<[u32; 4]> {
@ -131,7 +134,7 @@ impl<'a> TokenManager {
pub(crate) fn lookup_name(&self, token: &[u32; 4]) -> Option<String> {
for entry in self.tokens.iter() {
if entry.token == *token {
if bool::from(entry.token.ct_eq(token)) {
return Some(entry.name.to_string());
}
}