Skip to content
Open
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 @@ -1350,4 +1350,22 @@ public void releaseAllOperations() {
public KMComputedHmacKey getComputedHmacKey() {
return computedHmacKey;
}

@Override
public short messageDigest256(byte[] inBuff, short inOffset,
short inLength, byte[] outBuff, short outOffset) {
MessageDigest.OneShot mDigest = null;
short len = 0;
try {
mDigest = MessageDigest.OneShot.open(MessageDigest.ALG_SHA_256);
len = mDigest.doFinal(inBuff, inOffset, inLength, outBuff, outOffset);
} finally {
if (mDigest != null) {
mDigest.close();
mDigest = null;
}
}
return len;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.android.javacard.keymaster;

import javacard.framework.Util;

public class KMPKCS8DecoderImpl implements KMPKCS8Decoder {
public static final byte ASN1_OCTET_STRING= 0x04;
public static final byte ASN1_SEQUENCE= 0x30;
public static final byte ASN1_INTEGER= 0x02;
public static final byte ASN1_A0_TAG = (byte) 0xA0;
public static final byte ASN1_A1_TAG = (byte) 0xA1;
public static final byte ASN1_BIT_STRING = 0x03;
public static final byte[] EC_CURVE = {
0x06,0x08,0x2a,(byte)0x86,0x48,(byte)0xce,0x3d,0x03,
0x01,0x07
};
public static final byte[] RSA_ALGORITHM = {
0x06,0x09,0x2A,(byte)0x86,0x48,(byte)0x86,
(byte)0xF7,0x0D,0x01,0x01,0x01,0x05,0x00
};
public static final byte[] EC_ALGORITHM = {
0x06,0x07,0x2a,(byte)0x86,0x48,(byte)0xce,
0x3d,0x02,0x01,0x06,0x08,0x2a,(byte)0x86,0x48,
(byte)0xce,0x3d,0x03,0x01,0x07
};
private byte[] data;
private short start;
private short length;
private short cur;
private static KMPKCS8DecoderImpl inst;
private KMPKCS8DecoderImpl(){
start = 0;
length = 0;
cur = 0;
}

@Override
public short decodeRsa(short blob){
init(blob);
decodeCommon((short)0, RSA_ALGORITHM);
return decodeRsaPrivateKey((short)0);
}

@Override
public short decodeEc(short blob){
init(blob);
decodeCommon((short)0, EC_ALGORITHM);
return decodeEcPrivateKey((short)1);
}

//Seq[Int,Int,Int,Int,<ignore rest>]
public short decodeRsaPrivateKey(short version){
short resp = KMArray.instance((short)3);
header(ASN1_OCTET_STRING);
header(ASN1_SEQUENCE);
short len =header(ASN1_INTEGER);
if(len != 1) KMException.throwIt(KMError.UNKNOWN_ERROR);
short ver = getByte();
if(ver != version) KMException.throwIt(KMError.UNKNOWN_ERROR);
len = header(ASN1_INTEGER);
short modulus = getModulus(len);
len = header(ASN1_INTEGER);
short pubKey = KMByteBlob.instance(len);
getBytes(pubKey);
len = header(ASN1_INTEGER);
short privKey = KMByteBlob.instance(len);
getBytes(privKey);
KMArray.cast(resp).add((short)0, modulus);
KMArray.cast(resp).add((short)1, pubKey);
KMArray.cast(resp).add((short)2, privKey);
return resp;
}

// Seq [Int, Blob]
public void decodeCommon(short version, byte[] alg){
short len = header(ASN1_SEQUENCE);
len = header(ASN1_INTEGER);
if(len != 1) KMException.throwIt(KMError.UNKNOWN_ERROR);
short ver = getByte();
if(ver !=version) KMException.throwIt(KMError.UNKNOWN_ERROR);
len = header(ASN1_SEQUENCE);
short blob = KMByteBlob.instance(len);
getBytes(blob);
if(Util.arrayCompare(
KMByteBlob.cast(blob).getBuffer(),
KMByteBlob.cast(blob).getStartOff(),
alg,
(short)0,KMByteBlob.cast(blob).length()) !=0){
KMException.throwIt(KMError.UNKNOWN_ERROR);
}
}

//Seq[Int,blob,blob]
public short decodeEcPrivateKey(short version){
short resp = KMArray.instance((short)2);
header(ASN1_OCTET_STRING);
header(ASN1_SEQUENCE);
short len = header(ASN1_INTEGER);
if(len != 1) KMException.throwIt(KMError.UNKNOWN_ERROR);
short ver = getByte();
if(ver != version) KMException.throwIt(KMError.UNKNOWN_ERROR);
len = header(ASN1_OCTET_STRING);
short privKey = KMByteBlob.instance(len);
getBytes(privKey);
validateTag0IfPresent();
header(ASN1_A1_TAG);
len = header(ASN1_BIT_STRING);
if(len < 1) KMException.throwIt(KMError.UNKNOWN_ERROR);
byte unusedBits = getByte();
if(unusedBits != 0) KMException.throwIt(KMError.UNIMPLEMENTED);
short pubKey = KMByteBlob.instance((short)(len -1));
getBytes(pubKey);
KMArray.cast(resp).add((short)0, pubKey);
KMArray.cast(resp).add((short)1, privKey);
return resp;
}
private void validateTag0IfPresent(){
if(data[cur] != ASN1_A0_TAG) return;;
short len = header(ASN1_A0_TAG);
if(len != EC_CURVE.length) KMException.throwIt(KMError.UNKNOWN_ERROR);
if(Util.arrayCompare(data, cur, EC_CURVE, (short)0, len) != 0) KMException.throwIt(KMError.UNKNOWN_ERROR);
incrementCursor(len);
}
private short header(short tag){
short t = getByte();
if(t != tag) KMException.throwIt(KMError.UNKNOWN_ERROR);
return getLength();
}

private byte getByte(){
byte d = data[cur];
incrementCursor((short)1);
return d;
}

private short getShort(){
short d = Util.getShort(data, cur);
incrementCursor((short)2);
return d;
}

private short getModulus(short modulusLen) {
if(0 == data[cur] && modulusLen == 257) {
incrementCursor((short) 1);
modulusLen--;
}
short blob = KMByteBlob.instance(modulusLen);
getBytes(blob);
return blob;
}

private void getBytes(short blob){
short len = KMByteBlob.cast(blob).length();
Util.arrayCopyNonAtomic(data, cur, KMByteBlob.cast(blob).getBuffer(),
KMByteBlob.cast(blob).getStartOff(), len);
incrementCursor(len);
}

private short getLength(){
byte len = getByte();
if(len >= 0) return len;
len = (byte)(len & 0x7F);
if(len == 1) return (short)(getByte() & 0xFF);
else if(len == 2) return getShort();
else KMException.throwIt(KMError.UNKNOWN_ERROR);
return KMType.INVALID_VALUE; //should not come here
}
public static KMPKCS8DecoderImpl instance() {
if (inst == null) {
inst = new KMPKCS8DecoderImpl();
}
return inst;
}

public void init(short blob) {
data = KMByteBlob.cast(blob).getBuffer();
start = KMByteBlob.cast(blob).getStartOff();
length = KMByteBlob.cast(blob).length();
cur = start;
}

public void incrementCursor(short n){
cur += n;
if(cur > ((short)(start+length))) KMException.throwIt(KMError.UNKNOWN_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javacard.security.Key;
import javacard.security.KeyBuilder;
import javacard.security.KeyPair;
import javacard.security.MessageDigest;
import javacard.security.RSAPrivateKey;
import javacard.security.RSAPublicKey;
import javacard.security.RandomData;
Expand Down Expand Up @@ -1383,4 +1384,19 @@ public KMComputedHmacKey getComputedHmacKey() {
public void releaseAllOperations() {
//Do nothing.
}

@Override
public short messageDigest256(byte[] inBuff, short inOffset,
short inLength, byte[] outBuff, short outOffset) {
MessageDigest mDigest = null;
short len = 0;
try {
mDigest = MessageDigest.getInitializedMessageDigestInstance(MessageDigest.ALG_SHA_256, false);
len = mDigest.doFinal(inBuff, inOffset, inLength, outBuff, outOffset);
} catch (Exception e) {

}
return len;
}

}
Loading