Example usage for org.bouncycastle.crypto.generators KDFCounterBytesGenerator init

List of usage examples for org.bouncycastle.crypto.generators KDFCounterBytesGenerator init

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.generators KDFCounterBytesGenerator init.

Prototype

public void init(DerivationParameters param) 

Source Link

Usage

From source file:com.github.horrorho.inflatabledonkey.crypto.NISTKDF.java

License:Open Source License

public static byte[] ctrHMac(byte[] keyDerivationKey, byte[] label, Supplier<Digest> digestSupplier,
        int keyLengthBytes) {

    logger.trace("<< ctrHMac() - keyDerivationKey: 0x{} label: {} digestSupplier: {} length: {}",
            Hex.toHexString(keyDerivationKey), Hex.toHexString(label), digestSupplier, keyLengthBytes);

    byte[] derivedKey = new byte[keyLengthBytes];

    // fixedInputData = label || 0x00 || dkLen in bits as 4 bytes big endian
    ByteBuffer buffer = ByteBuffer.allocate(label.length + 5);
    buffer.put(label);/*from   w w w. j  av  a  2 s .  c  om*/
    buffer.put((byte) 0);
    buffer.putInt(keyLengthBytes * 8);
    byte[] fixedInputData = buffer.array();
    logger.debug("-- ctrHMac() - fixed input data: 0x{}", Hex.toHexString(fixedInputData));

    HMac hMac = new HMac(digestSupplier.get());
    KDFCounterBytesGenerator generator = new KDFCounterBytesGenerator(hMac);
    generator.init(new KDFCounterParameters(keyDerivationKey, fixedInputData, R));
    generator.generateBytes(derivedKey, 0, derivedKey.length);

    logger.trace(">> ctrHMac() - derivedKey: 0x{}", Hex.toHexString(derivedKey));
    return derivedKey;
}