1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto.generators;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.AsymmetricCipherKeyPair;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.KeyGenerationParameters;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DHKeyGenerationParameters;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DHParameters;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DHPrivateKeyParameters;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.params.DHPublicKeyParameters;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
11c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.math.BigInteger;
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * a basic Diffie-Hellman key pair generator.
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * This generates keys consistent for use with the basic algorithm for
17c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * Diffie-Hellman.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DHBasicKeyPairGenerator
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements AsymmetricCipherKeyPairGenerator
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private DHKeyGenerationParameters param;
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public void init(
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        KeyGenerationParameters param)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.param = (DHKeyGenerationParameters)param;
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public AsymmetricCipherKeyPair generateKeyPair()
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
32c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
33c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DHParameters dhp = param.getParameters();
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
35c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
36c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger y = helper.calculatePublic(dhp, x);
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new AsymmetricCipherKeyPair(
39c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            new DHPublicKeyParameters(y, dhp),
40c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            new DHPrivateKeyParameters(x, dhp));
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
43