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

File DSAKeyParametersGenerationParameters.java

 

Coverage histogram

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

Code metrics

10
28
15
1
214
92
20
0.71
1.87
15
1.33

Classes

Class Line # Actions
DSAKeyParametersGenerationParameters 33 28 0% 20 15
0.716981171.7%
 

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.params.generator.asymmetric;
21   
22    import org.xwiki.crypto.params.generator.KeyGenerationParameters;
23    import org.xwiki.crypto.params.generator.KeyParametersGenerationParameters;
24    import org.xwiki.crypto.params.generator.symmetric.GenericKeyGenerationParameters;
25   
26    /**
27    * Parameters for RSA key pair generation using either FIPS186.2 or FIPS186.3.
28    * DSAParameters are generated from these and use to generated the key.
29    *
30    * @version $Id: 04a5ff7a00de90fd8ba1a08d55ca2604a9a5df3e $
31    * @since 5.4M1
32    */
 
33    public class DSAKeyParametersGenerationParameters extends GenericKeyGenerationParameters
34    implements KeyParametersGenerationParameters, KeyGenerationParameters
35    {
36    /** Default key strength. */
37    private static final int DEFAULT_STRENGTH = 128;
38   
39    /** Default certainty value for prime evaluation. */
40    private static final int DEFAULT_CERTAINTY = 20;
41   
42    /** True if the generated key should use FIPS 186-3. */
43    private final boolean use186r3;
44   
45    /** The certainty for prime evaluation. */
46    private final int certainty;
47   
48    /** Size of prime Q. */
49    private final int nSize;
50   
51    /** Specific usage index. */
52    private final DSAKeyValidationParameters.Usage usage;
53   
54    /** Cryptographic hash function to use for parameter generation. */
55    private final String digest;
56   
57    /**
58    * Parameters for FIPS186.2 using a default strength of {@value #DEFAULT_CERTAINTY},
59    * and a certainty of {@value #DEFAULT_CERTAINTY}.
60    */
 
61  3 toggle public DSAKeyParametersGenerationParameters()
62    {
63  3 this(DEFAULT_STRENGTH, DEFAULT_CERTAINTY);
64    }
65   
66    /**
67    * Parameters for FIPS186.2 using a default certainty of {@value #DEFAULT_CERTAINTY}.
68    *
69    * @param lsize size of the P prime number (the key strength) in bytes.
70    */
 
71  0 toggle public DSAKeyParametersGenerationParameters(int lsize)
72    {
73  0 this(lsize, DEFAULT_CERTAINTY);
74    }
75   
76    /**
77    * Parameters for FIPS186.2.
78    *
79    * @param lsize size of the P prime number (the key strength) in bytes.
80    * @param certainty certainty for prime evaluation.
81    */
 
82  3 toggle public DSAKeyParametersGenerationParameters(int lsize, int certainty)
83    {
84  3 this(false, lsize, -1, certainty, DSAKeyValidationParameters.Usage.ANY, getDigestHint(getDefaultNsize(lsize)));
85    }
86   
87    /**
88    * Parameters for FIPS186.3 with any usage of the key.
89    *
90    * @param lsize size of the P prime number (the key strength) in bytes.
91    * @param nsize size of the Q prime number in bytes.
92    * @param certainty certainty for prime evaluation.
93    */
 
94  1 toggle public DSAKeyParametersGenerationParameters(int lsize, int nsize, int certainty)
95    {
96  1 this(lsize, nsize, certainty, DSAKeyValidationParameters.Usage.ANY);
97    }
98   
99    /**
100    * Parameters for FIPS186.3.
101    *
102    * @param lsize size of the P prime number (the key strength) in bytes.
103    * @param nsize size of the Q prime number in bytes.
104    * @param certainty certainty for prime evaluation.
105    * @param usage target usage (this has the effect of using verifiable canonical generation of G).
106    */
 
107  1 toggle public DSAKeyParametersGenerationParameters(int lsize, int nsize, int certainty,
108    DSAKeyValidationParameters.Usage usage)
109    {
110  1 this(true, lsize, nsize, certainty, usage, getDigestHint(nsize));
111    }
112   
113    /**
114    * Parameters for FIPS186.3.
115    *
116    * @param lsize size of the P prime number (the key strength) in bytes.
117    * @param nsize size of the Q prime number in bytes.
118    * @param certainty certainty for prime evaluation.
119    * @param usage target usage (this has the effect of using verifiable canonical generation of G).
120    * @param hashHint hint of the cryptographic hash (digest) to use.
121    */
 
122  0 toggle public DSAKeyParametersGenerationParameters(int lsize, int nsize, int certainty,
123    DSAKeyValidationParameters.Usage usage, String hashHint)
124    {
125  0 this(true, lsize, nsize, certainty, usage, hashHint);
126    }
127   
128    /**
129    * Private constructor.
130    *
131    * @param use186r3 true if FIPS 183.3 should be used.
132    * @param lsize size of the P prime number (the key strength) in bytes.
133    * @param nsize size of the Q prime number in bytes.
134    * @param certainty certainty for prime evaluation.
135    * @param usage target usage (this has the effect of using verifiable canonical generation of G).
136    */
 
137  4 toggle private DSAKeyParametersGenerationParameters(boolean use186r3, int lsize, int nsize, int certainty,
138    DSAKeyValidationParameters.Usage usage, String digest)
139    {
140  4 super(lsize);
141  4 this.certainty = certainty;
142  4 this.nSize = nsize;
143  4 this.usage = usage;
144  4 this.use186r3 = use186r3;
145  4 this.digest = digest;
146    }
147   
 
148  3 toggle private static int getDefaultNsize(int lSize)
149    {
150  3 return lSize > 128 ? 32 : 20;
151    }
152   
 
153  4 toggle private static String getDigestHint(int nSize)
154    {
155  4 if (nSize <= 20) {
156  3 return "SHA-1";
157  1 } else if (nSize <= 28) {
158  1 return "SHA-224";
159  0 } else if (nSize <= 32) {
160  0 return "SHA-256";
161  0 } else if (nSize <= 48) {
162  0 return "SHA-384";
163    }
164  0 return "SHA-512";
165    }
166   
167    /**
168    * @return true if the key should be generated according to FIPS 186.3.
169    */
 
170  4 toggle public boolean use186r3()
171    {
172  4 return this.use186r3;
173    }
174   
175    /**
176    * @return the requested P prime size in byte.
177    */
 
178  1 toggle public int getPrimePsize()
179    {
180  1 return getStrength();
181    }
182   
183    /**
184    * @return the requested Q prime size in byte.
185    */
 
186  1 toggle public int getPrimeQsize()
187    {
188  1 return this.nSize;
189    }
190   
191    /**
192    * @return the requested certainty for prime number.
193    */
 
194  4 toggle public int getCertainty()
195    {
196  4 return this.certainty;
197    }
198   
199    /**
200    * @return the requested usage for the key.
201    */
 
202  1 toggle public DSAKeyValidationParameters.Usage getUsage()
203    {
204  1 return this.usage;
205    }
206   
207    /**
208    * @return the hints of the cryptographic hash to use.
209    */
 
210  4 toggle public String getHashHint()
211    {
212  4 return this.digest;
213    }
214    }