Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,12 @@ public static void add(byte[] buf, short op1, short op2, short result) {
byte index = 7;
byte carry = 0;
short tmp;
short val1 = 0;
short val2 = 0;
while (index >= 0) {
tmp =
(short) ((buf[(short) (op1 + index)] & 0xFF) +
(buf[(short) (op2 + index)] & 0xFF) + carry);
val1 = (short) (buf[(short) (op1 + index)] & 0x00FF);
val2 = (short) (buf[(short) (op2 + index)] & 0x00FF);
tmp = (short) (val1 + val2 + carry);
carry = 0;
if (tmp > 255) {
carry = 1; // max unsigned byte value is 255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,12 @@ public static void add(byte[] buf, short op1, short op2, short result) {
byte index = 7;
byte carry = 0;
short tmp;
short val1 = 0;
short val2 = 0;
while (index >= 0) {
tmp =
(short) ((buf[(short) (op1 + index)] & 0xFF) +
(buf[(short) (op2 + index)] & 0xFF) + carry);
val1 = (short) (buf[(short) (op1 + index)] & 0x00FF);
val2 = (short) (buf[(short) (op2 + index)] & 0x00FF);
tmp = (short) (val1 + val2 + carry);
carry = 0;
if (tmp > 255) {
carry = 1; // max unsigned byte value is 255
Expand Down
6 changes: 0 additions & 6 deletions Applet/src/com/android/javacard/keymaster/KMArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ public void add(short index, short objPtr) {
(short) (instanceTable[KM_ARRAY_OFFSET] + TLV_HEADER_SIZE + ARRAY_HEADER_SIZE + (short) (index * 2)),
objPtr);
}

public void deleteLastEntry() {
short len = length();
Util.setShort(heap, (short) (instanceTable[KM_ARRAY_OFFSET] + TLV_HEADER_SIZE + 2), (short) (len -1));
}


public short get(short index) {
short len = length();
Expand Down
17 changes: 17 additions & 0 deletions Applet/src/com/android/javacard/keymaster/KMDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,5 +468,22 @@ public short getCborBytesStartOffset(byte[] buf, short bufOffset, short bufLen)
readMajorTypeWithPayloadLength(BYTES_TYPE);
return scratchBuf[START_OFFSET];
}

public short readKeyblobVersion(byte[] buf, short bufOffset, short bufLen) {
bufferRef[0] = buf;
scratchBuf[START_OFFSET] = bufOffset;
scratchBuf[LEN_OFFSET] = (short) (bufOffset + bufLen);
short arrayLen = readMajorTypeWithPayloadLength(ARRAY_TYPE);
if (arrayLen == 0) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
short version = KMType.INVALID_VALUE;
try {
version = decodeInteger(KMInteger.exp());
} catch(Exception e) {
// Fail to decode Integer. It can happen if it is an old KeyBlob.
}
return version;
}

}
51 changes: 22 additions & 29 deletions Applet/src/com/android/javacard/keymaster/KMKeyParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ public static short makeHwEnforced(short keyParamsPtr, byte origin,
.instance(KMType.UINT_TAG, KMType.BOOT_PATCH_LEVEL, bootPatchObjPtr);
Util.setShort(scratchPad, arrInd, bootPatchTag);
arrInd += 2;
// Add custom tags at the end of the array. So it becomes easy to
// delete them when sending key characteristics back to HAL.
arrInd = addCustomTags(keyParamsPtr, scratchPad, arrInd);
return createKeyParameters(scratchPad, (short) (arrInd / 2));
}

Expand Down Expand Up @@ -276,11 +273,19 @@ public static short makeHidden(short keyParamsPtr, short rootOfTrustBlob, byte[]
short appId = KMKeyParameters.findTag(KMType.BYTES_TAG, KMType.APPLICATION_ID, keyParamsPtr);
if (appId != KMTag.INVALID_VALUE) {
appId = KMByteTag.cast(appId).getValue();
if (KMByteBlob.cast(appId).length() == 0) {
// Treat empty as INVALID.
return KMType.INVALID_VALUE;
}
}
short appData =
KMKeyParameters.findTag(KMType.BYTES_TAG, KMType.APPLICATION_DATA, keyParamsPtr);
if (appData != KMTag.INVALID_VALUE) {
appData = KMByteTag.cast(appData).getValue();
if (KMByteBlob.cast(appData).length() == 0) {
// Treat empty as INVALID.
return KMType.INVALID_VALUE;
}
}
return makeHidden(appId, appData, rootOfTrustBlob, scratchPad);
}
Expand Down Expand Up @@ -331,42 +336,30 @@ public static short createKeyParameters(byte[] ptrArr, short len) {
return KMKeyParameters.instance(arrPtr);
}

public static short addCustomTags(short keyParams, byte[] scratchPad, short offset) {
public static short makeCustomTags(short keyParams, byte[] scratchPad) {
short index = 0;
short tagPtr;
short offset = 0;
short len = (short) customTags.length;
short tagType;
while (index < len) {
tagType = customTags[(short) (index + 1)];
switch(tagType) {
case KMType.AUTH_TIMEOUT_MILLIS:
short authTimeOutTag =
KMKeyParameters.cast(keyParams).findTag(KMType.UINT_TAG, KMType.AUTH_TIMEOUT);
if (authTimeOutTag != KMType.INVALID_VALUE) {
tagPtr = createAuthTimeOutMillisTag(authTimeOutTag, scratchPad, offset);
Util.setShort(scratchPad, offset, tagPtr);
offset += 2;
}
break;
default:
break;
case KMType.AUTH_TIMEOUT_MILLIS:
short authTimeOutTag =
KMKeyParameters.cast(keyParams).findTag(KMType.UINT_TAG, KMType.AUTH_TIMEOUT);
if (authTimeOutTag != KMType.INVALID_VALUE) {
tagPtr = createAuthTimeOutMillisTag(authTimeOutTag, scratchPad, offset);
Util.setShort(scratchPad, offset, tagPtr);
offset += 2;
}
break;
default:
break;
}
index += 2;
}
return offset;
}

public void deleteCustomTags() {
short arrPtr = getVals();
short index = (short) (customTags.length - 1);
short obj;
while (index >= 0) {
obj = findTag(customTags[(short) (index - 1)], customTags[index]);
if (obj != KMType.INVALID_VALUE) {
KMArray.cast(arrPtr).deleteLastEntry();
}
index -= 2;
}
return createKeyParameters(scratchPad, (short) (offset / 2));
}

public static short createAuthTimeOutMillisTag(short authTimeOutTag, byte[] scratchPad, short offset) {
Expand Down
Loading