summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/asn1/x9/DomainParameters.java
blob: 0555190acadc8633aa58e7ab831a1873688c8cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.bouncycastle.asn1.x9;

import java.math.BigInteger;
import java.util.Enumeration;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERSequence;

/**
 * X9.44 Diffie-Hellman domain parameters.
 * <pre>
 *    DomainParameters ::= SEQUENCE {
 *       p                INTEGER,           -- odd prime, p=jq +1
 *       g                INTEGER,           -- generator, g
 *       q                INTEGER,           -- factor of p-1
 *       j                INTEGER OPTIONAL,  -- subgroup factor, j &gt;= 2
 *       validationParams  ValidationParams OPTIONAL
 *    }
 * </pre>
 */
public class DomainParameters
    extends ASN1Object
{
    private final ASN1Integer p, g, q, j;
    private final ValidationParams validationParams;

    /**
     * Return a DomainParameters object from the passed in tagged object.
     *
     * @param obj a tagged object.
     * @param explicit true if the contents of the object is explictly tagged, false otherwise.
     * @return a DomainParameters
     */
    public static DomainParameters getInstance(ASN1TaggedObject obj, boolean explicit)
    {
        return getInstance(ASN1Sequence.getInstance(obj, explicit));
    }

    /**
     * Return a DomainParameters object from the passed in object.
     *
     * @param obj an object for conversion or a byte[].
     * @return a DomainParameters
     */
    public static DomainParameters getInstance(Object obj)
    {
        if (obj instanceof DomainParameters)
        {
            return (DomainParameters)obj;
        }
        else if (obj != null)
        {
            return new DomainParameters(ASN1Sequence.getInstance(obj));
        }

        return null;
    }

    /**
     * Base constructor - the full domain parameter set.
     *
     * @param p the prime p defining the Galois field.
     * @param g the generator of the multiplicative subgroup of order g.
     * @param q specifies the prime factor of p - 1
     * @param j optionally specifies the value that satisfies the equation p = jq+1
     * @param validationParams parameters for validating these domain parameters.
     */
    public DomainParameters(BigInteger p, BigInteger g, BigInteger q, BigInteger j,
                            ValidationParams validationParams)
    {
        if (p == null)
        {
            throw new IllegalArgumentException("'p' cannot be null");
        }
        if (g == null)
        {
            throw new IllegalArgumentException("'g' cannot be null");
        }
        if (q == null)
        {
            throw new IllegalArgumentException("'q' cannot be null");
        }

        this.p = new ASN1Integer(p);
        this.g = new ASN1Integer(g);
        this.q = new ASN1Integer(q);

        if (j != null)
        {
            this.j = new ASN1Integer(j);
        }
        else
        {
            this.j = null;
        }
        this.validationParams = validationParams;
    }

    private DomainParameters(ASN1Sequence seq)
    {
        if (seq.size() < 3 || seq.size() > 5)
        {
            throw new IllegalArgumentException("Bad sequence size: " + seq.size());
        }

        Enumeration e = seq.getObjects();
        this.p = ASN1Integer.getInstance(e.nextElement());
        this.g = ASN1Integer.getInstance(e.nextElement());
        this.q = ASN1Integer.getInstance(e.nextElement());

        ASN1Encodable next = getNext(e);

        if (next != null && next instanceof ASN1Integer)
        {
            this.j = ASN1Integer.getInstance(next);
            next = getNext(e);
        }
        else
        {
            this.j = null;
        }

        if (next != null)
        {
            this.validationParams = ValidationParams.getInstance(next.toASN1Primitive());
        }
        else
        {
            this.validationParams = null;
        }
    }

    private static ASN1Encodable getNext(Enumeration e)
    {
        return e.hasMoreElements() ? (ASN1Encodable)e.nextElement() : null;
    }

    /**
     * Return the prime p defining the Galois field.
     *
     * @return the prime p.
     */
    public BigInteger getP()
    {
        return this.p.getPositiveValue();
    }

    /**
     * Return the generator of the multiplicative subgroup of order g.
     *
     * @return the generator g.
     */
    public BigInteger getG()
    {
        return this.g.getPositiveValue();
    }

    /**
     * Return q, the prime factor of p - 1
     *
     * @return q value
     */
    public BigInteger getQ()
    {
        return this.q.getPositiveValue();
    }

    /**
     * Return the value that satisfies the equation p = jq+1 (if present).
     *
     * @return j value or null.
     */
    public BigInteger getJ()
    {
        if (this.j == null)
        {
            return null;
        }

        return this.j.getPositiveValue();
    }

    /**
     * Return the validation parameters for this set (if present).
     *
     * @return validation parameters, or null if absent.
     */
    public ValidationParams getValidationParams()
    {
        return this.validationParams;
    }

    /**
     * Return an ASN.1 primitive representation of this object.
     *
     * @return a DERSequence containing the parameter values.
     */
    public ASN1Primitive toASN1Primitive()
    {
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(this.p);
        v.add(this.g);
        v.add(this.q);

        if (this.j != null)
        {
            v.add(this.j);
        }

        if (this.validationParams != null)
        {
            v.add(this.validationParams);
        }

        return new DERSequence(v);
    }
}