|
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 | import org.junit.jupiter.params.ParameterizedTest; |
26 | 26 | import org.junit.jupiter.params.provider.CsvSource; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
27 | 28 | import static org.junit.jupiter.api.Assertions.assertTrue; |
28 | 29 | import static org.junit.jupiter.api.Assertions.fail; |
29 | 30 |
|
@@ -74,6 +75,38 @@ public void testPQCKeyGenKEM_PlusToInterop() throws Exception { |
74 | 75 | assertTrue(same); |
75 | 76 | } |
76 | 77 |
|
| 78 | + @Test |
| 79 | + public void testPQCKeyGenKEMAutoKeyConvertion() throws Exception { |
| 80 | + String pqcAlgorithm = "ML-KEM-512"; |
| 81 | + |
| 82 | + if (getProviderName().equals("OpenJCEPlusFIPS") || |
| 83 | + getInteropProviderName().equals(Utils.PROVIDER_BC)) { |
| 84 | + //This is not in the FIPS provider yet and Boucy Castle does not support this test. |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + KEM kemInterop = KEM.getInstance(pqcAlgorithm, getProviderName()); |
| 89 | + |
| 90 | + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(pqcAlgorithm, getInteropProviderName()); |
| 91 | + KeyPair keyPair = generateKeyPair(keyPairGen); |
| 92 | + |
| 93 | + PublicKey publicKey = keyPair.getPublic(); |
| 94 | + PrivateKey privateKey = keyPair.getPrivate(); |
| 95 | + |
| 96 | + KEM.Encapsulator encr = kemInterop.newEncapsulator(publicKey); |
| 97 | + KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); |
| 98 | + if (enc == null){ |
| 99 | + System.out.println("enc = null"); |
| 100 | + fail("KEMPlusCreatesInteropGet failed no enc."); |
| 101 | + } |
| 102 | + SecretKey keyE = enc.key(); |
| 103 | + |
| 104 | + KEM.Decapsulator decr = kemInterop.newDecapsulator(privateKey); |
| 105 | + SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); |
| 106 | + |
| 107 | + assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); |
| 108 | + } |
| 109 | + |
77 | 110 | @Test |
78 | 111 | public void testPQCKeyGenKEM_Interop() throws Exception { |
79 | 112 | String pqcAlgorithm = "ML-KEM-512"; |
@@ -337,7 +370,7 @@ public void testSignInteropKeysPlusSignVerify(String algorithm) { |
337 | 370 | assertTrue(verifyingPlus.verify(signedBytesInterop), "Signature verification failed"); |
338 | 371 | } catch (Exception ex) { |
339 | 372 | ex.printStackTrace(); |
340 | | - assertTrue(false, "SignInteropAndVerifyPlus failed"); |
| 373 | + fail("SignInteropAndVerifyPlus failed"); |
341 | 374 | } |
342 | 375 | } |
343 | 376 |
|
@@ -372,7 +405,7 @@ public void testSignPlusKeysInteropSignVerify(String algorithm) { |
372 | 405 | assertTrue(verifyingPlus.verify(signedBytesInterop), "Signature verification failed"); |
373 | 406 | } catch (Exception ex) { |
374 | 407 | ex.printStackTrace(); |
375 | | - assertTrue(false, "SignInteropAndVerifyPlus failed"); |
| 408 | + fail("SignInteropAndVerifyPlus failed"); |
376 | 409 | } |
377 | 410 | } |
378 | 411 |
|
@@ -408,7 +441,7 @@ public void testSignPlusAndVerifyInterop(String algorithm) { |
408 | 441 | assertTrue(verifyingPlus.verify(signedBytesPlus), "Signature verification failed"); |
409 | 442 | } catch (Exception ex) { |
410 | 443 | ex.printStackTrace(); |
411 | | - assertTrue(false, "SignPlusAndVerifyInterop failed"); |
| 444 | + fail("SignPlusAndVerifyInterop failed"); |
412 | 445 | } |
413 | 446 | } |
414 | 447 |
|
@@ -440,17 +473,17 @@ public void testKEMPlusKeyInteropAll(String Algorithm) { |
440 | 473 | KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); |
441 | 474 | if (enc == null){ |
442 | 475 | System.out.println("enc = null"); |
443 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); |
| 476 | + fail("KEMPlusCreatesInteropGet failed no enc."); |
444 | 477 | } |
445 | 478 | SecretKey keyE = enc.key(); |
446 | 479 |
|
447 | 480 | KEM.Decapsulator decr = kemInterop.newDecapsulator(privateKeyInterop); |
448 | 481 | SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); |
449 | 482 |
|
450 | | - assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); |
| 483 | + assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); |
451 | 484 | } catch (Exception ex) { |
452 | 485 | ex.printStackTrace(); |
453 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed"); |
| 486 | + fail("KEMPlusCreatesInteropGet failed"); |
454 | 487 | } |
455 | 488 | } |
456 | 489 |
|
@@ -482,17 +515,17 @@ public void testKEMInteropKeyPlusAll(String Algorithm) { |
482 | 515 | KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); |
483 | 516 | if (enc == null){ |
484 | 517 | System.out.println("enc = null"); |
485 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); |
| 518 | + fail("KEMPlusCreatesInteropGet failed no enc."); |
486 | 519 | } |
487 | 520 | SecretKey keyE = enc.key(); |
488 | 521 |
|
489 | 522 | KEM.Decapsulator decr = kemPlus.newDecapsulator(privateKeyPlus); |
490 | 523 | SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); |
491 | 524 |
|
492 | | - assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); |
| 525 | + assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); |
493 | 526 | } catch (Exception ex) { |
494 | 527 | ex.printStackTrace(); |
495 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed"); |
| 528 | + fail("KEMPlusCreatesInteropGet failed"); |
496 | 529 | } |
497 | 530 | } |
498 | 531 |
|
@@ -522,17 +555,17 @@ public void testKEMPlusCreatesInteropGet(String Algorithm) { |
522 | 555 | KEM.Encapsulated enc = encr.encapsulate(0, 32, "AES"); |
523 | 556 | if (enc == null){ |
524 | 557 | System.out.println("enc = null"); |
525 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed no enc."); |
| 558 | + fail("KEMPlusCreatesInteropGet failed no enc."); |
526 | 559 | } |
527 | 560 | SecretKey keyE = enc.key(); |
528 | 561 |
|
529 | 562 | KEM.Decapsulator decr = kemPlus.newDecapsulator(privateKeyPlus); |
530 | 563 | SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); |
531 | 564 |
|
532 | | - assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); |
| 565 | + assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); |
533 | 566 | } catch (Exception ex) { |
534 | 567 | ex.printStackTrace(); |
535 | | - assertTrue(false, "KEMPlusCreatesInteropGet failed"); |
| 568 | + fail("KEMPlusCreatesInteropGet failed"); |
536 | 569 | } |
537 | 570 | } |
538 | 571 |
|
@@ -566,10 +599,10 @@ public void testKEMInteropCreatesPlusGet(String Algorithm) { |
566 | 599 |
|
567 | 600 | SecretKey keyD = decr.decapsulate(enc.encapsulation(), 0, 32, "AES"); |
568 | 601 |
|
569 | | - assertTrue(Arrays.equals(keyE.getEncoded(), keyD.getEncoded()), "Secrets do NOT match"); |
| 602 | + assertArrayEquals(keyE.getEncoded(), keyD.getEncoded(), "Secrets do NOT match"); |
570 | 603 | } catch (Exception ex) { |
571 | 604 | ex.printStackTrace(); |
572 | | - assertTrue(false, "KEMInteropCreatesPlusGet failed"); |
| 605 | + fail("KEMInteropCreatesPlusGet failed"); |
573 | 606 | } |
574 | 607 | } |
575 | 608 |
|
|
0 commit comments