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

File BcDSAKeyParameterGenerator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

16
31
6
1
172
99
16
0.52
5.17
6
2.67

Classes

Class Line # Actions
BcDSAKeyParameterGenerator 52 31 0% 16 13
0.75471775.5%
 

Contributing tests

This file is covered by 3 tests. .

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.DSAParametersGenerator;
30    import org.bouncycastle.crypto.params.DSAParameterGenerationParameters;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.crypto.DigestFactory;
35    import org.xwiki.crypto.KeyParametersGenerator;
36    import org.xwiki.crypto.internal.digest.factory.AbstractBcDigestFactory;
37    import org.xwiki.crypto.params.generator.KeyGenerationParameters;
38    import org.xwiki.crypto.params.generator.KeyParametersGenerationParameters;
39    import org.xwiki.crypto.params.generator.asymmetric.DSAKeyGenerationParameters;
40    import org.xwiki.crypto.params.generator.asymmetric.DSAKeyParametersGenerationParameters;
41    import org.xwiki.crypto.params.generator.asymmetric.DSAKeyValidationParameters;
42   
43    /**
44    * DSA key parameters generator.
45    *
46    * @version $Id: 27367aa49af8531072b271238179258c1f81d061 $
47    * @since 5.4M1
48    */
49    @Component
50    @Singleton
51    @Named("DSA")
 
52    public class BcDSAKeyParameterGenerator implements KeyParametersGenerator
53    {
54    @Inject
55    private Provider<SecureRandom> random;
56   
57    @Inject
58    private ComponentManager manager;
59   
 
60  1 toggle @Override
61    public KeyGenerationParameters generate()
62    {
63  1 return generate(new DSAKeyParametersGenerationParameters());
64    }
65   
 
66  4 toggle @Override
67    public KeyGenerationParameters generate(KeyParametersGenerationParameters parameters)
68    {
69  4 if (!(parameters instanceof DSAKeyParametersGenerationParameters)) {
70  0 throw new IllegalArgumentException("Invalid parameters for DSA key parameters generator: "
71    + parameters.getClass().getName());
72    }
73   
74  4 org.bouncycastle.crypto.params.DSAParameters dsaParams =
75    getDsaParameters(this.random.get(), (DSAKeyParametersGenerationParameters) parameters);
76   
77  4 org.bouncycastle.crypto.params.DSAValidationParameters dsaValidParams = dsaParams.getValidationParameters();
78   
79  4 return new DSAKeyGenerationParameters(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG(),
80    new DSAKeyValidationParameters(dsaValidParams.getSeed(), dsaValidParams.getCounter(),
81    getUsage(dsaValidParams.getUsageIndex())));
82    }
83   
84    /**
85    * Generate DSA parameters.
86    *
87    * Shared with the key generator to optimize key generation.
88    *
89    * @param params the parameters generation parameters.
90    * @return shared DSA parameters for key generation.
91    */
 
92  4 toggle org.bouncycastle.crypto.params.DSAParameters getDsaParameters(SecureRandom random,
93    DSAKeyParametersGenerationParameters params)
94    {
95  4 DSAParametersGenerator paramGen = getGenerator(params.getHashHint());
96   
97  4 if (params.use186r3()) {
98  1 DSAParameterGenerationParameters p = new DSAParameterGenerationParameters(
99    params.getPrimePsize() * 8, params.getPrimeQsize() * 8,
100    params.getCertainty(), random,
101    getUsageIndex(params.getUsage()));
102  1 paramGen.init(p);
103    } else {
104  3 paramGen.init(params.getStrength() * 8, params.getCertainty(), random);
105    }
106   
107  4 return paramGen.generateParameters();
108    }
109   
110    /**
111    * Create an instance of a DSA parameter generator using the appropriate hash algorithm.
112    *
113    * @param hint hint of the hash algorithm to use.
114    * @return a DSA parameter generator.
115    */
 
116  4 toggle private DSAParametersGenerator getGenerator(String hint)
117    {
118  4 if (hint == null || "SHA-1".equals(hint)) {
119  3 return new DSAParametersGenerator();
120    }
121   
122  1 DigestFactory factory;
123   
124  1 try {
125  1 factory = this.manager.getInstance(DigestFactory.class, hint);
126    } catch (ComponentLookupException e) {
127  0 throw new UnsupportedOperationException("Cryptographic hash (digest) algorithm not found.", e);
128    }
129   
130  1 if (!(factory instanceof AbstractBcDigestFactory)) {
131  0 throw new IllegalArgumentException(
132    "Requested cryptographic hash algorithm is not implemented by a factory compatible with this factory."
133    + " Factory found: " + factory.getClass().getName());
134    }
135   
136  1 return new DSAParametersGenerator(((AbstractBcDigestFactory) factory).getDigestInstance());
137    }
138   
139    /**
140    * Convert key usage to key usage index.
141    *
142    * Shared with the key generator to optimize key generation.
143    *
144    * @param usage a key usage.
145    * @return a BC key usage index.
146    */
 
147  6 toggle static int getUsageIndex(DSAKeyValidationParameters.Usage usage)
148    {
149  6 if (usage == DSAKeyValidationParameters.Usage.DIGITAL_SIGNATURE) {
150  0 return DSAParameterGenerationParameters.DIGITAL_SIGNATURE_USAGE;
151  6 } else if (usage == DSAKeyValidationParameters.Usage.KEY_ESTABLISHMENT) {
152  0 return DSAParameterGenerationParameters.KEY_ESTABLISHMENT_USAGE;
153    }
154  6 return -1;
155    }
156   
157    /**
158    * Convert usage index to key usage.
159    *
160    * @param usage usage index.
161    * @return key usage.
162    */
 
163  4 toggle private static DSAKeyValidationParameters.Usage getUsage(int usage)
164    {
165  4 if (usage == DSAParameterGenerationParameters.DIGITAL_SIGNATURE_USAGE) {
166  0 return DSAKeyValidationParameters.Usage.DIGITAL_SIGNATURE;
167  4 } else if (usage == DSAParameterGenerationParameters.KEY_ESTABLISHMENT_USAGE) {
168  0 return DSAKeyValidationParameters.Usage.KEY_ESTABLISHMENT;
169    }
170  4 return DSAKeyValidationParameters.Usage.ANY;
171    }
172    }