1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto.params;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.math.BigInteger;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
54c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.crypto.CipherParameters;
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DHParameters
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements CipherParameters
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final int DEFAULT_MINIMUM_LENGTH = 160;
11c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    // not final due to compiler bug in "simpler" JDKs
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private BigInteger              g;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private BigInteger              p;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private BigInteger              q;
16c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private BigInteger              j;
17c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private int                     m;
18c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private int                     l;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private DHValidationParameters  validation;
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
21c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static int getDefaultMParam(
22c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int lParam)
23c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
24c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (lParam == 0)
25c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
26c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return DEFAULT_MINIMUM_LENGTH;
27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
28c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
29c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return lParam < DEFAULT_MINIMUM_LENGTH ? lParam : DEFAULT_MINIMUM_LENGTH;
30c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
31c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DHParameters(
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger  p,
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger  g)
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
36c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(p, g, null, 0);
37c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
38c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
39c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DHParameters(
40c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  p,
41c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  g,
42c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  q)
43c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
44c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(p, g, q, 0);
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DHParameters(
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger  p,
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger  g,
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger  q,
51c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int         l)
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
53c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(p, g, q, getDefaultMParam(l), l, null, null);
54c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
56c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DHParameters(
57c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  p,
58c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  g,
59c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger  q,
60c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int         m,
61c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int         l)
62c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
63c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(p, g, q, m, l, null, null);
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DHParameters(
67c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger              p,
68c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger              g,
69c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger              q,
70c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger              j,
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DHValidationParameters  validation)
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(p, g, q, DEFAULT_MINIMUM_LENGTH, 0, j, validation);
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DHParameters(
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger              p,
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger              g,
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger              q,
80c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int                     m,
81c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int                     l,
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        BigInteger              j,
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DHValidationParameters  validation)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (l != 0)
86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
874c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            BigInteger bigL = BigInteger.valueOf(2L ^ (l - 1));
884c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            if (bigL.compareTo(p) == 1)
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
904c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                throw new IllegalArgumentException("when l value specified, it must satisfy 2^(l-1) <= p");
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (l < m)
93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                throw new IllegalArgumentException("when l value specified, it may not be less than m value");
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.g = g;
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.p = p;
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.q = q;
101c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.m = m;
102c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.l = l;
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.j = j;
104c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.validation = validation;
105c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getP()
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return p;
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getG()
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return g;
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getQ()
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return q;
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Return the subgroup factor J.
124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @return subgroup factor
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public BigInteger getJ()
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return j;
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Return the minimum length of the private value.
134c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @return the minimum length of the private value in bits.
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
137c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public int getM()
138c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
139c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return m;
140c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
141c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
142c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
143c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Return the private value length in bits - if set, zero otherwise
144c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
145c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @return the private value length in bits, zero otherwise.
146c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
147c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public int getL()
148c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
149c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return l;
150c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
151c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DHValidationParameters getValidationParameters()
153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
154b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return validation;
155b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
156b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
157b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public boolean equals(
158b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
159b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
160b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(obj instanceof DHParameters))
161b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
162b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
163b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
164b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
165b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DHParameters    pm = (DHParameters)obj;
166b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
167b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (this.getQ() != null)
168b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
169b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (!this.getQ().equals(pm.getQ()))
170b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
171b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return false;
172b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
174b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
175b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
176b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (pm.getQ() != null)
177b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
178b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return false;
179b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
180b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
181c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
182c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return pm.getP().equals(p) && pm.getG().equals(g);
183b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
184b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
185b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
186b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
187c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return getP().hashCode() ^ getG().hashCode() ^ (getQ() != null ? getQ().hashCode() : 0);
188b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
189b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
190