Skip to content

Commit 96dcd75

Browse files
authored
Merge pull request #49 from KVVat/keytestingpatch15
Add KeyTestingPatches for Android 15
2 parents 055f829 + 8d9d803 commit 96dcd75

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
From 55091e8d469bae3afbef58cfb33c50aa73861f26 Mon Sep 17 00:00:00 2001
2+
From: Paul Crowley <[email protected]>
3+
Date: Thu, 18 Aug 2022 22:33:22 -0700
4+
Subject: [PATCH] DO NOT SUBMIT log disk encryption keys
5+
6+
Bug: 121287968
7+
Test: DO NOT SUBMIT
8+
Change-Id: Ifc6f72b40dfe8c6edc5e9d9372ef670b9b3455ae
9+
---
10+
KeyStorage.cpp | 6 ++++++
11+
1 file changed, 6 insertions(+)
12+
13+
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
14+
index 3ede67e..24b309b 100644
15+
--- a/KeyStorage.cpp
16+
+++ b/KeyStorage.cpp
17+
@@ -655,6 +655,12 @@ bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffe
18+
return false;
19+
}
20+
}
21+
+
22+
+ KeyBuffer hexKey;
23+
+ StrToHex(*key, hexKey);
24+
+ hexKey.push_back('\0');
25+
+ LOG(DEBUG) << "DO NOT SUBMIT log of key in " << dir << " " << hexKey.data();
26+
+
27+
return true;
28+
}
29+
30+
--
31+
2.37.1.595.g718a3a8f04-goog
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
From 5bfeb985b2eacef0e37f4d6286ede353d9f4fb30 Mon Sep 17 00:00:00 2001
2+
From: Sunil Ravi <[email protected]>
3+
Date: Sun, 10 Mar 2019 12:49:53 -0700
4+
Subject: [PATCH] Dump security key
5+
6+
Dump security keys from supplicant
7+
8+
Bug: 123907624
9+
Test: Regression test
10+
Change-Id: I77254d92077d20d6a9520d7cf9f55eecbb2853f6
11+
---
12+
src/utils/wpa_debug.c | 2 +-
13+
1 file changed, 1 insertion(+), 1 deletion(-)
14+
15+
diff --git a/src/utils/wpa_debug.c b/src/utils/wpa_debug.c
16+
index a338a20..919dc8a 100644
17+
--- a/src/utils/wpa_debug.c
18+
+++ b/src/utils/wpa_debug.c
19+
@@ -390,7 +390,7 @@ void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
20+
21+
void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
22+
{
23+
- _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys, 0);
24+
+ _wpa_hexdump(level, title, buf, len, 1/* wpa_debug_show_keys */, 0);
25+
}
26+
27+
28+
--
29+
2.28.0.236.gb10cc79966-goog
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--- SyntheticPasswordCrypto.java 2023-10-02 05:35:22.189541116 +0000
2+
+++ SyntheticPasswordCrypto.java.patched 2023-09-29 02:57:08.030762456 +0000
3+
@@ -23,11 +23,13 @@ import android.security.keystore2.Androi
4+
import android.system.keystore2.Domain;
5+
import android.system.keystore2.KeyDescriptor;
6+
import android.text.TextUtils;
7+
+import android.util.Log;
8+
import android.util.Slog;
9+
10+
import com.android.internal.util.ArrayUtils;
11+
12+
import java.io.IOException;
13+
+import java.io.UnsupportedEncodingException;
14+
import java.security.InvalidAlgorithmParameterException;
15+
import java.security.InvalidKeyException;
16+
import java.security.KeyStore;
17+
@@ -253,6 +255,8 @@ class SyntheticPasswordCrypto {
18+
19+
protected static byte[] personalizedHash(byte[] personalization, byte[]... message) {
20+
try {
21+
+ StringBuilder logMessage = new StringBuilder();
22+
+ logMessage.append("DO NOT SUBMIT personalizedHash");
23+
final int PADDING_LENGTH = 128;
24+
MessageDigest digest = MessageDigest.getInstance("SHA-512");
25+
if (personalization.length > PADDING_LENGTH) {
26+
@@ -260,16 +264,55 @@ class SyntheticPasswordCrypto {
27+
}
28+
// Personalize the hash
29+
// Pad it to the block size of the hash function
30+
+ logMessage.append(" personalization: ");
31+
+ logMessage.append(new String(personalization, "UTF-8"));
32+
personalization = Arrays.copyOf(personalization, PADDING_LENGTH);
33+
digest.update(personalization);
34+
+ logMessage.append(" message: [");
35+
for (byte[] data : message) {
36+
+ logMessage.append(" ");
37+
+ logMessage.append(bytesToHex(data));
38+
digest.update(data);
39+
}
40+
- return digest.digest();
41+
+ logMessage.append(" ]");
42+
+ byte[] res = digest.digest();
43+
+ logMessage.append(" digest: ");
44+
+ logMessage.append(bytesToHex(res));
45+
+ Log.e(TAG, logMessage.toString());
46+
+ return res;
47+
} catch (NoSuchAlgorithmException e) {
48+
throw new IllegalStateException("NoSuchAlgorithmException for SHA-512", e);
49+
+ } catch (UnsupportedEncodingException e) {
50+
+ throw new IllegalStateException("Unable to represent bytes as UTF-8", e);
51+
}
52+
}
53+
+ /**
54+
+ * Uppercase hex string for byte array
55+
+ */
56+
+ public static String bytesToHex(byte[] bytes) {
57+
+ try {
58+
+ return new String(bytesToHexBytes(bytes), "UTF-8");
59+
+ } catch (UnsupportedEncodingException e) {
60+
+ throw new RuntimeException(e);
61+
+ }
62+
+ }
63+
+
64+
+ protected static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
65+
+ /**
66+
+ * Converts bytes to hex.
67+
+ */
68+
+ public static byte[] bytesToHexBytes(byte[] bytes) {
69+
+ if (bytes == null) {
70+
+ return "null".getBytes();
71+
+ }
72+
+ byte[] hexBytes = new byte[bytes.length * 2];
73+
+ for (int j = 0; j < bytes.length; j++) {
74+
+ int v = bytes[j] & 0xFF;
75+
+ hexBytes[j * 2] = HEX_ARRAY[v >>> 4];
76+
+ hexBytes[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
77+
+ }
78+
+ return hexBytes;
79+
+ }
80+
81+
static boolean migrateLockSettingsKey(String alias) {
82+
final KeyDescriptor legacyKey = new KeyDescriptor();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--- SyntheticPasswordManager.java 2023-10-02 05:35:22.189541116 +0000
2+
+++ SyntheticPasswordManager.java.patched 2023-09-29 02:55:56.394865625 +0000
3+
@@ -44,6 +44,7 @@ import android.service.gatekeeper.IGateK
4+
import android.text.TextUtils;
5+
import android.util.ArrayMap;
6+
import android.util.ArraySet;
7+
+import android.util.Log;
8+
import android.util.Slog;
9+
10+
import com.android.internal.annotations.VisibleForTesting;
11+
@@ -225,8 +226,20 @@ class SyntheticPasswordManager {
12+
*/
13+
private byte[] deriveSubkey(byte[] personalization) {
14+
if (mVersion == SYNTHETIC_PASSWORD_VERSION_V3) {
15+
- return (new SP800Derive(mSyntheticPassword))
16+
- .withContext(personalization, PERSONALIZATION_CONTEXT);
17+
+
18+
+ StringBuilder logMessage = new StringBuilder();
19+
+ logMessage.append("DO NOT SUBMIT derivePassword");
20+
+ logMessage.append(" personalization: ");
21+
+ logMessage.append(SyntheticPasswordCrypto.bytesToHex(personalization));
22+
+ logMessage.append(" context: ");
23+
+ logMessage.append(SyntheticPasswordCrypto.bytesToHex(PERSONALIZATION_CONTEXT));
24+
+ byte[] res = (new SP800Derive(mSyntheticPassword))
25+
+ .withContext(personalization, PERSONALIZATION_CONTEXT);
26+
+ logMessage.append(" result: ");
27+
+ logMessage.append(SyntheticPasswordCrypto.bytesToHex(res));
28+
+ Log.e(TAG, logMessage.toString());
29+
+
30+
+ return res;
31+
} else {
32+
return SyntheticPasswordCrypto.personalizedHash(personalization,
33+
mSyntheticPassword);
34+
@@ -234,7 +247,8 @@ class SyntheticPasswordManager {
35+
}
36+
37+
public byte[] deriveKeyStorePassword() {
38+
- return bytesToHex(deriveSubkey(PERSONALIZATION_KEY_STORE_PASSWORD));
39+
+ return SyntheticPasswordCrypto.bytesToHexBytes(
40+
+ deriveSubkey(PERSONALIZATION_KEY_STORE_PASSWORD));
41+
}
42+
43+
public byte[] deriveGkPassword() {
44+
@@ -926,6 +940,8 @@ class SyntheticPasswordManager {
45+
PasswordData pwd = credential.isNone() ? null :
46+
PasswordData.create(credential.getType(), pinLength);
47+
byte[] stretchedLskf = stretchLskf(credential, pwd);
48+
+ String hexPwdToken = String.valueOf(HexEncoding.encode(stretchedLskf));
49+
+ Log.i(TAG, "CKM.4.1 pwdToken " + hexPwdToken);
50+
long sid = GateKeeper.INVALID_SECURE_USER_ID;
51+
final byte[] protectorSecret;
52+
53+
@@ -1476,6 +1492,9 @@ class SyntheticPasswordManager {
54+
55+
private SyntheticPassword unwrapSyntheticPasswordBlob(long protectorId,
56+
byte expectedProtectorType, byte[] protectorSecret, long sid, int userId) {
57+
+ String hexApplicationId = String.valueOf(HexEncoding.encode(protectorSecret));
58+
+ Log.i(TAG, "CKM.4.2 protectorSecret " + hexApplicationId);
59+
+
60+
byte[] data = loadState(SP_BLOB_NAME, protectorId, userId);
61+
if (data == null) {
62+
return null;
63+
@@ -1510,6 +1529,8 @@ class SyntheticPasswordManager {
64+
}
65+
result.recreateFromEscrow(spSecret);
66+
} else {
67+
+ String hexSyntheticPassword = String.valueOf(HexEncoding.encode(spSecret));
68+
+ Log.i(TAG, "CKM.4.3 synthetic password " + hexSyntheticPassword);
69+
result.recreateDirectly(spSecret);
70+
}
71+
if (blob.mVersion == SYNTHETIC_PASSWORD_VERSION_V1) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Index: keystore2/src/super_key.rs
2+
IDEA additional info:
3+
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
4+
<+>UTF-8
5+
===================================================================
6+
diff --git a/keystore2/src/super_key.rs b/keystore2/src/super_key.rs
7+
--- a/keystore2/src/super_key.rs
8+
+++ b/keystore2/src/super_key.rs (date 1733981834874)
9+
@@ -51,7 +51,7 @@
10+
sync::{Mutex, RwLock, Weak},
11+
};
12+
use std::{convert::TryFrom, ops::Deref};
13+
-
14+
+use std::fmt;
15+
const MAX_MAX_BOOT_LEVEL: usize = 1_000_000_000;
16+
/// Allow up to 15 seconds between the user unlocking using a biometric, and the auth
17+
/// token being used to unlock in [`SuperKeyManager::try_unlock_user_with_biometric`].
18+
@@ -70,6 +70,16 @@
19+
EcdhP521,
20+
}
21+
22+
+
23+
+impl fmt::Display for SuperEncryptionAlgorithm {
24+
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
+ match self {
26+
+ SuperEncryptionAlgorithm::Aes256Gcm => write!(f, "AES-256-GCM"),
27+
+ SuperEncryptionAlgorithm::EcdhP521 => write!(f, "ECDH P-521"),
28+
+ }
29+
+ }
30+
+}
31+
+
32+
/// A particular user may have several superencryption keys in the database, each for a
33+
/// different purpose, distinguished by alias. Each is associated with a static
34+
/// constant of this type.
35+
@@ -557,6 +567,7 @@
36+
));
37+
}
38+
};
39+
+ log::debug!("CKM.4 keystore daemon Master key read:{}",format!("algorithm:{}; {:02x?}",algorithm,key));
40+
Ok(Arc::new(SuperKey {
41+
algorithm,
42+
key,
43+
@@ -590,6 +601,7 @@
44+
.context(ks_err!("Failed to encrypt new super key."))?;
45+
metadata.add(BlobMetaEntry::Iv(iv));
46+
metadata.add(BlobMetaEntry::AeadTag(tag));
47+
+ log::debug!("CKM.4 keystore daemon Password key:{}",format!("{:02x?}",encrypted_key));
48+
Ok((encrypted_key, metadata))
49+
}
50+
51+
@@ -773,6 +785,9 @@
52+
let key_entry = db
53+
.store_super_key(user_id, key_type, &encrypted_super_key, &blob_metadata, &key_metadata)
54+
.context(ks_err!("Failed to store super key."))?;
55+
+
56+
+ log::debug!("CKM.4 keystore daemon Master key generate:{}",format!("algorithm:{};{:02x?}",key_type.algorithm,super_key));
57+
+
58+
Ok(Arc::new(SuperKey {
59+
algorithm: key_type.algorithm,
60+
key: super_key,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Index: keystore2/src/crypto/zvec.rs
2+
IDEA additional info:
3+
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
4+
<+>UTF-8
5+
===================================================================
6+
diff --git a/keystore2/src/crypto/zvec.rs b/keystore2/src/crypto/zvec.rs
7+
--- a/keystore2/src/crypto/zvec.rs
8+
+++ b/keystore2/src/crypto/zvec.rs (date 1733902489725)
9+
@@ -107,7 +107,7 @@
10+
if self.elems.is_empty() {
11+
write!(f, "Zvec empty")
12+
} else {
13+
- write!(f, "Zvec size: {} {}", self.len)
14+
+ write!(f, "Zvec size: {} {:02x?}", self.len,self.elems)
15+
}
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
These patches are based off of the android15-dev branch
2+
This time, We can apply Android 14 patches except for keystore.
3+
4+
Here are the paths that the patches apply to:
5+
6+
`frameworks/base`:
7+
- 0001-SyntheticPasswordCrypto.java.patch
8+
- 0001-SyntheticPasswordManager.java.patch
9+
10+
`system/vold`:
11+
- 0001-DO-NOT-SUBMIT-log-disk-encryption-keys.patch
12+
13+
`external/wpa_supplicant_8`:
14+
- 0001-Dump-security-key.patch
15+
16+
`system/security/keystore2`:
17+
- DumpKeystore2/super_key.rs
18+
- DumpKeystore2/zvec.rs

0 commit comments

Comments
 (0)