Skip to content

Commit 5a7ba5d

Browse files
committed
Add support for HPKE
1 parent f0d8925 commit 5a7ba5d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cryptotest/tests/CipherTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import javax.crypto.*;
5252
import javax.crypto.spec.IvParameterSpec;
5353
import java.lang.reflect.Constructor;
54+
import java.lang.reflect.Method;
5455
import java.lang.reflect.InvocationTargetException;
5556
import java.security.*;
5657
import java.security.spec.AlgorithmParameterSpec;
@@ -120,6 +121,15 @@ protected void checkAlgorithm(Provider.Service service, String alias) throws
120121
initSpec = (AlgorithmParameterSpec) chachaConstr.newInstance(b, 10);
121122
kg.init(256);
122123
key = KeyGenerator.getInstance("ChaCha20").generateKey();
124+
} else if (service.getAlgorithm().contains("HPKE")) {
125+
Class<?> hpkeCls = Class.forName("javax.crypto.spec.HPKEParameterSpec");
126+
Method hpkeM = hpkeCls.getDeclaredMethod("of", int.class, int.class, int.class);
127+
int hpkeP1 = (Integer) hpkeCls.getDeclaredField("KEM_DHKEM_X25519_HKDF_SHA256").get(null);
128+
int hpkeP2 = (Integer) hpkeCls.getDeclaredField("KDF_HKDF_SHA256").get(null);
129+
int hpkeP3 = (Integer) hpkeCls.getDeclaredField("AEAD_AES_256_GCM").get(null);
130+
// based on: https://github.com/openjdk/jdk/blob/45a2fd37f0ebda35789006b4e607422f7c369017/test/jdk/com/sun/crypto/provider/Cipher/HPKE/Compliance.java#L65
131+
initSpec = (AlgorithmParameterSpec) hpkeM.invoke(null, hpkeP1, hpkeP2, hpkeP3);
132+
key = KeyPairGenerator.getInstance("X25519").generateKeyPair().getPublic();
123133
}
124134
if (initSpec != null){
125135
c.init(Cipher.ENCRYPT_MODE, key, initSpec);
@@ -133,7 +143,7 @@ else if (service.getAlgorithm().toLowerCase().contains("wrap")
133143
c.init(Cipher.ENCRYPT_MODE, key);
134144
AlgorithmTest.printResult(c.doFinal(b));
135145
}
136-
} catch(NoSuchAlgorithmException | ClassNotFoundException | NoSuchMethodException | NoSuchPaddingException | InvalidKeySpecException | InvalidAlgorithmParameterException | InstantiationException | IllegalAccessException | InvocationTargetException | NullPointerException ex){
146+
} catch(NoSuchAlgorithmException | ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | NoSuchPaddingException | InvalidKeySpecException | InvalidAlgorithmParameterException | InstantiationException | IllegalAccessException | InvocationTargetException | NullPointerException ex){
137147
throw new AlgorithmInstantiationException(ex);
138148
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException |
139149
UnsupportedOperationException | InvalidParameterException | ProviderException ex) {

0 commit comments

Comments
 (0)