From 1fff08ae92c997c0a335c58477a339c488878cb0 Mon Sep 17 00:00:00 2001 From: Subrahmanyaman Date: Tue, 26 Apr 2022 17:30:54 +0000 Subject: [PATCH] Added Comments --- .../javacard/keymaster/KMKeymasterApplet.java | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java b/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java index bd269dc1..a14933e3 100644 --- a/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java +++ b/Applet/src/com/android/javacard/keymaster/KMKeymasterApplet.java @@ -808,20 +808,20 @@ private short createKeyBlobExp(short version) { case (short) 0: // Old KeyBlob has a maximum of 5 elements. keyBlob = KMArray.instance(ASYM_KEY_BLOB_SIZE_V0); - KMArray.cast(keyBlob).add((short) 0, byteBlobExp); - KMArray.cast(keyBlob).add((short) 1, byteBlobExp); - KMArray.cast(keyBlob).add((short) 2, byteBlobExp); - KMArray.cast(keyBlob).add((short) 3, keyChar); - KMArray.cast(keyBlob).add((short) 4, byteBlobExp); + KMArray.cast(keyBlob).add((short) 0, byteBlobExp);// Secret + KMArray.cast(keyBlob).add((short) 1, byteBlobExp);// Nonce + KMArray.cast(keyBlob).add((short) 2, byteBlobExp);// AuthTag + KMArray.cast(keyBlob).add((short) 3, keyChar);// KeyChars + KMArray.cast(keyBlob).add((short) 4, byteBlobExp);// PubKey break; case (short) 1: keyBlob = KMArray.instance(ASYM_KEY_BLOB_SIZE_V1); - KMArray.cast(keyBlob).add((short) 0, KMInteger.exp()); - KMArray.cast(keyBlob).add((short) 1, byteBlobExp); - KMArray.cast(keyBlob).add((short) 2, byteBlobExp); - KMArray.cast(keyBlob).add((short) 3, byteBlobExp); - KMArray.cast(keyBlob).add((short) 4, keyChar); - KMArray.cast(keyBlob).add((short) 5, byteBlobExp); + KMArray.cast(keyBlob).add((short) 0, KMInteger.exp());// Version + KMArray.cast(keyBlob).add((short) 1, byteBlobExp);// Secret + KMArray.cast(keyBlob).add((short) 2, byteBlobExp);// Nonce + KMArray.cast(keyBlob).add((short) 3, byteBlobExp);// AuthTag + KMArray.cast(keyBlob).add((short) 4, keyChar);// KeyChars + KMArray.cast(keyBlob).add((short) 5, byteBlobExp);// PubKey break; case (short) 2: keyBlob = KMArray.instance(ASYM_KEY_BLOB_SIZE_V2); @@ -3031,9 +3031,6 @@ private void validateImportKey(short params, short keyFmt){ } private void importKey(APDU apdu, short keyFmt, byte[] scratchPad) { - // Take backup of the KeyParams before they get updated. The original - // key params are required to generate MAC in macKeyParams() function. - short keyParams = data[KEY_PARAMETERS]; validateImportKey(data[KEY_PARAMETERS], keyFmt); // Check algorithm and dispatch to appropriate handler. short alg = KMEnumTag.getValue(KMType.ALGORITHM, data[KEY_PARAMETERS]); @@ -3943,10 +3940,10 @@ private void decodeKeyBlob(short version, short keyBlob) { short minArraySize = 0; switch(version) { case 0: - minArraySize = (short) 4; + minArraySize = SYM_KEY_BLOB_SIZE_V0; break; case 1: - minArraySize = (short) 5; + minArraySize = SYM_KEY_BLOB_SIZE_V1; break; case 2: minArraySize = SYM_KEY_BLOB_SIZE_V2; @@ -3975,12 +3972,39 @@ private void processDecryptSecret(short version, short appId, short appData, byt short keyBlobSecretOff = 0; switch(version) { case 0: + // V0 KeyBlob + // KEY_BLOB = [ + // SECRET, + // NONCE, + // AUTH_TAG, + // KEY_CHARACTERISTICS, + // PUBKEY + // ] keyBlobSecretOff = (short) 0; break; case 1: + // V1 KeyBlob + // KEY_BLOB = [ + // VERSION, + // SECRET, + // NONCE, + // AUTH_TAG, + // KEY_CHARACTERISTICS, + // PUBKEY + // ] keyBlobSecretOff = (short) 1; break; case 2: + // V2 KeyBlob + // KEY_BLOB = [ + // VERSION, + // SECRET, + // NONCE, + // AUTH_TAG, + // KEY_CHARACTERISTICS, + // CUSTOM_TAGS, + // PUBKEY + // ] keyBlobSecretOff = KEY_BLOB_SECRET; break; default: @@ -4216,21 +4240,6 @@ private short addIntegers(short authTime, short timeStamp, byte[] scratchPad) { return KMInteger.uint_64(scratchPad, (short) 16); } - private void add(byte[] buf, short op1, short op2, short result) { - byte index = 7; - byte carry = 0; - short tmp; - while (index >= 0) { - tmp = (short) (buf[(short) (op1 + index)] + buf[(short) (op2 + index)] + carry); - carry = 0; - if (tmp > 255) { - carry = 1; // max unsigned byte value is 255 - } - buf[(short) (result + index)] = (byte) (tmp & (byte) 0xFF); - index--; - } - } - public void powerReset() { //TODO handle power reset signal. releaseAllOperations();