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

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

Introduction

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

Prototype

public int getMacSize() 

Source Link

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);//from w  ww .ja  v a  2 s .co  m

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