Skip to content

Commit 792c547

Browse files
committed
KEMTests: comment and cosmetic changes
1 parent e7518c8 commit 792c547

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

cryptotest/tests/KEMTests.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,22 @@ public static Object encapsulator_encapsulate(Object e) throws Exception {
8989
return m.invoke(e);
9090
}
9191

92-
public static Object encapsulated_encapsulation(Object e) throws Exception {
92+
public static byte[] encapsulated_encapsulation(Object e) throws Exception {
9393
Class c = Class.forName("javax.crypto.KEM$Encapsulated");
9494
Method m = c.getDeclaredMethod("encapsulation");
95-
return m.invoke(e);
95+
return (byte[]) m.invoke(e);
9696
}
9797

98-
public static Object encapsulated_key(Object e) throws Exception {
98+
public static SecretKey encapsulated_key(Object e) throws Exception {
9999
Class c = Class.forName("javax.crypto.KEM$Encapsulated");
100100
Method m = c.getDeclaredMethod("key");
101-
return m.invoke(e);
101+
return (SecretKey) m.invoke(e);
102102
}
103103

104-
public static Object decapsulator_decapsulate(Object d, Object o) throws Exception {
104+
public static SecretKey decapsulator_decapsulate(Object d, Object o) throws Exception {
105105
Class c = Class.forName("javax.crypto.KEM$Decapsulator");
106106
Method m = c.getDeclaredMethod("decapsulate", byte[].class);
107-
return m.invoke(d, o);
107+
return (SecretKey) m.invoke(d, o);
108108
}
109109

110110
@Override
@@ -122,15 +122,35 @@ protected void checkAlgorithm(Provider.Service service, String alias) throws Alg
122122
KeyPair kp = kpg.generateKeyPair();
123123
Object sender = kem_newEncapsulator(kem, kp.getPublic());
124124
Object encapsulated = encapsulator_encapsulate(sender);
125-
Object encapsulation = encapsulated_encapsulation(encapsulated);
126-
SecretKey k1 = (SecretKey) encapsulated_key(encapsulated);
125+
byte[] encapsulation = encapsulated_encapsulation(encapsulated);
126+
SecretKey k1 = encapsulated_key(encapsulated);
127127

128128
Object receiver = kem_newDecapsulator(kem, kp.getPrivate());
129-
SecretKey k2 = (SecretKey) decapsulator_decapsulate(receiver, encapsulation);
129+
SecretKey k2 = decapsulator_decapsulate(receiver, encapsulation);
130+
131+
if (!Arrays.equals(k1.getEncoded(), k2.getEncoded())) {
132+
throw new Exception("Keys are not equal");
133+
}
134+
135+
/*
136+
Code above uses reflection, so that it is buildable on all jdks,
137+
It is equivalent to following code:
130138
139+
KEM kem = KEM.getInstance(alias, service.getProvider());
140+
KeyPairGenerator kpg = null;
141+
... per algorithm key generator selection here ...
142+
KeyPair kp = kpg.generateKeyPair();
143+
KEM.Encapsulator sender = kem.newEncapsulator(kp.getPublic());
144+
KEM.Encapsulated encapsulated = sender.encapsulate();
145+
byte[] encapsulation = encapsulated.encapsulation();
146+
SecretKey k1 = encapsulated.key();
147+
KEM.Decapsulator receiver = kem.newDecapsulator(kp.getPrivate());
148+
SecretKey k2 = receiver.decapsulate(encapsulation);
131149
if (!Arrays.equals(k1.getEncoded(), k2.getEncoded())) {
132150
throw new Exception("Keys are not equal");
133151
}
152+
153+
*/
134154
} catch (AlgorithmIgnoredException aie) {
135155
throw aie;
136156
} catch (NoSuchAlgorithmException ex) {

0 commit comments

Comments
 (0)