blob: 95731f08706b9836af84795e67e1485aa33bb338 (
plain)
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
|
package org.spongycastle.crypto.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.spongycastle.crypto.generators.SCrypt;
import org.spongycastle.util.Strings;
import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTest;
/*
* scrypt test vectors from "Stronger Key Derivation Via Sequential Memory-hard Functions" Appendix B.
* (http://www.tarsnap.com/scrypt/scrypt.pdf)
*/
public class SCryptTest extends SimpleTest
{
public String getName()
{
return "SCrypt";
}
public void performTest() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("SCryptTestVectors.txt")));
int count = 0;
String line = br.readLine();
while (line != null)
{
++count;
String header = line;
StringBuffer data = new StringBuffer();
while (!isEndData(line = br.readLine()))
{
for (int i = 0; i != line.length(); i++)
{
if (line.charAt(i) != ' ')
{
data.append(line.charAt(i));
}
}
}
int start = header.indexOf('(') + 1;
int limit = header.lastIndexOf(')');
String argStr = header.substring(start, limit);
String[] args = Strings.split(argStr, ',');
byte[] P = extractQuotedString(args[0]);
byte[] S = extractQuotedString(args[1]);
int N = extractInteger(args[2]);
int r = extractInteger(args[3]);
int p = extractInteger(args[4]);
int dkLen = extractInteger(args[5]);
byte[] expected = Hex.decode(data.toString());
// This skips very expensive test case(s), remove check to re-enable
if (N <= 16384)
{
byte[] result = SCrypt.generate(P, S, N, r, p, dkLen);
if (!areEqual(expected, result))
{
fail("Result does not match expected value in test case " + count);
}
}
}
br.close();
}
private static boolean isEndData(String line)
{
return line == null || line.startsWith("scrypt");
}
private static byte[] extractQuotedString(String arg)
{
arg = arg.trim();
arg = arg.substring(1, arg.length() - 1);
return Strings.toByteArray(arg);
}
private static int extractInteger(String arg)
{
return Integer.parseInt(arg.trim());
}
public static void main(String[] args)
{
runTest(new SCryptTest());
}
}
|