diff --git a/src/main/c/KeyEncapsulation.c b/src/main/c/KeyEncapsulation.c index 3d867ec..0ebadc4 100644 --- a/src/main/c/KeyEncapsulation.c +++ b/src/main/c/KeyEncapsulation.c @@ -105,12 +105,20 @@ JNIEXPORT jobject JNICALL Java_org_openquantumsafe_KeyEncapsulation_get_1KEM_1de JNIEXPORT jint JNICALL Java_org_openquantumsafe_KeyEncapsulation_generate_1keypair (JNIEnv *env, jobject obj, jbyteArray jpublic_key, jbyteArray jsecret_key) { - jbyte *public_key_native = (*env)->GetByteArrayElements(env, jpublic_key, 0); - jbyte *secret_key_native = (*env)->GetByteArrayElements(env, jsecret_key, 0); - // Get pointer to KEM OQS_KEM *kem = (OQS_KEM *) getHandle(env, obj, "native_kem_handle_"); + // liboqs writes length_public_key and length_secret_key bytes into the + // key buffers, so refuse buffers that are too short to hold them + if (kem == NULL || + (size_t) (*env)->GetArrayLength(env, jpublic_key) < kem->length_public_key || + (size_t) (*env)->GetArrayLength(env, jsecret_key) < kem->length_secret_key) { + return -1; + } + + jbyte *public_key_native = (*env)->GetByteArrayElements(env, jpublic_key, 0); + jbyte *secret_key_native = (*env)->GetByteArrayElements(env, jsecret_key, 0); + // Invoke liboqs KEM keypair generation function OQS_STATUS rv_ = OQS_KEM_keypair(kem, (uint8_t*) public_key_native, (uint8_t*) secret_key_native); @@ -127,13 +135,23 @@ JNIEXPORT jint JNICALL Java_org_openquantumsafe_KeyEncapsulation_generate_1keypa JNIEXPORT jint JNICALL Java_org_openquantumsafe_KeyEncapsulation_generate_1keypair_1derand (JNIEnv *env, jobject obj, jbyteArray jpublic_key, jbyteArray jsecret_key, jbyteArray jseed) { + // Get pointer to KEM + OQS_KEM *kem = (OQS_KEM *) getHandle(env, obj, "native_kem_handle_"); + + // liboqs writes length_public_key and length_secret_key bytes into the + // key buffers and reads length_keypair_seed bytes from the seed, so refuse + // buffers that are too short for that + if (kem == NULL || + (size_t) (*env)->GetArrayLength(env, jpublic_key) < kem->length_public_key || + (size_t) (*env)->GetArrayLength(env, jsecret_key) < kem->length_secret_key || + (size_t) (*env)->GetArrayLength(env, jseed) < kem->length_keypair_seed) { + return -1; + } + jbyte *public_key_native = (*env)->GetByteArrayElements(env, jpublic_key, 0); jbyte *secret_key_native = (*env)->GetByteArrayElements(env, jsecret_key, 0); jbyte *seed_native = (*env)->GetByteArrayElements(env, jseed, 0); - // Get pointer to KEM - OQS_KEM *kem = (OQS_KEM *) getHandle(env, obj, "native_kem_handle_"); - // Invoke liboqs KEM keypair generation function OQS_STATUS rv_ = OQS_KEM_keypair_derand(kem, (uint8_t*) public_key_native, (uint8_t*) secret_key_native, (uint8_t*) seed_native); diff --git a/src/main/c/Signature.c b/src/main/c/Signature.c index 7b72a54..296a67d 100644 --- a/src/main/c/Signature.c +++ b/src/main/c/Signature.c @@ -97,12 +97,20 @@ JNIEXPORT jobject JNICALL Java_org_openquantumsafe_Signature_get_1sig_1details JNIEXPORT jint JNICALL Java_org_openquantumsafe_Signature_generate_1keypair (JNIEnv *env, jobject obj, jbyteArray jpublic_key, jbyteArray jsecret_key) { - jbyte *public_key_native = (*env)->GetByteArrayElements(env, jpublic_key, 0); - jbyte *secret_key_native = (*env)->GetByteArrayElements(env, jsecret_key, 0); - // Get pointer to sig OQS_SIG *sig = (OQS_SIG *) getHandle(env, obj, "native_sig_handle_"); + // liboqs writes length_public_key and length_secret_key bytes into the + // key buffers, so refuse buffers that are too short to hold them + if (sig == NULL || + (size_t) (*env)->GetArrayLength(env, jpublic_key) < sig->length_public_key || + (size_t) (*env)->GetArrayLength(env, jsecret_key) < sig->length_secret_key) { + return -1; + } + + jbyte *public_key_native = (*env)->GetByteArrayElements(env, jpublic_key, 0); + jbyte *secret_key_native = (*env)->GetByteArrayElements(env, jsecret_key, 0); + // Invoke liboqs sig keypair generation function OQS_STATUS rv_ = OQS_SIG_keypair(sig, (uint8_t*) public_key_native, (uint8_t*) secret_key_native); diff --git a/src/main/java/org/openquantumsafe/KeyEncapsulation.java b/src/main/java/org/openquantumsafe/KeyEncapsulation.java index 9bbe09d..914097d 100644 --- a/src/main/java/org/openquantumsafe/KeyEncapsulation.java +++ b/src/main/java/org/openquantumsafe/KeyEncapsulation.java @@ -174,12 +174,24 @@ private void check_not_disposed() throws IllegalStateException { } + /** + * \brief Make secret_key_ the right length to hold a newly generated secret + * key. A secret key passed to the constructor can have any length. + */ + private void ensure_secret_key_length() { + if (this.secret_key_.length != alg_details_.length_secret_key) { + Common.wipe(this.secret_key_); + this.secret_key_ = new byte[(int) alg_details_.length_secret_key]; + } + } + /** * \brief Invoke native generate_keypair method using the PK and SK lengths * from alg_details_. Check return value and if != 0 throw Exception. */ public byte[] generate_keypair() throws RuntimeException { check_not_disposed(); + ensure_secret_key_length(); int rv_ = generate_keypair(this.public_key_, this.secret_key_); if (rv_ != 0) throw new RuntimeException("Cannot generate keypair"); return this.public_key_; @@ -195,6 +207,7 @@ public byte[] generate_keypair(byte[] seed) throws RuntimeException { throw new RuntimeException("Incorrect seed length"); } + ensure_secret_key_length(); int rv_ = generate_keypair_derand(this.public_key_, this.secret_key_, seed); if (rv_ != 0) throw new RuntimeException("Cannot generate keypair from seed"); return this.public_key_; diff --git a/src/main/java/org/openquantumsafe/Signature.java b/src/main/java/org/openquantumsafe/Signature.java index b089d08..b103c32 100644 --- a/src/main/java/org/openquantumsafe/Signature.java +++ b/src/main/java/org/openquantumsafe/Signature.java @@ -210,12 +210,24 @@ private void check_not_disposed() throws IllegalStateException { } } + /** + * \brief Make secret_key_ the right length to hold a newly generated secret + * key. A secret key passed to the constructor can have any length. + */ + private void ensure_secret_key_length() { + if (this.secret_key_.length != alg_details_.length_secret_key) { + Common.wipe(this.secret_key_); + this.secret_key_ = new byte[(int) alg_details_.length_secret_key]; + } + } + /** * \brief Invoke native generate_keypair method using the PK and SK lengths * from alg_details_. Check return value and if != 0 throw RuntimeException. */ public byte[] generate_keypair() throws RuntimeException { check_not_disposed(); + ensure_secret_key_length(); int rv_ = generate_keypair(this.public_key_, this.secret_key_); if (rv_ != 0) throw new RuntimeException("Cannot generate keypair"); return this.public_key_; diff --git a/src/test/java/org/openquantumsafe/KEMTest.java b/src/test/java/org/openquantumsafe/KEMTest.java index 56420f6..b3d7c8f 100644 --- a/src/test/java/org/openquantumsafe/KEMTest.java +++ b/src/test/java/org/openquantumsafe/KEMTest.java @@ -6,8 +6,10 @@ import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Stream; @@ -166,6 +168,80 @@ public void testCreateKEMNewWithNullName() { assertArrayEquals(client.decap_secret(server_pair.getLeft()), server_pair.getRight()); } + /** + * Test that generate_keypair() produces a usable key pair when the secret + * key passed to the constructor has the wrong length. ML-KEM-512 secret + * keys are 1632 bytes long. + */ + @ParameterizedTest(name = "Testing secret key length {arguments}") + @ValueSource(ints = {0, 32, 4096}) + public void testGenerateKeypairWithWrongLengthSecretKey(int secret_key_len) { + KeyEncapsulation client = new KeyEncapsulation("ML-KEM-512", new byte[secret_key_len]); + KeyEncapsulation server = new KeyEncapsulation("ML-KEM-512"); + byte[] client_public_key = client.generate_keypair(); + + Pair server_pair = server.encap_secret(client_public_key); + assertArrayEquals(client.decap_secret(server_pair.getLeft()), server_pair.getRight()); + } + + /** + * Test that generate_keypair(seed) produces a usable key pair when the + * secret key passed to the constructor has the wrong length + */ + @ParameterizedTest(name = "Testing secret key length {arguments}") + @ValueSource(ints = {0, 32, 4096}) + public void testGenerateKeypairDerandWithWrongLengthSecretKey(int secret_key_len) { + KeyEncapsulation client = new KeyEncapsulation("ML-KEM-512", new byte[secret_key_len]); + KeyEncapsulation server = new KeyEncapsulation("ML-KEM-512"); + byte[] seed = Rand.randombytes(client.get_keypair_seed_length()); + byte[] client_public_key = client.generate_keypair(seed); + + Pair server_pair = server.encap_secret(client_public_key); + assertArrayEquals(client.decap_secret(server_pair.getLeft()), server_pair.getRight()); + } + + /** + * Test that native keypair generation refuses key buffers too short to + * hold the keys instead of writing past their end + */ + @Test + public void testNativeGenerateKeypairRejectsShortBuffers() throws Exception { + KeyEncapsulation kem = new KeyEncapsulation("ML-KEM-512"); + int pk_len = kem.export_public_key().length; + int sk_len = kem.export_secret_key().length; + Method generate_keypair = KeyEncapsulation.class.getDeclaredMethod( + "generate_keypair", byte[].class, byte[].class); + generate_keypair.setAccessible(true); + + Assertions.assertEquals(-1, (int) generate_keypair.invoke(kem, new byte[pk_len - 1], new byte[sk_len])); + Assertions.assertEquals(-1, (int) generate_keypair.invoke(kem, new byte[pk_len], new byte[sk_len - 1])); + Assertions.assertEquals(0, (int) generate_keypair.invoke(kem, new byte[pk_len], new byte[sk_len])); + } + + /** + * Test that native derandomized keypair generation refuses key buffers too + * short to hold the keys, and a seed too short to read from + */ + @Test + public void testNativeGenerateKeypairDerandRejectsShortBuffers() throws Exception { + KeyEncapsulation kem = new KeyEncapsulation("ML-KEM-512"); + int pk_len = kem.export_public_key().length; + int sk_len = kem.export_secret_key().length; + int seed_len = (int) kem.get_keypair_seed_length(); + Method generate_keypair_derand = KeyEncapsulation.class.getDeclaredMethod( + "generate_keypair_derand", byte[].class, byte[].class, byte[].class); + generate_keypair_derand.setAccessible(true); + + Assertions.assertEquals(-1, (int) generate_keypair_derand.invoke(kem, + new byte[pk_len - 1], new byte[sk_len], new byte[seed_len])); + Assertions.assertEquals(-1, (int) generate_keypair_derand.invoke(kem, + new byte[pk_len], new byte[sk_len - 1], new byte[seed_len])); + Assertions.assertEquals(-1, (int) generate_keypair_derand.invoke(kem, + new byte[pk_len], new byte[sk_len], new byte[seed_len - 1])); + Assertions.assertEquals(0, (int) generate_keypair_derand.invoke(kem, + new byte[pk_len], new byte[sk_len], new byte[seed_len])); + } + /** * Method to convert the list of KEMs to a stream for input to testAllKEMs */ diff --git a/src/test/java/org/openquantumsafe/SigTest.java b/src/test/java/org/openquantumsafe/SigTest.java index e338ae9..02dccd4 100644 --- a/src/test/java/org/openquantumsafe/SigTest.java +++ b/src/test/java/org/openquantumsafe/SigTest.java @@ -6,11 +6,13 @@ import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; // import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Stream; @@ -204,6 +206,38 @@ public void testCreateSigNewWithNullName() { assertTrue(signer.verify(message, signer.sign(message), signer_public_key)); } + /** + * Test that generate_keypair() produces a usable key pair when the secret + * key passed to the constructor has the wrong length. ML-DSA-44 secret + * keys are 2560 bytes long. + */ + @ParameterizedTest(name = "Testing secret key length {arguments}") + @ValueSource(ints = {0, 32, 4096}) + public void testGenerateKeypairWithWrongLengthSecretKey(int secret_key_len) { + Signature signer = new Signature("ML-DSA-44", new byte[secret_key_len]); + byte[] signer_public_key = signer.generate_keypair(); + + assertTrue(signer.verify(message, signer.sign(message), signer_public_key)); + } + + /** + * Test that native keypair generation refuses key buffers too short to + * hold the keys instead of writing past their end + */ + @Test + public void testNativeGenerateKeypairRejectsShortBuffers() throws Exception { + Signature signer = new Signature("ML-DSA-44"); + int pk_len = signer.export_public_key().length; + int sk_len = signer.export_secret_key().length; + Method generate_keypair = Signature.class.getDeclaredMethod( + "generate_keypair", byte[].class, byte[].class); + generate_keypair.setAccessible(true); + + Assertions.assertEquals(-1, (int) generate_keypair.invoke(signer, new byte[pk_len - 1], new byte[sk_len])); + Assertions.assertEquals(-1, (int) generate_keypair.invoke(signer, new byte[pk_len], new byte[sk_len - 1])); + Assertions.assertEquals(0, (int) generate_keypair.invoke(signer, new byte[pk_len], new byte[sk_len])); + } + /** * Method to convert the list of Sigs to a stream for input to testAllSigs */