From 6e4e34e2994dc8bbb5f68bd0ae91a83a15f0545f Mon Sep 17 00:00:00 2001 From: Subrahmanyaman Date: Wed, 25 May 2022 01:24:20 +0000 Subject: [PATCH] Removed unused imports and made attest_ids array as global instead of a local variable to reduce the number of writes on EPROM. --- .../android/javacard/keymaster/KMAESKey.java | 2 -- .../javacard/keymaster/KMAndroidSEApplet.java | 1 - .../keymaster/KMAndroidSEProvider.java | 9 ------ .../keymaster/KMAttestationCertImpl.java | 7 ---- .../javacard/keymaster/KMECPrivateKey.java | 5 --- .../android/javacard/keymaster/KMHmacKey.java | 2 -- .../javacard/keymaster/KMKeymasterApplet.java | 32 ++++++++----------- .../javacard/keymaster/KMOperationState.java | 2 -- 8 files changed, 14 insertions(+), 46 deletions(-) diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAESKey.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAESKey.java index cec6388e..3b396bab 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAESKey.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAESKey.java @@ -17,8 +17,6 @@ import org.globalplatform.upgrade.Element; -import com.android.javacard.keymaster.KMMasterKey; - import javacard.security.AESKey; public class KMAESKey implements KMMasterKey { diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEApplet.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEApplet.java index 2fd17006..587b2653 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEApplet.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEApplet.java @@ -21,7 +21,6 @@ import javacard.framework.ISO7816; import javacard.framework.ISOException; -import javacard.framework.Util; public class KMAndroidSEApplet extends KMKeymasterApplet implements OnUpgradeListener { diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEProvider.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEProvider.java index 326cdde7..57a2cb38 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEProvider.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAndroidSEProvider.java @@ -36,15 +36,6 @@ import javacardx.crypto.AEADCipher; import javacardx.crypto.Cipher; -import com.android.javacard.keymaster.KMAESKey; -import com.android.javacard.keymaster.KMAttestationKey; -import com.android.javacard.keymaster.KMECPrivateKey; -import com.android.javacard.keymaster.KMError; -import com.android.javacard.keymaster.KMException; -import com.android.javacard.keymaster.KMHmacKey; -import com.android.javacard.keymaster.KMMasterKey; -import com.android.javacard.keymaster.KMPreSharedKey; - public class KMAndroidSEProvider implements KMSEProvider { // static final variables diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAttestationCertImpl.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAttestationCertImpl.java index c2b2f1ce..e07cfdec 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAttestationCertImpl.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMAttestationCertImpl.java @@ -15,11 +15,6 @@ */ package com.android.javacard.keymaster; -import com.android.javacard.keymaster.KMAESKey; -import com.android.javacard.keymaster.KMByteBlob; -import com.android.javacard.keymaster.KMECPrivateKey; -import com.android.javacard.keymaster.KMMasterKey; - import javacard.framework.JCSystem; import javacard.framework.Util; import javacard.security.AESKey; @@ -144,7 +139,6 @@ public class KMAttestationCertImpl implements KMAttestationCert { private static byte verifiedState; private static short verifiedHash; private static short issuer; - private static short signPriv; private KMAttestationCertImpl() { } @@ -187,7 +181,6 @@ private static void init() { verifiedState = 0; rsaCert = true; deviceLocked = 0; - signPriv = 0; } @Override diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMECPrivateKey.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMECPrivateKey.java index 3188ad19..2ff77da5 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMECPrivateKey.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMECPrivateKey.java @@ -17,11 +17,6 @@ import org.globalplatform.upgrade.Element; -import com.android.javacard.keymaster.KMAESKey; -import com.android.javacard.keymaster.KMAttestationCert; -import com.android.javacard.keymaster.KMAttestationKey; - -import javacard.security.AESKey; import javacard.security.ECPrivateKey; import javacard.security.KeyPair; diff --git a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMHmacKey.java b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMHmacKey.java index 98f623b2..8f5e3534 100644 --- a/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMHmacKey.java +++ b/Applet/AndroidSEProvider/src/com/android/javacard/keymaster/KMHmacKey.java @@ -17,8 +17,6 @@ import org.globalplatform.upgrade.Element; -import com.android.javacard.keymaster.KMPreSharedKey; - import javacard.security.HMACKey; public class KMHmacKey implements KMPreSharedKey, KMComputedHmacKey { diff --git a/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java b/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java index e30f1ca0..0e67dc06 100644 --- a/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java +++ b/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java @@ -41,7 +41,6 @@ public class KMKeymasterApplet extends Applet implements AppletEvent, ExtendedLe private static final byte CLA_ISO7816_NO_SM_NO_CHAN = (byte) 0x80; private static final short KM_HAL_VERSION = (short) 0x4000; private static final short MAX_AUTH_DATA_SIZE = (short) 512; - private static final short DERIVE_KEY_INPUT_SIZE = (short) 256; private static final short POWER_RESET_MASK_FLAG = (short) 0x4000; // Magic number version public static final byte KM_MAGIC_NUMBER = (byte) 0x81; @@ -89,6 +88,17 @@ public class KMKeymasterApplet extends Applet implements AppletEvent, ExtendedLe private static final byte[] OEM_UNLOCK_VERIFICATION_LABEL = { // "Enable RMA" 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x52, 0x4d, 0x41 }; + // Attestation IDs + private static final short[] ATTEST_ID_TAGS = { + KMType.ATTESTATION_ID_BRAND, + KMType.ATTESTATION_ID_DEVICE, + KMType.ATTESTATION_ID_IMEI, + KMType.ATTESTATION_ID_MANUFACTURER, + KMType.ATTESTATION_ID_MEID, + KMType.ATTESTATION_ID_MODEL, + KMType.ATTESTATION_ID_PRODUCT, + KMType.ATTESTATION_ID_SERIAL + }; // Possible states of the applet. @@ -811,7 +821,6 @@ private void processAddRngEntropyCmd(APDU apdu) { private void processSetVersionAndPatchLevels(APDU apdu) { receiveIncoming(apdu); - byte[] scratchPad = apdu.getBuffer(); // Argument 1 OS Version tmpVariables[0] = KMInteger.exp(); // Argument 2 OS Patch level @@ -991,8 +1000,6 @@ private void processProvisionAttestationKey(APDU apdu) { private void processProvisionOEMRootPublicKeyCmd(APDU apdu) { receiveIncoming(apdu); - // Re-purpose the apdu buffer as scratch pad. - byte[] scratchPad = apdu.getBuffer(); // Arguments short keyparams = KMKeyParameters.exp(); short keyFormatPtr = KMEnum.instance(KMType.KEY_FORMAT); @@ -1719,26 +1726,15 @@ private boolean isEmpty(byte[] buf, short offset, short len) { // id values of both the requested parameters and the provisioned parameters // then throw INVALID_TAG error. private void addAttestationIds(KMAttestationCert cert) { - final short[] attTags = - new short[]{ - KMType.ATTESTATION_ID_BRAND, - KMType.ATTESTATION_ID_DEVICE, - KMType.ATTESTATION_ID_IMEI, - KMType.ATTESTATION_ID_MANUFACTURER, - KMType.ATTESTATION_ID_MEID, - KMType.ATTESTATION_ID_MODEL, - KMType.ATTESTATION_ID_PRODUCT, - KMType.ATTESTATION_ID_SERIAL - }; byte index = 0; short attIdTag; short attIdTagValue; short storedAttId; - while (index < (short) attTags.length) { - attIdTag = KMKeyParameters.findTag(KMType.BYTES_TAG, attTags[index], data[KEY_PARAMETERS]); + while (index < (short) ATTEST_ID_TAGS.length) { + attIdTag = KMKeyParameters.findTag(KMType.BYTES_TAG, ATTEST_ID_TAGS[index], data[KEY_PARAMETERS]); if (attIdTag != KMType.INVALID_VALUE) { attIdTagValue = KMByteTag.cast(attIdTag).getValue(); - storedAttId = repository.getAttId(mapToAttId(attTags[index])); + storedAttId = repository.getAttId(mapToAttId(ATTEST_ID_TAGS[index])); // Return CANNOT_ATTEST_IDS if Attestation IDs are not provisioned or // Attestation IDs are deleted. if (storedAttId == KMType.INVALID_VALUE || diff --git a/Applet/src/com/android/javacard/keymaster/KMOperationState.java b/Applet/src/com/android/javacard/keymaster/KMOperationState.java index bfd67ceb..1daafabe 100644 --- a/Applet/src/com/android/javacard/keymaster/KMOperationState.java +++ b/Applet/src/com/android/javacard/keymaster/KMOperationState.java @@ -16,8 +16,6 @@ package com.android.javacard.keymaster; -import javacard.framework.ISO7816; -import javacard.framework.ISOException; import javacard.framework.JCSystem; import javacard.framework.Util;