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

File AbstractBcAsymmetricCipherFactory.java

 
testCipherWithAsymmetricParameters: Unexpected parameters received for a asymmetric cipher: o...
testCipherWithWrongParameters: Cipher parameters are incompatible with this cipher: org....
 

Coverage histogram

../../../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

12
28
9
1
161
94
16
0.57
3.11
9
1.78

Classes

Class Line # Actions
AbstractBcAsymmetricCipherFactory 42 28 0% 16 29
0.4081632840.8%
 

Contributing tests

This file is covered by 4 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.cipher.internal.asymmetric.factory;
21   
22    import java.security.SecureRandom;
23   
24    import javax.inject.Named;
25   
26    import org.bouncycastle.crypto.AsymmetricBlockCipher;
27    import org.bouncycastle.crypto.params.ParametersWithRandom;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.crypto.cipher.AsymmetricCipher;
30    import org.xwiki.crypto.cipher.CipherFactory;
31    import org.xwiki.crypto.cipher.internal.asymmetric.BcBufferedAsymmetricCipher;
32    import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters;
33    import org.xwiki.crypto.params.cipher.CipherParameters;
34    import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters;
35   
36    /**
37    * Abstract base class for a Asymmetric Cipher Factory of Bouncy Castle cipher.
38    *
39    * @version $Id: af539e114e19ffe6113ade72e4e59343ba4dbad4 $
40    * @since 5.4M1
41    */
 
42    Test failure here public abstract class AbstractBcAsymmetricCipherFactory implements CipherFactory
43    {
44    private static final int[] DEFAULT_SUPPORTED_KEYSIZE = new int[] { 32, 48, 64, 96, 128, 256, 384, 512 };
45   
46    /**
47    * @return a new cipher engine instance.
48    */
49    protected abstract AsymmetricBlockCipher getEngineInstance();
50   
51    /**
52    * @return a new initialized cipher engine.
53    */
54    protected abstract AsymmetricBlockCipher getCipherInstance(boolean forEncryption,
55    AsymmetricCipherParameters parameters);
56   
57    /**
58    * @return the random source that may be used by the cipher.
59    */
60    protected abstract SecureRandom getRandomSource();
61   
62    /**
63    * Helper function to create supported key size arrays.
64    *
65    * @param minSize minimum size supported.
66    * @param maxSize maximum size supported.
67    * @param step intermediate step supported.
68    * @return an array of sizes.
69    */
 
70  0 toggle protected static int[] newKeySizeArray(int minSize, int maxSize, int step)
71    {
72  0 int[] result = new int[((maxSize - minSize) / step) + 1];
73  0 for (int i = minSize, j = 0; i <= maxSize; i += step, j++) {
74  0 result[j] = i;
75    }
76  0 return result;
77    }
78   
 
79  8 toggle @Override
80    public String getCipherAlgorithmName()
81    {
82  8 String hint = null;
83  8 Named named = this.getClass().getAnnotation(Named.class);
84  8 if (named != null) {
85  0 hint = named.value();
86    } else {
87  8 Component component = this.getClass().getAnnotation(Component.class);
88  8 if (component != null && component.hints().length > 0) {
89  8 hint = component.hints()[0];
90    }
91    }
92   
93  8 return hint;
94    }
95   
 
96  0 toggle @Override
97    public int getIVSize()
98    {
99  0 return -1;
100    }
101   
 
102  0 toggle @Override
103    public int[] getSupportedKeySizes()
104    {
105  0 return DEFAULT_SUPPORTED_KEYSIZE;
106    }
107   
 
108  0 toggle @Override
109    public int getKeySize()
110    {
111  0 int[] sizes = getSupportedKeySizes();
112  0 return sizes[sizes.length - 1];
113    }
114   
 
115  0 toggle @Override
116    public boolean isSupportedKeySize(int keySize)
117    {
118  0 int[] sizes = getSupportedKeySizes();
119  0 for (int i : sizes) {
120  0 if (i == keySize) {
121  0 return true;
122    }
123    }
124  0 return false;
125    }
126   
 
127  9 toggle private ParametersWithRandom toParametersWithRandom(AsymmetricCipherParameters parameters)
128    {
129  9 return new ParametersWithRandom(
130    Test failure here getBcCipherParameter(parameters),
131    getRandomSource());
132    }
133   
134    /**
135    * Convert cipher parameters to Bouncy Castle equivalent.
136    *
137    * @param parameters some asymmetric cipher parameters.
138    * @return equivalent bouncy castle parameters.
139    */
 
140  9 toggle protected org.bouncycastle.crypto.CipherParameters getBcCipherParameter(AsymmetricCipherParameters parameters)
141    {
142  8 if (parameters instanceof BcAsymmetricKeyParameters) {
143  8 return ((BcAsymmetricKeyParameters) parameters).getParameters();
144    }
145   
146    // TODO: convert parameters to compatible ones
147  0 throw new UnsupportedOperationException("Cipher parameters are incompatible with this cipher: "
148    Test failure here + parameters.getClass().getName());
149    }
150   
 
151  10 toggle @Override
152    public AsymmetricCipher getInstance(boolean forEncryption, CipherParameters parameters)
153    {
154  9 if (!(parameters instanceof AsymmetricCipherParameters)) {
155  0 throw new IllegalArgumentException("Unexpected parameters received for a asymmetric cipher: "
156    Test failure here + parameters.getClass().getName());
157    }
158  9 return new BcBufferedAsymmetricCipher(getCipherInstance(forEncryption, (AsymmetricCipherParameters) parameters),
159    Test failure here forEncryption, toParametersWithRandom((AsymmetricCipherParameters) parameters), getCipherAlgorithmName());
160    }
161    }