From e46d69670f939d69407719a7cc2cd6a44b4c5ed6 Mon Sep 17 00:00:00 2001 From: zhenyolka Date: Mon, 8 Nov 2021 19:09:16 +0300 Subject: [PATCH] Add support of A12 keymaster_key_blob files structure In A12 keymaster_key_blob format changed Compared to A11 it contains another new 8 bytes at beginning "pKMblob\0" (in hex 0x704B4D626C6F6200) We can just ignore them Change-Id: I8a1701a248be536fdd000b9011122ef954c8e4d1 --- crypto/fscrypt/KeyStorage.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crypto/fscrypt/KeyStorage.cpp b/crypto/fscrypt/KeyStorage.cpp index edb23a27..8afc5792 100755 --- a/crypto/fscrypt/KeyStorage.cpp +++ b/crypto/fscrypt/KeyStorage.cpp @@ -62,6 +62,8 @@ static constexpr size_t STRETCHED_BYTES = 1 << 6; static constexpr uint32_t AUTH_TIMEOUT = 30; // Seconds +static const std::string kPkmBlob("pKMblob\x00", 8); + static const char* kCurrentVersion = "1"; static const char* kRmPath = "/system/bin/rm"; static const char* kSecdiscardPath = "/system/bin/secdiscard"; @@ -247,6 +249,10 @@ static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir, auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob; std::string kmKey; if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation(); + // In A12 keymaster_key_blob format changed: + // it have useless for us bytes in beginning, so remove them to correctly handle key + if (!kmKey.compare(0, kPkmBlob.size(), kPkmBlob)) + kmKey.erase(0, kPkmBlob.size()); km::AuthorizationSet inParams(keyParams); inParams.append(opParams.begin(), opParams.end()); for (;;) { @@ -590,6 +596,10 @@ bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffe static bool deleteKey(const std::string& dir) { std::string kmKey; if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false; + // In A12 keymaster_key_blob format changed: + // it have useless for us bytes in beginning, so remove them to correctly handle key + if (!kmKey.compare(0, kPkmBlob.size(), kPkmBlob)) + kmKey.erase(0, kPkmBlob.size()); Keymaster keymaster; if (!keymaster) return false; if (!keymaster.deleteKey(kmKey)) return false;