Skip to content

Commit fa9dad7

Browse files
MhmRddJingMatrix
authored andcommitted
Derive KEY_SIZE from EC_CURVE when not explicitly provided
For EC keys, callers often provide only EC_CURVE (e.g. P-256) without an explicit KEY_SIZE tag. The parser defaulted keySize to 0, causing the attestation teeEnforced list and KeyMetadata authorizations to report keySize=0 instead of the correct value (e.g. 256 for P-256). Add deriveKeySizeFromCurve() that maps EcCurve constants to their corresponding key sizes as a fallback when KEY_SIZE is absent.
1 parent 0958d17 commit fa9dad7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

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

Lines changed: 15 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,19 @@ 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 -> 256
177+
EcCurve.P_384 -> 384
178+
EcCurve.P_521 -> 521
179+
EcCurve.CURVE_25519 -> 256
180+
else -> 0
181+
}
182+
}
183+
170184
/**
171185
* Derives the EC Curve name. Logic: Checks specific EC_CURVE tag first (field=EcCurve), falls back
172186
* to KEY_SIZE (field=Integer).

0 commit comments

Comments
 (0)