From 06d9db443c2bb45eb581e8ad9d712749c48f0da6 Mon Sep 17 00:00:00 2001 From: Enginex0 Date: Tue, 17 Mar 2026 03:35:20 +0100 Subject: [PATCH] perf(keygen): replace Gaussian RTT normalization with 15ms floor fence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old Gaussian sleep (mean=55ms, stddev=12ms) triggered detection on Chunqiu Native Check 2.8. A flat 15ms floor satisfies the minimum RTT threshold without creating a detectable delay pattern — both attested and non-attested paths get identical treatment, keeping the D50 ratio at ~1.0 while staying above the >=15ms requirement. --- .../keystore/shim/KeyMintSecurityLevelInterceptor.kt | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt index cb4990a..955298a 100644 --- a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt +++ b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt @@ -350,7 +350,7 @@ class KeyMintSecurityLevelInterceptor( ) val elapsedMs = (System.nanoTime() - startNs) / 1_000_000 - val delayMs = sampleTeeLatencyMs() - elapsedMs + val delayMs = TEE_LATENCY_FLOOR_MS - elapsedMs if (delayMs > 0) Thread.sleep(delayMs) return InterceptorUtils.createTypedObjectReply(response.metadata) @@ -541,15 +541,12 @@ class KeyMintSecurityLevelInterceptor( private const val MAX_ALIAS_LENGTH = 256 * 1024 private const val KEYMINT_INVALID_INPUT_LENGTH = -21 private const val RESPONSE_INVALID_ARGUMENT = 20 + private const val TEE_LATENCY_FLOOR_MS = 15L private const val KEYMINT_CANNOT_ATTEST_IDS = -66 private const val MAX_CONCURRENT_HW_KEYGEN_PER_UID = 2 // Sliding window: max hardware keygen permits per UID within the burst window private const val MAX_HW_KEYGEN_PER_WINDOW = 2 private const val BURST_WINDOW_MS = 30_000L - private const val TEE_LATENCY_MEAN_MS = 55.0 - private const val TEE_LATENCY_STDDEV_MS = 12.0 - private const val TEE_LATENCY_FLOOR_MS = 15L - private val uidHardwareKeygenCount = ConcurrentHashMap() private val hardwareKeygenTxIds = ConcurrentHashMap.newKeySet() private val uidKeygenTimestamps = ConcurrentHashMap>() @@ -577,11 +574,6 @@ class KeyMintSecurityLevelInterceptor( } } - private fun sampleTeeLatencyMs(): Long { - val sample = TEE_LATENCY_MEAN_MS + secureRandom.nextGaussian() * TEE_LATENCY_STDDEV_MS - return sample.toLong().coerceAtLeast(TEE_LATENCY_FLOOR_MS) - } - private val GENERATE_KEY_TRANSACTION = InterceptorUtils.getTransactCode(IKeystoreSecurityLevel.Stub::class.java, "generateKey") private val IMPORT_KEY_TRANSACTION =