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.kdf; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
|
24 |
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
25 |
|
import org.bouncycastle.asn1.pkcs.KeyDerivationFunc; |
26 |
|
import org.bouncycastle.crypto.generators.SCrypt; |
27 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyParameter; |
28 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
29 |
|
import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters; |
30 |
|
import org.xwiki.crypto.password.params.ScryptParameters; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
@version |
36 |
|
@since |
37 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 6 |
Complexity Density: 0.5 |
|
38 |
|
public class BcScryptKDF extends AbstractBcKDF |
39 |
|
{ |
40 |
|
|
41 |
|
private static final ASN1ObjectIdentifier ALG_ID = new ASN1ObjectIdentifier("1.3.6.1.4.1.11591.4.11"); |
42 |
|
|
43 |
|
protected final ScryptParameters parameters; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@param |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
50 |
9 |
public BcScryptKDF(ScryptParameters parameters)... |
51 |
|
{ |
52 |
9 |
this.parameters = parameters; |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
@return |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
58 |
5 |
@Override... |
59 |
|
public KeyDerivationFunc getKeyDerivationFunction() |
60 |
|
{ |
61 |
5 |
return new KeyDerivationFunc(ALG_ID, |
62 |
|
new ScryptKDFParams(this.parameters.getSalt(), this.parameters.getCostParameter(), |
63 |
|
this.parameters.getBlockSize(), |
64 |
|
this.parameters.getParallelizationParameter(), this.parameters.getKeySize())); |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
13 |
@Override... |
68 |
|
public KeyDerivationFunctionParameters getParameters() |
69 |
|
{ |
70 |
13 |
return this.parameters; |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
5 |
@Override... |
74 |
|
public KeyParameter derive(byte[] password) |
75 |
|
{ |
76 |
5 |
return new KeyParameter( |
77 |
|
SCrypt.generate(password, this.parameters.getSalt(), this.parameters.getCostParameter(), |
78 |
|
this.parameters.getBlockSize(), this.parameters.getParallelizationParameter(), getKeySize())); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
81 |
4 |
@Override... |
82 |
|
public KeyWithIVParameters derive(byte[] password, int ivSize) |
83 |
|
{ |
84 |
4 |
int keySize = getKeySize(); |
85 |
4 |
byte[] keyIV = SCrypt.generate(password, this.parameters.getSalt(), this.parameters.getCostParameter(), |
86 |
|
this.parameters.getBlockSize(), this.parameters.getParallelizationParameter(), keySize + ivSize); |
87 |
|
|
88 |
4 |
byte[] key = new byte[keySize]; |
89 |
4 |
System.arraycopy(keyIV, 0, key, 0, keySize); |
90 |
|
|
91 |
4 |
byte[] iv = new byte[ivSize]; |
92 |
4 |
System.arraycopy(keyIV, keySize, iv, 0, ivSize); |
93 |
|
|
94 |
4 |
return new KeyWithIVParameters(key, iv); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
2 |
@Override... |
98 |
|
public byte[] getEncoded() throws IOException |
99 |
|
{ |
100 |
2 |
return getKeyDerivationFunction().getEncoded(); |
101 |
|
} |
102 |
|
} |