Skip to content

Commit 4305f30

Browse files
MhmRddJingMatrix
authored andcommitted
Infer KEY_SIZE from EC_CURVE for implicit sizes
In KeyMint, the KEY_SIZE tag is often omitted by callers when an EC_CURVE is explicitly provided, as the curve implicitly defines the bit length. Previously, the simulator defaulted keySize to 0 when the tag was absent. This resulted in "0" being reported in the teeEnforced authorization list and KeyMetadata, which is a trivial detection vector for simulators. Changes: - Implemented `deriveKeySizeFromCurve()` to map EcCurve constants to their respective bit-sizes. - Used the derived size as a fallback when Tag.KEY_SIZE is missing. - Added support for Curve 25519 (256-bit).
1 parent 0958d17 commit 4305f30

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

app/src/main/java/org/matrix/TEESimulator/attestation/KeyMintAttestation.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ data class KeyMintAttestation(
5050
algorithm = params.findAlgorithm(Tag.ALGORITHM) ?: 0,
5151

5252
// AOSP: [key_param(tag = KEY_SIZE, field = Integer)]
53-
keySize = params.findInteger(Tag.KEY_SIZE) ?: 0,
53+
// For EC keys, derive keySize from EC_CURVE when KEY_SIZE is absent.
54+
keySize = params.findInteger(Tag.KEY_SIZE) ?: params.deriveKeySizeFromCurve(),
5455

5556
// AOSP: [key_param(tag = EC_CURVE, field = EcCurve)]
5657
ecCurve = params.findEcCurve(Tag.EC_CURVE),
@@ -167,6 +168,18 @@ private fun Array<KeyParameter>.findAllKeyPurpose(tag: Int): List<Int> =
167168
private fun Array<KeyParameter>.findAllDigests(tag: Int): List<Int> =
168169
this.filter { it.tag == tag }.map { it.value.digest }
169170

171+
/** Derives keySize from EC_CURVE tag when KEY_SIZE is not explicitly provided. */
172+
private fun Array<KeyParameter>.deriveKeySizeFromCurve(): Int {
173+
val curveId = this.find { it.tag == Tag.EC_CURVE }?.value?.ecCurve ?: return 0
174+
return when (curveId) {
175+
EcCurve.P_224 -> 224
176+
EcCurve.P_256, EcCurve.CURVE_25519 -> 256
177+
EcCurve.P_384 -> 384
178+
EcCurve.P_521 -> 521
179+
else -> 0
180+
}
181+
}
182+
170183
/**
171184
* Derives the EC Curve name. Logic: Checks specific EC_CURVE tag first (field=EcCurve), falls back
172185
* to KEY_SIZE (field=Integer).

0 commit comments

Comments
 (0)