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));
}
}
}
}
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;
}
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;
}
Aggregations