Search in sources :

Example 16 with DSAParameters

use of org.bouncycastle.crypto.params.DSAParameters in project XobotOS by xamarin.

the class DSAParametersGenerator method generateParameters_FIPS186_2.

private DSAParameters generateParameters_FIPS186_2() {
    byte[] seed = new byte[20];
    byte[] part1 = new byte[20];
    byte[] part2 = new byte[20];
    byte[] u = new byte[20];
    SHA1Digest sha1 = new SHA1Digest();
    int n = (L - 1) / 160;
    byte[] w = new byte[L / 8];
    for (; ; ) {
        random.nextBytes(seed);
        hash(sha1, seed, part1);
        System.arraycopy(seed, 0, part2, 0, seed.length);
        inc(part2);
        hash(sha1, part2, part2);
        for (int i = 0; i != u.length; i++) {
            u[i] = (byte) (part1[i] ^ part2[i]);
        }
        u[0] |= (byte) 0x80;
        u[19] |= (byte) 0x01;
        BigInteger q = new BigInteger(1, u);
        if (!q.isProbablePrime(certainty)) {
            continue;
        }
        byte[] offset = Arrays.clone(seed);
        inc(offset);
        for (int counter = 0; counter < 4096; ++counter) {
            for (int k = 0; k < n; k++) {
                inc(offset);
                hash(sha1, offset, part1);
                System.arraycopy(part1, 0, w, w.length - (k + 1) * part1.length, part1.length);
            }
            inc(offset);
            hash(sha1, offset, part1);
            System.arraycopy(part1, part1.length - ((w.length - (n) * part1.length)), w, 0, w.length - n * part1.length);
            w[0] |= (byte) 0x80;
            BigInteger x = new BigInteger(1, w);
            BigInteger c = x.mod(q.shiftLeft(1));
            BigInteger p = x.subtract(c.subtract(ONE));
            if (p.bitLength() != L) {
                continue;
            }
            if (p.isProbablePrime(certainty)) {
                BigInteger g = calculateGenerator_FIPS186_2(p, q, random);
                return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter));
            }
        }
    }
}
Also used : DSAValidationParameters(org.bouncycastle.crypto.params.DSAValidationParameters) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Example 17 with DSAParameters

use of org.bouncycastle.crypto.params.DSAParameters in project Skein3Fish by wernerd.

the class DSASigner method generateSignature.

/**
     * generate a signature for the given message using the key we were
     * initialised with. For conventional DSA the message should be a SHA-1
     * hash of the message of interest.
     *
     * @param message the message that will be verified later.
     */
public BigInteger[] generateSignature(byte[] message) {
    DSAParameters params = key.getParameters();
    BigInteger m = calculateE(params.getQ(), message);
    BigInteger k;
    int qBitLength = params.getQ().bitLength();
    do {
        k = new BigInteger(qBitLength, random);
    } while (k.compareTo(params.getQ()) >= 0);
    BigInteger r = params.getG().modPow(k, params.getP()).mod(params.getQ());
    k = k.modInverse(params.getQ()).multiply(m.add(((DSAPrivateKeyParameters) key).getX().multiply(r)));
    BigInteger s = k.mod(params.getQ());
    BigInteger[] res = new BigInteger[2];
    res[0] = r;
    res[1] = s;
    return res;
}
Also used : DSAPrivateKeyParameters(org.bouncycastle.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Example 18 with DSAParameters

use of org.bouncycastle.crypto.params.DSAParameters in project robovm by robovm.

the class KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof DSAParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a DSAParameterSpec");
    }
    DSAParameterSpec dsaParams = (DSAParameterSpec) params;
    param = new DSAKeyGenerationParameters(random, new DSAParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()));
    engine.init(param);
    initialised = true;
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DSAKeyGenerationParameters(org.bouncycastle.crypto.params.DSAKeyGenerationParameters) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Aggregations

DSAParameters (org.bouncycastle.crypto.params.DSAParameters)18 BigInteger (java.math.BigInteger)16 DSAPrivateKeyParameters (org.bouncycastle.crypto.params.DSAPrivateKeyParameters)7 DSAPublicKeyParameters (org.bouncycastle.crypto.params.DSAPublicKeyParameters)7 DHParameter (org.bouncycastle.asn1.pkcs.DHParameter)4 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 DSAParameter (org.bouncycastle.asn1.x509.DSAParameter)4 X962Parameters (org.bouncycastle.asn1.x9.X962Parameters)4 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)4 DHParameters (org.bouncycastle.crypto.params.DHParameters)4 DSAValidationParameters (org.bouncycastle.crypto.params.DSAValidationParameters)4 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 DSAParameterSpec (java.security.spec.DSAParameterSpec)2 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)2 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 DEREncodable (org.bouncycastle.asn1.DEREncodable)2 DERInteger (org.bouncycastle.asn1.DERInteger)2