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

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

Introduction

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

Prototype

public void init(final CipherParameters params) throws IllegalArgumentException 

Source Link

Document

Initialises the GMAC - requires a ParametersWithIV providing a KeyParameter and a nonce.

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);

    byte[] input = ByteBuffer.allocate(sc.length + authenticationKey.length + challenge.length).put(sc)
            .put(authenticationKey).put(challenge).array();
    mac.update(input, 0, input.length);//from  w  ww .j  av a  2  s. c  om
    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();
}