Skip to content

Commit 4a9043b

Browse files
Merge pull request #58 from divegeek/Javacard_KeyMint_100_master
Javacard key mint 100 master
2 parents 1a45800 + b6b1f3b commit 4a9043b

File tree

10 files changed

+509
-349
lines changed

10 files changed

+509
-349
lines changed

Applet/AndroidSEProviderLib/src/com/android/javacard/seprovider/KMAndroidSEProvider.java

Lines changed: 40 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@
4141

4242
public class KMAndroidSEProvider implements KMSEProvider {
4343

44-
// static final variables
45-
// --------------------------------------------------------------
46-
// P-256 Curve Parameters
47-
static byte[] secp256r1_P;
48-
static byte[] secp256r1_A;
49-
50-
static byte[] secp256r1_B;
51-
static byte[] secp256r1_S;
52-
53-
// Uncompressed form
54-
static byte[] secp256r1_UCG;
55-
static byte[] secp256r1_N;
56-
static final short secp256r1_H = 1;
57-
// --------------------------------------------------------------
5844
public static final short AES_GCM_TAG_LENGTH = 16;
5945
public static final short AES_GCM_NONCE_LENGTH = 12;
6046
public static final byte KEYSIZE_128_OFFSET = 0x00;
@@ -102,7 +88,6 @@ public static KMAndroidSEProvider getInstance() {
10288
}
10389

10490
public KMAndroidSEProvider() {
105-
initStatics();
10691
// Re-usable AES,DES and HMAC keys in persisted memory.
10792
aesKeys = new AESKey[2];
10893
aesKeys[KEYSIZE_128_OFFSET] = (AESKey) KeyBuilder.buildKey(
@@ -116,8 +101,8 @@ public KMAndroidSEProvider() {
116101
rsaKeyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
117102
ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, KeyBuilder.LENGTH_EC_FP_256);
118103
keyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DH_PLAIN, false);
119-
initECKey(ecKeyPair);
120104
poolMgr = KMPoolManager.getInstance();
105+
poolMgr.initECKey(ecKeyPair);
121106
//RsaOAEP Decipher
122107
rsaOaepDecipher = new KMRsaOAEPEncoding(KMRsaOAEPEncoding.ALG_RSA_PKCS1_OAEP_SHA256_MGF1_SHA1);
123108

@@ -136,95 +121,10 @@ public KMAndroidSEProvider() {
136121
resetFlag[0] = (byte) POWER_RESET_FALSE;
137122
}
138123

139-
public static void initStatics() {
140-
secp256r1_P = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00,
141-
(byte) 0x00,
142-
(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
143-
(byte) 0x00,
144-
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xFF,
145-
(byte) 0xFF,
146-
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
147-
(byte) 0xFF,
148-
(byte) 0xFF, (byte) 0xFF};
149-
150-
secp256r1_A = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00,
151-
(byte) 0x00,
152-
(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
153-
(byte) 0x00,
154-
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xFF,
155-
(byte) 0xFF,
156-
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
157-
(byte) 0xFF,
158-
(byte) 0xFF, (byte) 0xFC};
159-
160-
secp256r1_B = new byte[]{(byte) 0x5A, (byte) 0xC6, (byte) 0x35, (byte) 0xD8, (byte) 0xAA,
161-
(byte) 0x3A,
162-
(byte) 0x93, (byte) 0xE7, (byte) 0xB3, (byte) 0xEB, (byte) 0xBD, (byte) 0x55, (byte) 0x76,
163-
(byte) 0x98,
164-
(byte) 0x86, (byte) 0xBC, (byte) 0x65, (byte) 0x1D, (byte) 0x06, (byte) 0xB0, (byte) 0xCC,
165-
(byte) 0x53,
166-
(byte) 0xB0, (byte) 0xF6, (byte) 0x3B, (byte) 0xCE, (byte) 0x3C, (byte) 0x3E, (byte) 0x27,
167-
(byte) 0xD2,
168-
(byte) 0x60, (byte) 0x4B};
169-
170-
secp256r1_S = new byte[]{(byte) 0xC4, (byte) 0x9D, (byte) 0x36, (byte) 0x08, (byte) 0x86,
171-
(byte) 0xE7,
172-
(byte) 0x04, (byte) 0x93, (byte) 0x6A, (byte) 0x66, (byte) 0x78, (byte) 0xE1, (byte) 0x13,
173-
(byte) 0x9D,
174-
(byte) 0x26, (byte) 0xB7, (byte) 0x81, (byte) 0x9F, (byte) 0x7E, (byte) 0x90};
175-
176-
// Uncompressed form
177-
secp256r1_UCG = new byte[]{(byte) 0x04, (byte) 0x6B, (byte) 0x17, (byte) 0xD1, (byte) 0xF2,
178-
(byte) 0xE1,
179-
(byte) 0x2C, (byte) 0x42, (byte) 0x47, (byte) 0xF8, (byte) 0xBC, (byte) 0xE6, (byte) 0xE5,
180-
(byte) 0x63,
181-
(byte) 0xA4, (byte) 0x40, (byte) 0xF2, (byte) 0x77, (byte) 0x03, (byte) 0x7D, (byte) 0x81,
182-
(byte) 0x2D,
183-
(byte) 0xEB, (byte) 0x33, (byte) 0xA0, (byte) 0xF4, (byte) 0xA1, (byte) 0x39, (byte) 0x45,
184-
(byte) 0xD8,
185-
(byte) 0x98, (byte) 0xC2, (byte) 0x96, (byte) 0x4F, (byte) 0xE3, (byte) 0x42, (byte) 0xE2,
186-
(byte) 0xFE,
187-
(byte) 0x1A, (byte) 0x7F, (byte) 0x9B, (byte) 0x8E, (byte) 0xE7, (byte) 0xEB, (byte) 0x4A,
188-
(byte) 0x7C,
189-
(byte) 0x0F, (byte) 0x9E, (byte) 0x16, (byte) 0x2B, (byte) 0xCE, (byte) 0x33, (byte) 0x57,
190-
(byte) 0x6B,
191-
(byte) 0x31, (byte) 0x5E, (byte) 0xCE, (byte) 0xCB, (byte) 0xB6, (byte) 0x40, (byte) 0x68,
192-
(byte) 0x37,
193-
(byte) 0xBF, (byte) 0x51, (byte) 0xF5};
194-
195-
secp256r1_N = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00,
196-
(byte) 0x00,
197-
(byte) 0x00, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
198-
(byte) 0xFF,
199-
(byte) 0xFF, (byte) 0xFF, (byte) 0xBC, (byte) 0xE6, (byte) 0xFA, (byte) 0xAD, (byte) 0xA7,
200-
(byte) 0x17,
201-
(byte) 0x9E, (byte) 0x84, (byte) 0xF3, (byte) 0xB9, (byte) 0xCA, (byte) 0xC2, (byte) 0xFC,
202-
(byte) 0x63,
203-
(byte) 0x25, (byte) 0x51};
204-
}
205-
206124
public void clean() {
207125
Util.arrayFillNonAtomic(tmpArray, (short) 0, (short) 256, (byte) 0);
208126
}
209127

210-
private void initECKey(KeyPair ecKeyPair) {
211-
ECPrivateKey privKey = (ECPrivateKey) ecKeyPair.getPrivate();
212-
ECPublicKey pubkey = (ECPublicKey) ecKeyPair.getPublic();
213-
pubkey.setFieldFP(secp256r1_P, (short) 0, (short) secp256r1_P.length);
214-
pubkey.setA(secp256r1_A, (short) 0, (short) secp256r1_A.length);
215-
pubkey.setB(secp256r1_B, (short) 0, (short) secp256r1_B.length);
216-
pubkey.setG(secp256r1_UCG, (short) 0, (short) secp256r1_UCG.length);
217-
pubkey.setK(secp256r1_H);
218-
pubkey.setR(secp256r1_N, (short) 0, (short) secp256r1_N.length);
219-
220-
privKey.setFieldFP(secp256r1_P, (short) 0, (short) secp256r1_P.length);
221-
privKey.setA(secp256r1_A, (short) 0, (short) secp256r1_A.length);
222-
privKey.setB(secp256r1_B, (short) 0, (short) secp256r1_B.length);
223-
privKey.setG(secp256r1_UCG, (short) 0, (short) secp256r1_UCG.length);
224-
privKey.setK(secp256r1_H);
225-
privKey.setR(secp256r1_N, (short) 0, (short) secp256r1_N.length);
226-
}
227-
228128
public AESKey createAESKey(short keysize) {
229129
try {
230130
newRandomNumber(tmpArray, (short) 0, (short) (keysize / 8));
@@ -684,27 +584,25 @@ private byte mapCipherAlg(byte alg, byte padding, byte blockmode, byte digest) {
684584
public KMOperation createSymmetricCipher(short alg, short purpose, short macLength,
685585
short blockMode, short padding, byte[] secret, short secretStart,
686586
short secretLength, byte[] ivBuffer, short ivStart, short ivLength) {
687-
Key key = null;
587+
588+
short cipherAlg = mapCipherAlg((byte) alg, (byte) padding, (byte) blockMode, (byte) 0);
589+
KMOperation operation =
590+
poolMgr.getOperationImpl(purpose, cipherAlg, alg, padding, blockMode, macLength, secretLength, false);
591+
// Get the KeyObject from the operation and update the key with the secret key material.
592+
KMKeyObject keyObj = operation.getKeyObject();
593+
Key key = (Key)keyObj.getKeyObjectInstance();
688594
switch (secretLength) {
689595
case 32:
690-
key = aesKeys[KEYSIZE_256_OFFSET];
691-
((AESKey) key).setKey(secret, secretStart);
692-
break;
693-
case 16:
694-
key = aesKeys[KEYSIZE_128_OFFSET];
596+
case 16:
695597
((AESKey) key).setKey(secret, secretStart);
696598
break;
697599
case 24:
698-
key = triDesKey;
699600
((DESKey) key).setKey(secret, secretStart);
700601
break;
701602
default:
702603
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
703604
break;
704-
}
705-
short cipherAlg = mapCipherAlg((byte) alg, (byte) padding, (byte) blockMode, (byte) 0);
706-
KMOperation operation =
707-
poolMgr.getOperationImpl(purpose, cipherAlg, alg, padding, blockMode, macLength, false);
605+
}
708606
((KMOperationImpl) operation).init(key, KMType.INVALID_VALUE, ivBuffer, ivStart, ivLength);
709607
return operation;
710608
}
@@ -716,20 +614,27 @@ public KMOperation createHmacSignerVerifier(short purpose, short digest,
716614
}
717615
KMOperation operation =
718616
poolMgr.getOperationImpl(purpose, Signature.ALG_HMAC_SHA_256,
719-
KMType.HMAC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, false);
720-
HMACKey key = createHMACKey(secret, secretStart, secretLength);
617+
KMType.HMAC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, (short)0, false);
618+
// Get the KeyObject from the operation and update the key with the secret key material.
619+
KMKeyObject keyObj = operation.getKeyObject();
620+
HMACKey key = (HMACKey)keyObj.getKeyObjectInstance();
621+
key.setKey(secret, secretStart, secretLength);
721622
((KMOperationImpl) operation).init(key, digest, null, (short) 0, (short) 0);
722623
return operation;
723624
}
724625

725-
private KMOperation createHmacSignerVerifier(short purpose, short digest, HMACKey key, boolean isTrustedConf) {
626+
private KMOperation createHmacSignerVerifier(short purpose, short digest, HMACKey hmacKey, boolean isTrustedConf) {
726627
if (digest != KMType.SHA2_256) {
727628
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
728629
}
729630
KMOperation operation =
730631
poolMgr.getOperationImpl(purpose, Signature.ALG_HMAC_SHA_256,
731-
KMType.HMAC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, isTrustedConf);
732-
632+
KMType.HMAC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, (short)0, isTrustedConf);
633+
// Get the KeyObject from the operation and update the key with the secret key material.
634+
KMKeyObject keyObj = operation.getKeyObject();
635+
HMACKey key = (HMACKey)keyObj.getKeyObject();
636+
short len = hmacKey.getKey(tmpArray, (short) 0);
637+
key.setKey(tmpArray, (short) 0, len);
733638
((KMOperationImpl) operation).init(key, digest, null, (short) 0, (short) 0);
734639
return operation;
735640
}
@@ -769,8 +674,10 @@ public KMOperation createRsaSigner(short digest, short padding, byte[] secret,
769674
short modLength) {
770675
byte alg = mapSignature256Alg(KMType.RSA, (byte) padding, (byte) digest);
771676
KMOperation operation = poolMgr.getOperationImpl(KMType.SIGN, alg, KMType.RSA, padding,
772-
KMType.INVALID_VALUE, KMType.INVALID_VALUE, false);
773-
RSAPrivateKey key = (RSAPrivateKey) rsaKeyPair.getPrivate();
677+
KMType.INVALID_VALUE, KMType.INVALID_VALUE, secretLength, false);
678+
// Get the KeyObject from the operation and update the key with the secret key material.
679+
KMKeyObject keyObj = operation.getKeyObject();
680+
RSAPrivateKey key = (RSAPrivateKey)((KeyPair)(keyObj.getKeyObjectInstance())).getPrivate();
774681
key.setExponent(secret, secretStart, secretLength);
775682
key.setModulus(modBuffer, modOff, modLength);
776683
((KMOperationImpl) operation).init(key, digest, null, (short) 0, (short) 0);
@@ -782,8 +689,10 @@ public KMOperation createRsaDecipher(short padding, short mgfDigest, byte[] secr
782689
short modLength) {
783690
byte cipherAlg = mapCipherAlg(KMType.RSA, (byte) padding, (byte) 0, (byte) mgfDigest);
784691
KMOperation operation = poolMgr.getOperationImpl(KMType.DECRYPT, cipherAlg, KMType.RSA, padding,
785-
KMType.INVALID_VALUE, KMType.INVALID_VALUE, false);
786-
RSAPrivateKey key = (RSAPrivateKey) rsaKeyPair.getPrivate();
692+
KMType.INVALID_VALUE, KMType.INVALID_VALUE, secretLength, false);
693+
// Get the KeyObject from the operation and update the key with the secret key material.
694+
KMKeyObject keyObj = operation.getKeyObject();
695+
RSAPrivateKey key = (RSAPrivateKey) ((KeyPair)(keyObj.getKeyObjectInstance())).getPrivate();
787696
key.setExponent(secret, secretStart, secretLength);
788697
key.setModulus(modBuffer, modOff, modLength);
789698
((KMOperationImpl) operation).init(key, KMType.INVALID_VALUE, null, (short) 0, (short) 0);
@@ -793,22 +702,24 @@ public KMOperation createRsaDecipher(short padding, short mgfDigest, byte[] secr
793702
public KMOperation createEcSigner(short digest, byte[] secret,
794703
short secretStart, short secretLength) {
795704
byte alg = mapSignature256Alg(KMType.EC, (byte) 0, (byte) digest);
796-
ECPrivateKey key = (ECPrivateKey) ecKeyPair.getPrivate();
797-
key.setS(secret, secretStart, secretLength);
798705
KMOperation operation = poolMgr
799-
.getOperationImpl(KMType.SIGN, alg, KMType.EC, KMType.INVALID_VALUE,
800-
KMType.INVALID_VALUE, KMType.INVALID_VALUE, false);
706+
.getOperationImpl(KMType.SIGN, alg, KMType.EC, KMType.INVALID_VALUE,
707+
KMType.INVALID_VALUE, KMType.INVALID_VALUE, secretLength, false);
708+
KMKeyObject keyObj = operation.getKeyObject();
709+
ECPrivateKey key = (ECPrivateKey) ((KeyPair)(keyObj.getKeyObjectInstance())).getPrivate();
710+
key.setS(secret, secretStart, secretLength);
801711
((KMOperationImpl) operation).init(key, digest, null, (short) 0, (short) 0);
802712
return operation;
803713
}
804714

805715
public KMOperation createKeyAgreement(byte[] secret, short secretStart,
806716
short secretLength) {
807-
ECPrivateKey key = (ECPrivateKey) ecKeyPair.getPrivate();
808-
key.setS(secret, secretStart, secretLength);
809717
KMOperation operation = poolMgr
810718
.getOperationImpl(KMType.AGREE_KEY, KeyAgreement.ALG_EC_SVDP_DH_PLAIN,
811-
KMType.EC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, false);
719+
KMType.EC, KMType.INVALID_VALUE, KMType.INVALID_VALUE, KMType.INVALID_VALUE, (short)0, false);
720+
KMKeyObject keyObj = operation.getKeyObject();
721+
ECPrivateKey key = (ECPrivateKey) ((KeyPair)(keyObj.getKeyObjectInstance())).getPrivate();
722+
key.setS(secret, secretStart, secretLength);
812723
((KMOperationImpl) operation).init(key, KMType.INVALID_VALUE, null, (short) 0, (short) 0);
813724
return operation;
814725
}
@@ -884,19 +795,6 @@ public KMMasterKey createMasterKey(KMMasterKey masterKey, short keySizeBits) {
884795
}
885796
}
886797

887-
@Override
888-
public KMAttestationKey createAttestationKey(KMAttestationKey attestationKey, byte[] keyData, short offset,
889-
short length) {
890-
if (attestationKey == null) {
891-
// Strongbox supports only P-256 curve for EC key.
892-
KeyPair ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, KeyBuilder.LENGTH_EC_FP_256);
893-
initECKey(ecKeyPair);
894-
attestationKey = new KMECPrivateKey(ecKeyPair);
895-
}
896-
((KMECPrivateKey) attestationKey).setS(keyData, offset, length);
897-
return (KMAttestationKey) attestationKey;
898-
}
899-
900798
@Override
901799
public KMPreSharedKey createPreSharedKey(KMPreSharedKey preSharedKey, byte[] keyData, short offset, short length) {
902800
short lengthInBits = (short) (length * 8);
@@ -1113,7 +1011,7 @@ public KMDeviceUniqueKeyPair createRkpDeviceUniqueKeyPair(KMDeviceUniqueKeyPair
11131011
short privKeyOff, short privKeyLen) {
11141012
if (key == null) {
11151013
KeyPair ecKeyPair = new KeyPair(KeyPair.ALG_EC_FP, KeyBuilder.LENGTH_EC_FP_256);
1116-
initECKey(ecKeyPair);
1014+
poolMgr.initECKey(ecKeyPair);
11171015
key = new KMECDeviceUniqueKey(ecKeyPair);
11181016
}
11191017
((KMECDeviceUniqueKey) key).setS(privKey, privKeyOff, privKeyLen);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.android.javacard.seprovider;
2+
3+
public class KMKeyObject {
4+
private byte algorithm;
5+
private Object keyObjectInst;
6+
7+
public void setKeyObjectData(byte alg, Object keyObject) {
8+
algorithm = alg;
9+
keyObjectInst = keyObject;
10+
}
11+
12+
public byte getAlgorithm() {
13+
return this.algorithm;
14+
}
15+
16+
public Object getKeyObjectInstance() {
17+
return keyObjectInst;
18+
}
19+
}

Applet/AndroidSEProviderLib/src/com/android/javacard/seprovider/KMOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ boolean verify(byte[] inputDataBuf, short inputDataStart, short inputDataLength,
5151
// include the auth tag which is appended at the end of the encrypted data. For decryption this will be
5252
// size of the decrypted data only.
5353
short getAESGCMOutputSize(short dataSize, short macLength);
54+
55+
KMKeyObject getKeyObject();
5456
}

0 commit comments

Comments
 (0)