Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for X25519 algorithm alias to XDH #1156

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/src/main/java/org/conscrypt/OpenSSLProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public OpenSSLProvider(String providerName) {

put("KeyPairGenerator.XDH", PREFIX + "OpenSSLXDHKeyPairGenerator");
put("Alg.Alias.KeyPairGenerator.1.3.101.110", "XDH");
put("Alg.Alias.KeyPairGenerator.X25519", "XDH");

/* == KeyFactory == */
put("KeyFactory.RSA", PREFIX + "OpenSSLRSAKeyFactory");
Expand All @@ -211,6 +212,7 @@ public OpenSSLProvider(String providerName) {

put("KeyFactory.XDH", PREFIX + "OpenSSLXDHKeyFactory");
put("Alg.Alias.KeyFactory.1.3.101.110", "XDH");
put("Alg.Alias.KeyFactory.X25519", "XDH");

/* == SecretKeyFactory == */
put("SecretKeyFactory.DESEDE", PREFIX + "DESEDESecretKeyFactory");
Expand Down Expand Up @@ -624,6 +626,8 @@ private void putXDHKeyAgreementImplClass(String className) {
PREFIX + className,
supportedKeyClasses,
supportedKeyFormats);

put("Alg.Alias.KeyAgreement.X25519", "XDH");
}

private void putImplClassWithKeyConstraints(String typeAndAlgName,
Expand Down
5 changes: 3 additions & 2 deletions common/src/main/java/org/conscrypt/OpenSSLXDHKeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
throw new InvalidKeySpecException("keySpec == null");
}

if (!"XDH".equals(key.getAlgorithm())) {
throw new InvalidKeySpecException("Key must be an XDH key");
// Support XDH or X25519 algorithm names per JEP 324
if (!"XDH".equals(key.getAlgorithm()) || !"X25519".equals(key.getAlgorithm()) ) {
throw new InvalidKeySpecException("Key must be an XDH or X25519 key");
}

Class<?> publicKeySpec = getJavaPublicKeySpec();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.conscrypt.javax.crypto;

import static org.junit.Assert.assertArrayEquals;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.KeyAgreement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


/**
* Tests for all registered X25519 {@link KeyAgreement} providers.
*/
@RunWith(JUnit4.class)
public class X25519KeyAgreementTest extends XDHKeyAgreementTest {

@Override
protected String getAlgorithm() {
return "X25519";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class XDHKeyAgreementTest {
private PublicKey rfc7748X25519PublicKey;

private void setupKeys(Provider p) throws Exception {
KeyFactory kf = KeyFactory.getInstance("XDH", p);
KeyFactory kf = KeyFactory.getInstance(getAlgorithm(), p);

byte[] privateKey;
if ("SunEC".equalsIgnoreCase(p.getName())
Expand All @@ -84,15 +84,20 @@ private void setupKeys(Provider p) throws Exception {

@Test
public void test_XDHKeyAgreement() throws Exception {
for (Provider p : Security.getProviders("KeyAgreement.XDH")) {
final String keyAgreementAlgorithm = String.format("KeyAgreement.%s", getAlgorithm());
for (Provider p : Security.getProviders(keyAgreementAlgorithm)) {
setupKeys(p);

KeyAgreement ka = KeyAgreement.getInstance("XDH", p);
KeyAgreement ka = KeyAgreement.getInstance(getAlgorithm(), p);

test_x25519_keyAgreement_rfc7748_kat_success(ka);
}
}

protected String getAlgorithm() {
return "XDH";
}

private void test_x25519_keyAgreement_rfc7748_kat_success(KeyAgreement ka) throws Exception {
ka.init(rfc7748X25519PrivateKey);
ka.doPhase(rfc7748X25519PublicKey, true);
Expand Down
Loading