Skip to content

Commit f340ab2

Browse files
committed
8342181: Update tests to use stronger Key and Salt size
Reviewed-by: rhalade, ascarpino
1 parent cc2fb4d commit f340ab2

40 files changed

+240
-102
lines changed

test/jdk/com/sun/crypto/provider/Cipher/DES/KeyWrapping.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
/*
2525
* @test
2626
* @bug 0000000
27+
* @library /test/lib
2728
* @summary KeyWrapping
2829
* @author Jan Luehe
2930
*/
3031
import javax.crypto.*;
3132
import java.security.*;
33+
import jdk.test.lib.security.SecurityUtils;
3234

3335
public class KeyWrapping {
3436

@@ -71,8 +73,9 @@ public static void main(String[] args) throws Exception {
7173
if (!msg.equals(new String(clearText)))
7274
throw new Exception("The unwrapped session key is corrupted.");
7375

74-
KeyPairGenerator kpairGen = KeyPairGenerator.getInstance("DSA");
75-
kpairGen.initialize(1024);
76+
String kpgAlgorithm = "DSA";
77+
KeyPairGenerator kpairGen = KeyPairGenerator.getInstance(kpgAlgorithm);
78+
kpairGen.initialize(SecurityUtils.getTestKeySize(kpgAlgorithm));
7679

7780
KeyPair kpair = kpairGen.genKeyPair();
7881

test/jdk/com/sun/crypto/provider/Cipher/DES/Sealtest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,20 +24,23 @@
2424
/*
2525
* @test
2626
* @bug 0000000 7055362
27+
* @library /test/lib
2728
* @summary Sealtest
2829
* @author Jan Luehe
2930
*/
3031
import java.io.*;
3132
import java.security.*;
3233
import javax.crypto.*;
34+
import jdk.test.lib.security.SecurityUtils;
3335

3436
public class Sealtest {
3537

3638
public static void main(String[] args) throws Exception {
3739

3840
// create DSA keypair
39-
KeyPairGenerator kpgen = KeyPairGenerator.getInstance("DSA");
40-
kpgen.initialize(512);
41+
String kpgAlgorithm = "DSA";
42+
KeyPairGenerator kpgen = KeyPairGenerator.getInstance(kpgAlgorithm);
43+
kpgen.initialize(SecurityUtils.getTestKeySize(kpgAlgorithm));
4144
KeyPair kp = kpgen.generateKeyPair();
4245

4346
// create DES key

test/jdk/com/sun/crypto/provider/Cipher/KeyWrap/TestCipherKeyWrapperTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@
5050
import javax.crypto.SecretKeyFactory;
5151
import javax.crypto.spec.PBEKeySpec;
5252
import javax.crypto.spec.PBEParameterSpec;
53+
import jdk.test.lib.security.SecurityUtils;
5354

5455
/*
5556
* @test
5657
* @bug 8048599 8248268 8288050
58+
* @library /test/lib
5759
* @summary Tests for key wrap and unwrap operations
5860
*/
5961

@@ -270,7 +272,7 @@ private void wrapperPublicPriviteKeyTest(Provider p, String[] algorithms)
270272
System.out.println("Generate key pair (algorithm: " + algo
271273
+ ", provider: " + p.getName() + ")");
272274
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
273-
kpg.initialize(512);
275+
kpg.initialize(SecurityUtils.getTestKeySize(algo));
274276
KeyPair kp = kpg.genKeyPair();
275277
// key generated
276278
String algoWrap = "DES";

test/jdk/com/sun/crypto/provider/Cipher/PBE/PBEKeyCleanupTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/*
2525
* @test
26+
* @library /test/lib
2627
* @modules java.base/com.sun.crypto.provider:+open
2728
* @run main/othervm PBEKeyCleanupTest
2829
* @summary Verify that key storage is cleared
@@ -38,6 +39,7 @@
3839
import javax.crypto.SecretKey;
3940
import javax.crypto.SecretKeyFactory;
4041
import javax.crypto.spec.PBEKeySpec;
42+
import jdk.test.lib.security.SecurityUtils;
4143

4244
/**
4345
* Test that the array holding the key bytes is cleared when it is
@@ -67,7 +69,7 @@ private static void testPBESecret(String algorithm) throws Exception {
6769
}
6870

6971
private static void testPBKSecret(String algorithm) throws Exception {
70-
byte[] salt = new byte[8];
72+
byte[] salt = new byte[SecurityUtils.getTestSaltSize()];
7173
new Random().nextBytes(salt);
7274
char[] password = new char[] {'f', 'o', 'o'};
7375
PBEKeySpec pbeKeySpec = new PBEKeySpec(PASS_PHRASE.toCharArray(), salt,

test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPPadding.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 8020081 8022669
27+
* @library /test/lib
2728
* @summary encryption/decryption test for using OAEPPadding with
2829
* OAEPParameterSpec specified and not specified during a Cipher.init().
2930
* @author Anthony Scarpino
@@ -43,7 +44,7 @@
4344
import javax.crypto.spec.OAEPParameterSpec;
4445
import javax.crypto.IllegalBlockSizeException;
4546
import javax.crypto.spec.PSource;
46-
47+
import jdk.test.lib.security.SecurityUtils;
4748

4849
public class TestOAEPPadding {
4950
private static RSAPrivateKey privateKey;
@@ -57,8 +58,9 @@ public static void main(String args[]) throws Exception {
5758
System.out.println("Testing provider " + cp.getName() + "...");
5859
Provider kfp = Security.getProvider(
5960
System.getProperty("test.providername", "SunRsaSign"));
60-
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
61-
kpg.initialize(2048);
61+
String kpgAlgorithm = "RSA";
62+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgorithm, kfp);
63+
kpg.initialize(SecurityUtils.getTestKeySize(kpgAlgorithm));
6264
KeyPair kp = kpg.generateKeyPair();
6365
privateKey = (RSAPrivateKey)kp.getPrivate();
6466
publicKey = (RSAPublicKey)kp.getPublic();

test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPWithParams.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 4923484 8146293
27+
* @library /test/lib
2728
* @summary encryption/decryption test for using OAEPParameterSpec.
2829
* @author Valerie Peng
2930
*/
@@ -35,6 +36,7 @@
3536
import javax.crypto.*;
3637
import javax.crypto.spec.PSource;
3738
import javax.crypto.spec.OAEPParameterSpec;
39+
import jdk.test.lib.security.SecurityUtils;
3840

3941
public class TestOAEPWithParams {
4042

@@ -59,8 +61,9 @@ public static void main(String[] args) throws Exception {
5961
System.out.println("Testing provider " + cp.getName() + "...");
6062
Provider kfp = Security.getProvider(
6163
System.getProperty("test.provider.name", "SunRsaSign"));
62-
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
63-
kpg.initialize(768);
64+
String kpgAlgorithm = "RSA";
65+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgorithm, kfp);
66+
kpg.initialize(SecurityUtils.getTestKeySize(kpgAlgorithm));
6467
KeyPair kp = kpg.generateKeyPair();
6568
privateKey = kp.getPrivate();
6669
publicKey = kp.getPublic();

test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyAgreementPadding.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,21 +34,22 @@
3434
import java.security.KeyPairGenerator;
3535
import java.util.Arrays;
3636
import java.util.HexFormat;
37+
import jdk.test.lib.security.SecurityUtils;
3738

3839
public class DHKeyAgreementPadding {
3940

4041
public static void main(String[] args) throws Exception {
41-
42-
byte[] aliceSecret = new byte[80];
43-
byte[] bobSecret = new byte[80];
44-
45-
KeyAgreement alice = KeyAgreement.getInstance("DiffieHellman");
46-
KeyAgreement bob = KeyAgreement.getInstance("DiffieHellman");
42+
String kpgAlgorithm = "DiffieHellman";
43+
KeyAgreement alice = KeyAgreement.getInstance(kpgAlgorithm);
44+
KeyAgreement bob = KeyAgreement.getInstance(kpgAlgorithm);
45+
int keySizeBits = SecurityUtils.getTestKeySize(kpgAlgorithm);
46+
byte[] aliceSecret = new byte[keySizeBits / 8];
47+
byte[] bobSecret = new byte[keySizeBits / 8];
4748

4849
// The probability of an error is 0.2% or 1/500. Try more times.
4950
for (int i = 0; i < 5000; i++) {
50-
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DiffieHellman");
51-
keyPairGen.initialize(512);
51+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(kpgAlgorithm);
52+
keyPairGen.initialize(keySizeBits);
5253
KeyPair aliceKeyPair = keyPairGen.generateKeyPair();
5354
KeyPair bobKeyPair = keyPairGen.generateKeyPair();
5455

test/jdk/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 6578538 8027624
27+
* @library /test/lib
2728
* @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and
2829
* LoginContext
2930
* @author Brad Wetmore
@@ -45,6 +46,7 @@
4546

4647
import java.util.*;
4748
import java.util.concurrent.*;
49+
import jdk.test.lib.security.SecurityUtils;
4850

4951
public class TestProviderLeak {
5052
private static final int MB = 1024 * 1024;
@@ -109,7 +111,7 @@ public static void main(String [] args) throws Exception {
109111
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1",
110112
System.getProperty("test.provider.name", "SunJCE"));
111113
final PBEKeySpec pbeKS = new PBEKeySpec(
112-
"passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);
114+
"passPhrase".toCharArray(), new byte [SecurityUtils.getTestSaltSize()], 1000, 512);
113115

114116
ExecutorService executor = Executors.newSingleThreadExecutor();
115117
Callable<SecretKey> task = new Callable<SecretKey>() {

test/jdk/com/sun/org/apache/xml/internal/security/SignatureKeyInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,6 +62,7 @@
6262
import java.io.File;
6363
import java.math.BigInteger;
6464
import java.security.*;
65+
import jdk.test.lib.security.SecurityUtils;
6566

6667
import static jdk.test.lib.Asserts.assertEquals;
6768

@@ -160,7 +161,7 @@ private static void addKeyInfoData(KeyInfo keyInfo, String algorithm) throws Exc
160161

161162
private static KeyPair getKeyPair(String algorithm) throws NoSuchAlgorithmException {
162163
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
163-
keyGen.initialize(2048);
164+
keyGen.initialize(SecurityUtils.getTestKeySize(algorithm));
164165

165166
return keyGen.genKeyPair();
166167
}

test/jdk/java/security/KeyAgreement/KeySpecTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 8184359
27+
* @library /test/lib
2728
* @summary Standard tests on KeySpec, KeyFactory, KeyPairs and Keys.
2829
* Arguments order <KeyExchangeAlgorithm> <Provider> <KeyGenAlgorithm> <Curve*>
2930
* @run main KeySpecTest DiffieHellman SunJCE DiffieHellman
@@ -55,6 +56,7 @@
5556
import javax.crypto.KeyAgreement;
5657
import javax.crypto.spec.DHPrivateKeySpec;
5758
import javax.crypto.spec.DHPublicKeySpec;
59+
import jdk.test.lib.security.SecurityUtils;
5860

5961
public class KeySpecTest {
6062

@@ -78,7 +80,7 @@ private static KeyPair genKeyPair(String provider, String kpgAlgo,
7880
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider);
7981
switch (kpgInit) {
8082
case "DiffieHellman":
81-
kpg.initialize(512);
83+
kpg.initialize(SecurityUtils.getTestKeySize(kpgInit));
8284
break;
8385
case "EC":
8486
kpg.initialize(256);

0 commit comments

Comments
 (0)