Example usage for org.bouncycastle.crypto.macs ISO9797Alg3Mac ISO9797Alg3Mac

List of usage examples for org.bouncycastle.crypto.macs ISO9797Alg3Mac ISO9797Alg3Mac

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.macs ISO9797Alg3Mac ISO9797Alg3Mac.

Prototype

public ISO9797Alg3Mac(BlockCipher cipher, int macSizeInBits, BlockCipherPadding padding) 

Source Link

Document

create a standard MAC based on a block cipher with the size of the MAC been given in bits.

Usage

From source file:com.licel.jcardsim.crypto.SymmetricSignatureImpl.java

License:Apache License

public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException {
    if (theKey == null) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }//from w  w  w  .  java  2  s.  co  m
    if (!theKey.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    if (!(theKey instanceof SymmetricKeyImpl)) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    CipherParameters cipherParams = null;
    BlockCipher cipher = ((SymmetricKeyImpl) theKey).getCipher();
    if (bArray == null) {
        cipherParams = ((SymmetricKeyImpl) theKey).getParameters();
    } else {
        if (bLen != cipher.getBlockSize()) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        cipherParams = new ParametersWithIV(((SymmetricKeyImpl) theKey).getParameters(), bArray, bOff, bLen);
    }
    switch (algorithm) {
    case ALG_DES_MAC4_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 32, null);
        break;
    case ALG_DES_MAC8_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 64, null);
        break;
    case ALG_DES_MAC4_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 32, new ZeroBytePadding());
        break;
    case ALG_DES_MAC8_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 64, new ZeroBytePadding());
        break;
    case ALG_DES_MAC4_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 32, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_1_M2_ALG3:
        engine = new ISO9797Alg3Mac(new DESEngine(), 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC4_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 32, new PKCS7Padding());
        break;
    case ALG_DES_MAC8_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 64, new PKCS7Padding());
        break;
    case ALG_AES_MAC_128_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 128, null);
        break;
    case ALG_HMAC_SHA1:
        engine = new HMac(new SHA1Digest());
        break;
    case ALG_HMAC_SHA_256:
        engine = new HMac(new SHA256Digest());
        break;
    case ALG_HMAC_SHA_384:
        engine = new HMac(new SHA384Digest());
        break;
    case ALG_HMAC_SHA_512:
        engine = new HMac(new SHA512Digest());
        break;
    case ALG_HMAC_MD5:
        engine = new HMac(new MD5Digest());
        break;
    case ALG_HMAC_RIPEMD160:
        engine = new HMac(new RIPEMD160Digest());
        break;
    default:
        CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
        break;
    }
    engine.init(cipherParams);
    isInitialized = true;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] data) {

    byte[] n = new byte[8 + data.length];
    System.arraycopy(sscBytes, 0, n, 0, 8);
    System.arraycopy(data, 0, n, 8, data.length);

    BlockCipher cipher = new DESEngine();
    Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding());

    ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);

    mac.init(parameterIV);/*w w w  .  ja va  2s .co m*/
    mac.update(n, 0, n.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] key, byte[] data) {
    BlockCipher cipher = new DESEngine();
    Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding());

    KeyParameter keyP = new KeyParameter(key);
    mac.init(keyP);//w w w  . j a va 2  s  . c om
    mac.update(data, 0, data.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}