etnt commented on code in PR #944:
URL: https://github.com/apache/mina-sshd/pull/944#discussion_r4493679024
##########
sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java:
##########
@@ -742,7 +749,11 @@ public static String getKeyType(Key key) {
return KeyPairProvider.SSH_ED25519;
}
- return null;
+ try {
+ return MLDSAUtils.getParameters(key).getSshName();
Review Comment:
Fixed in 2a33fdd35. `compareKeys(PrivateKey, PrivateKey)` now handles ML-DSA
keys: they are equal if they are the same instance, or if they have the same
parameter set and equal PKCS#8 encodings (compared in constant time with
`MessageDigest.isEqual`). Added regression tests:
`MLDSATest.compareEquivalentKeyPairs` (wrapper key pair, re-decoded private
key, different private key) and
`ClientAuthenticationManagerTest.removeEquivalentMLDSAPublicKeyIdentity`
(removal via an equivalent `KeyPair` wrapper on `SshClient`).
##########
sshd-common/src/test/java/org/apache/sshd/common/signature/SignatureMLDSATest.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.common.signature;
+
+import java.nio.charset.StandardCharsets;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.Security;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Arrays;
+
+import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
+import org.apache.sshd.common.util.security.MLDSAParameterSets;
+import org.apache.sshd.common.util.security.SecurityUtils;
+import org.apache.sshd.util.test.JUnitTestSupport;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+@Tag("NoIoTestCase")
+class SignatureMLDSATest extends JUnitTestSupport {
+ private static final byte[] DATA = "ML-DSA signature
test".getBytes(StandardCharsets.UTF_8);
+
+ SignatureMLDSATest() {
+ super();
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void signAndVerify(MLDSAParameterSets params) throws Exception {
+ SignatureFactory factory = factory(params);
+ KeyPair pair = generateKeyPair(params);
+ byte[] signature = sign(factory, pair, DATA);
+ assertEquals(params.getSignatureSize(), signature.length);
+ assertTrue(verify(factory, pair.getPublic(), DATA, signature));
+ assertTrue(verify(factory, pair.getPublic(), DATA,
encode(params.getSshName(), signature)));
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void rejectsTamperedSignatureAndData(MLDSAParameterSets params) throws
Exception {
+ SignatureFactory factory = factory(params);
+ KeyPair pair = generateKeyPair(params);
+ byte[] signature = sign(factory, pair, DATA);
+ for (int pos : new int[] { 0, signature.length / 2, signature.length -
1 }) {
+ byte[] tampered = signature.clone();
+ tampered[pos] ^= 1;
+ assertFalse(verify(factory, pair.getPublic(), DATA, tampered),
"Tampered at " + pos);
+ }
+ byte[] otherData = DATA.clone();
+ otherData[0] ^= 1;
+ assertFalse(verify(factory, pair.getPublic(), otherData, signature));
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void rejectsWrongLengthSignature(MLDSAParameterSets params) throws
Exception {
+ SignatureFactory factory = factory(params);
+ KeyPair pair = generateKeyPair(params);
+ byte[] signature = sign(factory, pair, DATA);
+ assertFalse(verify(factory, pair.getPublic(), DATA,
Arrays.copyOf(signature, signature.length - 1)));
+ assertFalse(verify(factory, pair.getPublic(), DATA,
Arrays.copyOf(signature, signature.length + 1)));
+ assertFalse(verify(factory, pair.getPublic(), DATA, new byte[0]));
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void rejectsOtherKey(MLDSAParameterSets params) throws Exception {
+ SignatureFactory factory = factory(params);
+ byte[] signature = sign(factory, generateKeyPair(params), DATA);
+ assertFalse(verify(factory, generateKeyPair(params).getPublic(), DATA,
signature));
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void rejectsMismatchedKeyType(MLDSAParameterSets params) throws Exception {
+ SignatureFactory factory = factory(params);
+ KeyPair pair = generateKeyPair(params);
+ byte[] signature = sign(factory, pair, DATA);
+ MLDSAParameterSets other
+ = params == MLDSAParameterSets.ML_DSA_44 ?
MLDSAParameterSets.ML_DSA_65 : MLDSAParameterSets.ML_DSA_44;
+ assertFalse(verify(factory, pair.getPublic(), DATA,
encode(other.getSshName(), signature)));
+ }
+
+ @ParameterizedTest
+ @EnumSource(MLDSAParameterSets.class)
+ void signatureVerifiesWithEveryProvider(MLDSAParameterSets params) throws
Exception {
+ SignatureFactory factory = factory(params);
+ KeyPair pair = generateKeyPair(params);
+ byte[] signature = sign(factory, pair, DATA);
+ int checked = 0;
+ for (String name : new String[] { "SUN", SecurityUtils.BOUNCY_CASTLE
}) {
+ Provider provider = Security.getProvider(name);
Review Comment:
Fixed in 72b445258. The test now looks up the provider via
`Security.getProvider(name)` and falls back to SSHD's registered provider
(`SecurityUtils.getRegisteredProvider(name).getSecurityProvider()`), so BC
verification no longer depends on BC being installed globally.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]