Skip to content

Commit

Permalink
Move computeMLDSAMu helper to TestUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
WillChilds-Klein committed Jan 31, 2025
1 parent 5ce6d0c commit 99ca3bc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 89 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ set(C_SRC
csrc/libcrypto_rng.cpp
csrc/loader.cpp
csrc/md5.cpp
csrc/public_utils.cpp
csrc/rsa_cipher.cpp
csrc/rsa_gen.cpp
csrc/sha1.cpp
csrc/sha256.cpp
csrc/sha384.cpp
csrc/sha512.cpp
csrc/sign.cpp
csrc/test_util.cpp
csrc/testhooks.cpp
csrc/util.cpp
csrc/util_class.cpp
Expand Down
4 changes: 2 additions & 2 deletions csrc/public_utils.cpp → csrc/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
namespace AmazonCorrettoCryptoProvider {

/*
* Class: com_amazon_corretto_crypto_provider_Utils
* Class: com_amazon_corretto_crypto_provider_test_TestUtil
* Method: computeMLDSAMuInternal
* Signature: ([B[B)[B
*/
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_amazon_corretto_crypto_provider_PublicUtils_computeMLDSAMuInternal(
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_amazon_corretto_crypto_provider_test_TestUtil_computeMLDSAMuInternal(
JNIEnv* pEnv, jclass, jbyteArray pubKeyEncodedArr, jbyteArray messageArr)
{
try {
Expand Down
28 changes: 0 additions & 28 deletions src/com/amazon/corretto/crypto/provider/PublicUtils.java

This file was deleted.

21 changes: 19 additions & 2 deletions tst/com/amazon/corretto/crypto/provider/test/MLDSATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider;
import com.amazon.corretto.crypto.provider.PublicUtils;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
Expand Down Expand Up @@ -271,7 +270,7 @@ public void testExtMu(TestParams params) throws Exception {
PublicKey pub = params.pub;

byte[] message = Arrays.copyOf(params.message, params.message.length);
byte[] mu = PublicUtils.computeMLDSAMu(pub, message);
byte[] mu = TestUtil.computeMLDSAMu(pub, message);
assertEquals(64, mu.length);
byte[] fakeMu = new byte[64];
Arrays.fill(fakeMu, (byte) 0);
Expand Down Expand Up @@ -317,4 +316,22 @@ public void testExtMu(TestParams params) throws Exception {
extMuVerifier.update(mu);
assertFalse(extMuVerifier.verify(signatureBytes));
}

@ParameterizedTest
@ValueSource(strings = {"ML-DSA-44", "ML-DSA-65", "ML-DSA-87"})
public void testComputeMLDSAExtMu(String algorithm) throws Exception {
KeyPair keyPair = KeyPairGenerator.getInstance(algorithm, NATIVE_PROVIDER).generateKeyPair();
PublicKey nativePub = keyPair.getPublic();
KeyFactory bcKf = KeyFactory.getInstance("ML-DSA", TestUtil.BC_PROVIDER);
PublicKey bcPub = bcKf.generatePublic(new X509EncodedKeySpec(nativePub.getEncoded()));

byte[] message = new byte[256];
Arrays.fill(message, (byte) 0x41);
byte[] mu = TestUtil.computeMLDSAMu(nativePub, message);
assertEquals(64, mu.length);
// We don't have any other implementations of mu calculation to test against, so just assert
// that mu is equivalent
// generated from both ACCP and BouncyCastle keys.
assertArrayEquals(mu, TestUtil.computeMLDSAMu(bcPub, message));
}
}
56 changes: 0 additions & 56 deletions tst/com/amazon/corretto/crypto/provider/test/PublicUtilsTest.java

This file was deleted.

19 changes: 19 additions & 0 deletions tst/com/amazon/corretto/crypto/provider/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.util.ArrayList;
Expand Down Expand Up @@ -841,4 +842,22 @@ static boolean edKeyFactoryRegistered() {
return "true"
.equals(System.getProperty("com.amazon.corretto.crypto.provider.registerEdKeyFactory"));
}

private static native byte[] computeMLDSAMuInternal(byte[] pubKeyEncoded, byte[] message);

/**
* Computes mu as defined on line 6 of Algorithm 7 and line 7 of Algorithm 8 in NIST FIPS 204.
*
* <p>See <a href="https://csrc.nist.gov/pubs/fips/204/final">FIPS 204</a>
*
* @param publicKey ML-DSA public key
* @param message byte array of the message over which to compute mu
* @return a byte[] of length 64 containing mu
*/
static byte[] computeMLDSAMu(PublicKey publicKey, byte[] message) {
if (publicKey == null || !publicKey.getAlgorithm().startsWith("ML-DSA") || message == null) {
throw new IllegalArgumentException();
}
return computeMLDSAMuInternal(publicKey.getEncoded(), message);
}
}

0 comments on commit 99ca3bc

Please sign in to comment.