Java 类org.bouncycastle.crypto.macs.CMac 实例源码

项目:GlobalPlatformPro    文件:GPCrypto.java   
private static byte[] scp03_kdf(byte[] key, byte constant, byte[] context, int blocklen_bits) {
    // 11 bytes
    byte[] label = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    try {
        bo.write(label); // 11 bytes of label
        bo.write(constant); // constant for the last byte
        bo.write(0x00); // separator
        bo.write((blocklen_bits >> 8) & 0xFF); // block size in two bytes
        bo.write(blocklen_bits & 0xFF);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    byte[] blocka = bo.toByteArray();
    byte[] blockb = context;

    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    KDFCounterBytesGenerator kdf = new KDFCounterBytesGenerator(cmac);
    kdf.init(new KDFCounterParameters(key, blocka, blockb, 8)); // counter size in bits

    byte[] cgram = new byte[blocklen_bits / 8];
    kdf.generateBytes(cgram, 0, cgram.length);
    return cgram;
}
项目:siv-mode    文件:SivMode.java   
byte[] s2v(byte[] macKey, byte[] plaintext, byte[]... associatedData) {
    // Maximum permitted AD length is the block size in bits - 2
    if (associatedData.length > 126) {
        // SIV mode cannot be used safely with this many AD fields
        throw new IllegalArgumentException("too many Associated Data fields");
    }

    final CipherParameters params = new KeyParameter(macKey);
    final CMac mac = new CMac(threadLocalCipher.get());
    mac.init(params);

    byte[] d = mac(mac, BYTES_ZERO);

    for (byte[] s : associatedData) {
        d = xor(dbl(d), mac(mac, s));
    }

    final byte[] t;
    if (plaintext.length >= 16) {
        t = xorend(plaintext, d);
    } else {
        t = xor(dbl(d), pad(plaintext));
    }

    return mac(mac, t);
}
项目:animamea    文件:AmAESCrypto.java   
@Override
public byte[] getMAC(byte[] data) {

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

    BlockCipher cipher = new AESFastEngine();
    Mac mac = new CMac(cipher, 64);

    mac.init(keyP);
    mac.update(n, 0, n.length);
    byte[] out = new byte[mac.getMacSize()];

    mac.doFinal(out, 0);

    return out;
}
项目:mDL-ILP    文件:AESUtils.java   
/**
 * AES [FIPS 197] SHALL be used in CMAC-mode [SP 800-38B] with a MAC length of 8 bytes.
 *
 * @param data the data to MAC
 * @param key the key to use
 * @return the 8 byte MAC of the data
 */
public static byte[] performCBC8(byte[] data, byte[] key) {

    // mac size in bits (64 bits = 8 bytes)
    final Mac cbc8 = new CMac(new AESEngine(), 64);
    CipherParameters params = new KeyParameter(key);
    cbc8.init(params);

    byte[] result = new byte[8];
    cbc8.update(data, 0, data.length);
    cbc8.doFinal(result, 0);

    return result;
}
项目:mDL-ILP    文件:AESUtils.java   
/**
 * AES [FIPS 197] SHALL be used in CMAC-mode [SP 800-38B] with a MAC length of 8 bytes.
 *
 * @param data the data to MAC
 * @param key the key to use
 * @return the 8 byte MAC of the data
 */
public static byte[] performCBC8(byte[] data, byte[] key) {

    // mac size in bits (64 bits = 8 bytes)
    final Mac cbc8 = new CMac(new AESEngine(), 64);
    CipherParameters params = new KeyParameter(key);
    cbc8.init(params);

    byte[] result = new byte[8];
    cbc8.update(data, 0, data.length);
    cbc8.doFinal(result, 0);

    return result;
}
项目:ipack    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    bufBlock = new byte[blockSize * 2];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:gwt-crypto    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:gwt-crypto    文件:CMacTest.java   
private void testExceptions()
{
    try 
    {
        CMac mac = new CMac(new AESEngine());
        mac.init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]));
        fail("CMac does not accept IV");
    } catch(IllegalArgumentException e)
    {
        // Expected
    }
}
项目:Aki-SSL    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:animamea    文件:AmAESCrypto.java   
@Override
public byte[] getMAC(byte[] key, byte[] data) {
    BlockCipher cipher = new AESFastEngine();
    Mac mac = new CMac(cipher, 64); // TODO Padding der Daten
    KeyParameter keyP = new KeyParameter(key);
    mac.init(keyP);

    mac.update(data, 0, data.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}
项目:TinyTravelTracker    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:CryptMeme    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    bufBlock = new byte[blockSize * 2];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:GlobalPlatformPro    文件:GPCrypto.java   
public static byte[] scp03_mac(byte[] keybytes, byte[] msg, int lengthBits) {
    // Use BouncyCastle light interface.
    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    cmac.init(new KeyParameter(keybytes));
    cmac.update(msg, 0, msg.length);
    byte[] out = new byte[cmac.getMacSize()];
    cmac.doFinal(out, 0);
    return Arrays.copyOf(out, lengthBits / 8);
}
项目:irma_future_id    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    bufBlock = new byte[blockSize * 2];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:irma_future_id    文件:MacThroughputTest.java   
public static void main(String[] args)
{
    testMac(new HMac(new SHA1Digest()), new KeyParameter(generateNonce(20)), 3);
    testMac(new SkeinMac(SkeinMac.SKEIN_512, 128), new KeyParameter(generateNonce(64)), 2);
    testMac(new SipHash(), new KeyParameter(generateNonce(16)), 1);
    testMac(new CMac(new AESFastEngine()), new KeyParameter(generateNonce(16)), 3);
    testMac(new GMac(new GCMBlockCipher(new AESFastEngine())), new ParametersWithIV(new KeyParameter(
            generateNonce(16)), generateNonce(16)), 5);
    testMac(new Poly1305(new NullEngine(16)), new ParametersWithIV(generatePoly1305Key(), generateNonce(16)), 1);
    testMac(new Poly1305(new AESFastEngine()), new ParametersWithIV(generatePoly1305Key(), generateNonce(16)), 1);
    testMac(new Poly1305Reference(new NullEngine(16)), new ParametersWithIV(generatePoly1305Key(),
            generateNonce(16)), 1);
}
项目:irma_future_id    文件:CMacTest.java   
private void testExceptions()
{
    try 
    {
        CMac mac = new CMac(new AESEngine());
        mac.init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]));
        fail("CMac does not accept IV");
    } catch(IllegalArgumentException e)
    {
        // Expected
    }
}
项目:bc-java    文件:EAXBlockCipher.java   
/**
 * Constructor that accepts an instance of a block cipher engine.
 *
 * @param cipher the engine to use
 */
public EAXBlockCipher(BlockCipher cipher)
{
    blockSize = cipher.getBlockSize();
    mac = new CMac(cipher);
    macBlock = new byte[blockSize];
    bufBlock = new byte[blockSize * 2];
    associatedTextMac = new byte[mac.getMacSize()];
    nonceMac = new byte[mac.getMacSize()];
    this.cipher = new SICBlockCipher(cipher);
}
项目:bc-java    文件:CMacTest.java   
private void testExceptions()
{
    try 
    {
        CMac mac = new CMac(new AESEngine());
        mac.init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]));
        fail("CMac does not accept IV");
    } catch(IllegalArgumentException e)
    {
        // Expected
    }
}
项目:ipack    文件:DES.java   
public CMAC()
{
    super(new CMac(new DESEngine()));
}
项目:ipack    文件:DESede.java   
public CMAC()
{
    super(new CMac(new DESedeEngine()));
}
项目:ipack    文件:AES.java   
public AESCMAC()
{
    super(new CMac(new AESFastEngine()));
}
项目:gwt-crypto    文件:CMacTest.java   
private void testCMac(BlockCipher cipher, byte[] keyBytes, byte[] input, byte[] expected)
{
    Mac mac = new CMac(cipher, cipher.getBlockSize() * 8);

    KeyParameter key = new KeyParameter(keyBytes);

    mac.init(key);

    mac.update(input, 0, input.length);

    byte[] out = new byte[mac.getMacSize()];

    mac.doFinal(out, 0);

    if (!areEqual(out, expected))
    {
        fail("Failed - expected " + Strings.fromByteArray(Hex.encode(expected)) + " got " + new String(Hex.encode(out)));
    }
}
项目:Aki-SSL    文件:DES.java   
public CMAC()
{
    super(new CMac(new DESEngine()));
}
项目:Aki-SSL    文件:SEED.java   
public CMAC()
{
    super(new CMac(new SEEDEngine()));
}
项目:Aki-SSL    文件:Shacal2.java   
public CMAC()
{
    super(new CMac(new Shacal2Engine()));
}
项目:Aki-SSL    文件:SM4.java   
public CMAC()
{
    super(new CMac(new SM4Engine()));
}
项目:Aki-SSL    文件:DESede.java   
public CMAC()
{
    super(new CMac(new DESedeEngine()));
}
项目:Aki-SSL    文件:Threefish.java   
public CMAC_256()
{
    super(new CMac(new ThreefishEngine(256)));
}
项目:Aki-SSL    文件:Threefish.java   
public CMAC_512()
{
    super(new CMac(new ThreefishEngine(512)));
}
项目:Aki-SSL    文件:Threefish.java   
public CMAC_1024()
{
    super(new CMac(new ThreefishEngine(1024)));
}
项目:Aki-SSL    文件:Blowfish.java   
public CMAC()
{
    super(new CMac(new BlowfishEngine()));
}
项目:Aki-SSL    文件:AES.java   
public AESCMAC()
{
    super(new CMac(new AESFastEngine()));
}
项目:CryptMeme    文件:DES.java   
public CMAC()
{
    super(new CMac(new DESEngine()));
}
项目:CryptMeme    文件:DESede.java   
public CMAC()
{
    super(new CMac(new DESedeEngine()));
}
项目:CryptMeme    文件:AES.java   
public AESCMAC()
{
    super(new CMac(new AESFastEngine()));
}
项目:irma_future_id    文件:DES.java   
public CMAC()
{
    super(new CMac(new DESEngine()));
}
项目:irma_future_id    文件:DESede.java   
public CMAC()
{
    super(new CMac(new DESedeEngine()));
}
项目:irma_future_id    文件:AES.java   
public AESCMAC()
{
    super(new CMac(new AESFastEngine()));
}
项目:bc-java    文件:DES.java   
public CMAC()
{
    super(new CMac(new DESEngine()));
}
项目:bc-java    文件:DESede.java   
public CMAC()
{
    super(new CMac(new DESedeEngine()));
}