Download this file

KeyDerivation.java    268 lines (228 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package cologne.eck.peafactory.crypto.kdf;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.peafactory.crypto.CipherStuff;
import cologne.eck.peafactory.crypto.HashStuff;
import cologne.eck.tools.Comparator;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* parent class of all used key derivation functions
*/
public abstract class KeyDerivation {
private static KeyDerivation kdf = null;
private static int tCost = 0;
private static int mCost = 0;
private static int arg3 = 0;
private static int arg4 = 0;
private static int arg5 = 0;
private static int arg6 = 0;
private final static int SALT_SIZE = 32;// at most 129 byte for fixed initialized salt
private static byte[] salt;
private static byte[] extraValues = "peafactory_extra".getBytes(); // 16 byte
private static String versionString = "";
/**
* Derive the key. All parameters are set in child classes
*
* @param pswMaterial material build from the password
*
* @return the derived key
*/
public abstract byte[] deriveKey( byte[] pswMaterial);
/**
* Get the name of the key derivation scheme
*
* @return the name of the key derivation scheme
*/
public abstract String getName();
/**
* Get the required memory for the key derivation function.
* Returns the required memory in MiB.
*
* @return memory requirement in MiB
*/
public abstract int getMemoryRequirement();
/**
* Get a key of required length (expand or extract) using HKDF;
* this is only used for Bcrypt and Scrypt
*
* @param keyMaterial the material from key derivation
* @return the key
*/
public final static byte[] adjustKeyMaterial(byte[] keyMaterial){
if (PeaProperties.getWorkingMode().equals("-t")){
Comparator.checkNullVector(keyMaterial);
}
//Help.printBytes("key", HashStuff.generateHKDFBytes(keyMaterial, null, CipherStuff.getKeySize()));
return HashStuff.generateHKDFBytes(keyMaterial, salt, CipherStuff.getKeySize());
}
/**
* Print informations about the used key derivation function
*
* @param print if true, informations are printed
*/
public final static void printInfos(boolean print) {
if (print == true) {
System.out.print("Key derivation: " + kdf.getName() );
if (! versionString.equals("")){
System.out.print(", " + versionString);
}
System.out.print(", time parameter: " + tCost
+ ", memory parameter: " + mCost
);
if (arg3 != 0) {
System.out.print(", parameter 3: " + arg3);
}
if (arg4 != 0) {
System.out.print(", parameter 4: " + arg4);
}
if (arg5 != 0) {
System.out.print(", parameter 5: " + arg5);
}
if (arg6 != 0) {
System.out.print(", parameter 6: " + arg6);
}
System.out.println("");
}
}
//=============================================
// Getter & Setter
/**
* @return the tCost
*/
public static int gettCost() {
return tCost;
}
/**
* @param tCost the tCost to set
*/
public static void settCost(int _tCost) {
KeyDerivation.tCost = _tCost;
}
/**
* @return the mCost
*/
public static int getmCost() {
return mCost;
}
/**
* @param mCost the mCost to set
*/
public static void setmCost(int _mCost) {
KeyDerivation.mCost = _mCost;
}
/**
* @return the arg3
*/
public static int getArg3() {
return arg3;
}
/**
* @param arg3 the arg3 to set
*/
public static void setArg3(int _arg3) {
KeyDerivation.arg3 = _arg3;
}
/**
* @return the arg4
*/
public static int getArg4() {
return arg4;
}
/**
* @param arg4 the arg4 to set
*/
public static void setArg4(int _arg4) {
KeyDerivation.arg4 = _arg4;
}
/**
* @return the arg5
*/
public static int getArg5() {
return arg5;
}
/**
* @param arg5 the arg5 to set
*/
public static void setArg5(int _arg5) {
KeyDerivation.arg5 = _arg5;
}
/**
* @return the arg6
*/
public static int getArg6() {
return arg6;
}
/**
* @param arg6 the arg6 to set
*/
public static void setArg6(int _arg6) {
KeyDerivation.arg6 = _arg6;
}
/**
* @return the salt
*/
public static byte[] getSalt() {
return salt;
}
/**
* @param salt the salt to set
*/
public static void setSalt(byte[] _salt) {
salt = new byte[SALT_SIZE];
System.arraycopy(_salt, 0, KeyDerivation.salt, 0, SALT_SIZE);
//Help.printBytes("KeyDerivation set Salt ", salt);
}
/**
* @return the extraValues
*/
public static byte[] getExtraValues() {
return extraValues;
}
/**
* @param extraValues the extraValues to set
*/
public static void setExtraValues(byte[] _extraValues) {
KeyDerivation.extraValues = _extraValues;
}
/**
* @return the kdf
*/
public static KeyDerivation getKdf() {
return kdf;
}
/**
* @param kdf the kdf to set
*/
public static void setKdf(KeyDerivation kdf) {
KeyDerivation.kdf = kdf;
}
/**
* @return the versionString
*/
public static String getVersionString() {
return versionString;
}
/**
* @param versionString the versionString to set
*/
public static void setVersionString(String versionString) {
KeyDerivation.versionString = versionString;
}
public static int getSaltSize(){
return SALT_SIZE;
}
}