Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / java / security / provider / DSASignature.java
blob1d3875d28e37fa077321488ad0bc31f75a2fafa0
1 /* DSASignature.java --
2 Copyright (C) 1999, 2003, 2004 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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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.provider;
41 import gnu.java.security.der.DER;
42 import gnu.java.security.der.DERReader;
43 import gnu.java.security.der.DERValue;
44 import gnu.java.security.der.DERWriter;
46 import java.io.ByteArrayOutputStream;
47 import java.io.IOException;
48 import java.math.BigInteger;
49 import java.security.InvalidAlgorithmParameterException;
50 import java.security.InvalidKeyException;
51 import java.security.InvalidParameterException;
52 import java.security.MessageDigest;
53 import java.security.NoSuchAlgorithmException;
54 import java.security.PrivateKey;
55 import java.security.PublicKey;
56 import java.security.SecureRandom;
57 import java.security.SignatureException;
58 import java.security.SignatureSpi;
59 import java.security.interfaces.DSAPrivateKey;
60 import java.security.interfaces.DSAPublicKey;
61 import java.security.spec.AlgorithmParameterSpec;
62 import java.util.ArrayList;
63 import java.util.Random;
65 public class DSASignature extends SignatureSpi
67 private DSAPublicKey publicKey;
68 private DSAPrivateKey privateKey;
69 private final MessageDigest digest;
70 private final SecureRandom random;
72 public DSASignature() throws NoSuchAlgorithmException
74 random = new SecureRandom();
75 digest = MessageDigest.getInstance ("SHA1");
78 private void init()
80 digest.reset();
83 public void engineInitVerify (PublicKey publicKey)
84 throws InvalidKeyException
86 if (publicKey instanceof DSAPublicKey)
87 this.publicKey = (DSAPublicKey) publicKey;
88 else
89 throw new InvalidKeyException();
90 init();
93 public void engineInitSign (PrivateKey privateKey)
94 throws InvalidKeyException
96 if (privateKey instanceof DSAPrivateKey)
97 this.privateKey = (DSAPrivateKey) privateKey;
98 else
99 throw new InvalidKeyException ("not a DSA private key");
101 init();
104 public void engineInitSign (PrivateKey privateKey,
105 SecureRandom random)
106 throws InvalidKeyException
108 if (privateKey instanceof DSAPrivateKey)
109 this.privateKey = (DSAPrivateKey) privateKey;
110 else
111 throw new InvalidKeyException ("not a DSA private key");
113 appRandom = random;
114 init();
117 public void engineUpdate(byte b)
118 throws SignatureException
120 digest.update (b);
123 public void engineUpdate (byte[] b, int off, int len)
124 throws SignatureException
126 digest.update (b, off, len);
129 public byte[] engineSign() throws SignatureException
131 if (privateKey == null)
132 throw new SignatureException ("not initialized for signing");
136 BigInteger g = privateKey.getParams().getG();
137 BigInteger p = privateKey.getParams().getP();
138 BigInteger q = privateKey.getParams().getQ();
140 BigInteger x = privateKey.getX();
142 BigInteger k = new BigInteger (159, appRandom != null ? appRandom : random);
144 BigInteger r = g.modPow(k, p);
145 r = r.mod(q);
147 byte bytes[] = digest.digest();
148 BigInteger sha = new BigInteger (1, bytes);
150 BigInteger s = sha.add (x.multiply (r));
151 s = s.multiply (k.modInverse(q)).mod (q);
153 ByteArrayOutputStream bout = new ByteArrayOutputStream();
154 ArrayList seq = new ArrayList (2);
155 seq.add(0, new DERValue (DER.INTEGER, r));
156 seq.add(1, new DERValue (DER.INTEGER, s));
157 DERWriter.write (bout, new DERValue (DER.CONSTRUCTED | DER.SEQUENCE, seq));
158 return bout.toByteArray();
160 catch (IOException ioe)
162 SignatureException se = new SignatureException();
163 se.initCause (ioe);
164 throw se;
166 catch (ArithmeticException ae)
168 SignatureException se = new SignatureException();
169 se.initCause (ae);
170 throw se;
174 public int engineSign (byte[] outbuf, int offset, int len)
175 throws SignatureException
177 byte tmp[] = engineSign();
178 if (tmp.length > len)
179 throw new SignatureException ("output buffer too short");
180 System.arraycopy (tmp, 0, outbuf, offset, tmp.length);
181 return tmp.length;
184 public boolean engineVerify (byte[] sigBytes)
185 throws SignatureException
187 // Decode sigBytes from ASN.1 DER encoding
190 DERReader in = new DERReader (sigBytes);
191 DERValue val = in.read();
192 if (!val.isConstructed())
193 throw new SignatureException ("badly formed signature");
194 BigInteger r = (BigInteger) in.read().getValue();
195 BigInteger s = (BigInteger) in.read().getValue();
197 BigInteger g = publicKey.getParams().getG();
198 BigInteger p = publicKey.getParams().getP();
199 BigInteger q = publicKey.getParams().getQ();
201 BigInteger y = publicKey.getY();
203 BigInteger w = s.modInverse (q);
205 byte bytes[] = digest.digest();
206 BigInteger sha = new BigInteger (1, bytes);
208 BigInteger u1 = w.multiply (sha).mod ( q );
210 BigInteger u2 = r.multiply (w).mod(q);
212 BigInteger v = g.modPow (u1, p).multiply (y.modPow (u2, p)).mod (p).mod (q);
214 if (v.equals (r))
215 return true;
216 else
217 return false;
219 catch (IOException ioe)
221 SignatureException se = new SignatureException ("badly formed signature");
222 se.initCause (ioe);
223 throw se;
227 public void engineSetParameter (String param,
228 Object value)
229 throws InvalidParameterException
231 throw new InvalidParameterException();
234 public void engineSetParameter (AlgorithmParameterSpec params)
235 throws InvalidAlgorithmParameterException
237 throw new InvalidParameterException();
241 public Object engineGetParameter (String param)
242 throws InvalidParameterException
244 throw new InvalidParameterException();
247 public Object clone() throws CloneNotSupportedException
249 return super.clone();