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.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 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 10 |
Complexity Density: 0.5 |
|
45 |
|
public abstract class AbstractBcPBES2Rc5CipherFactory extends AbstractBcPBES2CipherFactory |
46 |
|
{ |
47 |
|
private static final ASN1ObjectIdentifier ALG_ID = PKCSObjectIdentifiers.encryptionAlgorithm.branch("9"); |
48 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
49 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
0 |
@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 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
95 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
105 |
0 |
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 |
|
} |