1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.asymmetric.generator

File BcDHKeyParameterGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
9
3
1
98
53
5
0.56
3
3
1.67

Classes

Class Line # Actions
BcDHKeyParameterGenerator 47 9 0% 5 16
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.internal.asymmetric.generator;
21   
22    import java.security.SecureRandom;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.bouncycastle.crypto.generators.DHParametersGenerator;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.crypto.KeyParametersGenerator;
32    import org.xwiki.crypto.params.generator.KeyGenerationParameters;
33    import org.xwiki.crypto.params.generator.KeyParametersGenerationParameters;
34    import org.xwiki.crypto.params.generator.asymmetric.DHKeyGenerationParameters;
35    import org.xwiki.crypto.params.generator.asymmetric.DHKeyParametersGenerationParameters;
36    import org.xwiki.crypto.params.generator.asymmetric.DHKeyValidationParameters;
37   
38    /**
39    * Diffie-Hellman key parameters generator.
40    *
41    * @version $Id: 70f1715a30172197cd948c59ac507127f5379ce4 $
42    * @since 5.4M1
43    */
44    @Component
45    @Singleton
46    @Named("DH")
 
47    public class BcDHKeyParameterGenerator implements KeyParametersGenerator
48    {
49    @Inject
50    private Provider<SecureRandom> random;
51   
 
52  0 toggle @Override
53    public KeyGenerationParameters generate()
54    {
55  0 return generate(new DHKeyParametersGenerationParameters());
56    }
57   
 
58  0 toggle @Override
59    public KeyGenerationParameters generate(KeyParametersGenerationParameters parameters)
60    {
61  0 if (!(parameters instanceof DHKeyParametersGenerationParameters)) {
62  0 throw new IllegalArgumentException("Invalid parameters for DH key parameters generator: "
63    + parameters.getClass().getName());
64    }
65   
66  0 org.bouncycastle.crypto.params.DHParameters dhParams =
67    getDhParameters(this.random.get(), (DHKeyParametersGenerationParameters) parameters);
68   
69  0 org.bouncycastle.crypto.params.DHValidationParameters dhValidParams = dhParams.getValidationParameters();
70   
71  0 return new DHKeyGenerationParameters(
72    dhParams.getP(), dhParams.getG(), dhParams.getQ(),
73    dhParams.getM() / 8, dhParams.getL() / 8,
74    dhParams.getJ(),
75  0 ((dhValidParams != null)
76    ? new DHKeyValidationParameters(dhValidParams.getSeed(), dhValidParams.getCounter())
77    : null)
78    );
79    }
80   
81    /**
82    * Generate DH parameters.
83    *
84    * Shared with the key generator to optimize key generation.
85    *
86    * @param params the parameters generation parameters.
87    * @return shared DSA parameters for key generation.
88    */
 
89  0 toggle static org.bouncycastle.crypto.params.DHParameters getDhParameters(SecureRandom random,
90    DHKeyParametersGenerationParameters params)
91    {
92  0 DHParametersGenerator paramGen = new DHParametersGenerator();
93   
94  0 paramGen.init(params.getStrength() * 8, params.getCertainty(), random);
95   
96  0 return paramGen.generateParameters();
97    }
98    }