blob: e4343370e431bb0383617d619ce0967de3717898 (
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
97
98
99
100
101
102
103
104
105
106
107
|
package cz.crcs.ectester.standalone.test;
import cz.crcs.ectester.common.test.BaseTestable;
import cz.crcs.ectester.common.test.TestException;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
public class SignatureTestable extends BaseTestable {
private Signature sig;
private ECPrivateKey signKey;
private ECPublicKey verifyKey;
private KeyGeneratorTestable kgt;
private byte[] data;
private byte[] signature;
private boolean verified;
public SignatureTestable(Signature sig, ECPrivateKey signKey, ECPublicKey verifyKey, byte[] data) {
this.sig = sig;
this.signKey = signKey;
this.verifyKey = verifyKey;
this.data = data;
if (data == null) {
SecureRandom random = new SecureRandom();
this.data = new byte[32];
random.nextBytes(this.data);
}
}
public SignatureTestable(Signature sig, KeyGeneratorTestable kgt, byte[] data) {
this(sig, null, null, data);
this.kgt = kgt;
}
public Signature getSig() {
return sig;
}
public byte[] getData() {
return data;
}
public byte[] getSignature() {
return signature;
}
public boolean getVerified() {
return verified;
}
@Override
public void run() throws TestException {
if (kgt != null) {
signKey = (ECPrivateKey) kgt.getKeyPair().getPrivate();
verifyKey = (ECPublicKey) kgt.getKeyPair().getPublic();
}
try {
sig.initSign(signKey);
} catch (InvalidKeyException e) {
throw new TestException(e);
}
try {
sig.update(data);
} catch (SignatureException e) {
ok = false;
hasRun = true;
return;
}
try {
signature = sig.sign();
} catch (SignatureException e) {
ok = false;
hasRun = true;
return;
}
try {
sig.initVerify(verifyKey);
} catch (InvalidKeyException e) {
throw new TestException(e);
}
try {
sig.update(data);
} catch (SignatureException e) {
ok = false;
hasRun = true;
return;
}
try {
verified = sig.verify(signature);
} catch (SignatureException e) {
ok = false;
hasRun = true;
}
ok = true;
hasRun = true;
}
}
|