use of org.spongycastle.crypto.BlockCipher in project jmulticard by ctt-gob-es.
the class JseCryptoHelper method doAesCmac.
@Override
public byte[] doAesCmac(final byte[] data, final byte[] key) {
final BlockCipher cipher = new AESEngine();
final org.spongycastle.crypto.Mac mac = new CMac(cipher, 64);
final KeyParameter keyP = new KeyParameter(key);
mac.init(keyP);
mac.update(data, 0, data.length);
final byte[] out = new byte[8];
mac.doFinal(out, 0);
return out;
}
use of org.spongycastle.crypto.BlockCipher in project fitness-app by seemoo-lab.
the class AuthenticationInteraction method generateCMAC.
/**
* Calculates the cmac.
*
* @param sequenceNumber The sequence number to calculate the cmac with.
* @param authenticationKey The authentication key to calculate the cmac with.
* @return The calculated cmac.
*/
private String generateCMAC(byte[] sequenceNumber, String authenticationKey) {
String seqNum = Utilities.byteArrayToHexString(sequenceNumber);
if (seqNum.length() > 4 && seqNum.substring(0, 4).equals(ConstantValues.AUTHENTICATION_CHALLENGE)) {
seqNum = seqNum.substring(20);
seqNum = Utilities.rotateBytes(seqNum);
byte[] authKey = Utilities.hexStringToByteArray(authenticationKey);
BlockCipher cypher;
switch(deviceType) {
case 1:
cypher = new XTEAEngine();
break;
case 2:
cypher = new AESEngine();
break;
default:
cypher = null;
Log.e(TAG, "Error: AuthenticationInteraction key not available!");
}
if (cypher != null && authKey != null) {
CMac mac = new CMac(cypher, 64);
mac.init(new KeyParameter(authKey));
byte[] counter = Utilities.intToByteArray(Integer.parseInt(seqNum, 16));
mac.update(counter, 0, 4);
byte[] bArr = new byte[8];
mac.doFinal(bArr, 0);
return Utilities.byteArrayToHexString(bArr);
}
}
return null;
}
use of org.spongycastle.crypto.BlockCipher in project jmulticard by ctt-gob-es.
the class AmAESCrypto method getMAC.
@Override
public byte[] getMAC(final byte[] data) {
byte[] n = new byte[this.sscBytes.length + data.length];
System.arraycopy(this.sscBytes, 0, n, 0, this.sscBytes.length);
System.arraycopy(data, 0, n, this.sscBytes.length, data.length);
n = addPadding(n);
final BlockCipher cipher = new AESEngine();
final Mac mac = new CMac(cipher, 64);
mac.init(this.keyP);
mac.update(n, 0, n.length);
final byte[] out = new byte[mac.getMacSize()];
mac.doFinal(out, 0);
return out;
}
use of org.spongycastle.crypto.BlockCipher in project jmulticard by ctt-gob-es.
the class AmAESCrypto method encryptBlock.
/**
* Encripta un bloque usando AES.
* @param key Clave AES.
* @param z Bloque a crifrar.
* @return Bloque cifrado.
*/
public static byte[] encryptBlock(final byte[] key, final byte[] z) {
final byte[] s = new byte[BLOCK_SIZE];
final KeyParameter encKey = new KeyParameter(key);
final BlockCipher cipher = new AESEngine();
cipher.init(true, encKey);
cipher.processBlock(z, 0, s, 0);
return s;
}
Aggregations