Search in sources :

Example 1 with PBKDF2Config

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

the class BcFKSKeyStoreSpi method generatePkbdAlgorithmIdentifier.

private KeyDerivationFunc generatePkbdAlgorithmIdentifier(PBKDFConfig pbkdfConfig, int keySizeInBytes) {
    if (MiscObjectIdentifiers.id_scrypt.equals(pbkdfConfig.getAlgorithm())) {
        ScryptConfig scryptConfig = (ScryptConfig) pbkdfConfig;
        byte[] pbkdSalt = new byte[scryptConfig.getSaltLength()];
        getDefaultSecureRandom().nextBytes(pbkdSalt);
        ScryptParams params = new ScryptParams(pbkdSalt, scryptConfig.getCostParameter(), scryptConfig.getBlockSize(), scryptConfig.getParallelizationParameter(), keySizeInBytes);
        return new KeyDerivationFunc(MiscObjectIdentifiers.id_scrypt, params);
    } else {
        PBKDF2Config pbkdf2Config = (PBKDF2Config) pbkdfConfig;
        byte[] pbkdSalt = new byte[pbkdf2Config.getSaltLength()];
        getDefaultSecureRandom().nextBytes(pbkdSalt);
        return new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(pbkdSalt, pbkdf2Config.getIterationCount(), keySizeInBytes, pbkdf2Config.getPRF()));
    }
}
Also used : PBKDF2Config(com.github.zhenwei.core.crypto.util.PBKDF2Config) KeyDerivationFunc(com.github.zhenwei.core.asn1.pkcs.KeyDerivationFunc) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) ScryptConfig(com.github.zhenwei.core.crypto.util.ScryptConfig) ScryptParams(com.github.zhenwei.core.asn1.misc.ScryptParams)

Example 2 with PBKDF2Config

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

the class JcePKCSPBEOutputEncryptorBuilder method build.

public OutputEncryptor build(final char[] password) throws OperatorCreationException {
    final Cipher cipher;
    SecretKey key;
    if (random == null) {
        random = new SecureRandom();
    }
    final AlgorithmIdentifier encryptionAlg;
    try {
        if (isPKCS12(algorithm)) {
            byte[] salt = new byte[20];
            random.nextBytes(salt);
            cipher = helper.createCipher(algorithm.getId());
            cipher.init(Cipher.ENCRYPT_MODE, new PKCS12KeyWithParameters(password, salt, iterationCount));
            encryptionAlg = new AlgorithmIdentifier(algorithm, new PKCS12PBEParams(salt, iterationCount));
        } else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
            PBKDFConfig pbkDef = (pbkdf == null) ? pbkdfBuilder.build() : pbkdf;
            if (MiscObjectIdentifiers.id_scrypt.equals(pbkDef.getAlgorithm())) {
                ScryptConfig skdf = (ScryptConfig) pbkDef;
                byte[] salt = new byte[skdf.getSaltLength()];
                random.nextBytes(salt);
                ScryptParams params = new ScryptParams(salt, skdf.getCostParameter(), skdf.getBlockSize(), skdf.getParallelizationParameter());
                SecretKeyFactory keyFact = helper.createSecretKeyFactory("SCRYPT");
                key = keyFact.generateSecret(new ScryptKeySpec(password, salt, skdf.getCostParameter(), skdf.getBlockSize(), skdf.getParallelizationParameter(), keySizeProvider.getKeySize(new AlgorithmIdentifier(keyEncAlgorithm))));
                cipher = helper.createCipher(keyEncAlgorithm.getId());
                cipher.init(Cipher.ENCRYPT_MODE, simplifyPbeKey(key), random);
                AlgorithmParameters algP = cipher.getParameters();
                PBES2Parameters algParams;
                if (algP != null) {
                    algParams = new PBES2Parameters(new KeyDerivationFunc(MiscObjectIdentifiers.id_scrypt, params), new EncryptionScheme(keyEncAlgorithm, ASN1Primitive.fromByteArray(cipher.getParameters().getEncoded())));
                } else {
                    algParams = new PBES2Parameters(new KeyDerivationFunc(MiscObjectIdentifiers.id_scrypt, params), new EncryptionScheme(keyEncAlgorithm));
                }
                encryptionAlg = new AlgorithmIdentifier(algorithm, algParams);
            } else {
                PBKDF2Config pkdf = (PBKDF2Config) pbkDef;
                byte[] salt = new byte[pkdf.getSaltLength()];
                random.nextBytes(salt);
                SecretKeyFactory keyFact = helper.createSecretKeyFactory(JceUtils.getAlgorithm(pkdf.getPRF().getAlgorithm()));
                key = keyFact.generateSecret(new PBEKeySpec(password, salt, pkdf.getIterationCount(), keySizeProvider.getKeySize(new AlgorithmIdentifier(keyEncAlgorithm))));
                cipher = helper.createCipher(keyEncAlgorithm.getId());
                cipher.init(Cipher.ENCRYPT_MODE, simplifyPbeKey(key), random);
                AlgorithmParameters algP = cipher.getParameters();
                PBES2Parameters algParams;
                if (algP != null) {
                    algParams = new PBES2Parameters(new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, pkdf.getIterationCount(), pkdf.getPRF())), new EncryptionScheme(keyEncAlgorithm, ASN1Primitive.fromByteArray(cipher.getParameters().getEncoded())));
                } else {
                    algParams = new PBES2Parameters(new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, pkdf.getIterationCount(), pkdf.getPRF())), new EncryptionScheme(keyEncAlgorithm));
                }
                encryptionAlg = new AlgorithmIdentifier(algorithm, algParams);
            }
        } else {
            throw new OperatorCreationException("unrecognised algorithm");
        }
        return new OutputEncryptor() {

            public AlgorithmIdentifier getAlgorithmIdentifier() {
                return encryptionAlg;
            }

            public OutputStream getOutputStream(OutputStream out) {
                return new CipherOutputStream(out, cipher);
            }

            public GenericKey getKey() {
                if (isPKCS12(encryptionAlg.getAlgorithm())) {
                    return new GenericKey(encryptionAlg, PKCS12PasswordToBytes(password));
                } else {
                    return new GenericKey(encryptionAlg, PKCS5PasswordToBytes(password));
                }
            }
        };
    } catch (Exception e) {
        throw new OperatorCreationException("unable to create OutputEncryptor: " + e.getMessage(), e);
    }
}
Also used : PBKDF2Config(com.github.zhenwei.core.crypto.util.PBKDF2Config) PBEKeySpec(javax.crypto.spec.PBEKeySpec) EncryptionScheme(com.github.zhenwei.core.asn1.pkcs.EncryptionScheme) CipherOutputStream(com.github.zhenwei.provider.jcajce.io.CipherOutputStream) OutputStream(java.io.OutputStream) CipherOutputStream(com.github.zhenwei.provider.jcajce.io.CipherOutputStream) ScryptConfig(com.github.zhenwei.core.crypto.util.ScryptConfig) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) KeyDerivationFunc(com.github.zhenwei.core.asn1.pkcs.KeyDerivationFunc) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) ScryptParams(com.github.zhenwei.core.asn1.misc.ScryptParams) SecretKeyFactory(javax.crypto.SecretKeyFactory) PKCS12KeyWithParameters(com.github.zhenwei.provider.jcajce.PKCS12KeyWithParameters) PBKDFConfig(com.github.zhenwei.core.crypto.util.PBKDFConfig) PBES2Parameters(com.github.zhenwei.core.asn1.pkcs.PBES2Parameters) SecureRandom(java.security.SecureRandom) ScryptKeySpec(com.github.zhenwei.provider.jcajce.spec.ScryptKeySpec) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) Cipher(javax.crypto.Cipher) OutputEncryptor(com.github.zhenwei.pkix.operator.OutputEncryptor) AlgorithmParameters(java.security.AlgorithmParameters)

Example 3 with PBKDF2Config

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

the class BcFKSKeyStoreSpi method isSimilarHmacPbkd.

private boolean isSimilarHmacPbkd(PBKDFConfig storePBKDFConfig, KeyDerivationFunc hmacPkbdAlgorithm) {
    if (!storePBKDFConfig.getAlgorithm().equals(hmacPkbdAlgorithm.getAlgorithm())) {
        return false;
    }
    if (MiscObjectIdentifiers.id_scrypt.equals(hmacPkbdAlgorithm.getAlgorithm())) {
        if (!(storePBKDFConfig instanceof ScryptConfig)) {
            return false;
        }
        ScryptConfig scryptConfig = (ScryptConfig) storePBKDFConfig;
        ScryptParams sParams = ScryptParams.getInstance(hmacPkbdAlgorithm.getParameters());
        if (scryptConfig.getSaltLength() != sParams.getSalt().length || scryptConfig.getBlockSize() != sParams.getBlockSize().intValue() || scryptConfig.getCostParameter() != sParams.getCostParameter().intValue() || scryptConfig.getParallelizationParameter() != sParams.getParallelizationParameter().intValue()) {
            return false;
        }
    } else {
        if (!(storePBKDFConfig instanceof PBKDF2Config)) {
            return false;
        }
        PBKDF2Config pbkdf2Config = (PBKDF2Config) storePBKDFConfig;
        PBKDF2Params pbkdf2Params = PBKDF2Params.getInstance(hmacPkbdAlgorithm.getParameters());
        if (pbkdf2Config.getSaltLength() != pbkdf2Params.getSalt().length || pbkdf2Config.getIterationCount() != pbkdf2Params.getIterationCount().intValue()) {
            return false;
        }
    }
    return true;
}
Also used : PBKDF2Config(com.github.zhenwei.core.crypto.util.PBKDF2Config) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) ScryptConfig(com.github.zhenwei.core.crypto.util.ScryptConfig) ScryptParams(com.github.zhenwei.core.asn1.misc.ScryptParams)

Aggregations

ScryptParams (com.github.zhenwei.core.asn1.misc.ScryptParams)3 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)3 PBKDF2Config (com.github.zhenwei.core.crypto.util.PBKDF2Config)3 ScryptConfig (com.github.zhenwei.core.crypto.util.ScryptConfig)3 KeyDerivationFunc (com.github.zhenwei.core.asn1.pkcs.KeyDerivationFunc)2 EncryptionScheme (com.github.zhenwei.core.asn1.pkcs.EncryptionScheme)1 PBES2Parameters (com.github.zhenwei.core.asn1.pkcs.PBES2Parameters)1 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 PBKDFConfig (com.github.zhenwei.core.crypto.util.PBKDFConfig)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)1 OutputEncryptor (com.github.zhenwei.pkix.operator.OutputEncryptor)1 PKCS12KeyWithParameters (com.github.zhenwei.provider.jcajce.PKCS12KeyWithParameters)1 CipherOutputStream (com.github.zhenwei.provider.jcajce.io.CipherOutputStream)1 ScryptKeySpec (com.github.zhenwei.provider.jcajce.spec.ScryptKeySpec)1 OutputStream (java.io.OutputStream)1 AlgorithmParameters (java.security.AlgorithmParameters)1 SecureRandom (java.security.SecureRandom)1 Cipher (javax.crypto.Cipher)1