1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.crypto.password.internal.pbe.factory

File BcPBES2CipherFactoryTest.java

 

Code metrics

0
12
8
1
175
131
8
0.67
1.5
8
1

Classes

Class Line # Actions
BcPBES2CipherFactoryTest 58 12 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 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.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})
 
58    public class BcPBES2CipherFactoryTest
59    {
60    private static final byte[] PASSWORD = PasswordToByteConverter.convert("changeit");
61   
62    /**
63    * Sample RSA KEY and encrypted versions taken from not-yet-commons-ssl source code.
64    */
65    private static final byte[] RSAKEY = Base64.decode(
66    "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDIY6+Wgj6MqdEd\n"
67    + "Yq6FgH5xMgTBmFqAonR/eshjxY2C6MHs+WmCmNSDik2NgZWIaODvOF9uOEK2U0Zf"
68    + "JEG2LcZxoeIEgg/mfII2f4DLy1JYajm/llzwFBzAd/Rkcs3qwP2ba5VKn/pSqNLl"
69    + "nKHMXkXO+9SjfHDx95x2dK1dB8eGQGculOMcTm3uK7UlWNO4TSlwG9qHZ1aoM3GI"
70    + "g5C1fIpbxJqDVjFq6fFAapE3KRIWIQmKd3E5ICcDErqr/AapxnfO8UFNxVWSOLW7"
71    + "ZAfis4w/c8/EAgyQHw42R0dNyjUOZsToF8McCsOpRjGolSU8aUyqspvd8IWJPd5d"
72    + "6HBHueXNAgMBAAECggEAV3q9MpVVPQ79TTjBO2Km0D+nt+QMzk8dUHGHfZbGejmm"
73    + "Pw96shqJ24rK5FWHs+8lEwmnD3TcGsAr3mjzjtZY5U5oXtNwoYwFRElRLqZqIlLt"
74    + "NugrVltRWeyD8j30CuGJVQoYOGWyX9d3ielg8NjO3NcvMtembttLoKK68/vrbH11"
75    + "9W7wr5p8/xyMfyl9curnmCFk5QqJ1FBpjPWY05NDIBCUJB0tGAqViCpxEeWPSlvb"
76    + "xcElqWfdbtnsYUxYU+iOTHHotoKnz4nLHYK2/njMhlCEyMXfu1DJOd8rg5yXewJF"
77    + "v6NhXgWStSexAT1bZ17LROazVcHfWB9QmXF1Fm7vOQKBgQD+dZxPDOi3Y4gCFegn"
78    + "Z+epNyl2aPTkseEZxrIqPKLHsGxUfYjQqkX2RdfTrq2vf4vFlN6uCXhSlZKXfLH/"
79    + "iQ8FAzqenhVVHK2fv5xB0SE5zNmcHDrHshl+/zUNI2u5AMFECVO2SVbgoFjvgkou"
80    + "FolK8XUXfHfb4f732LUyYI0lEwKBgQDJmkWHhzekz3P5iWaAt1SH8bZpt2hqa6Bx"
81    + "A4VvMdtmjCxEDETN0Rb3CPYxw3qa3xGfW1y1j/49xi4gr69yaT2Tbca7PFGUmWRo"
82    + "OJwfCUB5uBUi6UVytK19OVKReOm4666x8P3YO4cxxSI/HeoSU0HR1kkX9rGmrsGN"
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   
 
119  6 toggle @Before
120    public void configure() throws Exception
121    {
122  6 factory = mocker.getComponentUnderTest();
123    }
124   
 
125  6 toggle 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   
 
140  1 toggle @Test
141    public void testPBES2BlowfishWithPBKDF2() throws Exception
142    {
143  1 PBESEncodeDecodeTest(blowfishMocker, 8, new PBKDF2Parameters(16, 2048));
144    }
145   
 
146  1 toggle @Test
147    public void testPBES2BlowfishWithScrypt() throws Exception
148    {
149  1 PBESEncodeDecodeTest(blowfishMocker, 8, new ScryptParameters(16));
150    }
151   
 
152  1 toggle @Test
153    public void testPBES2AESWithHmacSHA224() throws Exception
154    {
155  1 PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-224"));
156    }
157   
 
158  1 toggle @Test
159    public void testPBES2AESWithHmacSHA256() throws Exception
160    {
161  1 PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-256"));
162    }
163   
 
164  1 toggle @Test
165    public void testPBES2AESWithHmacSHA384() throws Exception
166    {
167  1 PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-384"));
168    }
169   
 
170  1 toggle @Test
171    public void testPBES2AESWithHmacSHA512() throws Exception
172    {
173  1 PBESEncodeDecodeTest(aesMocker, 16, new PBKDF2Parameters(16, 2048, "SHA-512"));
174    }
175    }