Skip to content

KeyStoreException (-59 / UNSUPPORTED_MIN_MAC_LENGTH) on Android 16 / MediaTek KeyMint 4.0 during Wrapped Key Import #254

Description

@cathome1

During device activation ("Use this device" / identity setup), OpenBubbles fails to import the activation wrapped key into Android Keystore on Android 16 with a MediaTek MT6895 SoC and TrustKernel TEE (KeyMint 4.0).
The operation fails with KeyStoreException: Failed to import wrapped key. Keystore error code: -59 (UNSUPPORTED_MIN_MAC_LENGTH).
Environment

  • App Version: OpenBubbles 1.15.0 (versionCode: 20002229, targetSdk: 36, minSdk: 24)
  • Package: com.openbubbles.messaging
  • OS: Android 16 (API Level 36)
  • Device: OBLUE OB-P12
  • SoC: MediaTek MT6895
  • TEE / KeyMint HAL: TrustKernel T6 OS (android.hardware.security.keymint@4.0-service.trustkernel, hardware_keystore=400)
    Steps to Reproduce
  • Open OpenBubbles 1.15.0 on an Android 16 device with strict KeyMint 4.0 (e.g. MediaTek MT6895 / TrustKernel).
  • Proceed through initial setup and select "Use this device".
  • Observe that activation hangs and fails.
    Logcat Output
    rust_lib_bluebubbles: Keystore destroying key activation:K6P4H19GPX
    rust_lib_bluebubbles: Keystore importing alias activation:K6P4H19GPX
    rust_lib_bluebubbles: wrapped asn.1 MIID1AIBAASCAQCQ/kv/Grxyiccr2BWe097i7wlEpTNLfYV+KY1AUV70nM2wZlLtyrGmLTs5S+mBTz4KGJ7Mg6IelfiACsHHT3y9HHZ0puRMUqBIhNCsbEO54dhmIWmcb1e1Cv+BzWGiRkWTBpFStEvnLY23/dBm6RD/DI5awpt0IyUtyhFOTwGw5y4Kehhec1Yq61BEtQlF3fqWsj1Bm+i1F5llkD9eW7JfOOvgFAZ7v08uvCGIx6zcZ8AgTIm62x+VOXY39H3Kh3OufeGFtU62NLf6yQzi5ihoPoadj74RGvHi3mL/TWtDtpEIGtUFIuu+M16MgrMmjsYsvL0B/rsVXRzUgJ3XC5IKBAzcJDpidIvB2MQLI+0wKwIBATAmoQUxAwIBAqIDAgEBowQCAgQApQUxAwIBAqYFMQMCAQW/g3cCBQAEggJ8mG+FLFU2IZtJ155BaLYw9pkHie/I+piN2Eeg/lFyP3Sc4Kwa3qTKxsfc5L/+qq3iJi3nOanPxymOOGBhs6bDj9jkkI45pGu1Fxw47mXzVHJeYkEvDfn7fLz8jDWKgLlXEkLvUp09NqlkxC4NGDh5FzFHTitrUciHz0K+nft2fFEpwyKo6YCxZOyzIWBIVNi4agygeYX2XS+ifsotIcU0mC10Ir1hjXiYr6daIzv/HxtfBswZXOfPhKxx4vO6GuEfrDGRMKl0RzhUd2wd24aED7FloDjCERi+FKi+JF6//icDLoeI2RxXWhS1BghI5QwDf1eCOcDEWhU0XUvIogF18PpWQfzIRZRdNGhXo63oFgeTbCXNwjeJYvsKojufl96FipFPMCQ6wLLunRW3cD64by+lwAf5nabomP1QnyIWOKQXGfUFU4mK7l1bsw/Nr0FaYambsDLthJvB1VKhIaOpgpfILDJE6bSelce7ryBmwDeo4znl+B2mRK9WpkMUvnrKqe0Nm3kcbtg87L7uHRnfwkQIMdoc/2jyxVH7Etuotdz13tzJzQ5eZMtHFe8XN4xMXG3yzrJc+/XqRIE+x4gruQxM/b4pLg7FG6SqBZo5GAGE6OWlBnRcs8fhWhkyszfrqADYmV811RUV0AOq1aF+4BdUcapYNfstILTgS+EXH6ey+w2cU0lV32zaP/bMMcmotU0mnQPNjWTIYGVD9Lc9Ix5WI4MDAUDshyTMKKYlJ9kKxlBPbB6G7fTGvE26M1o9JNp+ummGOT2l7X20OXup5VcEuOVX3S6mjf7r4f7XUoHjJNcjjqTzkHJct7wmCxJByXgZ8ykcRruzIwtYBBA3gA/4n7W3qcx3caJuRO2V
    keystore2: Error::Km(r#UNSUPPORTED_MIN_MAC_LENGTH)
    java.security.KeyStoreException: Failed to import wrapped key.
    Keystore error code: -59
    (public error code: 13 internal Keystore code: -59)

ASN.1 Decoding & Root Cause Analysis
Decoding the SecureKeyWrapper DER structure emitted by rust_lib_bluebubbles reveals two major issues in KeyDescription.AuthorizationList:
SEQUENCE (980 bytes) - SecureKeyWrapper
├── INTEGER : 00 (Format Version = 0)
├── OCTET STRING (256 bytes) [Encrypted Transport Key (RSA-OAEP)]
├── OCTET STRING (12 bytes) [AES-GCM IV]
├── SEQUENCE (43 bytes) [KeyDescription]
│ ├── INTEGER : 01 [keyFormat = 1 (PKCS#8 Private Key)]
│ └── SEQUENCE (38 bytes) [AuthorizationList / keyParams]
│ ├── [1] (PURPOSE) -> SET { INTEGER : 02 } (SIGN)
│ ├── [2] (ALGORITHM) -> INTEGER : 01 (RSA) <-- MISMATCH
│ ├── [3] (KEY_SIZE) -> INTEGER : 0100 (256) <-- MISMATCH
│ ├── [5] (DIGEST) -> SET { INTEGER : 02 } (SHA-256)
│ ├── [6] (PADDING) -> SET { INTEGER : 05 } (RSA_PKCS1_1_5_SIGN)
│ └── [503] (NO_AUTH_REQUIRED) -> NULL (05 00)
├── OCTET STRING (590 bytes) [Encrypted Key Payload]
└── OCTET STRING (16 bytes) [AES-GCM Auth Tag: 37 80 0f f8 9f b5 b7 a9 cc 77 71 a2 6e 44 ed 95]

Identified Issues:

  • Missing Tag::MIN_MAC_LENGTH / Tag::MAC_LENGTH:
    TrustKernel TEE on Android 16 KeyMint 4.0 strictly validates AEAD parameters. Because the wrapping key authorization does not explicitly specify the MAC length parameter (128 bits), the TEE rejects the operation during the unwrapping phase with UNSUPPORTED_MIN_MAC_LENGTH (-59).
  • Algorithm & KeySize Inconsistency:
    The KeyDescription specifies Algorithm = 1 (RSA) with KeySize = 256 and Padding = 5 (RSA_PKCS1_1_5_SIGN). A 256-bit RSA key is invalid in Android KeyStore (minimum 2048). If an EC P-256 key is being imported, Algorithm should be 3 (Algorithm::EC) and RSA padding tags should be omitted. While Qualcomm/Tensor HALs often silently ignore or patch this, TrustKernel performs strict spec enforcement and fails.
    Suggested Fix
    In rust_lib_bluebubbles (or the Android KeyStore ASN.1 generator module):
  • Ensure Algorithm is set to Algorithm::EC (3) when wrapping a 256-bit EC key, and omit RSA-specific padding.
  • Explicitly provide Tag::MIN_MAC_LENGTH = 128 (or add a Software Keystore fallback option for devices where hardware-backed import fails).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions