forked from 5ec1cff/TrickyStore
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLatencySimulator.kt
More file actions
63 lines (50 loc) · 1.79 KB
/
LatencySimulator.kt
File metadata and controls
63 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.android.keystore
import android.content.Context
import android.content.pm.PackageManager
import kotlinx.coroutines.delay
class LatencySimulator(private val context: Context) {
// LATENCY EMULATION LAYER
// Emulate real hardware delays
companion object {
const val MIN_TEE_WAIT_MS = 15L
const val STRONGBOX_GENERATE_WAIT_MS = 250L
const val STRONGBOX_CREATE_WAIT_MS = 80L
const val CANNOT_ATTEST_IDS = -66
}
suspend fun emulateTeeOperation(action: () -> Unit) {
val start = System.nanoTime()
action()
val elapsedMs = (System.nanoTime() - start) / 1_000_000
if (elapsedMs < MIN_TEE_WAIT_MS) {
delay(MIN_TEE_WAIT_MS - elapsedMs)
}
}
suspend fun emulateStrongBoxGenerateKey(action: () -> Unit) {
val start = System.nanoTime()
action()
val elapsedMs = (System.nanoTime() - start) / 1_000_000
if (elapsedMs < STRONGBOX_GENERATE_WAIT_MS) {
delay(STRONGBOX_GENERATE_WAIT_MS - elapsedMs)
}
}
suspend fun emulateStrongBoxCreateOperation(action: () -> Unit) {
val start = System.nanoTime()
action()
val elapsedMs = (System.nanoTime() - start) / 1_000_000
if (elapsedMs < STRONGBOX_CREATE_WAIT_MS) {
delay(STRONGBOX_CREATE_WAIT_MS - elapsedMs)
}
}
fun requestDeviceId(callerUid: Int, callerPid: Int): Int {
// Access Control: Enforce READ_PRIVILEGED_PHONE_STATE
val permission = context.checkPermission(
"android.permission.READ_PRIVILEGED_PHONE_STATE",
callerPid,
callerUid
)
if (permission != PackageManager.PERMISSION_GRANTED) {
return CANNOT_ATTEST_IDS
}
return 0 // Success
}
}