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

File ScryptKDFParams.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
32
10
1
176
86
16
0.5
3.2
10
1.6

Classes

Class Line # Actions
ScryptKDFParams 39 32 0% 16 11
0.796296379.6%
 

Contributing tests

This file is covered by 3 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.math.BigInteger;
23   
24    import org.bouncycastle.asn1.ASN1EncodableVector;
25    import org.bouncycastle.asn1.ASN1Integer;
26    import org.bouncycastle.asn1.ASN1Object;
27    import org.bouncycastle.asn1.ASN1OctetString;
28    import org.bouncycastle.asn1.ASN1Primitive;
29    import org.bouncycastle.asn1.ASN1Sequence;
30    import org.bouncycastle.asn1.DEROctetString;
31    import org.bouncycastle.asn1.DERSequence;
32   
33    /**
34    * Scrypt parameters as defined in IETF expired draft draft-josefsson-scrypt-kdf-01.
35    *
36    * @version $Id: 20e9df1956a73592fd4e2a6ee008aaf39d08b052 $
37    * @since 5.4M1
38    */
 
39    public class ScryptKDFParams extends ASN1Object
40    {
41    private ASN1OctetString salt;
42   
43    private ASN1Integer costParameter;
44   
45    private ASN1Integer blockSize;
46   
47    private ASN1Integer parallelizationParameter;
48   
49    private ASN1Integer keyLength;
50   
51    /**
52    * Create Scrypt parameters without a key length.
53    *
54    * @param salt is the salt value.
55    * @param costParameter is the CPU/Memory cost parameter N.
56    * @param blockSize is the block size parameter r.
57    * @param parallelizationParameter is the parallelization parameter.
58    */
 
59  0 toggle public ScryptKDFParams(byte[] salt, int costParameter, int blockSize, int parallelizationParameter)
60    {
61  0 this(salt, costParameter, blockSize, parallelizationParameter, -1);
62    }
63   
64    /**
65    * Create Scrypt parameters with a key length.
66    *
67    * @param salt is the salt value.
68    * @param costParameter is the CPU/Memory cost parameter N.
69    * @param blockSize is the block size parameter r.
70    * @param parallelizationParameter is the parallelization parameter.
71    * @param keyLength is the length in octets of the derived key.
72    */
 
73  5 toggle public ScryptKDFParams(byte[] salt, int costParameter, int blockSize, int parallelizationParameter, int keyLength)
74    {
75  5 this.salt = new DEROctetString(salt);
76  5 this.costParameter = new ASN1Integer(costParameter);
77  5 this.blockSize = new ASN1Integer(blockSize);
78  5 this.parallelizationParameter = new ASN1Integer(parallelizationParameter);
79  5 this.keyLength = (keyLength >= 0) ? new ASN1Integer(keyLength) : null;
80    }
81   
82    /**
83    * Create a new instance from a ASN.1 sequence.
84    *
85    * @param seq the ASN.1 sequence to parse.
86    */
 
87  3 toggle private ScryptKDFParams(ASN1Sequence seq)
88    {
89  3 this.salt = (ASN1OctetString) seq.getObjectAt(0);
90  3 this.costParameter = (ASN1Integer) seq.getObjectAt(1);
91  3 this.blockSize = (ASN1Integer) seq.getObjectAt(2);
92  3 this.parallelizationParameter = (ASN1Integer) seq.getObjectAt(3);
93  3 if (seq.size() > 4) {
94  3 this.keyLength = (ASN1Integer) seq.getObjectAt(4);
95    }
96    }
97   
98    /**
99    * Get an instance from an ASN.1 object.
100    *
101    * @param obj an ASN.1 object.
102    * @return an instance.
103    */
 
104  3 toggle public static ScryptKDFParams getInstance(Object obj)
105    {
106  3 if (obj instanceof ScryptKDFParams) {
107  0 return (ScryptKDFParams) obj;
108    }
109  3 if (obj != null) {
110  3 return new ScryptKDFParams(ASN1Sequence.getInstance(obj));
111    }
112  0 return null;
113    }
114   
115    /**
116    * @return the salt value.
117    */
 
118  3 toggle public byte[] getSalt()
119    {
120  3 return this.salt.getOctets();
121    }
122   
123    /**
124    * @return the CPU/Memory cost parameter N.
125    */
 
126  3 toggle public BigInteger getCostParameter()
127    {
128  3 return this.costParameter.getValue();
129    }
130   
131    /**
132    * @return the block size parameter r.
133    */
 
134  3 toggle public BigInteger getBlockSize()
135    {
136  3 return this.blockSize.getValue();
137    }
138   
139    /**
140    * @return the parallelization parameter.
141    */
 
142  3 toggle public BigInteger getParallelizationParameter()
143    {
144  3 return this.parallelizationParameter.getValue();
145    }
146   
147    /**
148    * @return the length in bytes of the derived key.
149    */
 
150  6 toggle public BigInteger getKeyLength()
151    {
152  6 if (this.keyLength == null) {
153  0 return null;
154    }
155  6 return this.keyLength.getValue();
156    }
157   
158    /**
159    * @return an ASN.1 structure representing these parameters.
160    */
 
161  16 toggle @Override
162    public ASN1Primitive toASN1Primitive()
163    {
164  16 ASN1EncodableVector v = new ASN1EncodableVector();
165   
166  16 v.add(this.salt);
167  16 v.add(this.costParameter);
168  16 v.add(this.blockSize);
169  16 v.add(this.parallelizationParameter);
170  16 if (this.keyLength != null) {
171  16 v.add(this.keyLength);
172    }
173   
174  16 return new DERSequence(v);
175    }
176    }