Home | History | Annotate | Download | only in provider
      1 package org.bouncycastle.jce.provider;
      2 
      3 import java.security.AlgorithmParameterGeneratorSpi;
      4 import java.security.AlgorithmParameters;
      5 import java.security.InvalidAlgorithmParameterException;
      6 import java.security.InvalidParameterException;
      7 import java.security.SecureRandom;
      8 import java.security.spec.AlgorithmParameterSpec;
      9 import java.security.spec.DSAParameterSpec;
     10 
     11 import javax.crypto.spec.DHGenParameterSpec;
     12 import javax.crypto.spec.DHParameterSpec;
     13 import javax.crypto.spec.IvParameterSpec;
     14 // BEGIN android-removed
     15 // import javax.crypto.spec.RC2ParameterSpec;
     16 // END android-removed
     17 
     18 import org.bouncycastle.crypto.generators.DHParametersGenerator;
     19 import org.bouncycastle.crypto.generators.DSAParametersGenerator;
     20 // BEGIN android-removed
     21 // import org.bouncycastle.crypto.generators.ElGamalParametersGenerator;
     22 // import org.bouncycastle.crypto.generators.GOST3410ParametersGenerator;
     23 // END android-removed
     24 import org.bouncycastle.crypto.params.DHParameters;
     25 import org.bouncycastle.crypto.params.DSAParameters;
     26 // BEGIN android-removed
     27 // import org.bouncycastle.crypto.params.ElGamalParameters;
     28 // import org.bouncycastle.crypto.params.GOST3410Parameters;
     29 // import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
     30 // import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec;
     31 // END android-removed
     32 
     33 public abstract class JDKAlgorithmParameterGenerator
     34     extends AlgorithmParameterGeneratorSpi
     35 {
     36     protected SecureRandom  random;
     37     protected int           strength = 1024;
     38 
     39     protected void engineInit(
     40         int             strength,
     41         SecureRandom    random)
     42     {
     43         this.strength = strength;
     44         this.random = random;
     45     }
     46 
     47     public static class DH
     48         extends JDKAlgorithmParameterGenerator
     49     {
     50         private int l = 0;
     51 
     52         protected void engineInit(
     53             AlgorithmParameterSpec  genParamSpec,
     54             SecureRandom            random)
     55             throws InvalidAlgorithmParameterException
     56         {
     57             if (!(genParamSpec instanceof DHGenParameterSpec))
     58             {
     59                 throw new InvalidAlgorithmParameterException("DH parameter generator requires a DHGenParameterSpec for initialisation");
     60             }
     61             DHGenParameterSpec  spec = (DHGenParameterSpec)genParamSpec;
     62 
     63             this.strength = spec.getPrimeSize();
     64             this.l = spec.getExponentSize();
     65             this.random = random;
     66         }
     67 
     68         protected AlgorithmParameters engineGenerateParameters()
     69         {
     70             DHParametersGenerator        pGen = new DHParametersGenerator();
     71 
     72             if (random != null)
     73             {
     74                 pGen.init(strength, 20, random);
     75             }
     76             else
     77             {
     78                 pGen.init(strength, 20, new SecureRandom());
     79             }
     80 
     81             DHParameters                p = pGen.generateParameters();
     82 
     83             AlgorithmParameters params;
     84 
     85             try
     86             {
     87                 params = AlgorithmParameters.getInstance("DH", BouncyCastleProvider.PROVIDER_NAME);
     88                 params.init(new DHParameterSpec(p.getP(), p.getG(), l));
     89             }
     90             catch (Exception e)
     91             {
     92                 throw new RuntimeException(e.getMessage());
     93             }
     94 
     95             return params;
     96         }
     97     }
     98 
     99     public static class DSA
    100         extends JDKAlgorithmParameterGenerator
    101     {
    102         protected void engineInit(
    103             int             strength,
    104             SecureRandom    random)
    105         {
    106             if (strength < 512 || strength > 1024 || strength % 64 != 0)
    107             {
    108                 throw new InvalidParameterException("strength must be from 512 - 1024 and a multiple of 64");
    109             }
    110 
    111             this.strength = strength;
    112             this.random = random;
    113         }
    114 
    115         protected void engineInit(
    116             AlgorithmParameterSpec  genParamSpec,
    117             SecureRandom            random)
    118             throws InvalidAlgorithmParameterException
    119         {
    120             throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DSA parameter generation.");
    121         }
    122 
    123         protected AlgorithmParameters engineGenerateParameters()
    124         {
    125             DSAParametersGenerator pGen = new DSAParametersGenerator();
    126 
    127             if (random != null)
    128             {
    129                 pGen.init(strength, 20, random);
    130             }
    131             else
    132             {
    133                 pGen.init(strength, 20, new SecureRandom());
    134             }
    135 
    136             DSAParameters p = pGen.generateParameters();
    137 
    138             AlgorithmParameters params;
    139 
    140             try
    141             {
    142                 params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
    143                 params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
    144             }
    145             catch (Exception e)
    146             {
    147                 throw new RuntimeException(e.getMessage());
    148             }
    149 
    150             return params;
    151         }
    152     }
    153 
    154     // BEGIN android-removed
    155     // public static class GOST3410
    156     //     extends JDKAlgorithmParameterGenerator
    157     // {
    158     //     protected void engineInit(
    159     //             AlgorithmParameterSpec  genParamSpec,
    160     //             SecureRandom            random)
    161     //     throws InvalidAlgorithmParameterException
    162     //     {
    163     //         throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for GOST3410 parameter generation.");
    164     //     }
    165     //
    166     //     protected AlgorithmParameters engineGenerateParameters()
    167     //     {
    168     //         GOST3410ParametersGenerator pGen = new GOST3410ParametersGenerator();
    169     //
    170     //         if (random != null)
    171     //         {
    172     //             pGen.init(strength, 2, random);
    173     //         }
    174     //         else
    175     //         {
    176     //             pGen.init(strength, 2, new SecureRandom());
    177     //         }
    178     //
    179     //         GOST3410Parameters p = pGen.generateParameters();
    180     //
    181     //         AlgorithmParameters params;
    182     //
    183     //         try
    184     //         {
    185     //             params = AlgorithmParameters.getInstance("GOST3410", BouncyCastleProvider.PROVIDER_NAME);
    186     //             params.init(new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(p.getP(), p.getQ(), p.getA())));
    187     //         }
    188     //         catch (Exception e)
    189     //         {
    190     //             throw new RuntimeException(e.getMessage());
    191     //         }
    192     //
    193     //         return params;
    194     //     }
    195     // }
    196     //
    197     // public static class ElGamal
    198     //     extends JDKAlgorithmParameterGenerator
    199     // {
    200     //     private int l = 0;
    201     //
    202     //     protected void engineInit(
    203     //         AlgorithmParameterSpec  genParamSpec,
    204     //         SecureRandom            random)
    205     //         throws InvalidAlgorithmParameterException
    206     //     {
    207     //         if (!(genParamSpec instanceof DHGenParameterSpec))
    208     //         {
    209     //             throw new InvalidAlgorithmParameterException("DH parameter generator requires a DHGenParameterSpec for initialisation");
    210     //         }
    211     //         DHGenParameterSpec  spec = (DHGenParameterSpec)genParamSpec;
    212     //
    213     //         this.strength = spec.getPrimeSize();
    214     //         this.l = spec.getExponentSize();
    215     //         this.random = random;
    216     //     }
    217     //
    218     //     protected AlgorithmParameters engineGenerateParameters()
    219     //     {
    220     //         ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
    221     //
    222     //         if (random != null)
    223     //         {
    224     //             pGen.init(strength, 20, random);
    225     //         }
    226     //         else
    227     //         {
    228     //             pGen.init(strength, 20, new SecureRandom());
    229     //         }
    230     //
    231     //         ElGamalParameters p = pGen.generateParameters();
    232     //
    233     //         AlgorithmParameters params;
    234     //
    235     //         try
    236     //         {
    237     //             params = AlgorithmParameters.getInstance("ElGamal", BouncyCastleProvider.PROVIDER_NAME);
    238     //             params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    239     //         }
    240     //         catch (Exception e)
    241     //         {
    242     //             throw new RuntimeException(e.getMessage());
    243     //         }
    244     //
    245     //         return params;
    246     //     }
    247     // }
    248     //
    249     // public static class DES
    250     //     extends JDKAlgorithmParameterGenerator
    251     // {
    252     //     protected void engineInit(
    253     //         AlgorithmParameterSpec  genParamSpec,
    254     //         SecureRandom            random)
    255     //         throws InvalidAlgorithmParameterException
    256     //     {
    257     //         throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DES parameter generation.");
    258     //     }
    259     //
    260     //     protected AlgorithmParameters engineGenerateParameters()
    261     //     {
    262     //         byte[]  iv = new byte[8];
    263     //
    264     //         if (random == null)
    265     //         {
    266     //             random = new SecureRandom();
    267     //         }
    268     //
    269     //         random.nextBytes(iv);
    270     //
    271     //         AlgorithmParameters params;
    272     //
    273     //         try
    274     //         {
    275     //             params = AlgorithmParameters.getInstance("DES", BouncyCastleProvider.PROVIDER_NAME);
    276     //             params.init(new IvParameterSpec(iv));
    277     //         }
    278     //         catch (Exception e)
    279     //         {
    280     //             throw new RuntimeException(e.getMessage());
    281     //         }
    282     //
    283     //         return params;
    284     //     }
    285     // }
    286     //
    287     // public static class RC2
    288     //     extends JDKAlgorithmParameterGenerator
    289     // {
    290     //     RC2ParameterSpec    spec = null;
    291     //
    292     //     protected void engineInit(
    293     //         AlgorithmParameterSpec  genParamSpec,
    294     //         SecureRandom            random)
    295     //         throws InvalidAlgorithmParameterException
    296     //     {
    297     //         if (genParamSpec instanceof RC2ParameterSpec)
    298     //         {
    299     //             spec = (RC2ParameterSpec)genParamSpec;
    300     //             return;
    301     //         }
    302     //
    303     //         throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for RC2 parameter generation.");
    304     //     }
    305     //
    306     //     protected AlgorithmParameters engineGenerateParameters()
    307     //     {
    308     //         AlgorithmParameters params;
    309     //
    310     //         if (spec == null)
    311     //         {
    312     //             byte[]  iv = new byte[8];
    313     //
    314     //             if (random == null)
    315     //             {
    316     //                 random = new SecureRandom();
    317     //             }
    318     //
    319     //             random.nextBytes(iv);
    320     //
    321     //             try
    322     //             {
    323     //                 params = AlgorithmParameters.getInstance("RC2", BouncyCastleProvider.PROVIDER_NAME);
    324     //                 params.init(new IvParameterSpec(iv));
    325     //             }
    326     //             catch (Exception e)
    327     //             {
    328     //                 throw new RuntimeException(e.getMessage());
    329     //             }
    330     //         }
    331     //         else
    332     //         {
    333     //             try
    334     //             {
    335     //                 params = AlgorithmParameters.getInstance("RC2", BouncyCastleProvider.PROVIDER_NAME);
    336     //                 params.init(spec);
    337     //             }
    338     //             catch (Exception e)
    339     //             {
    340     //                 throw new RuntimeException(e.getMessage());
    341     //             }
    342     //         }
    343     //
    344     //         return params;
    345     //     }
    346     // }
    347     // END android-removed
    348 }
    349