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

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

Introduction

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

Prototype

public GMac(final GCMBlockCipher cipher, final int macSizeBits) 

Source Link

Document

Creates a GMAC based on the operation of a 128 bit block cipher in GCM mode.

Usage

From source file:org.openmuc.jdlms.internal.security.HlsProcessorGmac.java

License:Open Source License

@Override
public byte[] process(byte[] challenge, byte[] authenticationKey, byte[] encryptionKey, byte[] systemTitle,
        int frameCounter) throws IOException, UnsupportedOperationException {

    byte[] sc = new byte[] { 0x10 };
    byte[] frameCounterBytes = ByteBuffer.allocate(4).putInt(frameCounter).array();
    byte[] iv = ByteBuffer.allocate(systemTitle.length + frameCounterBytes.length).put(systemTitle)
            .put(frameCounterBytes).array();

    CipherParameters cipherParameters = new KeyParameter(encryptionKey);
    ParametersWithIV parameterWithIV = new ParametersWithIV(cipherParameters, iv);

    GMac mac = new GMac(new GCMBlockCipher(new AESFastEngine()), 96);

    mac.init(parameterWithIV);// w w w  . j  a  v  a2s .com

    byte[] input = ByteBuffer.allocate(sc.length + authenticationKey.length + challenge.length).put(sc)
            .put(authenticationKey).put(challenge).array();
    mac.update(input, 0, input.length);
    final byte[] generatedMac = new byte[mac.getMacSize()];
    mac.doFinal(generatedMac, 0);

    return ByteBuffer.allocate(sc.length + frameCounterBytes.length + generatedMac.length).put(sc)
            .put(frameCounterBytes).put(generatedMac).array();
}