-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMSSSignerTest.java
More file actions
152 lines (115 loc) · 15.1 KB
/
XMSSSignerTest.java
File metadata and controls
152 lines (115 loc) · 15.1 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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.text.ParseException;
import java.util.Arrays;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
//import org.bouncycastle.pqc.crypto.xmss.NullPRNG;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.prng.FixedSecureRandom;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.crypto.xmss.XMSS;
import org.bouncycastle.pqc.crypto.xmss.XMSSKeyGenerationParameters;
import org.bouncycastle.pqc.crypto.xmss.XMSSKeyPairGenerator;
import org.bouncycastle.pqc.crypto.xmss.XMSSParameters;
import org.bouncycastle.pqc.crypto.xmss.XMSSSigner;
import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
public class XMSSSignerTest {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException {
Security.addProvider(new BouncyCastleProvider());
xmssTest();
}
public static void xmssTest() throws NoSuchAlgorithmException, NoSuchProviderException{
try {
/* //XMSSParameters xmssParams = new XMSSParameters(10, new SHAKEDigest(128));
//XMSSKeyPairGenerator xmss = new XMSSKeyPairGenerator();
// FixedSecureRandom fs = new FixedSecureRandom(new byte[99]);
// xmss.init(new XMSSKeyGenerationParameters(xmssParams, new SecureRandom()));
// AsymmetricCipherKeyPair kp = xmss.generateKeyPair();
XMSSSigner signer = new XMSSSigner();
// AsymmetricKeyParameter privateKey = kp.getPrivate();
byte[] message = "hi".getBytes();
signer.init(true, privateKey);
byte[] signature = signer.generateSignature(message);
privateKey = signer.getUpdatedPrivateKey();
signer.init(false, kp.getPublic());
boolean valid=false;
valid= signer.verifySignature(message, signature);
System.out.println("message: "+new String(message));
System.out.println("signature: "+new String(signature));
System.out.println("signature size: "+signature.length);
System.out.println("valid: "+valid);
//
*/
//keys for (10, new SHAKEDigest(128))
// byte[] publicKey = new byte[]{-43, -64, -86, 2, 11, 100, 66, -14, 103, -38, 110, -126, -102, 7, 81, 98, 77, -103, -14, -47, -119, 91, 59, -36, 95, 99, -53, 44, 119, -62, 36, -124, 50, -67, -41, -24, -128, -70, 105, 44, -80, 16, 123, 69, -121, 72, -33, 33, 111, -59, 57, 101, 44, -78, 1, 60, -41, -15, 78, -112, -110, -19, -61, 13};
//byte[] privateKey = new byte[]{0, 0, 0, 0, -118, 88, 26, 9, 82, -85, -111, 36, 71, 38, -122, -40, -93, 6, -15, 127, 35, -51, -42, 69, 103, 85, 89, 50, 123, 109, 58, 49, 18, -104, 48, 64, -121, 90, 37, -19, -25, -34, 12, -75, 86, 51, -14, 27, 94, 122, 109, 58, 64, -128, 6, 24, -107, 16, -108, -33, 74, 93, -8, -89, -4, -34, -49, -51, 50, -67, -41, -24, -128, -70, 105, 44, -80, 16, 123, 69, -121, 72, -33, 33, 111, -59, 57, 101, 44, -78, 1, 60, -41, -15, 78, -112, -110, -19, -61, 13, -43, -64, -86, 2, 11, 100, 66, -14, 103, -38, 110, -126, -102, 7, 81, 98, 77, -103, -14, -47, -119, 91, 59, -36, 95, 99, -53, 44, 119, -62, 36, -124, -84, -19, 0, 5, 115, 114, 0, 36, 111, 114, 103, 46, 98, 111, 117, 110, 99, 121, 99, 97, 115, 116, 108, 101, 46, 112, 113, 99, 46, 99, 114, 121, 112, 116, 111, 46, 120, 109, 115, 115, 46, 66, 68, 83, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 10, 73, 0, 5, 105, 110, 100, 101, 120, 73, 0, 1, 107, 73, 0, 10, 116, 114, 101, 101, 72, 101, 105, 103, 104, 116, 90, 0, 4, 117, 115, 101, 100, 76, 0, 18, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 105, 111, 110, 80, 97, 116, 104, 116, 0, 16, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 76, 105, 115, 116, 59, 76, 0, 4, 107, 101, 101, 112, 116, 0, 15, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 77, 97, 112, 59, 76, 0, 6, 114, 101, 116, 97, 105, 110, 113, 0, 126, 0, 2, 76, 0, 4, 114, 111, 111, 116, 116, 0, 43, 76, 111, 114, 103, 47, 98, 111, 117, 110, 99, 121, 99, 97, 115, 116, 108, 101, 47, 112, 113, 99, 47, 99, 114, 121, 112, 116, 111, 47, 120, 109, 115, 115, 47, 88, 77, 83, 83, 78, 111, 100, 101, 59, 76, 0, 5, 115, 116, 97, 99, 107, 116, 0, 17, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 83, 116, 97, 99, 107, 59, 76, 0, 17, 116, 114, 101, 101, 72, 97, 115, 104, 73, 110, 115, 116, 97, 110, 99, 101, 115, 113, 0, 126, 0, 1, 120, 112, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 10, 0, 115, 114, 0, 19, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 120, -127, -46, 29, -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105, 122, 101, 120, 112, 0, 0, 0, 10, 119, 4, 0, 0, 0, 10, 115, 114, 0, 41, 111, 114, 103, 46, 98, 111, 117, 110, 99, 121, 99, 97, 115, 116, 108, 101, 46, 112, 113, 99, 46, 99, 114, 121, 112, 116, 111, 46, 120, 109, 115, 115, 46, 88, 77, 83, 83, 78, 111, 100, 101, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2, 73, 0, 6, 104, 101, 105, 103, 104, 116, 91, 0, 5, 118, 97, 108, 117, 101, 116, 0, 2, 91, 66, 120, 112, 0, 0, 0, 0, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, 112, 0, 0, 0, 32, 0, -128, -23, -128, -83, -28, -87, -39, -3, -66, 50, 53, -20, -112, -29, -19, -28, -30, -3, -99, -64, 54, -23, 103, -69, 26, 55, -65, 73, -115, -122, -20, 115, 113, 0, 126, 0, 8, 0, 0, 0, 1, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 49, 109, -79, 119, -116, -94, 123, 38, 6, -96, 90, 9, -1, -52, -83, -58, -82, 73, -115, -126, 37, -12, -31, -16, 57, 120, 121, 100, 80, 111, 127, 60, 115, 113, 0, 126, 0, 8, 0, 0, 0, 2, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -15, -4, -85, 23, -16, 64, 82, 82, -100, 78, -71, 18, 20, -87, 3, -30, 70, -95, 81, -82, 61, -27, -87, 71, 125, 123, -53, 95, -5, -87, 94, 120, 115, 113, 0, 126, 0, 8, 0, 0, 0, 3, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -59, -29, 94, 96, 62, 21, 14, 31, 5, 40, -14, -12, -63, 126, 22, 33, 20, 43, 91, 86, -37, -87, 122, -95, -83, -81, -73, -25, -1, 74, -81, -63, 115, 113, 0, 126, 0, 8, 0, 0, 0, 4, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 36, 52, -74, -125, 57, 116, -115, -128, 34, 64, -18, -6, -121, -7, -63, 13, 43, 54, 67, -29, 22, -123, 22, 118, -93, 36, -77, -21, 97, 68, -105, -82, 115, 113, 0, 126, 0, 8, 0, 0, 0, 5, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 116, -86, -110, -82, 37, 10, 78, -104, 77, 31, -98, 70, 34, 49, 27, -89, -6, 9, -60, -48, 41, 40, 119, -111, 57, -85, -120, 88, 64, 115, 15, 28, 115, 113, 0, 126, 0, 8, 0, 0, 0, 6, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 96, -51, -4, -50, 17, 75, 15, -53, -59, 104, 94, -53, -54, -25, -4, 28, 94, 120, -82, -72, 3, 4, -37, -87, 37, 71, -3, -69, -80, 12, 10, -3, 115, 113, 0, 126, 0, 8, 0, 0, 0, 7, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 122, 108, 112, -7, -73, -127, -61, -61, -42, -107, -1, -44, -37, 64, -46, -54, 99, 27, 30, -69, 12, 74, -113, -24, 59, 33, -97, 30, 127, 52, 53, 68, 115, 113, 0, 126, 0, 8, 0, 0, 0, 8, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 9, 80, -112, 29, 74, 46, -52, -87, 102, -49, -68, 103, -115, 18, -39, -24, 119, -97, -94, -67, 79, -2, 85, -16, 36, 51, -92, -40, 110, -52, -99, 92, 115, 113, 0, 126, 0, 8, 0, 0, 0, 9, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 66, 85, -34, -28, 11, -85, 13, -109, 36, 41, 87, 68, -113, -101, 48, -109, 19, 107, 79, 16, -48, 116, 110, -37, 127, 33, -113, 71, 80, 40, 29, 117, 120, 115, 114, 0, 17, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 84, 114, 101, 101, 77, 97, 112, 12, -63, -10, 62, 45, 37, 106, -26, 3, 0, 1, 76, 0, 10, 99, 111, 109, 112, 97, 114, 97, 116, 111, 114, 116, 0, 22, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 67, 111, 109, 112, 97, 114, 97, 116, 111, 114, 59, 120, 112, 112, 119, 4, 0, 0, 0, 0, 120, 115, 113, 0, 126, 0, 31, 112, 119, 4, 0, 0, 0, 1, 115, 114, 0, 17, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 73, 110, 116, 101, 103, 101, 114, 18, -30, -96, -92, -9, -127, -121, 56, 2, 0, 1, 73, 0, 5, 118, 97, 108, 117, 101, 120, 114, 0, 16, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 78, 117, 109, 98, 101, 114, -122, -84, -107, 29, 11, -108, -32, -117, 2, 0, 0, 120, 112, 0, 0, 0, 8, 115, 114, 0, 20, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 76, 105, 110, 107, 101, 100, 76, 105, 115, 116, 12, 41, 83, 93, 74, 96, -120, 34, 3, 0, 0, 120, 112, 119, 4, 0, 0, 0, 1, 115, 113, 0, 126, 0, 8, 0, 0, 0, 8, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -91, -54, -22, -91, 73, 76, 14, 9, -76, -128, -14, -112, 78, -122, -65, 16, -13, 83, -7, -74, 34, -111, 66, 92, -66, 63, -37, -43, 20, -36, 124, 20, 120, 120, 115, 113, 0, 126, 0, 8, 0, 0, 0, 10, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -43, -64, -86, 2, 11, 100, 66, -14, 103, -38, 110, -126, -102, 7, 81, 98, 77, -103, -14, -47, -119, 91, 59, -36, 95, 99, -53, 44, 119, -62, 36, -124, 115, 114, 0, 15, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 83, 116, 97, 99, 107, 16, -2, 42, -62, -69, 9, -122, 29, 2, 0, 0, 120, 114, 0, 16, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 86, 101, 99, 116, 111, 114, -39, -105, 125, 91, -128, 59, -81, 1, 3, 0, 3, 73, 0, 17, 99, 97, 112, 97, 99, 105, 116, 121, 73, 110, 99, 114, 101, 109, 101, 110, 116, 73, 0, 12, 101, 108, 101, 109, 101, 110, 116, 67, 111, 117, 110, 116, 91, 0, 11, 101, 108, 101, 109, 101, 110, 116, 68, 97, 116, 97, 116, 0, 19, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 59, 120, 112, 0, 0, 0, 0, 0, 0, 0, 0, 117, 114, 0, 19, 91, 76, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 79, 98, 106, 101, 99, 116, 59, -112, -50, 88, -97, 16, 115, 41, 108, 2, 0, 0, 120, 112, 0, 0, 0, 10, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 120, 115, 113, 0, 126, 0, 6, 0, 0, 0, 8, 119, 4, 0, 0, 0, 8, 115, 114, 0, 44, 111, 114, 103, 46, 98, 111, 117, 110, 99, 121, 99, 97, 115, 116, 108, 101, 46, 112, 113, 99, 46, 99, 114, 121, 112, 116, 111, 46, 120, 109, 115, 115, 46, 66, 68, 83, 84, 114, 101, 101, 72, 97, 115, 104, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 6, 90, 0, 8, 102, 105, 110, 105, 115, 104, 101, 100, 73, 0, 6, 104, 101, 105, 103, 104, 116, 73, 0, 13, 105, 110, 105, 116, 105, 97, 108, 72, 101, 105, 103, 104, 116, 90, 0, 11, 105, 110, 105, 116, 105, 97, 108, 105, 122, 101, 100, 73, 0, 9, 110, 101, 120, 116, 73, 110, 100, 101, 120, 76, 0, 8, 116, 97, 105, 108, 78, 111, 100, 101, 113, 0, 126, 0, 3, 120, 112, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 0, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 35, -77, -94, 96, 83, 93, 92, -2, 116, -40, -86, 85, 125, 78, 26, -48, 46, -121, 76, 40, -98, -78, 52, -5, -6, -86, 110, 59, 115, -11, -115, -120, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 1, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 64, 116, -13, 120, -20, 46, -112, -78, -80, -12, -60, 48, -6, 11, -103, -73, 85, 101, -107, 31, -116, -104, 81, 41, 89, -28, 45, -56, 68, 34, 17, -94, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 2, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 68, 71, -79, -4, -3, 24, -69, 36, 73, -15, 3, 102, 17, -124, -68, 63, 60, -28, 9, -114, -104, 96, 34, -61, 66, 107, -117, -65, -22, 95, 96, 24, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 3, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -103, -17, -31, -111, 12, 37, 7, 33, 67, 74, -125, 15, -53, 97, 14, 89, 61, -16, -75, 21, -34, -71, 121, 60, -81, 70, 9, -30, 99, -100, 47, 36, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 4, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 54, -65, -81, -122, 30, 115, -37, 46, 13, -69, -30, 27, 45, 62, -34, 119, 36, 64, -35, 89, -119, -85, -15, -6, -121, 46, -10, 3, -66, -26, 63, -90, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 5, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, 9, 56, 113, 7, 24, -37, 75, -70, 53, -39, -95, 54, 14, 97, -55, 83, -17, -35, -98, -53, -23, -107, -38, -17, -40, -124, 41, 80, -47, -73, 82, 53, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 6, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -91, -122, -108, 33, -89, -18, -86, -116, -18, 89, -61, -93, -96, -74, -50, 105, -54, -92, 23, -35, 4, 74, -120, -59, -87, -102, -42, -111, -42, 64, -111, -54, 115, 113, 0, 126, 0, 51, 1, 0, 0, 0, 7, 0, 0, 0, 7, 0, 0, 0, 0, 0, 115, 113, 0, 126, 0, 8, 0, 0, 0, 7, 117, 113, 0, 126, 0, 11, 0, 0, 0, 32, -82, -122, -44, -93, 96, 10, 104, 78, -50, 126, -59, 14, -9, 24, 35, 76, 31, 126, -41, -117, -77, 119, 82, -126, 117, -15, 23, 26, 69, 58, 85, 104, 120};
// byte[] publicKey = getByte("E:\\public.txt");
// byte[] privateKey = getByte("E:\\private.txt");
// XMSSParameters xmssParams = new XMSSParameters(10, new SHA256Digest());
XMSSParameters xmssParams = new XMSSParameters(10, new SHA512Digest());
// XMSSParameters xmssParams = new XMSSParameters(10, new SHAKEDigest(128));
XMSS xmss = new XMSS(xmssParams, new SecureRandom());
long start3 = System.currentTimeMillis();
//xmss.importState(privateKey, publicKey);
xmss.generateKeys();
//get elapsed time for keygen
long elapsed3 = System.currentTimeMillis() - start3;
System.out.println("elapsed key gen: "+elapsed3);
byte[] privateKey = xmss.exportPrivateKey();
byte[] publicKey = xmss.exportPublicKey();
/* FileOutputStream fos1 = new FileOutputStream("E:\\public.txt");
fos1.write(publicKey);
fos1.close();
FileOutputStream fos2 = new FileOutputStream("E:\\private.txt");
fos2.write(privateKey);
fos2.close();*/
/* PrintWriter out = new PrintWriter("publicKeyx.txt");
out.println(Arrays.toString(publicKey));
out.close();
PrintWriter out2 = new PrintWriter("privateKeyx.txt");
out2.println(Arrays.toString(privateKey));
out2.close();*/
System.out.println("pub key size: "+publicKey.length);
System.out.println("pub key : "+Arrays.toString(publicKey));
System.out.println("priv key size: "+privateKey.length);
System.out.println("riv key : "+Arrays.toString(privateKey));
byte[] message = "hi".getBytes();
byte[] signature = xmss.sign(message);
boolean valid=false;
valid= xmss.verifySignature(message, signature, publicKey);
System.out.println("message: "+new String(message));
System.out.println("signature: "+new String(signature));
System.out.println("signature size: "+signature.length);
System.out.println("valid: "+valid);
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] getByte(String path) {
byte[] getBytes = {};
try {
File file = new File(path);
getBytes = new byte[(int) file.length()];
InputStream is = new FileInputStream(file);
is.read(getBytes);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return getBytes;
}
}