Search in sources :

Example 1 with BlockCipher

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;
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) CMac(org.spongycastle.crypto.macs.CMac) BlockCipher(org.spongycastle.crypto.BlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter)

Example 2 with BlockCipher

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;
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) CMac(org.spongycastle.crypto.macs.CMac) BlockCipher(org.spongycastle.crypto.BlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) XTEAEngine(org.spongycastle.crypto.engines.XTEAEngine)

Example 3 with BlockCipher

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;
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) CMac(org.spongycastle.crypto.macs.CMac) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) BlockCipher(org.spongycastle.crypto.BlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) Mac(org.spongycastle.crypto.Mac) CMac(org.spongycastle.crypto.macs.CMac)

Example 4 with BlockCipher

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;
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) BlockCipher(org.spongycastle.crypto.BlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter)

Aggregations

BlockCipher (org.spongycastle.crypto.BlockCipher)4 AESEngine (org.spongycastle.crypto.engines.AESEngine)4 CMac (org.spongycastle.crypto.macs.CMac)3 KeyParameter (org.spongycastle.crypto.params.KeyParameter)3 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)2 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)2 Mac (org.spongycastle.crypto.Mac)1 XTEAEngine (org.spongycastle.crypto.engines.XTEAEngine)1