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

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

Introduction

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

Prototype

public void update(byte[] in, int inOff, int len) throws DataLengthException, IllegalStateException 

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);/*  www.jav 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();
}