Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 328f0b84 authored by Bo Zhu's avatar Bo Zhu
Browse files

Use the same VaultParams encoding as the server side

Change-Id: I99887f2e52c24726b40fa4cfedc0a1854490160f
Test: adb shell am instrument -w -e package
com.android.server.locksettings.recoverablekeystore
com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
parent 5ec9db0e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -298,8 +298,8 @@ public class KeySyncUtils {
                .order(ByteOrder.LITTLE_ENDIAN)
                .put(SecureBox.encodePublicKey(thmPublicKey))
                .putLong(counterId)
                .putInt(maxAttempts)
                .putLong(deviceId)
                .putInt(maxAttempts)
                .array();
    }

+14 −13
Original line number Diff line number Diff line
@@ -386,37 +386,38 @@ public class KeySyncUtilsTest {
    }

    @Test
    public void packVaultParams_encodesMaxAttemptsAsThirdParam() throws Exception {
        int maxAttempts = 10;
    public void packVaultParams_encodesDeviceIdAsThirdParam() throws Exception {
        long deviceId = 102942158152L;

        byte[] packedForm = KeySyncUtils.packVaultParams(
                SecureBox.genKeyPair().getPublic(),
                /*counterId=*/ 1001L,
                maxAttempts,
                /*deviceId=*/ 1L);
                /*counterId=*/ 10021L,
                /*maxAttempts=*/ 10,
                deviceId);

        ByteBuffer byteBuffer = ByteBuffer.wrap(packedForm)
                .order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + Long.BYTES);
        assertEquals(maxAttempts, byteBuffer.getInt());
        assertEquals(deviceId, byteBuffer.getLong());
    }

    @Test
    public void packVaultParams_encodesDeviceIdAsLastParam() throws Exception {
        long deviceId = 102942158152L;
    public void packVaultParams_encodesMaxAttemptsAsLastParam() throws Exception {
        int maxAttempts = 10;

        byte[] packedForm = KeySyncUtils.packVaultParams(
                SecureBox.genKeyPair().getPublic(),
                /*counterId=*/ 10021L,
                /*maxAttempts=*/ 10,
                deviceId);
                /*counterId=*/ 1001L,
                maxAttempts,
                /*deviceId=*/ 1L);

        ByteBuffer byteBuffer = ByteBuffer.wrap(packedForm)
                .order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + Long.BYTES + Integer.BYTES);
        assertEquals(deviceId, byteBuffer.getLong());
        byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + 2 * Long.BYTES);
        assertEquals(maxAttempts, byteBuffer.getInt());
    }


    private static byte[] randomBytes(int n) {
        byte[] bytes = new byte[n];
        new Random().nextBytes(bytes);