|
45 | 45 | import java.util.Random;
|
46 | 46 |
|
47 | 47 | import javax.crypto.Cipher;
|
| 48 | +import javax.crypto.KEM; |
48 | 49 | import javax.crypto.KeyGenerator;
|
49 | 50 | import javax.crypto.SecretKey;
|
50 | 51 |
|
@@ -400,13 +401,17 @@ void testWithInstalledIntrinsic(String className, String methodName, String test
|
400 | 401 | Assume.assumeTrue(className + " is not available", false);
|
401 | 402 | return;
|
402 | 403 | }
|
| 404 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, methodName)), testSnippetName, args); |
| 405 | + } |
| 406 | + |
| 407 | + void testWithInstalledIntrinsic(ResolvedJavaMethod intrinsicMethod, String testSnippetName, Object... args) { |
403 | 408 | InstalledCode code = null;
|
404 | 409 | try {
|
405 | 410 | ResolvedJavaMethod method = getResolvedJavaMethod(testSnippetName);
|
406 | 411 | Object receiver = method.isStatic() ? null : this;
|
407 | 412 | GraalCompilerTest.Result expect = executeExpected(method, receiver, args);
|
408 |
| - code = compileAndInstallSubstitution(c, methodName); |
409 |
| - assertTrue("Failed to install " + methodName, code != null); |
| 413 | + code = compileAndInstallSubstitution(intrinsicMethod); |
| 414 | + assertTrue("Failed to install " + intrinsicMethod.getName(), code != null); |
410 | 415 | testAgainstExpected(method, expect, receiver, args);
|
411 | 416 | } catch (AssumptionViolatedException e) {
|
412 | 417 | // Suppress so that subsequent calls to this method within the
|
@@ -528,4 +533,68 @@ public void testMLDSASigVer() {
|
528 | 533 | testWithInstalledIntrinsic("sun.security.provider.ML_DSA", "implDilithiumMontMulByConstant", "testSignVer", "ML-DSA-87");
|
529 | 534 | testWithInstalledIntrinsic("sun.security.provider.ML_DSA", "implDilithiumDecomposePoly", "testSignVer", "ML-DSA-87");
|
530 | 535 | }
|
| 536 | + |
| 537 | + public boolean testMLKEMEncapsulateDecapsulate(String algorithm) throws GeneralSecurityException { |
| 538 | + var kp = generateKeyPair(algorithm); |
| 539 | + var senderKem = KEM.getInstance(algorithm); |
| 540 | + |
| 541 | + var encapsulator = senderKem.newEncapsulator(kp.getPublic(), new SeededSecureRandom()); |
| 542 | + var enc = encapsulator.encapsulate(); |
| 543 | + SecretKey key = enc.key(); |
| 544 | + |
| 545 | + var receiverKem = KEM.getInstance(algorithm); |
| 546 | + byte[] ciphertext = enc.encapsulation(); |
| 547 | + var decapsulator = receiverKem.newDecapsulator(kp.getPrivate()); |
| 548 | + SecretKey decapsulatedKey = decapsulator.decapsulate(ciphertext); |
| 549 | + |
| 550 | + return key.equals(decapsulatedKey); |
| 551 | + } |
| 552 | + |
| 553 | + @Test |
| 554 | + public void testMLKEM() { |
| 555 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberNtt != 0L); |
| 556 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberInverseNtt != 0L); |
| 557 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberNttMult != 0L); |
| 558 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberAddPoly2 != 0L); |
| 559 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberAddPoly3 != 0L); |
| 560 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyber12To16 != 0L); |
| 561 | + Assume.assumeTrue("ML_KEM not supported", runtime().getVMConfig().stubKyberBarrettReduce != 0L); |
| 562 | + |
| 563 | + Class<?> c; |
| 564 | + try { |
| 565 | + c = Class.forName("sun.security.provider.ML_KEM"); |
| 566 | + } catch (ClassNotFoundException e) { |
| 567 | + Assume.assumeTrue("sun.security.provider.ML_KEM is not available", false); |
| 568 | + return; |
| 569 | + } |
| 570 | + |
| 571 | + // ML-KEM-512 |
| 572 | + testWithInstalledIntrinsic("sun.security.provider.ML_KEM", "implKyberNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 573 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberInverseNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 574 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberNttMult", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 575 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 576 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", |
| 577 | + "ML-KEM-512"); |
| 578 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyber12To16", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 579 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyber12To16", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 580 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberBarrettReduce", "testMLKEMEncapsulateDecapsulate", "ML-KEM-512"); |
| 581 | + // ML-KEM-768 |
| 582 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 583 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberInverseNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 584 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberNttMult", "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 585 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 586 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", |
| 587 | + "ML-KEM-768"); |
| 588 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyber12To16", "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 589 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberBarrettReduce", "testMLKEMEncapsulateDecapsulate", "ML-KEM-768"); |
| 590 | + // ML-KEM-1024 |
| 591 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 592 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberInverseNtt", "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 593 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberNttMult", "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 594 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 595 | + testWithInstalledIntrinsic(getMetaAccess().lookupJavaMethod(getMethod(c, "implKyberAddPoly", short[].class, short[].class, short[].class, short[].class)), "testMLKEMEncapsulateDecapsulate", |
| 596 | + "ML-KEM-1024"); |
| 597 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyber12To16", "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 598 | + testWithInstalledIntrinsic("sun.security.provider.ML-KEM", "implKyberBarrettReduce", "testMLKEMEncapsulateDecapsulate", "ML-KEM-1024"); |
| 599 | + } |
531 | 600 | }
|
0 commit comments