是否可以生成 64 字节(256 位)密钥并使用 AndroidKeyStore 存储/检索它?

luc*_*mdo 0 java encryption android android-keystore

在我的 Android 应用程序中,我需要一种方法来加密本地数据库中存储的数据。我选择 Realm DB 是因为它提供了与加密的无缝集成。我只需要在初始化 Realm 实例时传递一个密钥。该密钥的大小必须为 64 字节。

出于安全原因,我发现存储此密钥的最佳方法是在 AndroidKeyStore 中。我正在努力寻找一种方法来生成具有该大小的密钥(使用任何算法),并将其放入 64 字节数组中。我试图保留 API 19 的 minSdk,但我相信如果需要的话我可以将其提高到 23(这两个版本之间对 AndroidKeyStore 进行了许多更改)。

有人有想法吗?这是我的代码:

类加密.java

private static KeyStore ks = null;
private static String ALIAS = "com.oi.pap";

public static byte[] loadkey(Context context) {

    byte[] content = new byte[64];
    try {
        if (ks == null) {
            createNewKeys(context);
        }

        ks = KeyStore.getInstance("AndroidKeyStore");
        ks.load(null);

        content= ks.getCertificate(ALIAS).getEncoded(); //<----- HERE, I GET SIZE GREATER THAN 64
        Log.e(TAG, "original key :" + Arrays.toString(content));
    } catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    content = Arrays.copyOfRange(content, 0, 64); //<---- I would like to remove this part.
    return content;
}

private static void createNewKeys(Context context) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {

    ks = KeyStore.getInstance("AndroidKeyStore");
    ks.load(null);
    try {
        // Create new key if needed
        if (!ks.containsAlias(ALIAS)) {
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);
            KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
                    .setAlias(ALIAS)
                    .setSubject(new X500Principal("CN=PapRealmKey, O=oipap"))
                    .setSerialNumber(BigInteger.ONE)
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .setKeySize(256)
                    .setKeyType(KeyProperties.KEY_ALGORITHM_EC)
                    .build();
            KeyPairGenerator generator = KeyPairGenerator
                    .getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
            generator.initialize(spec);

            KeyPair keyPair = generator.generateKeyPair();
            Log.e(TAG, "generated key :" + Arrays.toString(keyPair.getPrivate().getEncoded()));

        }
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }
}
Run Code Online (Sandbox Code Playgroud)

div*_*eek 6

AndroidKeyStore 的目的是将敏感的密钥材料从您的应用程序、操作系统中移出,转移到永远不会泄露或受到损害的安全硬件中。因此,根据设计,如果您在 AndroidKeyStore 中创建密钥,则永远无法取出密钥材料。

在这种情况下,Realm DB 需要密钥材料,因此您不能为其提供 AndroidKeyStore 密钥。另外,Realm 想要的是两个 AES 密钥,而不是您尝试生成的 EC 密钥。

生成您需要的密钥材料的正确方法是:

byte[] dbKey = new byte[64];
Random random = new SecureRandom();
random.nextBytes(dbKey);
// Pass dbKey to Realm DB...
Arrays.fill(dbKey, 0); // Wipe key after use.
Run Code Online (Sandbox Code Playgroud)

只有 64 个随机字节。但是,您需要将这些字节存储在某处。您可以使用 AndroidKeyStore 创建 AES 密钥并使用它来加密dbKey。就像是:

KeyGenerator keyGenerator = KeyGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(
        new KeyGenParameterSpec.Builder("dbKeyWrappingKey",
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)      
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .build());
SecretKey key = keyGenerator.generateKey();

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = cipher.getIV();
byte[] encryptedDbKey = cipher.doFinal(dbKey);
Run Code Online (Sandbox Code Playgroud)

您需要将iv和保存encryptedDbKey在某个地方(不是在数据库中!),以便可以恢复dbKey. 然后你可以用以下命令解密它:

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
key = (SecretKey) keyStore.getKey("dbKeyWrappingKey", null);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(128, iv));
byte[] dbKey = cipher.doFinal(encryptedDbKey);
// Pass dbKey to Realm DB and then wipe it.
Run Code Online (Sandbox Code Playgroud)

然而,话虽如此……我认为你不应该这样做。我认为这实际上并没有给你带来 Android 默认情况下没有给你的任何安全性。如果攻击者尝试转储包含数据库的设备存储,他将一无所获,因为 Android 无论如何都会加密所有存储。如果攻击者可以root设备,他就可以像您的应用程序一样运行代码,并使用它以与dbKey您的应用程序相同的方式进行解密。

AndroidKeyStore 真正可以增加价值的是,如果您在dbKeyWrappingKey. 例如,如果您将其设置为要求在五分钟内进行用户身份验证,则只有当用户在附近输入 PIN/图案/密码或触摸指纹扫描仪时才可以使用dbWrappingKey解密。dbKey请注意,这仅在用户拥有 PIN/图案/密码时才有效,但如果他们没有,那么您的数据库对任何拿起电话的人都是开放的。

查看KeyGenParameterSpec您可以采取的所有措施来限制dbKeyWrappingKey可以使用的方式。