Search in sources :

Example 16 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project xipki by xipki.

the class DSAPlainDigestSigner method init.

@Override
public void init(boolean forSigning, CipherParameters parameters) {
    this.forSigning = forSigning;
    AsymmetricKeyParameter param;
    if (parameters instanceof ParametersWithRandom) {
        param = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters();
    } else {
        param = (AsymmetricKeyParameter) parameters;
    }
    ParamUtil.requireNonNull("param", param);
    if (param instanceof ECPublicKeyParameters) {
        keyBitLen = ((ECPublicKeyParameters) param).getParameters().getCurve().getFieldSize();
    } else if (param instanceof ECPrivateKeyParameters) {
        keyBitLen = ((ECPrivateKeyParameters) param).getParameters().getCurve().getFieldSize();
    } else if (param instanceof DSAPublicKeyParameters) {
        keyBitLen = ((DSAPublicKeyParameters) param).getParameters().getQ().bitLength();
    } else if (param instanceof DSAPrivateKeyParameters) {
        keyBitLen = ((DSAPrivateKeyParameters) param).getParameters().getQ().bitLength();
    } else {
        throw new IllegalArgumentException("unknown parameters: " + param.getClass().getName());
    }
    if (forSigning && !param.isPrivate()) {
        throw new IllegalArgumentException("Signing Requires Private Key.");
    }
    if (!forSigning && param.isPrivate()) {
        throw new IllegalArgumentException("Verification Requires Public Key.");
    }
    reset();
    dsaSigner.init(forSigning, parameters);
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 17 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project keepass2android by PhilippC.

the class JCEBlockCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    // 
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    // 
    if (params == null && baseEngine.getAlgorithmName().startsWith("RC5-64")) {
        throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
    }
    // 
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getOID() != null) {
            pbeAlgorithm = k.getOID().getId();
        } else {
            pbeAlgorithm = k.getAlgorithm();
        }
        if (k.getParam() != null) {
            param = k.getParam();
            pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
        } else if (params instanceof PBEParameterSpec) {
            pbeSpec = (PBEParameterSpec) params;
            param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (param instanceof ParametersWithIV) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        if (ivLength != 0) {
            IvParameterSpec p = (IvParameterSpec) params;
            if (p.getIV().length != ivLength && !isAEADModeName(modeName)) {
                throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
            }
            param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
            ivParam = (ParametersWithIV) param;
        } else {
            if (modeName != null && modeName.equals("ECB")) {
                throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
            }
            param = new KeyParameter(key.getEncoded());
        }
    } else /*
        else if (params instanceof GOST28147ParameterSpec)
        {
            GOST28147ParameterSpec    gost28147Param = (GOST28147ParameterSpec)params;

            param = new ParametersWithSBox(
                       new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());

            if (gost28147Param.getIV() != null && ivLength != 0)
            {
                param = new ParametersWithIV(param, gost28147Param.getIV());
                ivParam = (ParametersWithIV)param;
            }
        }
        else if (params instanceof RC2ParameterSpec)
        {
            RC2ParameterSpec    rc2Param = (RC2ParameterSpec)params;

            param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits());

            if (rc2Param.getIV() != null && ivLength != 0)
            {
                param = new ParametersWithIV(param, rc2Param.getIV());
                ivParam = (ParametersWithIV)param;
            }
        }
        else if (params instanceof RC5ParameterSpec)
        {
            RC5ParameterSpec    rc5Param = (RC5ParameterSpec)params;

            param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds());
            if (baseEngine.getAlgorithmName().startsWith("RC5"))
            {
                if (baseEngine.getAlgorithmName().equals("RC5-32"))
                {
                    if (rc5Param.getWordSize() != 32)
                    {
                        throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
                    }
                }
                else if (baseEngine.getAlgorithmName().equals("RC5-64"))
                {
                    if (rc5Param.getWordSize() != 64)
                    {
                        throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
                    }
                }
            }
            else
            {
                throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
            }
            if ((rc5Param.getIV() != null) && (ivLength != 0))
            {
                param = new ParametersWithIV(param, rc5Param.getIV());
                ivParam = (ParametersWithIV)param;
            }
        }
        */
    {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    if (random != null && padded) {
        param = new ParametersWithRandom(param, random);
    }
    try {
        switch(opmode) {
            case Cipher.ENCRYPT_MODE:
            case Cipher.WRAP_MODE:
                cipher.init(true, param);
                break;
            case Cipher.DECRYPT_MODE:
            case Cipher.UNWRAP_MODE:
                cipher.init(false, param);
                break;
            default:
                throw new InvalidParameterException("unknown opmode " + opmode + " passed");
        }
    } catch (Exception e) {
        throw new InvalidKeyException(e.getMessage());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidParameterException(java.security.InvalidParameterException) SecretKey(javax.crypto.SecretKey) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 18 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project 360-Engine-for-Android by 360.

the class PKCS1Encoding method init.

public void init(boolean forEncryption, CipherParameters param) {
    AsymmetricKeyParameter kParam;
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;
        this.random = rParam.getRandom();
        kParam = (AsymmetricKeyParameter) rParam.getParameters();
    } else {
        this.random = new SecureRandom();
        kParam = (AsymmetricKeyParameter) param;
    }
    engine.init(forEncryption, param);
    this.forPrivateKey = kParam.isPrivate();
    this.forEncryption = forEncryption;
}
Also used : AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom)

Example 19 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project web3sdk by FISCO-BCOS.

the class ECDSASigner method init.

@Override
public void init(boolean forSigning, CipherParameters param) {
    SecureRandom providedRandom = null;
    if (forSigning) {
        if (param instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) param;
            this.key = (ECPrivateKeyParameters) rParam.getParameters();
            providedRandom = rParam.getRandom();
        } else {
            this.key = (ECPrivateKeyParameters) param;
        }
    } else {
        this.key = (ECPublicKeyParameters) param;
    }
    this.random = initSecureRandom(forSigning && !kCalculator.isDeterministic(), providedRandom);
}
Also used : SecureRandom(java.security.SecureRandom) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom)

Example 20 with ParametersWithRandom

use of org.bouncycastle.crypto.params.ParametersWithRandom in project web3sdk by FISCO-BCOS.

the class SM2Signer method initWithCache.

/**
 * The same as init method with better performance by adding the cache for the z value
 * corresponding to the privateKey value
 *
 * @param forSigning
 * @param param
 */
public void initWithCache(boolean forSigning, CipherParameters param) {
    CipherParameters baseParam;
    byte[] userID;
    if (param instanceof ParametersWithID) {
        baseParam = ((ParametersWithID) param).getParameters();
        userID = ((ParametersWithID) param).getID();
    } else {
        baseParam = param;
        // the default value
        userID = Hex.decode("31323334353637383132333435363738");
    }
    if (forSigning) {
        if (baseParam instanceof ParametersWithRandom) {
            ParametersWithRandom rParam = (ParametersWithRandom) baseParam;
            ecKey = (ECKeyParameters) rParam.getParameters();
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), rParam.getRandom());
        } else {
            ecKey = (ECKeyParameters) baseParam;
            ecParams = ecKey.getParameters();
            kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
        }
        BigInteger privateKey = ((ECPrivateKeyParameters) ecKey).getD();
        /**
         * First find z value from zValueCache
         */
        z = zValueCache.get(privateKey);
        if (Objects.isNull(z)) {
            // z value of privateKey not exist, calculate it and set it to the cache
            pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), ((ECPrivateKeyParameters) ecKey).getD()).normalize();
            z = getZ(userID);
            zValueCache.put(privateKey, z);
            logger.info(" privateKey: {} z value not exist, caculate z: {}", privateKey, Hex.toHexString(z));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(" privateKey: {} z value, z: {}", privateKey, Hex.toHexString(z));
            }
        }
        digest.update(z, 0, z.length);
    } else {
        ecKey = (ECKeyParameters) baseParam;
        ecParams = ecKey.getParameters();
        pubPoint = ((ECPublicKeyParameters) ecKey).getQ();
        z = getZ(userID);
        digest.update(z, 0, z.length);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ParametersWithID(org.bouncycastle.crypto.params.ParametersWithID) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) BigInteger(java.math.BigInteger)

Aggregations

ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)41 SecureRandom (java.security.SecureRandom)20 CipherParameters (org.bouncycastle.crypto.CipherParameters)12 AsymmetricKeyParameter (org.bouncycastle.crypto.params.AsymmetricKeyParameter)8 InvalidKeyException (java.security.InvalidKeyException)7 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)5 InvalidParameterException (java.security.InvalidParameterException)5 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)5 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)5 ParametersWithID (org.bouncycastle.crypto.params.ParametersWithID)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 BadPaddingException (javax.crypto.BadPaddingException)3 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 SecretKey (javax.crypto.SecretKey)3 ShortBufferException (javax.crypto.ShortBufferException)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)3 DataLengthException (org.bouncycastle.crypto.DataLengthException)3 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)3