Skip to content

Commit b5d1ec3

Browse files
authored
Merge pull request ibmruntimes#371 from jasonkatonica/katonica/feature/brainpool
Support brainpoolP512r1 TLS 1.3 RFC 8734
2 parents 4de544b + 2f088d1 commit b5d1ec3

File tree

10 files changed

+184
-26
lines changed

10 files changed

+184
-26
lines changed

closed/src/jdk.crypto.ec/share/classes/sun/security/ec/NativeECDHKeyAgreement.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ private void init(Key key)
117117
/* attempt to translate the key if it is not an ECKey */
118118
ECKey ecKey = ECKeyFactory.toECKey(key);
119119
if (ecKey instanceof ECPrivateKey ecPrivateKey) {
120-
Optional<ECOperations> opsOpt =
121-
ECOperations.forParameters(ecPrivateKey.getParams());
122-
if (opsOpt.isEmpty()) {
123-
NamedCurve nc = CurveDB.lookup(ecPrivateKey.getParams());
120+
ECParameterSpec params = ecPrivateKey.getParams();
121+
ECOperations ops = ECOperations.forParameters(params).orElse(null);
122+
if ((ops == null) && !NativeECUtil.isBrainpoolP512r1(params)) {
123+
NamedCurve nc = CurveDB.lookup(params);
124124
throw new InvalidAlgorithmParameterException(
125125
"Curve not supported: " +
126126
((nc != null) ? nc.toString() : "unknown"));
@@ -136,9 +136,9 @@ private void init(Key key)
136136
this.initializeJavaImplementation(key);
137137
return;
138138
}
139-
this.privateKeyOps = opsOpt.get();
140139

141-
ECParameterSpec params = this.privateKey.getParams();
140+
this.privateKeyOps = ops;
141+
142142
this.curve = NativeECUtil.getCurveName(params);
143143
if ((this.curve != null) && NativeECUtil.isCurveSupported(this.curve, params)) {
144144
this.javaImplementation = null;
@@ -198,8 +198,10 @@ protected Key engineDoPhase(Key key, boolean lastPhase)
198198
("Key must be an instance of PublicKey");
199199
}
200200

201-
// Validate public key.
202-
validate(privateKeyOps, ecKey);
201+
// Validate public key when we are not making use of a brainpoolP512r1 based key.
202+
if (!NativeECUtil.isBrainpoolP512r1(this.privateKey.getParams())) {
203+
validate(privateKeyOps, ecKey);
204+
}
203205

204206
this.publicKey = ecKey;
205207
this.nativePublicKey = NativeECUtil.getPublicKeyNativePtr(ecKey);

closed/src/jdk.crypto.ec/share/classes/sun/security/ec/NativeECDSASignature.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,11 @@ protected byte[] engineSign() throws SignatureException {
560560
int sigLen = ((params.getOrder().bitLength() + 7) / 8) * 2;
561561
byte[] sig = new byte[sigLen];
562562

563-
ECDSAOperations.forParameters(params)
564-
.orElseThrow(() -> new SignatureException("Curve not supported: " + params));
563+
if (ECDSAOperations.forParameters(params).isEmpty()
564+
&& !NativeECUtil.isBrainpoolP512r1(params)
565+
) {
566+
throw new SignatureException("Curve not supported: " + params);
567+
}
565568

566569
if (nativeCrypto == null) {
567570
nativeCrypto = NativeCrypto.getNativeCrypto();
@@ -603,11 +606,13 @@ protected boolean engineVerify(byte[] signature) throws SignatureException {
603606
return false;
604607
}
605608

606-
ECDSAOperations ops = ECDSAOperations.forParameters(params)
607-
.orElseThrow(() -> new SignatureException("Curve not supported: " + params));
609+
ECDSAOperations ops = ECDSAOperations.forParameters(params).orElse(null);
610+
if ((ops == null) && !NativeECUtil.isBrainpoolP512r1(params)) {
611+
throw new SignatureException("Curve not supported: " + params);
612+
}
608613

609614
// Full public key validation, only necessary when h != 1.
610-
if (params.getCofactor() != 1) {
615+
if ((ops != null) && params.getCofactor() != 1) {
611616
if (!ops.getEcOperations().checkOrder(w)) {
612617
return false;
613618
}

closed/src/jdk.crypto.ec/share/classes/sun/security/ec/NativeECKeyPairGenerator.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/*
2727
* ===========================================================================
28-
* (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved
28+
* (c) Copyright IBM Corp. 2022, 2024 All Rights Reserved
2929
* ===========================================================================
3030
*/
3131

@@ -51,10 +51,10 @@
5151
import java.security.spec.ECFieldFp;
5252
import java.security.spec.ECFieldF2m;
5353
import java.security.spec.InvalidParameterSpecException;
54-
import java.util.Arrays;
5554

5655
import jdk.crypto.jniprovider.NativeCrypto;
5756

57+
import sun.security.action.GetPropertyAction;
5858
import sun.security.ec.point.*;
5959
import sun.security.jca.JCAUtil;
6060
import sun.security.provider.Sun;
@@ -89,6 +89,8 @@ public final class NativeECKeyPairGenerator extends KeyPairGeneratorSpi {
8989
/* the java implementation, initialized if needed */
9090
private ECKeyPairGenerator javaImplementation;
9191

92+
private static final boolean isAIX = "AIX".equals(GetPropertyAction.privilegedGetProperty("os.name"));
93+
9294
/**
9395
* Constructs a new NativeECKeyPairGenerator.
9496
*/
@@ -138,6 +140,23 @@ public void initialize(int keySize, SecureRandom random) {
138140
this.random = random;
139141

140142
this.curve = NativeECUtil.getCurveName(this.params);
143+
144+
/*
145+
* Only brainpoolP512r1 curve is supported on AIX. Other curves are disabled
146+
* for use with OpenSSL on AIX due to performance regressions observed. This
147+
* method does not specify brainpool so use the Java implementation for
148+
* ECKeyPairGenerator instead.
149+
*/
150+
if (isAIX) {
151+
/* Disabling OpenSSL usage on AIX due to performance regression observed. */
152+
if (nativeCryptTrace) {
153+
System.err.println("Not using OpenSSL integration on AIX.");
154+
}
155+
this.javaImplementation = new ECKeyPairGenerator();
156+
this.javaImplementation.initialize(this.keySize, this.random);
157+
return;
158+
}
159+
141160
if ((this.curve != null) && NativeECUtil.isCurveSupported(this.curve, this.params)) {
142161
this.javaImplementation = null;
143162
} else {
@@ -191,14 +210,27 @@ public void initialize(AlgorithmParameterSpec params, SecureRandom random)
191210
"ECParameterSpec or ECGenParameterSpec required for EC");
192211
}
193212

194-
// Not all known curves are supported by the native implementation
195-
ECKeyPairGenerator.ensureCurveIsSupported(ecSpec);
213+
// Not all known curves are supported by the native implementation.
214+
if (!NativeECUtil.isBrainpoolP512r1(ecSpec)) {
215+
ECKeyPairGenerator.ensureCurveIsSupported(ecSpec);
216+
}
217+
196218
this.params = ecSpec;
197219

198220
this.keySize = ecSpec.getCurve().getField().getFieldSize();
199221
this.random = random;
200222

201223
this.curve = NativeECUtil.getCurveName(this.params);
224+
225+
/* Disabling OpenSSL usage on AIX due to performance regression observed. */
226+
if (isAIX && !NativeECUtil.isBrainpoolP512r1(ecSpec)) {
227+
if (nativeCryptTrace) {
228+
System.err.println("Not using OpenSSL integration on AIX, only curve brainpoolP512r1 supported.");
229+
}
230+
this.initializeJavaImplementation();
231+
return;
232+
}
233+
202234
if ((this.curve != null) && (NativeECUtil.isCurveSupported(this.curve, this.params))) {
203235
this.javaImplementation = null;
204236
} else {

closed/src/jdk.crypto.ec/share/classes/sun/security/ec/NativeECUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.security.spec.ECParameterSpec;
4848
import java.security.spec.ECGenParameterSpec;
4949
import java.security.AlgorithmParameters;
50+
import sun.security.util.CurveDB;
5051
import sun.security.util.NamedCurve;
5152

5253
import jdk.crypto.jniprovider.NativeCrypto;
@@ -217,4 +218,17 @@ static long getPrivateKeyNativePtr(ECPrivateKey key) {
217218
return nativePointer;
218219
}
219220
}
221+
222+
static boolean isBrainpoolP512r1(ECParameterSpec name) {
223+
NamedCurve curve = CurveDB.lookup(name);
224+
if (curve != null) {
225+
String[] nameAndAliases = curve.getNameAndAliases();
226+
for (String nameOrAlias : nameAndAliases) {
227+
if ("brainpoolP512r1".equalsIgnoreCase(nameOrAlias)) {
228+
return true;
229+
}
230+
}
231+
}
232+
return false;
233+
}
220234
}

src/java.base/share/classes/sun/security/ssl/NamedGroup.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
29+
* ===========================================================================
30+
*/
2531
package sun.security.ssl;
2632

2733
import javax.crypto.spec.DHParameterSpec;
@@ -180,6 +186,12 @@ enum NamedGroup {
180186
ProtocolVersion.PROTOCOLS_TO_13,
181187
CurveDB.lookup("secp521r1")),
182188

189+
// Brainpool named curve definition as per RFC 8734.
190+
BRAINPOOLP512_R1TLS13(0x0021, "brainpoolP512r1tls13",
191+
NamedGroupSpec.NAMED_GROUP_ECDHE,
192+
ProtocolVersion.PROTOCOLS_OF_13,
193+
CurveDB.lookup("brainpoolP512r1")),
194+
183195
// x25519 and x448 (RFC 8422/8446)
184196
X25519(0x001D, "x25519",
185197
NamedGroupSpec.NAMED_GROUP_XDH,

src/java.base/share/classes/sun/security/ssl/SignatureScheme.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
package sun.security.ssl;
2733

2834
import java.security.*;
@@ -63,6 +69,12 @@ enum SignatureScheme {
6369
"EC",
6470
NamedGroup.SECP521_R1,
6571
ProtocolVersion.PROTOCOLS_TO_13),
72+
// Brainpool signature defintion for curve ecdsa_brainpoolP512r1tls13_sha512 as per RFC 8734.
73+
ECDSA_BRAINPOOLP512R1TLS13_SHA512(0x081C, "ecdsa_brainpoolP512r1tls13_sha512",
74+
"SHA512withECDSA",
75+
"EC",
76+
NamedGroup.BRAINPOOLP512_R1TLS13,
77+
ProtocolVersion.PROTOCOLS_OF_13),
6678

6779
// EdDSA algorithms
6880
ED25519 (0x0807, "ed25519", "Ed25519",
@@ -381,10 +393,29 @@ static List<SignatureScheme> getSupportedAlgorithms(
381393
// SSLConfiguration.getCustomizedSignatureScheme() the list will
382394
// only contain schemes that are in the enum.
383395
// Otherwise, use the enum constants (converted to a List).
384-
List<SignatureScheme> schemesToCheck =
385-
config.signatureSchemes.isEmpty() ?
386-
Arrays.asList(SignatureScheme.values()) :
387-
config.signatureSchemes;
396+
//
397+
// Additional logic is added here to remove the ecdsa_brainpoolP512r1tls13_sha512
398+
// signature scheme by default. We only want to make use of ecdsa_brainpoolP512r1tls13_sha512
399+
// when explicitly set via system properties jdk.tls.client.SignatureSchemes or
400+
// jdk.tls.server.SignatureSchemes.
401+
List<SignatureScheme> schemesToCheck;
402+
if (!config.signatureSchemes.isEmpty()) {
403+
schemesToCheck = config.signatureSchemes;
404+
} else {
405+
SignatureScheme[] schemes = SignatureScheme.values();
406+
schemesToCheck = new ArrayList<>(schemes.length);
407+
for (SignatureScheme scheme : schemes) {
408+
if (scheme != ECDSA_BRAINPOOLP512R1TLS13_SHA512) {
409+
schemesToCheck.add(scheme);
410+
} else {
411+
if (SSLLogger.isOn &&
412+
SSLLogger.isOn("ssl,handshake,verbose")) {
413+
SSLLogger.finest("Ignore " + ECDSA_BRAINPOOLP512R1TLS13_SHA512.name
414+
+ " from supported signature schemes");
415+
}
416+
}
417+
}
418+
}
388419

389420
for (SignatureScheme ss: schemesToCheck) {
390421
if (!ss.isAvailable) {

src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,6 @@ public Object newInstance(Object ctrParamObj)
257257
if (nativeCryptTrace) {
258258
System.err.println("EC KeyPair Generation - Not using OpenSSL integration due to older version of OpenSSL (<1.1.0).");
259259
}
260-
} else if (isAIX) {
261-
/* Disabling OpenSSL usage on AIX due to perfomance regression observed. */
262-
if (nativeCryptTrace) {
263-
System.err.println("EC KeyPair Generation - Not using OpenSSL integration on AIX.");
264-
}
265260
} else {
266261
if (nativeCryptTrace) {
267262
System.err.println("EC KeyPair Generation - Using OpenSSL integration.");

test/jdk/javax/net/ssl/TLSCommon/TLSTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
29+
* ===========================================================================
30+
*/
2531
import java.io.ByteArrayInputStream;
2632
import java.io.InputStream;
2733
import java.io.OutputStream;
@@ -53,6 +59,11 @@
5359
* @bug 8205111
5460
* @summary Test TLS with different types of supported keys.
5561
* @run main/othervm TLSTest TLSv1.3 rsa_pkcs1_sha1 TLS_AES_128_GCM_SHA256
62+
* @run main/othervm
63+
* -Djdk.tls.client.SignatureSchemes=ecdsa_brainpoolP512r1tls13_sha512
64+
* -Djdk.tls.namedGroups=brainpoolP512r1tls13
65+
* -Djdk.tls.server.SignatureSchemes=ecdsa_brainpoolP512r1tls13_sha512
66+
* TLSTest TLSv1.3 ecdsa_brainpoolP512r1_sha512 TLS_AES_128_GCM_SHA256
5667
* @run main/othervm TLSTest TLSv1.3 rsa_pkcs1_sha256 TLS_AES_128_GCM_SHA256
5768
* @run main/othervm TLSTest TLSv1.3 rsa_pkcs1_sha384 TLS_AES_128_GCM_SHA256
5869
* @run main/othervm TLSTest TLSv1.3 rsa_pkcs1_sha512 TLS_AES_128_GCM_SHA256
@@ -455,6 +466,31 @@ enum KeyType {
455466
+ "t89mrRZ1jMeD8fAbgijAG7WfgtGhRANCAAR6LMO6lBGdmpo87XTjtA2vsXvq1kd8\n"
456467
+ "ktaIGEdCrA8BKk0A30LW8SY5Be29ScYu8d+IjQ3X/fpblrVh/64pOgQz"
457468
),
469+
ecdsa_brainpoolP512r1_sha512(
470+
"EC",
471+
472+
"-----BEGIN CERTIFICATE-----\n"
473+
+ "MIICRzCCAaygAwIBAgIIRwv8F2wpI+gwCgYIKoZIzj0EAwQwVjELMAkGA1UEBhMC\n"
474+
+ "VVMxCzAJBgNVBAgTAk5ZMQ0wCwYDVQQHEwRUZXN0MQ0wCwYDVQQKEwRUZXN0MQ0w\n"
475+
+ "CwYDVQQLEwRUZXN0MQ0wCwYDVQQDEwRUZXN0MB4XDTI0MDUwOTE3MzEwOVoXDTI1\n"
476+
+ "MDUwOTE3MzEwOVowVjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAk5ZMQ0wCwYDVQQH\n"
477+
+ "EwRUZXN0MQ0wCwYDVQQKEwRUZXN0MQ0wCwYDVQQLEwRUZXN0MQ0wCwYDVQQDEwRU\n"
478+
+ "ZXN0MIGbMBQGByqGSM49AgEGCSskAwMCCAEBDQOBggAEKW44Kx0jbGqLa0YqK2zc\n"
479+
+ "6/95LIgJseQjKAE5bxyr92hnGwXQV4Xpu9ncZKFEPx1XJpfeb68+ds6CF4oRI8cf\n"
480+
+ "YR3KEXazpDOZ6EoM8qYawch61QZlJmfBw9+SzDI26Kr7yOphqi8WTO1X6LWRjCTT\n"
481+
+ "KpBiIfWcIBw25G1NNDM26/ujITAfMB0GA1UdDgQWBBSQ5LauX//LL5I3Re1m5Z92\n"
482+
+ "9iVd3jAKBggqhkjOPQQDBAOBiAAwgYQCQHIcs0OAiPOjknW4scGqxBkOTgdjOaEE\n"
483+
+ "ts0Q6O0kzOYYBYEjsyNTWAO6cIZjXovvdwbs0j+YXaPV6bh0aerKXMACQFVMMJJF\n"
484+
+ "tDZNP+FsegcRWA14Jx+aeNIRWeEa7cVZ9lRzf5/IsFS9mQnXpyI8oQStnNncqyLR\n"
485+
+ "RIW0f9OAnOvzApQ=\n"
486+
+ "-----END CERTIFICATE-----\n",
487+
//
488+
// Private key.
489+
//
490+
"MGICAQAwFAYHKoZIzj0CAQYJKyQDAwIIAQENBEcwRQIBAQRAgPx92Cu2UnmeC/NG\n"
491+
+ "KdwrYso1y3MHfY8UbcvuC/POxDqvrYsaSqBBWq8uSFlgRAwFXhdMJDzF9jGbaw79\n"
492+
+ "gNzowQ==\n"
493+
),
458494
rsa_pss_pss_sha256(
459495
"RSASSA-PSS",
460496
/**

test/jdk/javax/net/ssl/TLSv13/ClientHelloKeyShares.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
* questions.
2222
*/
2323

24+
/*
25+
* ===========================================================================
26+
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
27+
* ===========================================================================
28+
*/
29+
2430
// SunJSSE does not support dynamic system properties, no way to re-use
2531
// system properties in samevm/agentvm mode. For further debugging output
2632
// set the -Djavax.net.debug=ssl:handshake property on the @run lines.
@@ -31,6 +37,7 @@
3137
* @summary Use two key share entries
3238
* @run main/othervm ClientHelloKeyShares 29 23
3339
* @run main/othervm -Djdk.tls.namedGroups=secp384r1,secp521r1,x448,ffdhe2048 ClientHelloKeyShares 24 30
40+
* @run main/othervm -Djdk.tls.namedGroups=brainpoolP512r1tls13,x448,ffdhe2048 ClientHelloKeyShares 33 30
3441
* @run main/othervm -Djdk.tls.namedGroups=sect163k1,sect163r1,x25519 ClientHelloKeyShares 29
3542
* @run main/othervm -Djdk.tls.namedGroups=sect163k1,sect163r1,secp256r1 ClientHelloKeyShares 23
3643
* @run main/othervm -Djdk.tls.namedGroups=sect163k1,sect163r1,ffdhe2048,ffdhe3072,ffdhe4096 ClientHelloKeyShares 256

0 commit comments

Comments
 (0)