From 31ce4c0604b896fcb7b2d6e4594177d6c4d53f26 Mon Sep 17 00:00:00 2001 From: duke Date: Fri, 29 Aug 2025 02:47:07 +0000 Subject: [PATCH] Backport 993babb326f937dc1630a5a8fa5e469a64c51206 --- .../security/pkcs11/Cipher/ReinitCipher.java | 7 +- .../security/pkcs11/Cipher/Test4512704.java | 21 ++--- .../pkcs11/Cipher/TestCICOWithGCM.java | 27 +++--- .../pkcs11/Cipher/TestCICOWithGCMAndAAD.java | 10 +-- .../pkcs11/Cipher/TestChaChaPoly.java | 7 +- .../pkcs11/Cipher/TestChaChaPolyKAT.java | 16 ++-- .../pkcs11/Cipher/TestChaChaPolyNoReuse.java | 15 ++-- .../Cipher/TestChaChaPolyOutputSize.java | 9 +- .../pkcs11/Cipher/TestCipherMode.java | 37 ++++++--- .../pkcs11/Cipher/TestGCMKeyAndIvCheck.java | 25 ++++-- .../security/pkcs11/Cipher/TestKATForGCM.java | 9 +- .../security/pkcs11/Cipher/TestRSACipher.java | 6 +- .../pkcs11/Cipher/TestRSACipherWrap.java | 6 +- .../pkcs11/Cipher/TestRawRSACipher.java | 6 +- .../pkcs11/Cipher/TestSymmCiphers.java | 82 +++++++++---------- .../pkcs11/Cipher/TestSymmCiphersNoPad.java | 60 +++++++------- 16 files changed, 187 insertions(+), 156 deletions(-) diff --git a/test/jdk/sun/security/pkcs11/Cipher/ReinitCipher.java b/test/jdk/sun/security/pkcs11/Cipher/ReinitCipher.java index fe93dff5050..c54889fbdc4 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/ReinitCipher.java +++ b/test/jdk/sun/security/pkcs11/Cipher/ReinitCipher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,6 +32,8 @@ * @run main/othervm ReinitCipher */ +import jtreg.SkippedException; + import java.security.Provider; import java.util.Random; import javax.crypto.Cipher; @@ -46,8 +48,7 @@ public static void main(String[] args) throws Exception { @Override public void main(Provider p) throws Exception { if (p.getService("Cipher", "ARCFOUR") == null) { - System.out.println("Not supported by provider, skipping"); - return; + throw new SkippedException("Algorithm ARCFOUR is not supported by provider, skipping"); } Random random = new Random(); byte[] data1 = new byte[10 * 1024]; diff --git a/test/jdk/sun/security/pkcs11/Cipher/Test4512704.java b/test/jdk/sun/security/pkcs11/Cipher/Test4512704.java index ddca64ecb69..7aafa4fd70f 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/Test4512704.java +++ b/test/jdk/sun/security/pkcs11/Cipher/Test4512704.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,14 +29,16 @@ * @run main Test4512704 * @summary Verify that AES cipher can generate default IV in encrypt mode */ -import java.io.PrintStream; -import java.security.*; -import java.security.spec.*; -import java.util.Random; +import jtreg.SkippedException; -import javax.crypto.*; -import javax.crypto.spec.*; +import java.security.GeneralSecurityException; +import java.security.InvalidAlgorithmParameterException; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; import java.security.Provider; +import java.security.spec.AlgorithmParameterSpec; public class Test4512704 extends PKCS11Test { @@ -48,9 +50,8 @@ public void test(String mode, Provider p) throws Exception { transformation = "AES/" + mode + "/NoPadding"; c = Cipher.getInstance(transformation, p); } catch (GeneralSecurityException e) { - System.out.println("Skip testing " + p.getName() + - ", no support for " + mode); - return; + throw new SkippedException("Skip testing " + p.getName() + + ", no support for " + mode); } SecretKey key = new SecretKeySpec(new byte[16], "AES"); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java b/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java index 06c1e84392c..f6b6157cefa 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCM.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,13 +31,21 @@ * @key randomness */ -import java.security.*; -import javax.crypto.*; -import javax.crypto.spec.*; -import java.math.*; -import java.io.*; +import jtreg.SkippedException; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +import java.security.GeneralSecurityException; +import java.security.Provider; +import java.util.Arrays; +import java.util.Random; -import java.util.*; public class TestCICOWithGCM extends PKCS11Test { public static void main(String[] args) throws Exception { @@ -55,9 +63,8 @@ public void test(String mode, Provider p) throws Exception { String transformation = "AES/" + mode + "/NoPadding"; c = Cipher.getInstance(transformation, p); } catch (GeneralSecurityException e) { - System.out.println("Skip testing " + p.getName() + - ", no support for " + mode); - return; + throw new SkippedException("Skip testing " + p.getName() + + ", no support for " + mode); } SecretKey key = new SecretKeySpec(new byte[16], "AES"); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCMAndAAD.java b/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCMAndAAD.java index be2b1d18c8f..13ab8541351 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCMAndAAD.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestCICOWithGCMAndAAD.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,6 +30,8 @@ * @summary Test CipherInputStream/OutputStream with AES GCM mode with AAD. * @key randomness */ +import jtreg.SkippedException; + import java.io.*; import java.security.*; import java.util.*; @@ -44,7 +46,6 @@ public static void main(String[] args) throws Exception { @Override public void main(Provider p) throws Exception { test("GCM", p); -// test("CCM", p); } public void test(String mode, Provider p) throws Exception { @@ -53,9 +54,8 @@ public void test(String mode, Provider p) throws Exception { String transformation = "AES/" + mode + "/NoPadding"; c = Cipher.getInstance(transformation, p); } catch (GeneralSecurityException e) { - System.out.println("Skip testing " + p.getName() + - ", no support for " + mode); - return; + throw new SkippedException("Skip testing " + p.getName() + + ", no support for " + mode); } SecretKey key = new SecretKeySpec(new byte[16], "AES"); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPoly.java b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPoly.java index 26853ae3ee6..da351bc7493 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPoly.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPoly.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,7 +47,7 @@ import javax.crypto.spec.SecretKeySpec; import javax.crypto.NoSuchPaddingException; -import jdk.test.lib.Utils; +import jtreg.SkippedException; public class TestChaChaPoly extends PKCS11Test { @@ -70,8 +70,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance(ALGO, p); } catch (NoSuchAlgorithmException nsae) { - System.out.println("Skip; no support for " + ALGO); - return; + throw new SkippedException("Skip; no support for " + ALGO); } this.p = p; testTransformations(); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyKAT.java b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyKAT.java index 5649ed013ef..d2590b2c3cb 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyKAT.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyKAT.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,22 +26,25 @@ * @bug 8255410 * @library /test/lib .. * @modules jdk.crypto.cryptoki - * @build jdk.test.lib.Convert * @run main/othervm TestChaChaPolyKAT * @summary ChaCha20-Poly1305 Cipher Implementation (KAT) */ -import java.util.*; +import jtreg.SkippedException; + import java.security.GeneralSecurityException; import java.security.Provider; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; -import javax.crypto.spec.ChaCha20ParameterSpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import javax.crypto.AEADBadTagException; import java.nio.ByteBuffer; -import jdk.test.lib.Convert; +import java.util.Arrays; +import java.util.HexFormat; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; public class TestChaChaPolyKAT extends PKCS11Test { public static class TestData { @@ -126,8 +129,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance(ALGO, p); } catch (NoSuchAlgorithmException nsae) { - System.out.println("Skip; no support for " + ALGO); - return; + throw new SkippedException("Skip; no support for " + ALGO); } int testsPassed = 0; diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyNoReuse.java b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyNoReuse.java index 94272367caa..60d80b9e365 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyNoReuse.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyNoReuse.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,18 +30,22 @@ * (key/nonce reuse check) */ -import java.util.*; +import jtreg.SkippedException; + import javax.crypto.Cipher; import java.security.spec.AlgorithmParameterSpec; import java.security.Provider; import java.security.NoSuchAlgorithmException; -import javax.crypto.spec.ChaCha20ParameterSpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import javax.crypto.AEADBadTagException; import javax.crypto.SecretKey; import java.security.InvalidKeyException; import java.security.InvalidAlgorithmParameterException; +import java.util.Arrays; +import java.util.HexFormat; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; public class TestChaChaPolyNoReuse extends PKCS11Test { @@ -238,8 +242,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance(CIPHER_ALGO, p); } catch (NoSuchAlgorithmException nsae) { - System.out.println("Skip; no support for " + CIPHER_ALGO); - return; + throw new SkippedException("Skip; no support for " + CIPHER_ALGO); } int testsPassed = 0; diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyOutputSize.java b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyOutputSize.java index 57a7b9a4606..f68340658fa 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyOutputSize.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestChaChaPolyOutputSize.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,14 +30,14 @@ * @run main TestChaChaPolyOutputSize */ +import jtreg.SkippedException; + import java.nio.ByteBuffer; import java.security.GeneralSecurityException; -import java.security.Key; import java.security.SecureRandom; import java.security.Provider; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; -import javax.crypto.spec.ChaCha20ParameterSpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; @@ -60,8 +60,7 @@ public void main(Provider p) throws GeneralSecurityException { try { Cipher.getInstance(ALGO, p); } catch (NoSuchAlgorithmException nsae) { - System.out.println("Skip; no support for " + ALGO); - return; + throw new SkippedException("Skip; no support for " + ALGO); } testGetOutSize(p); testMultiPartAEADDec(p); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java b/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java index 76f0c9dc412..cf3d948be17 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,6 +30,8 @@ * @run main/othervm TestCipherMode */ +import jtreg.SkippedException; + import java.security.Provider; import java.security.Key; import java.security.KeyPair; @@ -38,20 +40,22 @@ import java.security.PublicKey; import java.security.InvalidParameterException; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class TestCipherMode extends PKCS11Test { - private static String[] TRANSFORMATIONS = { - "AES/ECB/PKCS5Padding", "AES/GCM/NoPadding", - "RSA/ECB/PKCS1Padding" + private static final String[] TRANSFORMATIONS = { + "AES/ECB/PKCS5Padding", "AES/GCM/NoPadding", + "RSA/ECB/PKCS1Padding" }; - private static byte[] BYTES16 = - Arrays.copyOf(TRANSFORMATIONS[0].getBytes(), 16); + private static final byte[] BYTES16 = + Arrays.copyOf("AES/ECB/PKCS5Padding".getBytes(), 16); private static SecretKey AES_KEY = new SecretKeySpec(BYTES16, "AES"); private static PublicKey RSA_PUBKEY = null; private static PrivateKey RSA_PRIVKEY = null; @@ -97,18 +101,29 @@ public void main(Provider p) throws Exception { // test all cipher impls, e.g. P11Cipher, P11AEADCipher, and // P11RSACipher - for (String t : TRANSFORMATIONS) { - checkModes(t, p); + List skipped = new ArrayList<>(); + for (final String t : TRANSFORMATIONS) { + try { + checkModes(t, p); + } catch (SkippedException skippedException) { + // printing to System.out, so it's easier to see which test it relates to + skippedException.printStackTrace(System.out); + skipped.add(t); + } + } + + if (!skipped.isEmpty()) { + throw new SkippedException("Some tests skipped: " + skipped); + } else { + System.out.println("All tests passed"); } - System.out.println("All tests passed"); } private static void checkModes(String t, Provider p) throws Exception { try { Cipher.getInstance(t, p); } catch (Exception e) { - System.out.println("Skip " + t + " due to " + e.getMessage()); - return; + throw new SkippedException("Skip " + t + " due to " + e.getMessage()); } for (CipherMode m : CipherMode.values()) { diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java b/test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java index adabcc571aa..4e78d8d39d7 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,13 +31,21 @@ */ -import java.security.*; +import jtreg.SkippedException; + +import java.security.AlgorithmParameters; +import java.security.GeneralSecurityException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.Provider; import java.security.spec.AlgorithmParameterSpec; -import javax.crypto.*; -import javax.crypto.spec.*; -import java.math.*; +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; -import java.util.*; +import java.util.Arrays; public class TestGCMKeyAndIvCheck extends PKCS11Test { @@ -77,9 +85,8 @@ public void test(String mode, Provider p) throws Exception { String transformation = "AES/" + mode + "/NoPadding"; c = Cipher.getInstance(transformation, p); } catch (GeneralSecurityException e) { - System.out.println("Skip testing " + p.getName() + - ", no support for " + mode); - return; + throw new SkippedException("Skip testing " + p.getName() + + ", no support for " + mode); } System.out.println("Testing against " + p.getName()); SecretKey key = new SecretKeySpec(new byte[16], "AES"); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestKATForGCM.java b/test/jdk/sun/security/pkcs11/Cipher/TestKATForGCM.java index 95e6e5b1a0a..9844e8ecfd2 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestKATForGCM.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestKATForGCM.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,6 +30,8 @@ * @summary Known Answer Test for AES cipher with GCM mode support in * PKCS11 provider. */ +import jtreg.SkippedException; + import java.security.GeneralSecurityException; import java.security.Provider; import java.util.Arrays; @@ -311,9 +313,8 @@ public void main(Provider p) throws Exception { try { c = Cipher.getInstance(transformation, p); } catch (GeneralSecurityException e) { - System.out.println("Skip testing " + p.getName() + - ", no support for " + transformation); - return; + throw new SkippedException("Skip testing " + p.getName() + + ", no support for " + transformation); } try { if (execute(testValues, c)) { diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestRSACipher.java b/test/jdk/sun/security/pkcs11/Cipher/TestRSACipher.java index 73039ba2f65..204b852e4d6 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestRSACipher.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestRSACipher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -44,6 +44,7 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import jdk.test.lib.security.SecurityUtils; +import jtreg.SkippedException; public class TestRSACipher extends PKCS11Test { @@ -55,8 +56,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance(RSA_ALGOS[0], p); } catch (GeneralSecurityException e) { - System.out.println("Not supported by provider, skipping"); - return; + throw new SkippedException("Algorithm " + RSA_ALGOS[0] + " is not supported by provider, skipping"); } String kpgAlgorithm = "RSA"; int keySize = SecurityUtils.getTestKeySize(kpgAlgorithm); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.java b/test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.java index 559c7680c06..01d30934049 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestRSACipherWrap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,6 +43,7 @@ import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import jdk.test.lib.security.SecurityUtils; +import jtreg.SkippedException; public class TestRSACipherWrap extends PKCS11Test { @@ -54,8 +55,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance(RSA_ALGOS[0], p); } catch (GeneralSecurityException e) { - System.out.println(RSA_ALGOS[0] + " unsupported, skipping"); - return; + throw new SkippedException(RSA_ALGOS[0] + " unsupported, skipping"); } String kpgAlgorithm = "RSA"; KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgorithm, p); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestRawRSACipher.java b/test/jdk/sun/security/pkcs11/Cipher/TestRawRSACipher.java index 2c553f7e452..8989e951b09 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestRawRSACipher.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestRawRSACipher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,6 +41,7 @@ import java.util.Random; import javax.crypto.Cipher; import jdk.test.lib.security.SecurityUtils; +import jtreg.SkippedException; public class TestRawRSACipher extends PKCS11Test { @@ -49,8 +50,7 @@ public void main(Provider p) throws Exception { try { Cipher.getInstance("RSA/ECB/NoPadding", p); } catch (GeneralSecurityException e) { - System.out.println("Not supported by provider, skipping"); - return; + throw new SkippedException("Algorithm RSA/ECB/NoPadding is not supported by provider, skipping"); } String kpgAlgorithm = "RSA"; diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java b/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java index b818adfe589..2e66370b807 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,11 +32,15 @@ * @run main/othervm TestSymmCiphers */ +import jtreg.SkippedException; + import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.security.AlgorithmParameters; import java.security.NoSuchAlgorithmException; import java.security.Provider; +import java.util.ArrayList; +import java.util.List; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; @@ -44,43 +48,33 @@ public class TestSymmCiphers extends PKCS11Test { - private static class CI { // class for holding Cipher Information - - String transformation; - String keyAlgo; - int dataSize; + private record CI (String transformation, String keyAlgo, int dataSize){} // record for holding Cipher Information - CI(String transformation, String keyAlgo, int dataSize) { - this.transformation = transformation; - this.keyAlgo = keyAlgo; - this.dataSize = dataSize; - } - } private static final CI[] TEST_LIST = { - new CI("ARCFOUR", "ARCFOUR", 400), - new CI("RC4", "RC4", 401), - new CI("DES/CBC/NoPadding", "DES", 400), - new CI("DESede/CBC/NoPadding", "DESede", 160), - new CI("AES/CBC/NoPadding", "AES", 4800), - new CI("Blowfish/CBC/NoPadding", "Blowfish", 24), - new CI("DES/cbc/PKCS5Padding", "DES", 6401), - new CI("DESede/CBC/PKCS5Padding", "DESede", 402), - new CI("AES/CBC/PKCS5Padding", "AES", 30), - new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19), - new CI("DES/ECB/NoPadding", "DES", 400), - new CI("DESede/ECB/NoPadding", "DESede", 160), - new CI("AES/ECB/NoPadding", "AES", 4800), - new CI("DES/ECB/PKCS5Padding", "DES", 32), - new CI("DES/ECB/PKCS5Padding", "DES", 6400), - new CI("DESede/ECB/PKCS5Padding", "DESede", 400), - new CI("AES/ECB/PKCS5Padding", "AES", 64), - - new CI("DES", "DES", 6400), - new CI("DESede", "DESede", 408), - new CI("AES", "AES", 128), - - new CI("AES/CTR/NoPadding", "AES", 3200), - new CI("AES/CTS/NoPadding", "AES", 3200), + new CI("ARCFOUR", "ARCFOUR", 400), + new CI("RC4", "RC4", 401), + new CI("DES/CBC/NoPadding", "DES", 400), + new CI("DESede/CBC/NoPadding", "DESede", 160), + new CI("AES/CBC/NoPadding", "AES", 4800), + new CI("Blowfish/CBC/NoPadding", "Blowfish", 24), + new CI("DES/cbc/PKCS5Padding", "DES", 6401), + new CI("DESede/CBC/PKCS5Padding", "DESede", 402), + new CI("AES/CBC/PKCS5Padding", "AES", 30), + new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19), + new CI("DES/ECB/NoPadding", "DES", 400), + new CI("DESede/ECB/NoPadding", "DESede", 160), + new CI("AES/ECB/NoPadding", "AES", 4800), + new CI("DES/ECB/PKCS5Padding", "DES", 32), + new CI("DES/ECB/PKCS5Padding", "DES", 6400), + new CI("DESede/ECB/PKCS5Padding", "DESede", 400), + new CI("AES/ECB/PKCS5Padding", "AES", 64), + + new CI("DES", "DES", 6400), + new CI("DESede", "DESede", 408), + new CI("AES", "AES", 128), + + new CI("AES/CTR/NoPadding", "AES", 3200), + new CI("AES/CTS/NoPadding", "AES", 3200), }; private static final StringBuffer debugBuf = new StringBuffer(); @@ -90,11 +84,10 @@ public void main(Provider p) throws Exception { // NSS reports CKR_DEVICE_ERROR when the data passed to // its EncryptUpdate/DecryptUpdate is not multiple of blocks int firstBlkSize = 16; - boolean status = true; + List skippedList = new ArrayList<>(); Random random = new Random(); try { - for (int i = 0; i < TEST_LIST.length; i++) { - CI currTest = TEST_LIST[i]; + for (CI currTest : TEST_LIST) { System.out.println("===" + currTest.transformation + "==="); try { KeyGenerator kg = @@ -123,7 +116,8 @@ public void main(Provider p) throws Exception { System.out.println("Decryption tests: DONE"); } catch (NoSuchAlgorithmException nsae) { System.out.println("Skipping unsupported algorithm: " + - nsae); + nsae); + skippedList.add(currTest); } } } catch (Exception ex) { @@ -131,11 +125,15 @@ public void main(Provider p) throws Exception { System.out.println(debugBuf); throw ex; } + + if (!skippedList.isEmpty()){ + throw new SkippedException("Some tests skipped: " + skippedList); + } } private static void test(Cipher cipher, int mode, SecretKey key, - AlgorithmParameters params, int firstBlkSize, - byte[] in, byte[] answer) throws Exception { + AlgorithmParameters params, int firstBlkSize, + byte[] in, byte[] answer) throws Exception { // test setup long startTime, endTime; cipher.init(mode, key, params); diff --git a/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java b/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java index 9290fe8d6eb..973da3b0c55 100644 --- a/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java +++ b/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,6 +32,8 @@ * @run main/othervm TestSymmCiphersNoPad */ +import jtreg.SkippedException; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; @@ -39,6 +41,8 @@ import java.security.AlgorithmParameters; import java.security.NoSuchAlgorithmException; import java.security.Provider; +import java.util.ArrayList; +import java.util.List; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; @@ -47,48 +51,37 @@ public class TestSymmCiphersNoPad extends PKCS11Test { - private static class CI { // class for holding Cipher Information - String transformation; - String keyAlgo; - int dataSize; + private record CI (String transformation, String keyAlgo, int dataSize){} // record for holding Cipher Information - CI(String transformation, String keyAlgo, int dataSize) { - this.transformation = transformation; - this.keyAlgo = keyAlgo; - this.dataSize = dataSize; - } - } + private static final StringBuffer debugBuf = new StringBuffer(); - private static final CI TEST_LIST[] = { - new CI("ARCFOUR", "ARCFOUR", 400), - new CI("RC4", "RC4", 401), - new CI("DES/CBC/NoPadding", "DES", 400), - new CI("DESede/CBC/NoPadding", "DESede", 160), - new CI("AES/CBC/NoPadding", "AES", 4800), - new CI("Blowfish/CBC/NoPadding", "Blowfish", 24), - new CI("AES/CTR/NoPadding", "AES", 1600), - new CI("AES/CTR/NoPadding", "AES", 65), - new CI("AES/CTS/NoPadding", "AES", 1600), - new CI("AES/CTS/NoPadding", "AES", 65), + private static final CI[] TEST_LIST = { + new CI("ARCFOUR", "ARCFOUR", 400), + new CI("RC4", "RC4", 401), + new CI("DES/CBC/NoPadding", "DES", 400), + new CI("DESede/CBC/NoPadding", "DESede", 160), + new CI("AES/CBC/NoPadding", "AES", 4800), + new CI("Blowfish/CBC/NoPadding", "Blowfish", 24), + new CI("AES/CTR/NoPadding", "AES", 1600), + new CI("AES/CTR/NoPadding", "AES", 65), + new CI("AES/CTS/NoPadding", "AES", 1600), + new CI("AES/CTS/NoPadding", "AES", 65), }; - private static final StringBuffer debugBuf = new StringBuffer(); - @Override public void main(Provider p) throws Exception { - boolean status = true; + List skippedList = new ArrayList<>(); Random random = new Random(); try { - for (int i = 0; i < TEST_LIST.length; i++) { - CI currTest = TEST_LIST[i]; + for (CI currTest : TEST_LIST) { System.out.println("===" + currTest.transformation + "==="); try { KeyGenerator kg = - KeyGenerator.getInstance(currTest.keyAlgo, p); + KeyGenerator.getInstance(currTest.keyAlgo, p); SecretKey key = kg.generateKey(); Cipher c1 = Cipher.getInstance(currTest.transformation, p); Cipher c2 = Cipher.getInstance(currTest.transformation, - System.getProperty("test.provider.name", "SunJCE")); + System.getProperty("test.provider.name", "SunJCE")); byte[] plainTxt = new byte[currTest.dataSize]; random.nextBytes(plainTxt); @@ -98,16 +91,17 @@ public void main(Provider p) throws Exception { AlgorithmParameters params = c2.getParameters(); byte[] answer = c2.doFinal(plainTxt); test(c1, Cipher.ENCRYPT_MODE, key, params, - plainTxt, answer); + plainTxt, answer); System.out.println("Encryption tests: DONE"); c2.init(Cipher.DECRYPT_MODE, key, params); byte[] answer2 = c2.doFinal(answer); test(c1, Cipher.DECRYPT_MODE, key, params, - answer, answer2); + answer, answer2); System.out.println("Decryption tests: DONE"); } catch (NoSuchAlgorithmException nsae) { System.out.println("Skipping unsupported algorithm: " + nsae); + skippedList.add(currTest); } } } catch (Exception ex) { @@ -115,6 +109,10 @@ public void main(Provider p) throws Exception { System.out.println(debugBuf); throw ex; } + + if (!skippedList.isEmpty()){ + throw new SkippedException("Some tests skipped: " + skippedList); + } } private static void test(Cipher cipher, int mode, SecretKey key,