1 /* DSSParameters.java -- DSS parameters DAO
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.jce.sig;
41 import gnu.java.lang.CPStringBuilder;
43 import gnu.java.security.Registry;
44 import gnu.java.security.der.DER;
45 import gnu.java.security.der.DERReader;
46 import gnu.java.security.der.DERValue;
47 import gnu.java.security.der.DERWriter;
48 import gnu.java.security.util.DerUtil;
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.math.BigInteger;
53 import java.security.AlgorithmParametersSpi;
54 import java.security.spec.AlgorithmParameterSpec;
55 import java.security.spec.DSAParameterSpec;
56 import java.security.spec.InvalidParameterSpecException;
57 import java.util.ArrayList;
60 * A JCE-specific Data Access Object (DAO) for DSS parameters.
62 public class DSSParameters
63 extends AlgorithmParametersSpi
66 * A prime modulus, where <code>2<sup>L-1</sup> < p < 2<sup>L</sup></code>
67 * for <code>512 <= L <= 1024</code> and <code>L</code> a multiple of
73 * A prime divisor of <code>p - 1</code>, where <code>2<sup>159</sup> < q
74 * < 2<sup>160</sup></code>.
79 * <code>g = h<sup>(p-1)</sup>/q mod p</code>, where <code>h</code> is any
80 * integer with <code>1 < h < p - 1</code> such that <code>h<sup>
81 * (p-1)</sup>/q mod p > 1</code> (<code>g</code> has order <code>q mod p
86 // default 0-arguments constructor
88 protected void engineInit(AlgorithmParameterSpec spec)
89 throws InvalidParameterSpecException
91 if (! (spec instanceof DSAParameterSpec))
92 throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
93 + spec.getClass().getName());
94 DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
101 * Decodes the set of DSS parameters as per RFC-2459; i.e. the DER-encoded
102 * form of the following ASN.1 construct:
105 * DssParams ::= SEQUENCE {
112 protected void engineInit(byte[] params) throws IOException
114 DERReader der = new DERReader(params);
116 DERValue derParams = der.read();
117 DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
119 DERValue val = der.read();
120 DerUtil.checkIsBigInteger(val, "Wrong P field");
121 p = (BigInteger) val.getValue();
123 DerUtil.checkIsBigInteger(val, "Wrong Q field");
124 q = (BigInteger) val.getValue();
126 DerUtil.checkIsBigInteger(val, "Wrong G field");
127 g = (BigInteger) val.getValue();
130 protected void engineInit(byte[] params, String format) throws IOException
134 format = format.trim();
135 if (format.length() == 0)
136 throw new IOException("Format MUST NOT be an empty string");
138 if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
139 throw new IOException("Unknown or unsupported format: " + format);
144 protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
145 throws InvalidParameterSpecException
147 if (! paramSpec.isAssignableFrom(DSAParameterSpec.class))
148 throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
149 + paramSpec.getName());
150 return new DSAParameterSpec(p, q, g);
154 * Encodes the set of DSS parameters as per RFC-2459; i.e. as the DER-encoded
155 * form of the following ASN.1 construct:
158 * DssParams ::= SEQUENCE {
165 protected byte[] engineGetEncoded() throws IOException
167 DERValue derP = new DERValue(DER.INTEGER, p);
168 DERValue derQ = new DERValue(DER.INTEGER, q);
169 DERValue derG = new DERValue(DER.INTEGER, g);
171 ArrayList params = new ArrayList(3);
175 DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
177 ByteArrayOutputStream baos = new ByteArrayOutputStream();
178 DERWriter.write(baos, derParams);
179 byte[] result = baos.toByteArray();
184 protected byte[] engineGetEncoded(String format) throws IOException
188 format = format.trim();
189 if (format.length() == 0)
190 throw new IOException("Format MUST NOT be an empty string");
192 if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
193 throw new IOException("Unknown or unsupported format: " + format);
195 return engineGetEncoded();
198 protected String engineToString()
200 CPStringBuilder sb = new CPStringBuilder("p=");
204 sb.append("0x").append(p.toString(16));
210 sb.append("0x").append(q.toString(16));
216 sb.append("0x").append(g.toString(16));
218 return sb.toString();