Download this file

CatenaDBG.java    262 lines (221 with data), 6.5 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
package cologne.eck.crypto;
/**
* This implementation refers to:
* Paper v3.3
*/
/*
* Password Hashing Scheme Catena: Instance Catena-Butterfly (v3.3)
* Copyright (C) 2016 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* See: https://www.gnu.org/licenses/lgpl-2.1.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
import java.util.Arrays;
import org.bouncycastle.crypto.digests.Blake2bDigest;
public class CatenaDBG extends Catena {
private final static String VERSION_ID = "Butterfly";
private final static String VERSION_ID_FULL = "Butterfly-Full";
private final static int DEFAULT_LAMBDA = 4; // �� (depth of F)
private final static int DEFAULT_GARLIC = 16; // defines time and memory requirements
private final static int DEFAULT_MIN_GARLIC = 16; // minimum garlic
private CatenaHelper helper;
/**
* Default constructor.
* Uses round-reduced hash function
* and does not clear the password
*/
public CatenaDBG() {
setOverwrite(false);
setVersionID(VERSION_ID);
setFast(true);
setDigest(new Blake2bDigest());
setFastHash(new Blake2b_1());
helper = new CatenaHelper(getDigest(), getFastHash(), isFast());
}
/**
* Constructor for round-reduced hash function
*
* @param fast if true, use round-reduced
* hash function for some computations
*/
public CatenaDBG(boolean _fast) {
setOverwrite(false);
setFast(_fast);
setDigest(new Blake2bDigest());
if (_fast == false) {
setVersionID(VERSION_ID_FULL);
} else {
setFastHash(new Blake2b_1());
setVersionID(VERSION_ID);
}
helper = new CatenaHelper(getDigest(), getFastHash(), isFast());
}
/**
* Constructor for round-reduced hash function
* and clearing the password
*
*
* @param fast if true, use round-reduced
* hash function for some computations
* @param overwrite if true, clear password as soon
* as possible
*/
public CatenaDBG(boolean _fast, boolean overwrite) {
setFast(_fast);
setDigest(new Blake2bDigest());
setOverwrite(overwrite);
if (_fast == false) {
setVersionID(VERSION_ID_FULL);
} else {
setVersionID(VERSION_ID);
setFastHash(new Blake2b_1());
}
helper = new CatenaHelper(getDigest(), getFastHash(), isFast());
}
/**
* an optional randomization layer ��,
* to harden the memory initialization;
* updates the state array,
* depending on the public input (salt)
*
* @param garlic cost parameter
* @param salt salt
* @param r memory consuming state vector
*/
protected void gamma(int garlic, byte[] salt, byte[] r) {
helper.saltMix(garlic, salt, r);
}
/**
* Memory-hard function: Double-Butterfly Graph
*
* @param r the memory consuming state vector
* @param garlic cost parameter
* @param lambda depth of graph
* @param h value, holds the result
*/
protected void F(byte[] r, int garlic, int lambda, byte[] h) {
byte[] tmp = new byte[hLen];
long i,j;
int k;
int co = 0; //carry over from last iteration
long c = 1L << garlic;
long m = 1L << (garlic-1); //0.5 * 2^g
int l = 2 * garlic;
for (k = 0; k < lambda; k++) {
for(i = 1; i < l; i++) {
XOR(r, (int) idx(i-1,c-1,co,c,m) * hLen,
r, (int) idx(i-1,0,co,c,m) * hLen,
tmp);
helper.H_First(
tmp, 0,
r, (int)idx(i-1,sigma(garlic,(int)i-1,0),co,c,m) * hLen,
r, (int)idx(i,0,co,c,m) * hLen);
if (fastHash != null){
fastHash.reset();
}
for(j = 1; j < c; j++){
XOR(r, (int) idx(i,j-1,co,c,m)*hLen,
r, (int) idx(i-1,j,co,c,m)*hLen,
tmp);
helper.hashFast((int)j,
tmp, 0,
r, (int) idx(i-1,sigma(garlic,(int) (i-1),j),co,c,m)*hLen,
r, ((int) idx(i,j,co,c,m) * hLen));
}
}
co = (int) ((co + (i-1)) % 3);
}
System.arraycopy(r, (int) idx(0,c-1,co,c,m) * hLen, h, 0, hLen);
Arrays.fill(tmp, (byte) 0);
Arrays.fill(r, (byte) 0);
}
private final long sigma(int g, int i, long j) {
if (i < g) {
return (j ^ (1L << (g-1-i))); //diagonal front
}
else {
return (j ^ (1L << (i-(g-1)))); //diagonal back
}
}
private final long idx(long i, long j, int co, long c, long m) {
i += co;
if (i % 3 == 0) {
return j;
} else if (i % 3 == 1) {
if(j < m) { //still fits in the array
return j + c;
} else{ //start overwriting elements at the beginning
return j - m;
}
} else { //i % 3 == 2
return j + m;
}
}
/**
* XOR two vectors and store the result in a vector
*
* @param input1 first input vector
* @param index1 index of first vector to start
* @param input2 second input vector
* @param index2 index of second vector to start
* @param output vector to store the result
*/
private void XOR(byte[] input1, int index1, byte[] input2, int index2, byte[] output) {
int i;
for(i = 0; i < hLen; i++){
output[i] = (byte) (input1[index1 + i] ^ input2[index2 + i]);
}
}
@Override
public int getDefaultGarlic() {
return DEFAULT_GARLIC;
}
@Override
public int getDefaultMinGarlic() {
return DEFAULT_MIN_GARLIC;
}
@Override
public int getDefaultLambda() {
return DEFAULT_LAMBDA;
}
@Override
public String getAlgorithmName() {
return versionID;
}
@Override
public byte[] hashPassword(int outlen, byte[] in, byte[] salt, int t_cost,
int m_cost, Object... varArgs) throws Exception {
byte[] hash = new byte[outlen];
hashPassword(
in, salt, null,
t_cost, m_cost, m_cost,// lambda, minGarlic, garlic
hash);
return hash;
}
@Override
public boolean isWipePassword() {
return overwrite;
}
@Override
public void setWipePassword(boolean _wipe) {
overwrite = true;
}
@Override
public void flap(byte[] x, int lambda, int garlic, byte[] salt, byte[] h) {
byte[] r = new byte[ (int) (( (1 << garlic) + (1 << (garlic-1)) ) * hLen) ];
helper.initmem(x, (1 << garlic), r);
gamma(garlic, salt, r);
F(r, garlic, lambda, h);
}
}