Search in sources :

Example 16 with Digest

use of org.bouncycastle.crypto.Digest in project XobotOS by xamarin.

the class JCERSACipher method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = JCEDigestUtil.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(org.bouncycastle.crypto.Digest) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 17 with Digest

use of org.bouncycastle.crypto.Digest in project XobotOS by xamarin.

the class JCERSACipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (params == null || params instanceof OAEPParameterSpec) {
        if (key instanceof RSAPublicKey) {
            if (privateKeyOnly) {
                throw new InvalidKeyException("mode 1 requires RSAPrivateKey");
            }
            param = RSAUtil.generatePublicKeyParameter((RSAPublicKey) key);
        } else if (key instanceof RSAPrivateKey) {
            if (publicKeyOnly) {
                throw new InvalidKeyException("mode 2 requires RSAPublicKey");
            }
            param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) key);
        } else {
            throw new InvalidKeyException("unknown key type passed to RSA");
        }
        if (params != null) {
            OAEPParameterSpec spec = (OAEPParameterSpec) params;
            paramSpec = params;
            if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !spec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
                throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
            }
            if (!(spec.getMGFParameters() instanceof MGF1ParameterSpec)) {
                throw new InvalidAlgorithmParameterException("unkown MGF parameters");
            }
            Digest digest = JCEDigestUtil.getDigest(spec.getDigestAlgorithm());
            if (digest == null) {
                throw new InvalidAlgorithmParameterException("no match on digest algorithm: " + spec.getDigestAlgorithm());
            }
            MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) spec.getMGFParameters();
            Digest mgfDigest = JCEDigestUtil.getDigest(mgfParams.getDigestAlgorithm());
            if (mgfDigest == null) {
                throw new InvalidAlgorithmParameterException("no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
            }
            cipher = new OAEPEncoding(new RSABlindedEngine(), digest, mgfDigest, ((PSource.PSpecified) spec.getPSource()).getValue());
        }
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if (!(cipher instanceof RSABlindedEngine)) {
        if (random != null) {
            param = new ParametersWithRandom(param, random);
        } else {
            param = new ParametersWithRandom(param, new SecureRandom());
        }
    }
    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 to RSA");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Digest(org.bouncycastle.crypto.Digest) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec) CipherParameters(org.bouncycastle.crypto.CipherParameters) InvalidParameterException(java.security.InvalidParameterException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 18 with Digest

use of org.bouncycastle.crypto.Digest in project XobotOS by xamarin.

the class CacheManager method setupFiles.

@SuppressWarnings("deprecation")
private static void setupFiles(String url, CacheResult cacheRet) {
    assert !JniUtil.useChromiumHttpStack();
    if (true) {
        // Note: SHA1 is much stronger hash. But the cost of setupFiles() is
        // 3.2% cpu time for a fresh load of nytimes.com. While a simple
        // String.hashCode() is only 0.6%. If adding the collision resolving
        // to String.hashCode(), it makes the cpu time to be 1.6% for a 
        // fresh load, but 5.3% for the worst case where all the files 
        // already exist in the file system, but database is gone. So it
        // needs to resolve collision for every file at least once.
        int hashCode = url.hashCode();
        StringBuffer ret = new StringBuffer(8);
        appendAsHex(hashCode, ret);
        String path = ret.toString();
        File file = new File(mBaseDir, path);
        if (true) {
            boolean checkOldPath = true;
            // cache file. If it is not, resolve the collision.
            while (file.exists()) {
                if (checkOldPath) {
                    CacheResult oldResult = mDataBase.getCache(url);
                    if (oldResult != null && oldResult.contentLength > 0) {
                        if (path.equals(oldResult.localPath)) {
                            path = oldResult.localPath;
                        } else {
                            path = oldResult.localPath;
                            file = new File(mBaseDir, path);
                        }
                        break;
                    }
                    checkOldPath = false;
                }
                ret = new StringBuffer(8);
                appendAsHex(++hashCode, ret);
                path = ret.toString();
                file = new File(mBaseDir, path);
            }
        }
        cacheRet.localPath = path;
        cacheRet.outFile = file;
    } else {
        // get hash in byte[]
        Digest digest = new SHA1Digest();
        int digestLen = digest.getDigestSize();
        byte[] hash = new byte[digestLen];
        int urlLen = url.length();
        byte[] data = new byte[urlLen];
        url.getBytes(0, urlLen, data, 0);
        digest.update(data, 0, urlLen);
        digest.doFinal(hash, 0);
        // convert byte[] to hex String
        StringBuffer result = new StringBuffer(2 * digestLen);
        for (int i = 0; i < digestLen; i = i + 4) {
            int h = (0x00ff & hash[i]) << 24 | (0x00ff & hash[i + 1]) << 16 | (0x00ff & hash[i + 2]) << 8 | (0x00ff & hash[i + 3]);
            appendAsHex(h, result);
        }
        cacheRet.localPath = result.toString();
        cacheRet.outFile = new File(mBaseDir, cacheRet.localPath);
    }
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) File(java.io.File)

Example 19 with Digest

use of org.bouncycastle.crypto.Digest in project robovm by robovm.

the class CipherSpi method initFromSpec.

private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException {
    MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) pSpec.getMGFParameters();
    Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
    if (digest == null) {
        throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: " + mgfParams.getDigestAlgorithm());
    }
    cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified) pSpec.getPSource()).getValue());
    paramSpec = pSpec;
}
Also used : Digest(org.bouncycastle.crypto.Digest) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OAEPEncoding(org.bouncycastle.crypto.encodings.OAEPEncoding) MGF1ParameterSpec(java.security.spec.MGF1ParameterSpec)

Example 20 with Digest

use of org.bouncycastle.crypto.Digest in project cxf by apache.

the class PbesHmacAesWrapKeyEncryptionAlgorithm method createDerivedKey.

static byte[] createDerivedKey(String keyAlgoJwt, int keySize, byte[] password, byte[] saltInput, int pbesCount) {
    byte[] saltValue = createSaltValue(keyAlgoJwt, saltInput);
    Digest digest = null;
    int macSigSize = PBES_HMAC_MAP.get(keyAlgoJwt);
    if (macSigSize == 256) {
        digest = new SHA256Digest();
    } else if (macSigSize == 384) {
        digest = new SHA384Digest();
    } else {
        digest = new SHA512Digest();
    }
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(digest);
    gen.init(password, saltValue, pbesCount);
    return ((KeyParameter) gen.generateDerivedParameters(keySize * 8)).getKey();
}
Also used : SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest) Digest(org.bouncycastle.crypto.Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest)

Aggregations

Digest (org.bouncycastle.crypto.Digest)31 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)9 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)8 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)5 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)5 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 OAEPEncoding (org.bouncycastle.crypto.encodings.OAEPEncoding)4 InvalidParameterException (java.security.InvalidParameterException)3 BigInteger (java.math.BigInteger)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 SecureRandom (java.security.SecureRandom)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)2 CipherParameters (org.bouncycastle.crypto.CipherParameters)2 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)2 DSAValidationParameters (org.bouncycastle.crypto.params.DSAValidationParameters)2 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)2