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

File BcScryptKDF.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
12
6
1
102
55
6
0.5
2
6
1

Classes

Class Line # Actions
BcScryptKDF 38 12 0% 6 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.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    * Scrypt key derivation function based on Bouncy Castle.
34    *
35    * @version $Id: bb7f4bc643ee90a63430236241dcde1739f517e8 $
36    * @since 5.4M1
37    */
 
38    public class BcScryptKDF extends AbstractBcKDF
39    {
40    /** This OID, part of the GNU space, is not really reserved but suggested byt the IETF expired draft. */
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    * Construct a new SCrypt key derivation function.
47    *
48    * @param parameters the parameter for initializing the generator.
49    */
 
50  9 toggle public BcScryptKDF(ScryptParameters parameters)
51    {
52  9 this.parameters = parameters;
53    }
54   
55    /**
56    * @return an ASN.1 representation of the key derivation function parameters.
57    */
 
58  5 toggle @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   
 
67  13 toggle @Override
68    public KeyDerivationFunctionParameters getParameters()
69    {
70  13 return this.parameters;
71    }
72   
 
73  5 toggle @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   
 
81  4 toggle @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   
 
97  2 toggle @Override
98    public byte[] getEncoded() throws IOException
99    {
100  2 return getKeyDerivationFunction().getEncoded();
101    }
102    }