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
|
package org.spongycastle.crypto.test.cavp;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.regex.Matcher;
import org.spongycastle.crypto.Mac;
import org.spongycastle.crypto.generators.KDFFeedbackBytesGenerator;
import org.spongycastle.crypto.params.KDFFeedbackParameters;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTestResult;
import org.spongycastle.util.test.TestFailedException;
public final class KDFFeedbackCounterTests
implements CAVPListener
{
private PrintWriter out;
public void receiveCAVPVectors(String name, Properties config,
Properties vectors)
{
// out.println(" === " + name + " === ");
// out.println(" --- config --- ");
// out.println(config);
// out.println(" --- vectors --- ");
// out.println(vectors);
// always skip AFTER_FIXED
if (!config.getProperty("CTRLOCATION").matches("AFTER_ITER"))
{
return;
}
// create Mac based PRF from PRF property, create the KDF
final Mac prf = CAVPReader.createPRF(config);
final KDFFeedbackBytesGenerator gen = new KDFFeedbackBytesGenerator(prf);
Matcher matcherForR = CAVPReader.PATTERN_FOR_R.matcher(config.getProperty("RLEN"));
if (!matcherForR.matches())
{
throw new IllegalStateException("RLEN value should always match");
}
final int r = Integer.parseInt(matcherForR.group(1));
final int count = Integer.parseInt(vectors.getProperty("COUNT"));
final int l = Integer.parseInt(vectors.getProperty("L"));
final byte[] ki = Hex.decode(vectors.getProperty("KI"));
final byte[] iv = Hex.decode(vectors.getProperty("IV"));
final byte[] fixedInputData = Hex.decode(vectors.getProperty("FixedInputData"));
final KDFFeedbackParameters params = KDFFeedbackParameters.createWithCounter(ki, iv, fixedInputData, r);
gen.init(params);
final byte[] koGenerated = new byte[l / 8];
gen.generateBytes(koGenerated, 0, koGenerated.length);
final byte[] koVectors = Hex.decode(vectors.getProperty("KO"));
compareKO(name, config, count, koGenerated, koVectors);
}
private static void compareKO(
String name, Properties config, int test, byte[] calculatedOKM, byte[] testOKM)
{
if (!Arrays.areEqual(calculatedOKM, testOKM))
{
throw new TestFailedException(new SimpleTestResult(
false, name + " using " + config + " test " + test + " failed"));
}
}
public void receiveCommentLine(String commentLine)
{
// out.println("# " + commentLine);
}
public void receiveStart(String name)
{
// do nothing
}
public void receiveEnd()
{
out.println(" *** *** *** ");
}
public void setup()
{
try
{
out = new PrintWriter(new FileWriter("KDFFeedbackCounter.gen"));
}
catch (IOException e)
{
throw new IllegalStateException(e);
}
}
public void tearDown()
{
out.close();
}
}
|