0N/A/*
2362N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.math.BigInteger;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.interfaces.DSAParams;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.security.spec.InvalidParameterSpecException;
0N/Aimport java.security.spec.DSAParameterSpec;
0N/A
0N/Aimport sun.security.jca.JCAUtil;
0N/A
0N/A/**
0N/A * This class generates DSA key parameters and public/private key
0N/A * pairs according to the DSS standard NIST FIPS 186. It uses the
0N/A * updated version of SHA, SHA-1 as described in FIPS 180-1.
0N/A *
0N/A * @author Benjamin Renaud
0N/A * @author Andreas Sterbenz
0N/A *
0N/A */
0N/Apublic class DSAKeyPairGenerator extends KeyPairGenerator
0N/Aimplements java.security.interfaces.DSAKeyPairGenerator {
0N/A
0N/A /* The modulus length */
0N/A private int modlen;
0N/A
0N/A /* whether to force new parameters to be generated for each KeyPair */
0N/A private boolean forceNewParameters;
0N/A
0N/A /* preset algorithm parameters. */
0N/A private DSAParameterSpec params;
0N/A
0N/A /* The source of random bits to use */
0N/A private SecureRandom random;
0N/A
0N/A public DSAKeyPairGenerator() {
0N/A super("DSA");
0N/A initialize(1024, null);
0N/A }
0N/A
0N/A private static void checkStrength(int strength) {
0N/A if ((strength < 512) || (strength > 1024) || (strength % 64 != 0)) {
0N/A throw new InvalidParameterException
0N/A ("Modulus size must range from 512 to 1024 "
0N/A + "and be a multiple of 64");
0N/A }
0N/A }
0N/A
0N/A public void initialize(int modlen, SecureRandom random) {
0N/A checkStrength(modlen);
0N/A this.random = random;
0N/A this.modlen = modlen;
0N/A this.params = null;
0N/A this.forceNewParameters = false;
0N/A }
0N/A
0N/A /**
0N/A * Initializes the DSA key pair generator. If <code>genParams</code>
0N/A * is false, a set of pre-computed parameters is used.
0N/A */
0N/A public void initialize(int modlen, boolean genParams, SecureRandom random) {
0N/A checkStrength(modlen);
0N/A if (genParams) {
0N/A params = null;
0N/A } else {
0N/A params = ParameterCache.getCachedDSAParameterSpec(modlen);
0N/A if (params == null) {
0N/A throw new InvalidParameterException
0N/A ("No precomputed parameters for requested modulus size "
0N/A + "available");
0N/A }
0N/A }
0N/A this.modlen = modlen;
0N/A this.random = random;
0N/A this.forceNewParameters = genParams;
0N/A }
0N/A
0N/A /**
0N/A * Initializes the DSA object using a DSA parameter object.
0N/A *
0N/A * @param params a fully initialized DSA parameter object.
0N/A */
0N/A public void initialize(DSAParams params, SecureRandom random) {
0N/A if (params == null) {
0N/A throw new InvalidParameterException("Params must not be null");
0N/A }
0N/A DSAParameterSpec spec = new DSAParameterSpec
0N/A (params.getP(), params.getQ(), params.getG());
0N/A initialize0(spec, random);
0N/A }
0N/A
0N/A /**
0N/A * Initializes the DSA object using a parameter object.
0N/A *
0N/A * @param params the parameter set to be used to generate
0N/A * the keys.
0N/A * @param random the source of randomness for this generator.
0N/A *
0N/A * @exception InvalidAlgorithmParameterException if the given parameters
0N/A * are inappropriate for this key pair generator
0N/A */
0N/A public void initialize(AlgorithmParameterSpec params, SecureRandom random)
0N/A throws InvalidAlgorithmParameterException {
0N/A if (!(params instanceof DSAParameterSpec)) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Inappropriate parameter");
0N/A }
0N/A initialize0((DSAParameterSpec)params, random);
0N/A }
0N/A
0N/A private void initialize0(DSAParameterSpec params, SecureRandom random) {
0N/A int modlen = params.getP().bitLength();
0N/A checkStrength(modlen);
0N/A this.modlen = modlen;
0N/A this.params = params;
0N/A this.random = random;
0N/A this.forceNewParameters = false;
0N/A }
0N/A
0N/A /**
0N/A * Generates a pair of keys usable by any JavaSecurity compliant
0N/A * DSA implementation.
0N/A */
0N/A public KeyPair generateKeyPair() {
0N/A if (random == null) {
0N/A random = JCAUtil.getSecureRandom();
0N/A }
0N/A DSAParameterSpec spec;
0N/A try {
0N/A if (forceNewParameters) {
0N/A // generate new parameters each time
0N/A spec = ParameterCache.getNewDSAParameterSpec(modlen, random);
0N/A } else {
0N/A if (params == null) {
0N/A params =
0N/A ParameterCache.getDSAParameterSpec(modlen, random);
0N/A }
0N/A spec = params;
0N/A }
0N/A } catch (GeneralSecurityException e) {
0N/A throw new ProviderException(e);
0N/A }
0N/A return generateKeyPair(spec.getP(), spec.getQ(), spec.getG(), random);
0N/A }
0N/A
0N/A public KeyPair generateKeyPair(BigInteger p, BigInteger q, BigInteger g,
0N/A SecureRandom random) {
0N/A
0N/A BigInteger x = generateX(random, q);
0N/A BigInteger y = generateY(x, p, g);
0N/A
0N/A try {
0N/A
0N/A // See the comments in DSAKeyFactory, 4532506, and 6232513.
0N/A
0N/A DSAPublicKey pub;
0N/A if (DSAKeyFactory.SERIAL_INTEROP) {
0N/A pub = new DSAPublicKey(y, p, q, g);
0N/A } else {
0N/A pub = new DSAPublicKeyImpl(y, p, q, g);
0N/A }
0N/A DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);
0N/A
0N/A KeyPair pair = new KeyPair(pub, priv);
0N/A return pair;
0N/A } catch (InvalidKeyException e) {
0N/A throw new ProviderException(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Generate the private key component of the key pair using the
0N/A * provided source of random bits. This method uses the random but
0N/A * source passed to generate a seed and then calls the seed-based
0N/A * generateX method.
0N/A */
0N/A private BigInteger generateX(SecureRandom random, BigInteger q) {
0N/A BigInteger x = null;
0N/A while (true) {
0N/A int[] seed = new int[5];
0N/A for (int i = 0; i < 5; i++) {
0N/A seed[i] = random.nextInt();
0N/A }
0N/A x = generateX(seed, q);
0N/A if (x.signum() > 0 && (x.compareTo(q) < 0)) {
0N/A break;
0N/A }
0N/A }
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Given a seed, generate the private key component of the key
0N/A * pair. In the terminology used in the DSA specification
0N/A * (FIPS-186) seed is the XSEED quantity.
0N/A *
0N/A * @param seed the seed to use to generate the private key.
0N/A */
0N/A BigInteger generateX(int[] seed, BigInteger q) {
0N/A
0N/A // check out t in the spec.
0N/A int[] t = { 0x67452301, 0xEFCDAB89, 0x98BADCFE,
0N/A 0x10325476, 0xC3D2E1F0 };
0N/A //
0N/A
0N/A int[] tmp = DSA.SHA_7(seed, t);
0N/A byte[] tmpBytes = new byte[tmp.length * 4];
0N/A for (int i = 0; i < tmp.length; i++) {
0N/A int k = tmp[i];
0N/A for (int j = 0; j < 4; j++) {
0N/A tmpBytes[(i * 4) + j] = (byte) (k >>> (24 - (j * 8)));
0N/A }
0N/A }
0N/A BigInteger x = new BigInteger(1, tmpBytes).mod(q);
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Generate the public key component y of the key pair.
0N/A *
0N/A * @param x the private key component.
0N/A *
0N/A * @param p the base parameter.
0N/A */
0N/A BigInteger generateY(BigInteger x, BigInteger p, BigInteger g) {
0N/A BigInteger y = g.modPow(x, p);
0N/A return y;
0N/A }
0N/A
0N/A}