forked from canonical/openssl-fips-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTest.java
More file actions
262 lines (216 loc) · 10 KB
/
SignatureTest.java
File metadata and controls
262 lines (216 loc) · 10 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
262
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import com.canonical.openssl.signature.*;
import com.canonical.openssl.key.OpenSSLKey;
import com.canonical.openssl.key.OpenSSLPublicKey;
import com.canonical.openssl.key.OpenSSLPrivateKey;
import com.canonical.openssl.key.KeyConverter;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.util.Arrays;
import java.nio.ByteBuffer;
import java.security.spec.AlgorithmParameterSpec;
import java.security.AlgorithmParameters;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.Security;
import java.security.Signature;
import com.canonical.openssl.provider.OpenSSLFIPSProvider;
import org.junit.Test;
import org.junit.BeforeClass;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class SignatureTest {
static String message = "Apollo is one of the Olympian deities in classical "
+ "Greek and Roman religion and Greek and Roman mythology. Apollo "
+ "has been recognized as a god of archery, music and dance, truth "
+ "and prophecy, healing and diseases, the Sun and light, poetry, "
+ "and more. One of the most important and complex of the Greek gods, "
+ "he is the son of Zeus and Leto, and the twin brother of Artemis, "
+ "goddess of the hunt. He is considered to be the most beautiful "
+ "god and is represented as the ideal of the kouros (ephebe, or a "
+ "beardless, athletic youth). Apollo is known in Greek-influenced "
+ "Etruscan mythology as Apulu.";
@Test
public void testRSABasic() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
signer.update(bytes, 0, bytes.length);
byte[] sigBytes = signer.sign();
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
assertTrue("SignatureTest for RSA failed.", verifier.verify(sigBytes));
}
@Test
public void testRSAwithMultipleUpdates() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(4096);
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
signer.update(bytes, 0, bytes.length);
signer.update(bytes, 2, bytes.length-2);
signer.update(bytes, 3, bytes.length-3);
byte[] sigBytes = signer.sign();
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
verifier.update(bytes, 2, bytes.length-2);
verifier.update(bytes, 3, bytes.length-3);
assertTrue("SignatureTest with multiple updates for RSA failed.", verifier.verify(sigBytes));
}
@Test
public void testRSAsingleByteUpdates() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
for (var b : bytes) {
signer.update(b);
}
byte[] sigBytes = signer.sign();
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
assertTrue("RSA SignatureTest with byte updates failed.", verifier.verify(sigBytes));
}
@Test
public void testRSAmultipleByteBufferUpdates() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(8192);
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
signer.update(ByteBuffer.wrap(message.getBytes()));
byte[] sigBytes = signer.sign();
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
assertTrue("RSA SignatureTest with ByteBuffer updates failed.", verifier.verify(sigBytes));
}
@Test
public void testRSAsignNonzeroOffset() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(4096);
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
byte[] sigBytes = new byte[612];
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
signer.update(ByteBuffer.wrap(message.getBytes()));
signer.sign(sigBytes, 100, 512);
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
assertTrue("RSA SignatureTest with non-zero offset failed.", verifier.verify(sigBytes, 100, 512));
}
@Test
public void testRSAtamperedSignature() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
for (var b : bytes) {
signer.update(b);
}
byte[] sigBytes = signer.sign();
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
// tamper signature
sigBytes[0] += 1;
assertFalse("RSA SignatureTest with tampered signature failed.", verifier.verify(sigBytes));
}
@Test
public void testRSAtamperedContent() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
KeyPair kp = gen.generateKeyPair();
PublicKey publicKey = new RSAPublicKey(KeyConverter.publicKeyToEVPKey(kp.getPublic()));
PrivateKey privateKey = new RSAPrivateKey(KeyConverter.privateKeyToEVPKey(kp.getPrivate()));
Signature signer = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
signer.initSign(privateKey);
byte[] bytes = message.getBytes();
for (var b : bytes) {
signer.update(b);
}
byte[] sigBytes = signer.sign();
// tamper content
bytes[0] += 1;
Signature verifier = Signature.getInstance("RSAwithSHA256", "OpenSSLFIPSProvider");
verifier.initVerify(publicKey);
verifier.update(bytes, 0, bytes.length);
assertFalse("RSA SignatureTest with tampered content failed.", verifier.verify(sigBytes));
}
@BeforeClass
public static void addProvider() throws Exception {
Security.addProvider(new OpenSSLFIPSProvider());
}
}
class TestKey {
public byte[] getEncoded() {
return null;
}
public String getFormat() {
return null;
}
public String getAlgorithm() {
return "";
}
}
class RSAPublicKey extends TestKey implements OpenSSLPublicKey {
long nativeKey = 0L;
public RSAPublicKey(long nativeKey) {
this.nativeKey = nativeKey;
}
public long getNativeKeyHandle() {
return nativeKey;
}
}
class RSAPrivateKey extends TestKey implements OpenSSLPrivateKey {
long nativeKey = 0L;
public RSAPrivateKey(long nativeKey) {
this.nativeKey = nativeKey;
}
public long getNativeKeyHandle() {
return nativeKey;
}
}