Example usage for org.bouncycastle.crypto Wrapper init

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

Introduction

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

Prototype

public void init(boolean forWrapping, CipherParameters param);

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 {/*from   w w w .j a  v a 2s.  co  m*/
        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);
}

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

License:Apache License

@Override
public byte[] encodeCek(JWEEncryptionProvider encryptionProvider, JWEKeyStorage keyStorage, Key encryptionKey)
        throws Exception {
    Wrapper encrypter = new AESWrapEngine();
    encrypter.init(true, new KeyParameter(encryptionKey.getEncoded()));
    byte[] cekBytes = keyStorage.getCekBytes();
    return encrypter.wrap(cekBytes, 0, cekBytes.length);
}