Example usage for org.bouncycastle.crypto Wrapper unwrap

List of usage examples for org.bouncycastle.crypto Wrapper unwrap

Introduction

In this page you can find the example usage for org.bouncycastle.crypto Wrapper unwrap.

Prototype

public byte[] unwrap(byte[] in, int inOff, int inLen) throws InvalidCipherTextException;

Source Link

Usage

From source file:com.github.horrorho.inflatabledonkey.crypto.rfc6637.RFC6637.java

License:Open Source License

public byte[] unwrap(byte[] data, byte[] fingerprint, BigInteger d) {
    try {/*ww w  . j  av a2  s.  com*/
        logger.trace("-- unwrap() - data: 0x{} fingerprint: 0x{} d: 0x{}", Hex.toHexString(data),
                Hex.toHexString(fingerprint), d.toString(16));
        // TODO write verifcation/ exception handling code.
        ByteBuffer buffer = ByteBuffer.wrap(data);

        int wKeySize = (buffer.getShort() + 7) / 8;
        byte[] wKey = new byte[wKeySize];
        buffer.get(wKey);

        int wrappedSize = Byte.toUnsignedInt(buffer.get());
        byte[] wrapped = new byte[wrappedSize];
        buffer.get(wrapped);

        ECPoint Q = decodePoint(wKey);

        // ECDH assuming curve has a cofactor of 1
        ECPoint S = Q.multiply(d).normalize();

        byte[] hash = kdf.apply(S, fingerprint);

        byte[] derivedKey = Arrays.copyOf(hash, symAlgIDKeyLength);

        Wrapper wrapper = wrapperFactory.get();
        wrapper.init(false, new KeyParameter(derivedKey));
        byte[] unwrap = wrapper.unwrap(wrapped, 0, wrapped.length);

        // TODO sym alg
        byte[] finalize = finalize(unwrap);
        logger.trace("-- unwrap() - unwrapped: 0x{}", Hex.toHexString(finalize));
        return finalize;

    } catch (IOException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException(ex);
    }
}

From source file:org.keycloak.jose.jwe.alg.AesKeyWrapAlgorithmProvider.java

License:Apache License

@Override
public byte[] decodeCek(byte[] encodedCek, Key encryptionKey) throws Exception {
    Wrapper encrypter = new AESWrapEngine();
    encrypter.init(false, new KeyParameter(encryptionKey.getEncoded()));
    return encrypter.unwrap(encodedCek, 0, encodedCek.length);
}