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

File AbstractBcPBES2Rc5CipherFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

10
20
5
1
116
77
10
0.5
4
5
2

Classes

Class Line # Actions
AbstractBcPBES2Rc5CipherFactory 45 20 0% 10 35
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.asn1.ASN1ObjectIdentifier;
23    import org.bouncycastle.asn1.pkcs.EncryptionScheme;
24    import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
25    import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
26    import org.xwiki.crypto.params.cipher.symmetric.KeyParameter;
27    import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters;
28    import org.xwiki.crypto.params.cipher.symmetric.RC5KeyParameters;
29    import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters;
30    import org.xwiki.crypto.password.KeyDerivationFunction;
31    import org.xwiki.crypto.password.PasswordBasedCipher;
32    import org.xwiki.crypto.password.internal.pbe.AbstractBcPBES2Cipher;
33    import org.xwiki.crypto.password.internal.pbe.RC5CBCParameter;
34   
35    /**
36    * Abstract base class for PBES2 RC5 cipher factory.
37    *
38    * WARNING: RC5 is protected by U.S. Patents 5,724,428 and 5,835,600. Therefore, before expiration of these patents,
39    * the usage of this algorithm is subject to restricted usage on the US territories.
40    * RC5 is a trademark of RSA Security Inc.
41    *
42    * @version $Id: a595e2efff5f7712ad524f8451992dd0d13df747 $
43    * @since 5.4M1
44    */
 
45    public abstract class AbstractBcPBES2Rc5CipherFactory extends AbstractBcPBES2CipherFactory
46    {
47    private static final ASN1ObjectIdentifier ALG_ID = PKCSObjectIdentifiers.encryptionAlgorithm.branch("9");
48   
 
49  0 toggle @Override
50    public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password,
51    KeyDerivationFunction kdf)
52    {
53  0 KeyWithIVParameters params = null;
54   
55  0 if (password instanceof KeyWithIVParameters) {
56  0 KeyParameter passkey = ((KeyWithIVParameters) password).getKeyParameter();
57  0 if (passkey instanceof RC5KeyParameters) {
58  0 params = new KeyWithIVParameters(
59    new RC5KeyParameters(kdf.derive(passkey.getKey()).getKey(),
60    ((RC5KeyParameters) passkey).getRounds()),
61    ((KeyWithIVParameters) password).getIV());
62    }
63  0 } else if (password instanceof RC5KeyParameters) {
64  0 params = kdf.derive(((KeyParameter) password).getKey(), getIVSize());
65  0 params = new KeyWithIVParameters(new RC5KeyParameters(params.getKey(),
66    ((RC5KeyParameters) password).getRounds()), params.getIV());
67    }
68   
69  0 if (params == null) {
70  0 throw new IllegalArgumentException("Invalid cipher parameters for RC5-32 password based cipher: "
71    + password.getClass().getName());
72    }
73   
74  0 return getPasswordBasedCipher(forEncryption, kdf, params);
75    }
76   
 
77  0 toggle @Override
78    protected PasswordBasedCipher getPasswordBasedCipher(boolean forEncryption, KeyDerivationFunction kdf,
79    SymmetricCipherParameters params)
80    {
81  0 return new AbstractBcPBES2Cipher(getCipherFactory().getInstance(forEncryption, params), kdf, params)
82    {
 
83  0 toggle @Override
84    protected EncryptionScheme getScheme(SymmetricCipherParameters parameters)
85    {
86  0 return new EncryptionScheme(ALG_ID,
87    new RC5CBCParameter(
88    ((RC5KeyParameters) ((KeyWithIVParameters) parameters).getKeyParameter()).getRounds(),
89    getOutputBlockSize(),
90    ((KeyWithIVParameters) parameters).getIV()));
91    }
92    };
93    }
94   
 
95  0 toggle @Override
96    protected PasswordBasedCipher getInstance(boolean forEncryption, byte[] password, KeyDerivationFunc kdfParams,
97    EncryptionScheme scheme)
98    {
99  0 KeyDerivationFunction kdf = getKeyDerivationFunction(kdfParams);
100  0 RC5CBCParameter rc5Params = RC5CBCParameter.getInstance(scheme.getParameters());
101   
102  0 return getPasswordBasedCipher(forEncryption, kdf, getRC5CipherParameters(password, rc5Params, kdf));
103    }
104   
 
105  0 toggle private SymmetricCipherParameters getRC5CipherParameters(byte[] password, RC5CBCParameter rc5Params,
106    KeyDerivationFunction df)
107    {
108  0 KeyParameter keyParam =
109    new RC5KeyParameters(df.derive(password).getKey(), rc5Params.getRounds().intValue());
110  0 if (rc5Params.getIV() != null) {
111  0 return new KeyWithIVParameters(keyParam, rc5Params.getIV());
112    } else {
113  0 return keyParam;
114    }
115    }
116    }