Search in sources :

Example 1 with CMac

use of org.spongycastle.crypto.macs.CMac 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 CMac

use of org.spongycastle.crypto.macs.CMac 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 CMac

use of org.spongycastle.crypto.macs.CMac 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)

Aggregations

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