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

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

Introduction

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

Prototype

public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException 

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);//  ww w  .  ja va  2s .  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;
}