jdk/src/share/classes/sun/security/provider/DSAParameterGenerator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AlgorithmParameterGeneratorSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.DSAParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class generates parameters for the DSA algorithm. It uses a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * prime modulus size of 1024 bits, which can be overwritten during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see java.security.AlgorithmParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see java.security.spec.AlgorithmParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see DSAParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class DSAParameterGenerator extends AlgorithmParameterGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // the modulus length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int modLen = 1024; // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // useful constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final BigInteger ZERO = BigInteger.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final BigInteger ONE = BigInteger.valueOf(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final BigInteger TWO = BigInteger.valueOf(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Make a SHA-1 hash function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private SHA sha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public DSAParameterGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.sha = new SHA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Initializes this parameter generator for a certain strength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param strength the strength (size of prime) in bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected void engineInit(int strength, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * Bruce Schneier, "Applied Cryptography", 2nd Edition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * Description of DSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * [...] The algorithm uses the following parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * p=a prime number L bits long, when L ranges from 512 to 1024 and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * a multiple of 64. [...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if ((strength < 512) || (strength > 1024) || (strength % 64 != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                ("Prime size must range from 512 to 1024 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                 + "and be a multiple of 64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.modLen = strength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.random = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Initializes this parameter generator with a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * algorithm-specific parameter generation values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param params the set of algorithm-specific parameter generation values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception InvalidAlgorithmParameterException if the given parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * generation values are inappropriate for this parameter generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void engineInit(AlgorithmParameterSpec genParamSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new InvalidAlgorithmParameterException("Invalid parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Generates the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the new AlgorithmParameters object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected AlgorithmParameters engineGenerateParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        AlgorithmParameters algParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (this.random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                this.random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            BigInteger[] pAndQ = generatePandQ(this.random, this.modLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            BigInteger paramP = pAndQ[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            BigInteger paramQ = pAndQ[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            BigInteger paramG = generateG(paramP, paramQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            DSAParameterSpec dsaParamSpec = new DSAParameterSpec(paramP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                                 paramQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                                                 paramG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            algParams = AlgorithmParameters.getInstance("DSA", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            algParams.init(dsaParamSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (InvalidParameterSpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // this should never happen, because we provide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Generates the prime and subprime parameters for DSA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * using the provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * This method will generate new seeds until a suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * seed has been found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param random the source of randomness to generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * seed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param L the size of <code>p</code>, in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return an array of BigInteger, with <code>p</code> at index 0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>q</code> at index 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    BigInteger[] generatePandQ(SecureRandom random, int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        BigInteger[] result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        byte[] seed = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        while(result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (int i = 0; i < 20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                seed[i] = (byte)random.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            result = generatePandQ(seed, L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Generates the prime and subprime parameters for DSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>The seed parameter corresponds to the <code>SEED</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * referenced in the FIPS specification of the DSA algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * and L is the size of <code>p</code>, in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param seed the seed to generate the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param L the size of <code>p</code>, in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return an array of BigInteger, with <code>p</code> at index 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <code>q</code> at index 1, the seed at index 2, and the counter value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * at index 3, or null if the seed does not yield suitable numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    BigInteger[] generatePandQ(byte[] seed, int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /* Useful variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int g = seed.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int n = (L - 1) / 160;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int b = (L - 1) % 160;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        BigInteger SEED = new BigInteger(1, seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        BigInteger TWOG = TWO.pow(2 * g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /* Step 2 (Step 1 is getting seed). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        byte[] U1 = SHA(seed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        byte[] U2 = SHA(toByteArray((SEED.add(ONE)).mod(TWOG)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        xor(U1, U2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] U = U1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        /* Step 3: For q by setting the msb and lsb to 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        U[0] |= 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        U[19] |= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        BigInteger q = new BigInteger(1, U);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /* Step 5 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         if (!q.isProbablePrime(80)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
             return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
             BigInteger V[] = new BigInteger[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             BigInteger offset = TWO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             /* Step 6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
             for (int counter = 0; counter < 4096; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                 /* Step 7 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                 for (int k = 0; k <= n; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                     BigInteger K = BigInteger.valueOf(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                     BigInteger tmp = (SEED.add(offset).add(K)).mod(TWOG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                     V[k] = new BigInteger(1, SHA(toByteArray(tmp)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                 /* Step 8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                 BigInteger W = V[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                 for (int i = 1; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                     W = W.add(V[i].multiply(TWO.pow(i * 160)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 W = W.add((V[n].mod(TWO.pow(b))).multiply(TWO.pow(n * 160)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                 BigInteger TWOLm1 = TWO.pow(L - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                 BigInteger X = W.add(TWOLm1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                 /* Step 9 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                 BigInteger c = X.mod(q.multiply(TWO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                 BigInteger p = X.subtract(c.subtract(ONE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                 /* Step 10 - 13 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                 if (p.compareTo(TWOLm1) > -1 && p.isProbablePrime(80)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                     BigInteger[] result = {p, q, SEED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                            BigInteger.valueOf(counter)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                     return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                 offset = offset.add(BigInteger.valueOf(n)).add(ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Generates the <code>g</code> parameter for DSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param p the prime, <code>p</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param q the subprime, <code>q</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param the <code>g</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    BigInteger generateG(BigInteger p, BigInteger q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        BigInteger h = ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        BigInteger pMinusOneOverQ = (p.subtract(ONE)).divide(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        BigInteger g = ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        while (g.compareTo(TWO) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            g = h.modPow(pMinusOneOverQ, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            h = h.add(ONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns the SHA-1 digest of some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private byte[] SHA(byte[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        sha.engineReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        sha.engineUpdate(array, 0, array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return sha.engineDigest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Converts the result of a BigInteger.toByteArray call to an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * signed magnitude representation for any positive number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private byte[] toByteArray(BigInteger bigInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        byte[] result = bigInt.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (result[0] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            byte[] tmp = new byte[result.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            System.arraycopy(result, 1, tmp, 0, tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            result = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * XORs U2 into U1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private void xor(byte[] U1, byte[] U2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        for (int i = 0; i < U1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            U1[i] ^= U2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}