Crypto: Change RFC 7748 DH error to a checked exception

so things are cleaned up properly
This commit is contained in:
zzz committed 2026-04-25 15:22:21 -04:00
1 parent e9d4b0c7e1
commit 20a10ae0ae
9 files changed
+72 -36

No files matched your search

@@ -22,6 +22,7 @@
package com.southernstorm.noise.crypto.x25519;
import java.security.GeneralSecurityException;
import java.util.Arrays;
/**
@@ -471,9 +472,9 @@ public final class Curve25519 {
* @param publicKey The public key to use in the evaluation, or null
* MUST have MSB high bit cleared, i.e. publicKey[31] & 0x80 == 0
* if the base point of the curve should be used.
* @throws IllegalArgumentException on low-order input see RFC 7748
* @throws GeneralSecurityException on low-order input see RFC 7748
*/
public static void eval(byte[] result, int offset, byte[] privateKey, byte[] publicKey)
public static void eval(byte[] result, int offset, byte[] privateKey, byte[] publicKey) throws GeneralSecurityException
{
Curve25519 state = new Curve25519();
try {
@@ -530,7 +531,7 @@ public final class Curve25519 {
b |= result[offset + index];
}
if (b == 0)
throw new IllegalArgumentException("low order input RFC 7748");
throw new GeneralSecurityException("low order input RFC 7748");
} finally {
// Clean up all temporary state before we exit.
state.destroy();
+10 -2
View File
@@ -206,7 +206,11 @@ public final class KeyGenerator {
// worth doing?
} while (bpriv[31] == 0);
byte[] bpub = new byte[32];
Curve25519.eval(bpub, 0, bpriv, null);
try {
Curve25519.eval(bpub, 0, bpriv, null);
} catch (GeneralSecurityException gse) {
throw new IllegalArgumentException("Bad keys", gse);
}
pub = new PublicKey(type, bpub);
priv = new PrivateKey(type, bpriv, pub);
break;
@@ -245,7 +249,11 @@ public final class KeyGenerator {
case MLKEM768_X25519:
case MLKEM1024_X25519:
data = new byte[32];
Curve25519.eval(data, 0, priv.getData(), null);
try {
Curve25519.eval(data, 0, priv.getData(), null);
} catch (GeneralSecurityException gse) {
throw new IllegalArgumentException("Bad keys", gse);
}
break;
default:
@@ -1,5 +1,7 @@
package net.i2p.crypto.x25519;
import java.security.GeneralSecurityException;
import com.southernstorm.noise.crypto.x25519.Curve25519;
import net.i2p.crypto.EncType;
@@ -30,7 +32,11 @@ public class X25519DH {
if (priv.getType() != TYPE || pub.getType() != TYPE)
throw new IllegalArgumentException();
byte[] rv = new byte[32];
Curve25519.eval(rv, 0, priv.getData(), pub.getData());
try {
Curve25519.eval(rv, 0, priv.getData(), pub.getData());
} catch (GeneralSecurityException gse) {
throw new IllegalArgumentException("Bad keys", gse);
}
return new SessionKey(rv);
}