1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.password.internal.pbe.factory; |
21 |
|
|
22 |
|
import org.bouncycastle.util.encoders.Base64; |
23 |
|
import org.junit.Before; |
24 |
|
import org.junit.Rule; |
25 |
|
import org.junit.Test; |
26 |
|
import org.xwiki.crypto.cipher.internal.symmetric.factory.BcAesCbcPaddedCipherFactory; |
27 |
|
import org.xwiki.crypto.cipher.internal.symmetric.factory.BcBlowfishCbcPaddedCipherFactory; |
28 |
|
import org.xwiki.crypto.cipher.internal.symmetric.factory.BcDesCbcPaddedCipherFactory; |
29 |
|
import org.xwiki.crypto.cipher.internal.symmetric.factory.BcDesEdeCbcPaddedCipherFactory; |
30 |
|
import org.xwiki.crypto.cipher.internal.symmetric.factory.BcRc2CbcPaddedCipherFactory; |
31 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA224DigestFactory; |
32 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA256DigestFactory; |
33 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA384DigestFactory; |
34 |
|
import org.xwiki.crypto.internal.digest.factory.BcSHA512DigestFactory; |
35 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
36 |
|
import org.xwiki.crypto.password.PasswordBasedCipher; |
37 |
|
import org.xwiki.crypto.password.PasswordBasedCipherFactory; |
38 |
|
import org.xwiki.crypto.password.PasswordToByteConverter; |
39 |
|
import org.xwiki.crypto.password.internal.kdf.factory.BcPKCS5S2KeyDerivationFunctionFactory; |
40 |
|
import org.xwiki.crypto.password.internal.kdf.factory.BcScryptKeyDerivationFunctionFactory; |
41 |
|
import org.xwiki.crypto.password.internal.kdf.factory.DefaultKeyDerivationFunctionFactory; |
42 |
|
import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters; |
43 |
|
import org.xwiki.crypto.password.params.PBKDF2Parameters; |
44 |
|
import org.xwiki.crypto.password.params.ScryptParameters; |
45 |
|
import org.xwiki.test.annotation.ComponentList; |
46 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
47 |
|
|
48 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
49 |
|
import static org.junit.Assert.assertThat; |
50 |
|
|
51 |
|
@ComponentList({DefaultKeyDerivationFunctionFactory.class, BcPKCS5S2KeyDerivationFunctionFactory.class, |
52 |
|
BcPBES2Rc2CipherFactory.class, BcRc2CbcPaddedCipherFactory.class, |
53 |
|
BcPBES2DesCipherFactory.class, BcDesCbcPaddedCipherFactory.class, |
54 |
|
BcPBES2DesEdeCipherFactory.class, BcDesEdeCbcPaddedCipherFactory.class, |
55 |
|
BcPBES2BlowfishCipherFactory.class, BcBlowfishCbcPaddedCipherFactory.class, |
56 |
|
BcPBES2AesCipherFactory.class, BcAesCbcPaddedCipherFactory.class, BcScryptKeyDerivationFunctionFactory.class, |
57 |
|
BcSHA224DigestFactory.class, BcSHA256DigestFactory.class, BcSHA384DigestFactory.class, BcSHA512DigestFactory.class}) |
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 8 |
Complexity Density: 0.67 |
|
58 |
|
public class BcPBES2CipherFactoryTest |
59 |
|
{ |
60 |
|
private static final byte[] PASSWORD = PasswordToByteConverter.convert("changeit"); |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final byte[] RSAKEY = Base64.decode( |
66 |
|
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDIY6+Wgj6MqdEd\n" |
67 |
|
+ "Yq6FgH5xMgTBmFqAonR/eshjxY2C6MHs+WmCmNSDik2NgZWIaODvOF9uOEK2U0Zf" |
68 |
|
+ "JEG2LcZxoeIEgg/mfII2f4DLy1JYajm/llzwFBzAd/Rkcs3qwP2ba5VK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|
83 |
|
+ "MgUQ15+FnwKBgAKf6/DUzUG3ARwkZbSiWb1hGEhkZMJHI29EoWnWHke5BiUI9nRQ" |
84 |
|
+ "jVAxADzqvFfnFOYA1xssddVEPbLaUmu0WjdPBTfFoaqzFQdkzpPPOGyENGpr0B9n" |
85 |
|
+ "MuQgdceg6eeKnnO5NOfYcdD3VnOCAInhKaFgRDjty7604hBkZ9oRLOOJAoGBAIJ+" |
86 |
|
+ "dmUMlGr80XADjTLh+DhqsA1r542DDv44LkXUetS9BOYjHuIuZnQO+/UoOBNJMsn4" |
87 |
|
+ "xGDNzN7FihQkRCeFkZL9arbFi3TpeUGw6vV38qEXE69eWVKvOuEkmpqJLphBDfom" |
88 |
|
+ "KNmvZopDtTAvt9SWybL+xp9ZUpK26ZfwebD2MU63AoGBAOa2Kt01DxoqiWiQP/ds" |
89 |
|
+ "Mc9wOw1zJsSmIK7wEiW3FkCz8uw3UgjF9ymYY/YAW3eZtuUpuxEzyENb9f21p4b2" |
90 |
|
+ "zYoZ7nCUo4TmVXxgCjiEWglg3b/R3xjQr1dAABhTeI8bXMv5r/tMUsnS79uKqwGD" |
91 |
|
+ "2Gc1syc3+055K4qcfZHH0XWu"); |
92 |
|
|
93 |
|
@Rule |
94 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> mocker = |
95 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2CipherFactory.class); |
96 |
|
|
97 |
|
@Rule |
98 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> desMocker = |
99 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2DesCipherFactory.class); |
100 |
|
|
101 |
|
@Rule |
102 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> desEdeMocker = |
103 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2DesEdeCipherFactory.class); |
104 |
|
|
105 |
|
@Rule |
106 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> rc2Mocker = |
107 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2Rc2CipherFactory.class); |
108 |
|
|
109 |
|
@Rule |
110 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> blowfishMocker = |
111 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2BlowfishCipherFactory.class); |
112 |
|
|
113 |
|
@Rule |
114 |
|
public final MockitoComponentMockingRule<PasswordBasedCipherFactory> aesMocker = |
115 |
|
new MockitoComponentMockingRule<PasswordBasedCipherFactory>(BcPBES2AesCipherFactory.class); |
116 |
|
|
117 |
|
PasswordBasedCipherFactory factory; |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
6 |
@Before... |
120 |
|
public void configure() throws Exception |
121 |
|
{ |
122 |
6 |
factory = mocker.getComponentUnderTest(); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
125 |
6 |
private void PBESEncodeDecodeTest(MockitoComponentMockingRule<? extends PasswordBasedCipherFactory> mocker,... |
126 |
|
int blockSize, KeyDerivationFunctionParameters kdfParams) throws Exception |
127 |
|
{ |
128 |
6 |
PasswordBasedCipher encCipher = mocker.getComponentUnderTest().getInstance(true, |
129 |
|
new KeyWithIVParameters(PASSWORD, blockSize), |
130 |
|
kdfParams); |
131 |
|
|
132 |
6 |
byte[] encoded = encCipher.doFinal(RSAKEY); |
133 |
|
|
134 |
6 |
PasswordBasedCipher decCipher = factory.getInstance(false, PASSWORD, encCipher.getEncoded()); |
135 |
|
|
136 |
6 |
assertThat(decCipher.getEncoded(), equalTo(encCipher.getEncoded())); |
137 |
6 |
assertThat(decCipher.doFinal(encoded),equalTo(RSAKEY)); |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
140 |
1 |
@Test... |
141 |
|
public void testPBES2BlowfishWithPBKDF2() throws Exception |
142 |
|
{ |
143 |
1 |
PBESEncodeDecodeTest(blowfishMocker, 8, new PBKDF2Parameters(16, 2048)); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void testPBES2BlowfishWithScrypt() throws Exception |
148 |
|
{ |
149 |
1 |
PBESEncodeDecodeTest(blowfishMocker, 8, new ScryptParameters(16)); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void testPBES2AESWithHmacSHA224() throws Exception |
154 |
|
{ |
155 |
1 |
PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-224")); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
158 |
1 |
@Test... |
159 |
|
public void testPBES2AESWithHmacSHA256() throws Exception |
160 |
|
{ |
161 |
1 |
PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-256")); |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
164 |
1 |
@Test... |
165 |
|
public void testPBES2AESWithHmacSHA384() throws Exception |
166 |
|
{ |
167 |
1 |
PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-384")); |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
170 |
1 |
@Test... |
171 |
|
public void testPBES2AESWithHmacSHA512() throws Exception |
172 |
|
{ |
173 |
1 |
PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-512")); |
174 |
|
} |
175 |
|
} |