Source for gnu.java.security.jce.sig.DSSParametersGenerator

   1: /* DSSParametersGenerator.java -- JCE Adapter for a generator of DSS parameters
   2:    Copyright (C) 2006 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.jce.sig;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.key.dss.DSSKeyPairGenerator;
  43: import gnu.java.security.key.dss.FIPS186;
  44: import gnu.java.security.provider.Gnu;
  45: 
  46: import java.math.BigInteger;
  47: import java.security.AlgorithmParameterGeneratorSpi;
  48: import java.security.AlgorithmParameters;
  49: import java.security.InvalidAlgorithmParameterException;
  50: import java.security.InvalidParameterException;
  51: import java.security.NoSuchAlgorithmException;
  52: import java.security.Provider;
  53: import java.security.SecureRandom;
  54: import java.security.spec.AlgorithmParameterSpec;
  55: import java.security.spec.DSAParameterSpec;
  56: import java.security.spec.InvalidParameterSpecException;
  57: 
  58: /**
  59:  * A JCE Adapter for a generator of DSS parameters.
  60:  */
  61: public class DSSParametersGenerator
  62:     extends AlgorithmParameterGeneratorSpi
  63: {
  64:   private static final Provider GNU = new Gnu();
  65: 
  66:   /** Size of the public modulus in bits. */
  67:   private int modulusLength = -1;
  68: 
  69:   /** User specified source of randomness. */
  70:   private SecureRandom rnd;
  71: 
  72:   /** Our concrete DSS parameters generator. */
  73:   private FIPS186 fips;
  74: 
  75:   // default 0-arguments constructor
  76: 
  77:   protected void engineInit(int size, SecureRandom random)
  78:   {
  79:     if ((size % 64) != 0 || size < 512 || size > 1024)
  80:       throw new InvalidParameterException("Modulus size/length (in bits) MUST "
  81:                                           + "be a multiple of 64, greater than "
  82:                                           + "or equal to 512, and less than or "
  83:                                           + "equal to 1024");
  84:     this.modulusLength = size;
  85:     this.rnd = random;
  86:   }
  87: 
  88:   protected void engineInit(AlgorithmParameterSpec spec, SecureRandom random)
  89:       throws InvalidAlgorithmParameterException
  90:   {
  91:     if (! (spec instanceof DSAParameterSpec))
  92:       throw new InvalidAlgorithmParameterException("Wrong AlgorithmParameterSpec type: "
  93:                                                    + spec.getClass().getName());
  94:     DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
  95:     BigInteger p = dsaSpec.getP();
  96:     int size = p.bitLength();
  97:     this.engineInit(size, random);
  98:   }
  99: 
 100:   protected AlgorithmParameters engineGenerateParameters()
 101:   {
 102:     if (modulusLength < 1)
 103:       modulusLength = DSSKeyPairGenerator.DEFAULT_MODULUS_LENGTH;
 104: 
 105:     fips = new FIPS186(modulusLength, rnd);
 106:     BigInteger[] params = fips.generateParameters();
 107:     BigInteger p = params[3];
 108:     BigInteger q = params[2];
 109:     BigInteger g = params[5];
 110:     DSAParameterSpec spec = new DSAParameterSpec(p, q, g);
 111:     AlgorithmParameters result = null;
 112:     try
 113:       {
 114:         result = AlgorithmParameters.getInstance(Registry.DSS_KPG, GNU);
 115:         result.init(spec);
 116:       }
 117:     catch (NoSuchAlgorithmException ignore)
 118:       {
 119:       }
 120:     catch (InvalidParameterSpecException ignore)
 121:       {
 122:       }
 123:     return result;
 124:   }
 125: }