Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ This library is supported for Java LTS versions 8, 11, 17 and 21. For issues on
| ES256 | ECDSA256 | ECDSA with curve P-256 and SHA-256 |
| ES384 | ECDSA384 | ECDSA with curve P-384 and SHA-384 |
| ES512 | ECDSA512 | ECDSA with curve P-521 and SHA-512 |
| ML-DSA-44 | MLDSA44 | ML-DSA-44 (FIPS 204, post-quantum) |
| ML-DSA-65 | MLDSA65 | ML-DSA-65 (FIPS 204, post-quantum) |
| ML-DSA-87 | MLDSA87 | ML-DSA-87 (FIPS 204, post-quantum) |

> Note - Support for ECDSA with curve secp256k1 and SHA-256 (ES256K) has been dropped since it has been [disabled in Java 15](https://www.oracle.com/java/technologies/javase/15-relnote-issues.html#JDK-8237219)

> Note - The ML-DSA algorithms (ML-DSA-44, ML-DSA-65, ML-DSA-87, as defined for JOSE in [RFC 9964](https://www.rfc-editor.org/info/rfc9964/)) rely on the JVM for ML-DSA support, which is available natively from Java 24 onwards ([JEP 497](https://openjdk.org/jeps/497)). On earlier versions, a security provider that implements `ML-DSA` (such as [BouncyCastle](https://www.bouncycastle.org/)) must be registered; no additional provider is bundled by this library.

> Note - The RSASSA-PSS algorithms (PS256, PS384, PS512) rely on the JVM for RSASSA-PSS support, which is available natively from Java 11 onwards via the `SunRsaSign` provider ([JDK-8146293](https://bugs.openjdk.org/browse/JDK-8146293)). On Java 8, a security provider that implements `RSASSA-PSS` (such as [BouncyCastle](https://www.bouncycastle.org/)) must be registered on the classpath; no additional provider is bundled by this library.

> :warning: **Important security note:** JVM has a critical vulnerability for ECDSA Algorithms - [CVE-2022-21449](https://nvd.nist.gov/vuln/detail/CVE-2022-21449). Please review the details of the vulnerability and update your environment.
Expand Down
88 changes: 88 additions & 0 deletions lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.ECDSAKeyProvider;
import com.auth0.jwt.interfaces.MLDSAKeyProvider;
import com.auth0.jwt.interfaces.RSAKeyProvider;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.*;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
Expand Down Expand Up @@ -437,6 +440,91 @@ public static Algorithm ECDSA512(ECKey key) throws IllegalArgumentException {
}


/**
* Creates a new Algorithm instance using ML-DSA-44 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-44" (RFC 9964).
* <p>
* The JVM must have a security provider that implements ML-DSA: Java 24 and later include one,
* earlier versions need a provider such as BouncyCastle to be registered.
*
* @param keyProvider the provider of the Public Key and Private Key for the verify and signing instance.
* @return a valid ML-DSA-44 Algorithm.
* @throws IllegalArgumentException if the Key Provider is null.
*/
public static Algorithm MLDSA44(MLDSAKeyProvider keyProvider) throws IllegalArgumentException {
return new MLDSAAlgorithm("ML-DSA-44", "2.16.840.1.101.3.4.3.17", 2420, keyProvider);
}

/**
* Creates a new Algorithm instance using ML-DSA-44 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-44" (RFC 9964).
*
* @param publicKey the key to use in the verify instance.
* @param privateKey the key to use in the signing instance.
* @return a valid ML-DSA-44 Algorithm.
* @throws IllegalArgumentException if both provided Keys are null.
*/
public static Algorithm MLDSA44(PublicKey publicKey, PrivateKey privateKey) throws IllegalArgumentException {
return MLDSA44(MLDSAAlgorithm.providerForKeys(publicKey, privateKey));
}

/**
* Creates a new Algorithm instance using ML-DSA-65 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-65" (RFC 9964).
* <p>
* The JVM must have a security provider that implements ML-DSA: Java 24 and later include one,
* earlier versions need a provider such as BouncyCastle to be registered.
*
* @param keyProvider the provider of the Public Key and Private Key for the verify and signing instance.
* @return a valid ML-DSA-65 Algorithm.
* @throws IllegalArgumentException if the Key Provider is null.
*/
public static Algorithm MLDSA65(MLDSAKeyProvider keyProvider) throws IllegalArgumentException {
return new MLDSAAlgorithm("ML-DSA-65", "2.16.840.1.101.3.4.3.18", 3309, keyProvider);
}

/**
* Creates a new Algorithm instance using ML-DSA-65 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-65" (RFC 9964).
*
* @param publicKey the key to use in the verify instance.
* @param privateKey the key to use in the signing instance.
* @return a valid ML-DSA-65 Algorithm.
* @throws IllegalArgumentException if both provided Keys are null.
*/
public static Algorithm MLDSA65(PublicKey publicKey, PrivateKey privateKey) throws IllegalArgumentException {
return MLDSA65(MLDSAAlgorithm.providerForKeys(publicKey, privateKey));
}

/**
* Creates a new Algorithm instance using ML-DSA-87 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-87" (RFC 9964).
* <p>
* The JVM must have a security provider that implements ML-DSA: Java 24 and later include one,
* earlier versions need a provider such as BouncyCastle to be registered.
*
* @param keyProvider the provider of the Public Key and Private Key for the verify and signing instance.
* @return a valid ML-DSA-87 Algorithm.
* @throws IllegalArgumentException if the Key Provider is null.
*/
public static Algorithm MLDSA87(MLDSAKeyProvider keyProvider) throws IllegalArgumentException {
return new MLDSAAlgorithm("ML-DSA-87", "2.16.840.1.101.3.4.3.19", 4627, keyProvider);
}

/**
* Creates a new Algorithm instance using ML-DSA-87 (FIPS 204), a post-quantum signature algorithm.
* Tokens specify this as "ML-DSA-87" (RFC 9964).
*
* @param publicKey the key to use in the verify instance.
* @param privateKey the key to use in the signing instance.
* @return a valid ML-DSA-87 Algorithm.
* @throws IllegalArgumentException if both provided Keys are null.
*/
public static Algorithm MLDSA87(PublicKey publicKey, PrivateKey privateKey) throws IllegalArgumentException {
return MLDSA87(MLDSAAlgorithm.providerForKeys(publicKey, privateKey));
}


public static Algorithm none() {
return new NoneAlgorithm();
}
Expand Down
154 changes: 154 additions & 0 deletions lib/src/main/java/com/auth0/jwt/algorithms/MLDSAAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.auth0.jwt.algorithms;

import com.auth0.jwt.exceptions.SignatureGenerationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.MLDSAKeyProvider;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SignatureException;
import java.util.Base64;

/**
* Subclass representing an ML-DSA (FIPS 204) signing algorithm, used as defined for JOSE in RFC 9964:
* the pure variant, with an empty context string, over the JWS signing input.
* <p>
* This class is thread-safe.
*/
class MLDSAAlgorithm extends Algorithm {

private static final String FAMILY = "ML-DSA";

private final MLDSAKeyProvider keyProvider;
private final CryptoHelper crypto;
private final String oid;
private final int signatureLength;

//Visible for testing
MLDSAAlgorithm(CryptoHelper crypto, String id, String oid, int signatureLength, MLDSAKeyProvider keyProvider)
throws IllegalArgumentException {
super(id, id);
if (keyProvider == null) {
throw new IllegalArgumentException("The Key Provider cannot be null.");
}
this.keyProvider = keyProvider;
this.crypto = crypto;
this.oid = oid;
this.signatureLength = signatureLength;
}

MLDSAAlgorithm(String id, String oid, int signatureLength, MLDSAKeyProvider keyProvider)
throws IllegalArgumentException {
this(new CryptoHelper(), id, oid, signatureLength, keyProvider);
}

@Override
public void verify(DecodedJWT jwt) throws SignatureVerificationException {
try {
byte[] signatureBytes = Base64.getUrlDecoder().decode(jwt.getSignature());
PublicKey publicKey = keyProvider.getPublicKeyById(jwt.getKeyId());
if (publicKey == null) {
throw new IllegalStateException("The given Public Key is null.");
}
validateKey(publicKey);
if (signatureBytes.length != signatureLength) {
throw new SignatureException("Invalid " + getName() + " signature length.");
}
boolean valid = crypto.verifySignatureFor(
getDescription(), publicKey, jwt.getHeader(), jwt.getPayload(), signatureBytes);
if (!valid) {
throw new SignatureVerificationException(this);
}
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException
| IllegalArgumentException | IllegalStateException e) {
throw new SignatureVerificationException(this, e);
}
}

@Override
public byte[] sign(byte[] headerBytes, byte[] payloadBytes) throws SignatureGenerationException {
try {
PrivateKey privateKey = signingKey();
return checkLength(crypto.createSignatureFor(getDescription(), privateKey, headerBytes, payloadBytes));
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
throw new SignatureGenerationException(this, e);
}
}

@Override
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
try {
PrivateKey privateKey = signingKey();
return checkLength(crypto.createSignatureFor(getDescription(), privateKey, contentBytes));
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
throw new SignatureGenerationException(this, e);
}
}

@Override
public String getSigningKeyId() {
return keyProvider.getPrivateKeyId();
}

private PrivateKey signingKey() throws InvalidKeyException {
PrivateKey privateKey = keyProvider.getPrivateKey();
if (privateKey == null) {
throw new IllegalStateException("The given Private Key is null.");
}
validateKey(privateKey);
return privateKey;
}

private byte[] checkLength(byte[] signature) throws SignatureException {
if (signature.length != signatureLength) {
throw new SignatureException("Unexpected " + getName() + " signature length.");
}
return signature;
}

/**
* Rejects a key that is not an ML-DSA key, or that belongs to another ML-DSA parameter set.
* Providers name these keys differently: the parameter set ("ML-DSA-65"), the family ("ML-DSA") or the
* algorithm OID. When only the family is reported the security provider enforces the parameter set.
*/
private void validateKey(Key key) throws InvalidKeyException {
String keyAlgorithm = key.getAlgorithm();
if (keyAlgorithm == null) {
throw new InvalidKeyException("The given Key has no algorithm.");
}
boolean matches = keyAlgorithm.equalsIgnoreCase(getName())
|| keyAlgorithm.equalsIgnoreCase(FAMILY)
|| keyAlgorithm.equals(oid);
if (!matches) {
throw new InvalidKeyException(
"The given Key is for " + keyAlgorithm + " and cannot be used with " + getName() + ".");
}
}

//Visible for testing
static MLDSAKeyProvider providerForKeys(final PublicKey publicKey, final PrivateKey privateKey) {
if (publicKey == null && privateKey == null) {
throw new IllegalArgumentException("Both provided Keys cannot be null.");
}
return new MLDSAKeyProvider() {
@Override
public PublicKey getPublicKeyById(String keyId) {
return publicKey;
}

@Override
public PrivateKey getPrivateKey() {
return privateKey;
}

@Override
public String getPrivateKeyId() {
return null;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.auth0.jwt.interfaces;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
* ML-DSA (FIPS 204) Public/Private Key provider.
* <p>
* ML-DSA keys have no dedicated type in {@code java.security.interfaces} on the Java versions this library
* supports, so the general {@link PublicKey} and {@link PrivateKey} types are used.
*/
public interface MLDSAKeyProvider extends KeyProvider<PublicKey, PrivateKey> {
}
Loading
Loading