1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto.generators;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.math.BigInteger;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.SecureRandom;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.crypto.params.DHParameters;
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.BigIntegers;
8c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamclass DHKeyGeneratorHelper
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static final DHKeyGeneratorHelper INSTANCE = new DHKeyGeneratorHelper();
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final BigInteger ONE = BigInteger.valueOf(1);
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final BigInteger TWO = BigInteger.valueOf(2);
15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private DHKeyGeneratorHelper()
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
19c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
20c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    BigInteger calculatePrivate(DHParameters dhParams, SecureRandom random)
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
22c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger p = dhParams.getP();
23c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int limit = dhParams.getL();
24c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
25c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (limit != 0)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return new BigInteger(limit, random).setBit(limit - 1);
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
30c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger min = TWO;
31c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int m = dhParams.getM();
32c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (m != 0)
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
34c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            min = ONE.shiftLeft(m - 1);
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
36c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
37c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger max = p.subtract(TWO);
38c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger q = dhParams.getQ();
39c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (q != null)
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
41c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            max = q.subtract(TWO);
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
43c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
44c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return BigIntegers.createRandomInRange(min, max, random);
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
46c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
47c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    BigInteger calculatePublic(DHParameters dhParams, BigInteger x)
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
49c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return dhParams.getG().modPow(x, dhParams.getP());
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
52