Search in sources :

Example 1 with Wrapper

use of com.github.zhenwei.core.crypto.Wrapper in project LinLong-Java by zhenwei1108.

the class BcPasswordRecipient method extractSecretKey.

protected KeyParameter extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
    Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
    keyEncryptionCipher.init(false, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
    try {
        return new KeyParameter(keyEncryptionCipher.unwrap(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length));
    } catch (InvalidCipherTextException e) {
        throw new CMSException("unable to unwrap key: " + e.getMessage(), e);
    }
}
Also used : Wrapper(com.github.zhenwei.core.crypto.Wrapper) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 2 with Wrapper

use of com.github.zhenwei.core.crypto.Wrapper in project LinLong-Java by zhenwei1108.

the class BcPasswordRecipientInfoGenerator method generateEncryptedBytes.

public byte[] generateEncryptedBytes(AlgorithmIdentifier keyEncryptionAlgorithm, byte[] derivedKey, GenericKey contentEncryptionKey) throws CMSException {
    byte[] contentEncryptionKeySpec = ((KeyParameter) CMSUtils.getBcKey(contentEncryptionKey)).getKey();
    Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
    keyEncryptionCipher.init(true, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
    return keyEncryptionCipher.wrap(contentEncryptionKeySpec, 0, contentEncryptionKeySpec.length);
}
Also used : Wrapper(com.github.zhenwei.core.crypto.Wrapper) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 3 with Wrapper

use of com.github.zhenwei.core.crypto.Wrapper in project LinLong-Java by zhenwei1108.

the class RFC5649WrapEngine method wrap.

public byte[] wrap(byte[] in, int inOff, int inLen) {
    if (!forWrapping) {
        throw new IllegalStateException("not set for wrapping");
    }
    byte[] iv = new byte[8];
    // MLI = size of key to be wrapped
    byte[] mli = Pack.intToBigEndian(inLen);
    // copy in the fixed portion of the AIV
    System.arraycopy(preIV, 0, iv, 0, preIV.length);
    // copy in the MLI after the AIV
    System.arraycopy(mli, 0, iv, preIV.length, mli.length);
    // get the relevant plaintext to be wrapped
    byte[] relevantPlaintext = new byte[inLen];
    System.arraycopy(in, inOff, relevantPlaintext, 0, inLen);
    byte[] paddedPlaintext = padPlaintext(relevantPlaintext);
    if (paddedPlaintext.length == 8) {
        // if the padded plaintext contains exactly 8 octets,
        // then prepend iv and encrypt using AES in ECB mode.
        // prepend the IV to the plaintext
        byte[] paddedPlainTextWithIV = new byte[paddedPlaintext.length + iv.length];
        System.arraycopy(iv, 0, paddedPlainTextWithIV, 0, iv.length);
        System.arraycopy(paddedPlaintext, 0, paddedPlainTextWithIV, iv.length, paddedPlaintext.length);
        engine.init(true, param);
        for (int i = 0; i < paddedPlainTextWithIV.length; i += engine.getBlockSize()) {
            engine.processBlock(paddedPlainTextWithIV, i, paddedPlainTextWithIV, i);
        }
        return paddedPlainTextWithIV;
    } else {
        // otherwise, apply the RFC 3394 wrap to
        // the padded plaintext with the new IV
        Wrapper wrapper = new RFC3394WrapEngine(engine);
        ParametersWithIV paramsWithIV = new ParametersWithIV(param, iv);
        wrapper.init(true, paramsWithIV);
        return wrapper.wrap(paddedPlaintext, 0, paddedPlaintext.length);
    }
}
Also used : Wrapper(com.github.zhenwei.core.crypto.Wrapper) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV)

Aggregations

Wrapper (com.github.zhenwei.core.crypto.Wrapper)3 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)1 CMSException (com.github.zhenwei.pkix.cms.CMSException)1