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