-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathKEMTest.java
More file actions
120 lines (97 loc) · 4.14 KB
/
KEMTest.java
File metadata and controls
120 lines (97 loc) · 4.14 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
package org.openquantumsafe;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;
public class KEMTest {
private static ArrayList<String> enabled_kems;
/**
* Before running the tests, get a list of enabled KEMs
*/
@BeforeAll
public static void init(){
System.out.println("Initialize list of enabled KEMs");
enabled_kems = KEMs.get_enabled_KEMs();
}
/**
* Test all enabled KEMs
*/
@ParameterizedTest(name = "Testing {arguments}")
@MethodSource("getEnabledKEMsAsStream")
public void testAllKEMs(String kem_name) {
StringBuilder sb = new StringBuilder();
sb.append(kem_name);
sb.append(String.format("%1$" + (40 - kem_name.length()) + "s", ""));
// Create client and server
KeyEncapsulation client = new KeyEncapsulation(kem_name);
KeyEncapsulation server = new KeyEncapsulation(kem_name);
// Generate client key pair
byte[] client_public_key = client.generate_keypair();
// Server: encapsulate secret with client's public key
Pair<byte[], byte[]> server_pair = server.encap_secret(client_public_key);
byte[] ciphertext = server_pair.getLeft();
byte[] shared_secret_server = server_pair.getRight();
// Client: decapsulate
byte[] shared_secret_client = client.decap_secret(ciphertext);
// Check if equal
assertArrayEquals(shared_secret_client, shared_secret_server, kem_name);
// If successful print KEM name, otherwise an exception will be thrown
sb.append("\033[0;32m").append("PASSED").append("\033[0m");
System.out.println(sb.toString());
}
/**
* Test KEMs with derandomized keypair generation.
*/
@ParameterizedTest(name = "Testing {arguments}")
@MethodSource("getDerandSupportedKEMsAsStream")
public void testKEMsWithDerand(String kem_name) {
StringBuilder sb = new StringBuilder();
sb.append(kem_name);
sb.append(" (derand)");
sb.append(String.format("%1$" + (40 - kem_name.length() - 9) + "s", ""));
// Create client and server
KeyEncapsulation client = new KeyEncapsulation(kem_name);
KeyEncapsulation server = new KeyEncapsulation(kem_name);
// Generate seed
byte[] seed = Rand.randombytes(client.get_keypair_seed_length());
// Generate client key pair
byte[] client_public_key = client.generate_keypair(seed);
// Server: encapsulate secret with client's public key
Pair<byte[], byte[]> server_pair = server.encap_secret(client_public_key);
byte[] ciphertext = server_pair.getLeft();
byte[] shared_secret_server = server_pair.getRight();
// Client: decapsulate
byte[] shared_secret_client = client.decap_secret(ciphertext);
// Check if equal
assertArrayEquals(shared_secret_client, shared_secret_server, kem_name);
// If successful print KEM name, otherwise an exception will be thrown
sb.append("\033[0;32m").append("PASSED").append("\033[0m");
System.out.println(sb.toString());
}
/**
* Test the MechanismNotSupported Exception
*/
@Test
public void testUnsupportedKEMExpectedException() {
Assertions.assertThrows(MechanismNotSupportedError.class, () -> new KeyEncapsulation("MechanismNotSupported"));
}
/**
* Method to convert the list of KEMs to a stream for input to testAllKEMs
*/
private static Stream<String> getEnabledKEMsAsStream() {
return enabled_kems.parallelStream();
}
/**
* Method to convert the list of derand-supported KEMs to a stream for input to testAllSigs
*/
private static Stream<String> getDerandSupportedKEMsAsStream() {
return Arrays.asList(
"ML-KEM-512", "ML-KEM-768", "ML-KEM-1024"
).parallelStream();
}
}