Merge pull request #863 from tunnell/fix/gam-token-ct-eq

Use constant-time compare for GAM auth tokens
This commit is contained in:
bunnie 2026-05-08 03:21:21 +08:00 committed by GitHub
commit 12268b32f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2692,6 +2692,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());
}
}