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 numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
package com.android.javacard.keymaster;

import com.android.javacard.keymaster.KMCipher;
import javacard.framework.Util;
import javacard.security.CryptoException;
import javacardx.crypto.Cipher;


public class KMCipherImpl extends KMCipher{
Cipher cipher;
short cipherAlg;
short paddingAlg;
short mode;
KMCipherImpl(Cipher c){
cipher = c;
}

@Override
public short doFinal(byte[] buffer, short startOff, short length, byte[] scratchPad, short i) {
return cipher.doFinal(buffer, startOff, length, scratchPad, i);
short len = cipher.doFinal(buffer, startOff, length, scratchPad, i);
// JCard Sim removes leading zeros during decryption in case of no padding - we add that back.
// TODO confirm whether this is fine to pass the VTS.
if(cipherAlg == Cipher.ALG_RSA_NOPAD &&
mode == Cipher.MODE_DECRYPT &&
len < 256){
byte[] tempBuf = new byte[256];
Util.arrayFillNonAtomic(tempBuf,(short)0, (short)256, (byte)0);
Util.arrayCopyNonAtomic(scratchPad,(short)0,tempBuf, (short)(i+256-len),len );
Util.arrayCopyNonAtomic(tempBuf,(short)0,scratchPad,i,(short)256);
len = 256;
}
return len;
}

@Override
Expand All@@ -39,4 +54,12 @@ public short getPaddingAlgorithm() {
public void setPaddingAlgorithm(short alg) {
paddingAlg = alg;
}

public short getMode() {
return mode;
}

public void setMode(short mode) {
this.mode = mode;
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -198,10 +198,12 @@ public short aesGCMEncrypt(
short authTagStart,
short authTagLen) {
//Create the sun jce compliant aes key
if(key.getSize() != 128){
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
}
byte[] keyMaterial = new byte[16];
if(key.getSize() == 128){
keyMaterial = new byte[16];
}else if(key.getSize() == 256){
keyMaterial = new byte[32];
}
key.getKey(keyMaterial,(short)0);
//print("KeyMaterial Enc", keyMaterial);
//print("Authdata Enc", authData, authDataStart, authDataLen);
Expand DownExpand Up@@ -323,10 +325,12 @@ public boolean aesGCMDecrypt(
short authTagStart,
short authTagLen) {
//Create the sun jce compliant aes key
if(key.getSize() != 128){
CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
}
byte[] keyMaterial = new byte[16];
if(key.getSize() == 128){
keyMaterial = new byte[16];
}else if(key.getSize() == 256){
keyMaterial = new byte[32];
}
key.getKey(keyMaterial,(short)0);
//print("KeyMaterial Dec", keyMaterial);
//print("Authdata Dec", authData, authDataStart, authDataLen);
Expand DownExpand Up@@ -476,7 +480,11 @@ public KMCipher createRsaDecipher(short padding, byte[] secret, short secretStar
key.setExponent(secret,secretStart,secretLength);
key.setModulus(modBuffer, modOff, modLength);
rsaCipher.init(key,Cipher.MODE_DECRYPT);
return new KMCipherImpl(rsaCipher);
KMCipherImpl inst = new KMCipherImpl(rsaCipher);
inst.setCipherAlgorithm(cipherAlg);
inst.setMode(Cipher.MODE_DECRYPT);
inst.setPaddingAlgorithm(padding);
return inst;
}

@Override
Expand DownExpand Up@@ -570,9 +578,10 @@ public KMCipher createSymmetricCipher(short cipherAlg, short mode, short padding
CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
break;
}
KMCipher cipher = new KMCipherImpl(symmCipher);
KMCipherImpl cipher = new KMCipherImpl(symmCipher);
cipher.setCipherAlgorithm(cipherAlg);
cipher.setPaddingAlgorithm(padding);
cipher.setMode(mode);
return cipher;
}

Expand All@@ -594,6 +603,7 @@ public KMCipher createGCMCipher(short mode, byte[] secret, short secretStart, sh
if(secretLength == 32){
len = KeyBuilder.LENGTH_AES_256;
}

return new KMCipherImpl(null);
}

Expand DownExpand Up@@ -729,7 +739,11 @@ public KMCipher createRsaCipher(short padding, byte[] modBuffer, short modOff, s
key.setExponent(exponent,(short)0,(short)3);
key.setModulus(modBuffer, modOff, modLength);
rsaCipher.init(key,Cipher.MODE_ENCRYPT);
return new KMCipherImpl(rsaCipher);
KMCipherImpl inst = new KMCipherImpl(rsaCipher);
inst.setCipherAlgorithm(cipherAlg);
inst.setMode(Cipher.MODE_ENCRYPT);
inst.setPaddingAlgorithm(padding);
return inst;
}

@Override
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -263,6 +263,9 @@ public boolean isValidPurpose(byte alg){
return false;
}
break;
case KMType.WRAP_KEY:
if(alg != KMType.RSA) return false;
break;
default:
return false;
}
Expand Down
Loading