Search in sources :

Example 1 with SHA256Digest

use of org.bouncycastle.crypto.digests.SHA256Digest in project robovm by robovm.

the class BcDefaultDigestProvider method createTable.

private static Map createTable() {
    Map table = new HashMap();
    table.put(OIWObjectIdentifiers.idSHA1, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA1Digest();
        }
    });
    // BEGIN android-removed
    // table.put(NISTObjectIdentifiers.id_sha224, new BcDigestProvider()
    // {
    //     public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier)
    //     {
    //         return new SHA224Digest();
    //     }
    // });
    // END android-removed
    table.put(NISTObjectIdentifiers.id_sha256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA256Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha384, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA384Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha512, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA512Digest();
        }
    });
    table.put(PKCSObjectIdentifiers.md5, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new MD5Digest();
        }
    });
    return Collections.unmodifiableMap(table);
}
Also used : ExtendedDigest(org.bouncycastle.crypto.ExtendedDigest) SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) MD5Digest(org.bouncycastle.crypto.digests.MD5Digest) HashMap(java.util.HashMap) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) HashMap(java.util.HashMap) Map(java.util.Map) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 2 with SHA256Digest

use of org.bouncycastle.crypto.digests.SHA256Digest in project XobotOS by xamarin.

the class DSAParametersGenerator method generateParameters_FIPS186_3.

/**
     * generate suitable parameters for DSA, in line with
     * <i>FIPS 186-3 A.1 Generation of the FFC Primes p and q</i>.
     */
private DSAParameters generateParameters_FIPS186_3() {
    // A.1.1.2 Generation of the Probable Primes p and q Using an Approved Hash Function
    // FIXME This should be configurable (digest size in bits must be >= N)
    Digest d = new SHA256Digest();
    int outlen = d.getDigestSize() * 8;
    // 1. Check that the (L, N) pair is in the list of acceptable (L, N pairs) (see Section 4.2). If
    //    the pair is not in the list, then return INVALID.
    // Note: checked at initialisation
    // 2. If (seedlen < N), then return INVALID.
    // FIXME This should be configurable (must be >= N)
    int seedlen = N;
    byte[] seed = new byte[seedlen / 8];
    // 3. n = ceiling(L ⁄ outlen) – 1.
    int n = (L - 1) / outlen;
    // 4. b = L – 1 – (n ∗ outlen).
    int b = (L - 1) % outlen;
    byte[] output = new byte[d.getDigestSize()];
    for (; ; ) {
        // 5. Get an arbitrary sequence of seedlen bits as the domain_parameter_seed.
        random.nextBytes(seed);
        // 6. U = Hash (domain_parameter_seed) mod 2^(N–1).
        hash(d, seed, output);
        BigInteger U = new BigInteger(1, output).mod(ONE.shiftLeft(N - 1));
        // 7. q = 2^(N–1) + U + 1 – ( U mod 2).
        BigInteger q = ONE.shiftLeft(N - 1).add(U).add(ONE).subtract(U.mod(TWO));
        // TODO Review C.3 for primality checking
        if (!q.isProbablePrime(certainty)) {
            // 9. If q is not a prime, then go to step 5.
            continue;
        }
        // 10. offset = 1.
        // Note: 'offset' value managed incrementally
        byte[] offset = Arrays.clone(seed);
        // 11. For counter = 0 to (4L – 1) do
        int counterLimit = 4 * L;
        for (int counter = 0; counter < counterLimit; ++counter) {
            // 11.1 For j = 0 to n do
            //      Vj = Hash ((domain_parameter_seed + offset + j) mod 2^seedlen).
            // 11.2 W = V0 + (V1 ∗ 2^outlen) + ... + (V^(n–1) ∗ 2^((n–1) ∗ outlen)) + ((Vn mod 2^b) ∗ 2^(n ∗ outlen)).
            // TODO Assemble w as a byte array
            BigInteger W = ZERO;
            for (int j = 0, exp = 0; j <= n; ++j, exp += outlen) {
                inc(offset);
                hash(d, offset, output);
                BigInteger Vj = new BigInteger(1, output);
                if (j == n) {
                    Vj = Vj.mod(ONE.shiftLeft(b));
                }
                W = W.add(Vj.shiftLeft(exp));
            }
            // 11.3 X = W + 2^(L–1). Comment: 0 ≤ W < 2L–1; hence, 2L–1 ≤ X < 2L.
            BigInteger X = W.add(ONE.shiftLeft(L - 1));
            // 11.4 c = X mod 2q.
            BigInteger c = X.mod(q.shiftLeft(1));
            // 11.5 p = X - (c - 1). Comment: p ≡ 1 (mod 2q).
            BigInteger p = X.subtract(c.subtract(ONE));
            // 11.6 If (p < 2^(L - 1)), then go to step 11.9
            if (p.bitLength() != L) {
                continue;
            }
            // TODO Review C.3 for primality checking
            if (p.isProbablePrime(certainty)) {
                // 11.8 If p is determined to be prime, then return VALID and the values of p, q and
                //      (optionally) the values of domain_parameter_seed and counter.
                // TODO Make configurable (8-bit unsigned)?
                //                    int index = 1;
                //                    BigInteger g = calculateGenerator_FIPS186_3_Verifiable(d, p, q, seed, index);
                //                    if (g != null)
                //                    {
                //                        // TODO Should 'index' be a part of the validation parameters?
                //                        return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter));
                //                    }
                BigInteger g = calculateGenerator_FIPS186_3_Unverifiable(p, q, random);
                return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter));
            }
        // 11.9 offset = offset + n + 1.      Comment: Increment offset; then, as part of
        //                                    the loop in step 11, increment counter; if
        //                                    counter < 4L, repeat steps 11.1 through 11.8.
        // Note: 'offset' value already incremented in inner loop
        }
    // 12. Go to step 5.
    }
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) DSAValidationParameters(org.bouncycastle.crypto.params.DSAValidationParameters) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Example 3 with SHA256Digest

use of org.bouncycastle.crypto.digests.SHA256Digest in project ddf by codice.

the class RandomNumberGenerator method createSeed.

public static byte[] createSeed() {
    EntropySourceProvider esp = new BasicEntropySourceProvider(new SecureRandom(), true);
    byte[] nonce = new byte[256];
    new SecureRandom().nextBytes(nonce);
    DualECSP800DRBG bcRbg = new DualECSP800DRBG(new SHA256Digest(), 256, esp.get(256), null, nonce);
    byte[] seed = new byte[256];
    bcRbg.generate(seed, null, true);
    return seed;
}
Also used : DualECSP800DRBG(org.bouncycastle.crypto.prng.drbg.DualECSP800DRBG) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BasicEntropySourceProvider(org.bouncycastle.crypto.prng.BasicEntropySourceProvider) SecureRandom(java.security.SecureRandom) BasicEntropySourceProvider(org.bouncycastle.crypto.prng.BasicEntropySourceProvider) EntropySourceProvider(org.bouncycastle.crypto.prng.EntropySourceProvider)

Example 4 with SHA256Digest

use of org.bouncycastle.crypto.digests.SHA256Digest in project robovm by robovm.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    DSAParametersGenerator pGen;
    if (strength <= 1024) {
        pGen = new DSAParametersGenerator();
    } else {
        pGen = new DSAParametersGenerator(new SHA256Digest());
    }
    if (random == null) {
        random = new SecureRandom();
    }
    if (strength == 1024) {
        params = new DSAParameterGenerationParameters(1024, 160, 80, random);
        pGen.init(params);
    } else if (strength > 1024) {
        params = new DSAParameterGenerationParameters(strength, 256, 80, random);
        pGen.init(params);
    } else {
        pGen.init(strength, 20, random);
    }
    DSAParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
        params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : DSAParametersGenerator(org.bouncycastle.crypto.generators.DSAParametersGenerator) DSAParameterSpec(java.security.spec.DSAParameterSpec) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(org.bouncycastle.crypto.params.DSAParameterGenerationParameters) SecureRandom(java.security.SecureRandom) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 SecureRandom (java.security.SecureRandom)2 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)2 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)2 BigInteger (java.math.BigInteger)1 AlgorithmParameters (java.security.AlgorithmParameters)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidParameterException (java.security.InvalidParameterException)1 DSAParameterSpec (java.security.spec.DSAParameterSpec)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 Digest (org.bouncycastle.crypto.Digest)1 ExtendedDigest (org.bouncycastle.crypto.ExtendedDigest)1 MD5Digest (org.bouncycastle.crypto.digests.MD5Digest)1 SHA384Digest (org.bouncycastle.crypto.digests.SHA384Digest)1 SHA512Digest (org.bouncycastle.crypto.digests.SHA512Digest)1 DSAParametersGenerator (org.bouncycastle.crypto.generators.DSAParametersGenerator)1 DSAParameterGenerationParameters (org.bouncycastle.crypto.params.DSAParameterGenerationParameters)1 DSAValidationParameters (org.bouncycastle.crypto.params.DSAValidationParameters)1