From 328f0b849e06f3eb0d007ce441a734c36e6f668f Mon Sep 17 00:00:00 2001 From: Bo Zhu Date: Thu, 4 Jan 2018 00:11:35 -0800 Subject: [PATCH] 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 --- .../recoverablekeystore/KeySyncUtils.java | 2 +- .../recoverablekeystore/KeySyncUtilsTest.java | 37 ++++++++++--------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/services/core/java/com/android/server/locksettings/recoverablekeystore/KeySyncUtils.java b/services/core/java/com/android/server/locksettings/recoverablekeystore/KeySyncUtils.java index bc080be70bcb8..e851d8cf21b3e 100644 --- a/services/core/java/com/android/server/locksettings/recoverablekeystore/KeySyncUtils.java +++ b/services/core/java/com/android/server/locksettings/recoverablekeystore/KeySyncUtils.java @@ -298,8 +298,8 @@ public class KeySyncUtils { .order(ByteOrder.LITTLE_ENDIAN) .put(SecureBox.encodePublicKey(thmPublicKey)) .putLong(counterId) - .putInt(maxAttempts) .putLong(deviceId) + .putInt(maxAttempts) .array(); } diff --git a/services/tests/servicestests/src/com/android/server/locksettings/recoverablekeystore/KeySyncUtilsTest.java b/services/tests/servicestests/src/com/android/server/locksettings/recoverablekeystore/KeySyncUtilsTest.java index ba40c67cb35d2..114da1aaebb52 100644 --- a/services/tests/servicestests/src/com/android/server/locksettings/recoverablekeystore/KeySyncUtilsTest.java +++ b/services/tests/servicestests/src/com/android/server/locksettings/recoverablekeystore/KeySyncUtilsTest.java @@ -386,23 +386,7 @@ public class KeySyncUtilsTest { } @Test - public void packVaultParams_encodesMaxAttemptsAsThirdParam() throws Exception { - int maxAttempts = 10; - - byte[] packedForm = KeySyncUtils.packVaultParams( - SecureBox.genKeyPair().getPublic(), - /*counterId=*/ 1001L, - maxAttempts, - /*deviceId=*/ 1L); - - ByteBuffer byteBuffer = ByteBuffer.wrap(packedForm) - .order(ByteOrder.LITTLE_ENDIAN); - byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + Long.BYTES); - assertEquals(maxAttempts, byteBuffer.getInt()); - } - - @Test - public void packVaultParams_encodesDeviceIdAsLastParam() throws Exception { + public void packVaultParams_encodesDeviceIdAsThirdParam() throws Exception { long deviceId = 102942158152L; byte[] packedForm = KeySyncUtils.packVaultParams( @@ -413,10 +397,27 @@ public class KeySyncUtilsTest { ByteBuffer byteBuffer = ByteBuffer.wrap(packedForm) .order(ByteOrder.LITTLE_ENDIAN); - byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + Long.BYTES + Integer.BYTES); + byteBuffer.position(PUBLIC_KEY_LENGTH_BYTES + Long.BYTES); assertEquals(deviceId, byteBuffer.getLong()); } + @Test + public void packVaultParams_encodesMaxAttemptsAsLastParam() throws Exception { + int maxAttempts = 10; + + byte[] packedForm = KeySyncUtils.packVaultParams( + SecureBox.genKeyPair().getPublic(), + /*counterId=*/ 1001L, + maxAttempts, + /*deviceId=*/ 1L); + + ByteBuffer byteBuffer = ByteBuffer.wrap(packedForm) + .order(ByteOrder.LITTLE_ENDIAN); + 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);