Search in sources :

Example 1 with StreamCipher

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

the class BcRSAKeyTransEnvelopedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    CipherParameters secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
    final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm);
    return new RecipientOperator(new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataIn) {
            if (dataCipher instanceof BufferedBlockCipher) {
                return new CipherInputStream(dataIn, (BufferedBlockCipher) dataCipher);
            } else {
                return new CipherInputStream(dataIn, (StreamCipher) dataCipher);
            }
        }
    });
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputStream(java.io.InputStream) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with StreamCipher

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

the class Seed method prg.

static void prg(byte[] r, int rOff, long rlen, byte[] key, int keyOff) {
    byte[] nonce = new byte[8];
    StreamCipher cipher = new ChaChaEngine(12);
    cipher.init(true, new ParametersWithIV(new KeyParameter(key, keyOff, 32), nonce));
    cipher.processBytes(r, rOff, (int) rlen, r, rOff);
// crypto_stream_chacha12(r, rlen, nonce, key);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) ChaChaEngine(com.github.zhenwei.core.crypto.engines.ChaChaEngine) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher)

Example 3 with StreamCipher

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

the class BcKEKEnvelopedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    KeyParameter secretKey = (KeyParameter) extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
    final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm);
    return new RecipientOperator(new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataOut) {
            if (dataCipher instanceof BufferedBlockCipher) {
                return new com.github.zhenwei.core.crypto.io.CipherInputStream(dataOut, (BufferedBlockCipher) dataCipher);
            } else {
                return new com.github.zhenwei.core.crypto.io.CipherInputStream(dataOut, (StreamCipher) dataCipher);
            }
        }
    });
}
Also used : InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) InputStream(java.io.InputStream) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 4 with StreamCipher

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

the class BcPasswordEnvelopedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
    KeyParameter secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, derivedKey, encryptedContentEncryptionKey);
    final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm);
    return new RecipientOperator(new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataOut) {
            if (dataCipher instanceof BufferedBlockCipher) {
                return new CipherInputStream(dataOut, (BufferedBlockCipher) dataCipher);
            } else {
                return new CipherInputStream(dataOut, (StreamCipher) dataCipher);
            }
        }
    });
}
Also used : CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputStream(java.io.InputStream) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 5 with StreamCipher

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

the class CipherFactory method createContentCipher.

/**
 * Create a content cipher for encrypting bulk data.
 *
 * @param forEncryption   true if the cipher is for encryption, false otherwise.
 * @param encKey          the basic key to use.
 * @param encryptionAlgID identifying algorithm OID and parameters to use.
 * @return a StreamCipher or a BufferedBlockCipher depending on the algorithm.
 * @throws IllegalArgumentException
 */
public static Object createContentCipher(boolean forEncryption, CipherParameters encKey, AlgorithmIdentifier encryptionAlgID) throws IllegalArgumentException {
    ASN1ObjectIdentifier encAlg = encryptionAlgID.getAlgorithm();
    if (encAlg.equals(PKCSObjectIdentifiers.rc4)) {
        StreamCipher cipher = new RC4Engine();
        cipher.init(forEncryption, encKey);
        return cipher;
    } else if (encAlg.equals(NISTObjectIdentifiers.id_aes128_GCM) || encAlg.equals(NISTObjectIdentifiers.id_aes192_GCM) || encAlg.equals(NISTObjectIdentifiers.id_aes256_GCM)) {
        AEADBlockCipher cipher = createAEADCipher(encryptionAlgID.getAlgorithm());
        GCMParameters gcmParameters = GCMParameters.getInstance(encryptionAlgID.getParameters());
        if (!(encKey instanceof KeyParameter)) {
            throw new IllegalArgumentException("key data must be accessible for GCM operation");
        }
        AEADParameters aeadParameters = new AEADParameters((KeyParameter) encKey, gcmParameters.getIcvLen() * 8, gcmParameters.getNonce());
        cipher.init(forEncryption, aeadParameters);
        return cipher;
    } else if (encAlg.equals(NISTObjectIdentifiers.id_aes128_CCM) || encAlg.equals(NISTObjectIdentifiers.id_aes192_CCM) || encAlg.equals(NISTObjectIdentifiers.id_aes256_CCM)) {
        AEADBlockCipher cipher = createAEADCipher(encryptionAlgID.getAlgorithm());
        CCMParameters ccmParameters = CCMParameters.getInstance(encryptionAlgID.getParameters());
        if (!(encKey instanceof KeyParameter)) {
            throw new IllegalArgumentException("key data must be accessible for GCM operation");
        }
        AEADParameters aeadParameters = new AEADParameters((KeyParameter) encKey, ccmParameters.getIcvLen() * 8, ccmParameters.getNonce());
        cipher.init(forEncryption, aeadParameters);
        return cipher;
    } else {
        BufferedBlockCipher cipher = createCipher(encryptionAlgID.getAlgorithm());
        ASN1Primitive sParams = encryptionAlgID.getParameters().toASN1Primitive();
        if (sParams != null && !(sParams instanceof ASN1Null)) {
            if (encAlg.equals(PKCSObjectIdentifiers.des_EDE3_CBC) || encAlg.equals(AlgorithmIdentifierFactory.IDEA_CBC) || encAlg.equals(NISTObjectIdentifiers.id_aes128_CBC) || encAlg.equals(NISTObjectIdentifiers.id_aes192_CBC) || encAlg.equals(NISTObjectIdentifiers.id_aes256_CBC) || encAlg.equals(NTTObjectIdentifiers.id_camellia128_cbc) || encAlg.equals(NTTObjectIdentifiers.id_camellia192_cbc) || encAlg.equals(NTTObjectIdentifiers.id_camellia256_cbc) || encAlg.equals(KISAObjectIdentifiers.id_seedCBC) || encAlg.equals(OIWObjectIdentifiers.desCBC)) {
                cipher.init(forEncryption, new ParametersWithIV(encKey, ASN1OctetString.getInstance(sParams).getOctets()));
            } else if (encAlg.equals(AlgorithmIdentifierFactory.CAST5_CBC)) {
                CAST5CBCParameters cbcParams = CAST5CBCParameters.getInstance(sParams);
                cipher.init(forEncryption, new ParametersWithIV(encKey, cbcParams.getIV()));
            } else if (encAlg.equals(PKCSObjectIdentifiers.RC2_CBC)) {
                RC2CBCParameter cbcParams = RC2CBCParameter.getInstance(sParams);
                cipher.init(forEncryption, new ParametersWithIV(new RC2Parameters(((KeyParameter) encKey).getKey(), rc2Ekb[cbcParams.getRC2ParameterVersion().intValue()]), cbcParams.getIV()));
            } else {
                throw new IllegalArgumentException("cannot match parameters");
            }
        } else {
            if (encAlg.equals(PKCSObjectIdentifiers.des_EDE3_CBC) || encAlg.equals(AlgorithmIdentifierFactory.IDEA_CBC) || encAlg.equals(AlgorithmIdentifierFactory.CAST5_CBC)) {
                cipher.init(forEncryption, new ParametersWithIV(encKey, new byte[8]));
            } else {
                cipher.init(forEncryption, encKey);
            }
        }
        return cipher;
    }
}
Also used : RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) CAST5CBCParameters(com.github.zhenwei.core.asn1.misc.CAST5CBCParameters) CCMParameters(com.github.zhenwei.core.internal.asn1.cms.CCMParameters) RC2CBCParameter(com.github.zhenwei.core.asn1.pkcs.RC2CBCParameter) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) GCMParameters(com.github.zhenwei.core.internal.asn1.cms.GCMParameters) AEADParameters(com.github.zhenwei.core.crypto.params.AEADParameters) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) RC4Engine(com.github.zhenwei.core.crypto.engines.RC4Engine) AEADBlockCipher(com.github.zhenwei.core.crypto.modes.AEADBlockCipher) ASN1Null(com.github.zhenwei.core.asn1.ASN1Null)

Aggregations

StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)5 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RecipientOperator (com.github.zhenwei.pkix.cms.RecipientOperator)3 InputDecryptor (com.github.zhenwei.pkix.operator.InputDecryptor)3 InputStream (java.io.InputStream)3 CipherInputStream (com.github.zhenwei.core.crypto.io.CipherInputStream)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2 ASN1Null (com.github.zhenwei.core.asn1.ASN1Null)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 CAST5CBCParameters (com.github.zhenwei.core.asn1.misc.CAST5CBCParameters)1 RC2CBCParameter (com.github.zhenwei.core.asn1.pkcs.RC2CBCParameter)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 ChaChaEngine (com.github.zhenwei.core.crypto.engines.ChaChaEngine)1 RC4Engine (com.github.zhenwei.core.crypto.engines.RC4Engine)1 AEADBlockCipher (com.github.zhenwei.core.crypto.modes.AEADBlockCipher)1 PaddedBufferedBlockCipher (com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher)1 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)1