1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: 
  46: import ;
  47: import ;
  48: import ;
  49: import ;
  50: import ;
  51: import ;
  52: import ;
  53: import ;
  54: import ;
  55: import ;
  56: 
  57: import ;
  58: import ;
  59: 
  60: 
  63: public class DHParametersGenerator
  64:     extends AlgorithmParameterGeneratorSpi
  65: {
  66:   private static final Provider GNU_CRYPTO = new GnuCrypto();
  67: 
  68:   
  69:   private int modulusSize = -1;
  70: 
  71:   
  72:   private int exponentSize = -1;
  73: 
  74:   
  75:   private SecureRandom rnd;
  76: 
  77:   
  78:   private RFC2631 rfc2631;
  79: 
  80: 
  81:   protected void engineInit(int size, SecureRandom random)
  82:   {
  83:     if ((size % 256) != 0 || size < GnuDHKeyPairGenerator.DEFAULT_PRIME_SIZE)
  84:       throw new InvalidParameterException("Prime modulus (p) size (in bits) "
  85:                                           + "MUST be a multiple of 256, and "
  86:                                           + "greater than or equal to 1024");
  87:     this.modulusSize = size;
  88:     this.rnd = random;
  89:   }
  90: 
  91:   protected void engineInit(AlgorithmParameterSpec spec, SecureRandom random)
  92:       throws InvalidAlgorithmParameterException
  93:   {
  94:     if (spec instanceof DHParameterSpec)
  95:       {
  96:         DHParameterSpec dhSpec = (DHParameterSpec) spec;
  97:         BigInteger p = dhSpec.getP();
  98:         int size = p.bitLength();
  99:         this.engineInit(size, random);
 100:       }
 101:     else if (spec instanceof DHGenParameterSpec)
 102:       {
 103:         DHGenParameterSpec dhSpec = (DHGenParameterSpec) spec;
 104:         int size = dhSpec.getPrimeSize();
 105:         this.engineInit(size, random);
 106:         exponentSize = dhSpec.getExponentSize();
 107: 
 108:         if ((exponentSize % 8) != 0
 109:             || exponentSize < GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE)
 110:           throw new InvalidParameterException("Random exponent size (in bits) "
 111:                                               + "MUST be a multiple of 8, and "
 112:                                               + "greater than or equal to "
 113:                                               + GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE);
 114:         if (exponentSize > modulusSize)
 115:           throw new InvalidParameterException("Random exponent size (in bits) "
 116:                                               + "MUST be less than that of the "
 117:                                               + "public prime modulus (p)");
 118:       }
 119: 
 120:     throw new InvalidAlgorithmParameterException("Wrong AlgorithmParameterSpec type: "
 121:                                                  + spec.getClass().getName());
 122:   }
 123: 
 124:   protected AlgorithmParameters engineGenerateParameters()
 125:   {
 126:     if (modulusSize < 1)
 127:       modulusSize = GnuDHKeyPairGenerator.DEFAULT_PRIME_SIZE;
 128: 
 129:     if (exponentSize < 1)
 130:       exponentSize = GnuDHKeyPairGenerator.DEFAULT_EXPONENT_SIZE;
 131: 
 132:     rfc2631 = new RFC2631(exponentSize, modulusSize, rnd);
 133:     BigInteger[] params = rfc2631.generateParameters();
 134:     BigInteger p = params[RFC2631.DH_PARAMS_P];
 135:     BigInteger g = params[RFC2631.DH_PARAMS_G];
 136:     int l = params[RFC2631.DH_PARAMS_Q].bitLength();
 137:     DHParameterSpec spec = new DHParameterSpec(p, g, l);
 138:     AlgorithmParameters result = null;
 139:     try
 140:       {
 141:         result = AlgorithmParameters.getInstance(Registry.DH_KPG, GNU_CRYPTO);
 142:         result.init(spec);
 143:       }
 144:     catch (NoSuchAlgorithmException ignore)
 145:       {
 146:       }
 147:     catch (InvalidParameterSpecException ignore)
 148:       {
 149:       }
 150:     return result;
 151:   }
 152: }