OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [security/] [provider/] [DSAKeyPairGenerator.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* GnuDSAKeyPairGenerator.java --- Gnu DSA Key Pair Generator
2
   Copyright (C) 1999, 2005  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.java.security.provider;
40
 
41
import java.math.BigInteger;
42
import java.security.AlgorithmParameterGenerator;
43
import java.security.AlgorithmParameters;
44
import java.security.InvalidAlgorithmParameterException;
45
import java.security.InvalidParameterException;
46
import java.security.KeyPair;
47
import java.security.KeyPairGeneratorSpi;
48
import java.security.NoSuchAlgorithmException;
49
import java.security.SecureRandom;
50
import java.security.interfaces.DSAParams;
51
import java.security.spec.AlgorithmParameterSpec;
52
import java.security.spec.DSAParameterSpec;
53
import java.security.spec.InvalidParameterSpecException;
54
import java.util.Random;
55
 
56
public class DSAKeyPairGenerator extends KeyPairGeneratorSpi
57
        implements java.security.interfaces.DSAKeyPairGenerator
58
{
59
int keysize;
60
SecureRandom random;
61
private BigInteger q = null; // the small prime
62
private BigInteger p = null; // the big prime
63
private BigInteger g = null;
64
 
65
public DSAKeyPairGenerator()
66
{
67
        keysize = 1024;
68
        getDefaults ();
69
}
70
 
71
public void initialize(int keysize, SecureRandom random)
72
{
73
        initialize (keysize, false, random);
74
}
75
 
76
public void initialize(AlgorithmParameterSpec params,
77
                       SecureRandom random)
78
                throws InvalidAlgorithmParameterException
79
{
80
        if( !( params instanceof DSAParameterSpec ) )
81
                throw new InvalidAlgorithmParameterException("Must be DSAParameterSpec");
82
 
83
        try
84
        {
85
                initialize ((DSAParams) params, random);
86
        }
87
        catch (InvalidParameterException ipe)
88
        {
89
                InvalidAlgorithmParameterException iape =
90
                        new InvalidAlgorithmParameterException();
91
                iape.initCause (ipe);
92
                throw iape;
93
        }
94
}
95
 
96
public void initialize (DSAParams params, SecureRandom random)
97
{
98
        DSAParameterSpec dsaparameterspec = (DSAParameterSpec)params;
99
        if (dsaparameterspec.getP() == null
100
            || dsaparameterspec.getQ() == null
101
            || dsaparameterspec.getG() == null)
102
        {
103
                throw new InvalidParameterException ("none of p, q, or g may be null");
104
        }
105
        p = dsaparameterspec.getP();
106
        q = dsaparameterspec.getQ();
107
        g = dsaparameterspec.getG();
108
        this.random = random;
109
}
110
 
111
public void initialize(int modlen, boolean genParams, SecureRandom random)
112
        throws InvalidParameterException
113
{
114
        if( ((modlen % 64) != 0) || (modlen < 512) || (modlen > 1024) )
115
                throw new InvalidParameterException();
116
 
117
        this.keysize = modlen;
118
        this.random = random;
119
        if (this.random == null)
120
        {
121
                this.random = new SecureRandom ();
122
        }
123
        if (genParams)
124
        {
125
                try
126
                {
127
                        AlgorithmParameterGenerator apgDSA = AlgorithmParameterGenerator.getInstance("DSA");
128
                        apgDSA.init (modlen, random);
129
                        AlgorithmParameters apDSA = apgDSA.generateParameters();
130
                        DSAParameterSpec dsaparameterspec = (DSAParameterSpec)apDSA.getParameterSpec( DSAParameterSpec.class );
131
                        p = dsaparameterspec.getP();
132
                        q = dsaparameterspec.getQ();
133
                        g = dsaparameterspec.getG();
134
                }
135
                catch (NoSuchAlgorithmException nsae)
136
                {
137
                        InvalidParameterException ipe =
138
                                new InvalidParameterException ("can't generate DSA parameters");
139
                        ipe.initCause (nsae);
140
                        throw ipe;
141
                }
142
                catch (InvalidParameterSpecException ipse)
143
                {
144
                        InvalidParameterException ipe =
145
                                new InvalidParameterException ("can't generate DSA parameters");
146
                        ipe.initCause (ipse);
147
                        throw ipe;
148
                }
149
        }
150
        else if (!getDefaults ())
151
        {
152
                throw new InvalidParameterException ("unsupported key size: " + modlen);
153
        }
154
}
155
 
156
public KeyPair generateKeyPair()
157
{
158
        if (random == null)
159
                {
160
                        random = new SecureRandom ();
161
                }
162
 
163
        BigInteger x = new BigInteger( 159, random );
164
 
165
        BigInteger y = g.modPow( x, p );
166
 
167
        return new KeyPair( new GnuDSAPublicKey(y,p,q,g), new GnuDSAPrivateKey(x,p,q,g));
168
        //return new KeyPair( public, private );
169
}
170
 
171
//These constants are Sun's Constants copied from the 
172
//Cryptography Specification
173
private boolean getDefaults()
174
{
175
        if( keysize == 512) {
176
                p = new BigInteger("fca682ce8e12caba26efccf7110e526db078b05edecbcd1eb4a208f3ae1617ae01f35b91a47e6df63413c5e12ed0899bcd132acd50d99151bdc43ee737592e17", 16);
177
                q = new BigInteger("962eddcc369cba8ebb260ee6b6a126d9346e38c5", 16);
178
                g = new BigInteger("678471b27a9cf44ee91a49c5147db1a9aaf244f05a434d6486931d2d14271b9e35030b71fd73da179069b32e2935630e1c2062354d0da20a6c416e50be794ca4", 16);
179
                return true;
180
        } else if( keysize == 768) {
181
                p = new BigInteger("e9e642599d355f37c97ffd3567120b8e25c9cd43e927b3a9670fbec5d890141922d2c3b3ad2480093799869d1e846aab49fab0ad26d2ce6a22219d470bce7d777d4a21fbe9c270b57f607002f3cef8393694cf45ee3688c11a8c56ab127a3daf", 16);
182
                q = new BigInteger("9cdbd84c9f1ac2f38d0f80f42ab952e7338bf511", 16);
183
                g = new BigInteger("30470ad5a005fb14ce2d9dcd87e38bc7d1b1c5facbaecbe95f190aa7a31d23c4dbbcbe06174544401a5b2c020965d8c2bd2171d3668445771f74ba084d2029d83c1c158547f3a9f1a2715be23d51ae4d3e5a1f6a7064f316933a346d3f529252", 16);
184
                return true;
185
        } else if( keysize == 1024) {
186
                p = new BigInteger("fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7", 16);
187
                q = new BigInteger("9760508f15230bccb292b982a2eb840bf0581cf5", 16);
188
                g = new BigInteger("f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a", 16);
189
                return true;
190
        }
191
        return false;
192
}
193
 
194
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.