Skip to main content

jose_rs/
algorithm.rs

1//! JOSE algorithm identifiers and mapping to kryptering enums.
2//!
3//! Parse case-sensitive wire names from configuration or a JOSE header:
4//!
5//! ```
6//! use jose_rs::{JweAlgorithm, JweEncryption, JwsAlgorithm};
7//!
8//! let signature: JwsAlgorithm = "Ed448".parse()?;
9//! let key_management: JweAlgorithm = "RSA-OAEP-256".parse()?;
10//! let encryption: JweEncryption = "A256GCM".parse()?;
11//! # Ok::<(), jose_rs::JoseError>(())
12//! ```
13//!
14//! Parsing identifies the algorithm. Verification and decryption still require
15//! the caller's algorithm policy and a suitable key.
16
17use crate::error::{JoseError, Result};
18use serde::{Deserialize, Serialize};
19use std::str::FromStr;
20
21// ---------------------------------------------------------------------------
22// JWS signature algorithms (RFC 7518 §3)
23// ---------------------------------------------------------------------------
24
25/// JWS signature algorithm.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27pub enum JwsAlgorithm {
28    /// HMAC using SHA-256
29    HS256,
30    /// HMAC using SHA-384
31    HS384,
32    /// HMAC using SHA-512
33    HS512,
34    /// RSASSA-PKCS1-v1_5 using SHA-256
35    RS256,
36    /// RSASSA-PKCS1-v1_5 using SHA-384
37    RS384,
38    /// RSASSA-PKCS1-v1_5 using SHA-512
39    RS512,
40    /// RSASSA-PSS using SHA-256
41    PS256,
42    /// RSASSA-PSS using SHA-384
43    PS384,
44    /// RSASSA-PSS using SHA-512
45    PS512,
46    /// ECDSA using P-256 and SHA-256
47    ES256,
48    /// ECDSA using P-384 and SHA-384
49    ES384,
50    /// ECDSA using P-521 and SHA-512
51    ES512,
52    /// Legacy curve-dependent EdDSA identifier (Ed25519 or Ed448).
53    /// Distinct from fully specified names in application algorithm allowlists.
54    EdDSA,
55    /// Fully specified Ed25519 identifier (RFC 9864 section 2.2).
56    /// Uses the same primitive as [`Self::EdDSA`] without aliasing its wire name.
57    Ed25519,
58    /// Pure Ed448 with an empty context (RFC 9864 section 2.2).
59    Ed448,
60    /// ECDSA using secp256k1 and SHA-256
61    ES256K,
62    /// ML-DSA-44 (FIPS 204) post-quantum signature using the RFC 9964 AKP form.
63    #[cfg(feature = "post-quantum")]
64    #[serde(rename = "ML-DSA-44")]
65    MlDsa44,
66    /// ML-DSA-65 (FIPS 204) post-quantum signature using the RFC 9964 AKP form.
67    #[cfg(feature = "post-quantum")]
68    #[serde(rename = "ML-DSA-65")]
69    MlDsa65,
70    /// ML-DSA-87 (FIPS 204) post-quantum signature using the RFC 9964 AKP form.
71    #[cfg(feature = "post-quantum")]
72    #[serde(rename = "ML-DSA-87")]
73    MlDsa87,
74    /// Composite ML-DSA-44 and ECDSA P-256 from draft-ietf-jose-pq-composite-sigs-07.
75    #[cfg(feature = "post-quantum")]
76    #[serde(rename = "ML-DSA-44-ES256")]
77    MlDsa44Es256,
78    /// Composite ML-DSA-65 and ECDSA P-256 from draft-ietf-jose-pq-composite-sigs-07.
79    #[cfg(feature = "post-quantum")]
80    #[serde(rename = "ML-DSA-65-ES256")]
81    MlDsa65Es256,
82    /// Composite ML-DSA-65 and ECDSA P-384 from draft-ietf-jose-pq-composite-sigs-07.
83    #[cfg(feature = "post-quantum")]
84    #[serde(rename = "ML-DSA-65-ES384")]
85    MlDsa65Es384,
86    /// Composite ML-DSA-87 and ECDSA P-384 from draft-ietf-jose-pq-composite-sigs-07.
87    #[cfg(feature = "post-quantum")]
88    #[serde(rename = "ML-DSA-87-ES384")]
89    MlDsa87Es384,
90    /// Composite ML-DSA-44 and Ed25519 from draft-ietf-jose-pq-composite-sigs-07.
91    #[cfg(feature = "post-quantum")]
92    #[serde(rename = "ML-DSA-44-Ed25519")]
93    MlDsa44Ed25519,
94    /// No digital signature (DANGEROUS -- only available with the `deprecated` feature)
95    #[cfg(feature = "deprecated")]
96    None,
97}
98
99impl JwsAlgorithm {
100    /// Map to the corresponding kryptering signature algorithm.
101    ///
102    /// Returns `Err` for algorithms that kryptering does not yet support
103    /// (ES256K) or that have no meaningful cryptographic operation (None).
104    /// Without key context, `EdDSA` retains its historical Ed25519 mapping.
105    /// JWK APIs dispatch it on the key curve; raw Ed448 signers/verifiers also
106    /// accept `EdDSA`. Use exact JOSE enum values for application allowlists.
107    pub fn to_crypto(self) -> Result<kryptering::SignatureAlgorithm> {
108        use kryptering::{EcCurve, HashAlgorithm, SignatureAlgorithm};
109        match self {
110            Self::HS256 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha256)),
111            Self::HS384 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha384)),
112            Self::HS512 => Ok(SignatureAlgorithm::Hmac(HashAlgorithm::Sha512)),
113            Self::RS256 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha256)),
114            Self::RS384 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha384)),
115            Self::RS512 => Ok(SignatureAlgorithm::RsaPkcs1v15(HashAlgorithm::Sha512)),
116            Self::PS256 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha256)),
117            Self::PS384 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha384)),
118            Self::PS512 => Ok(SignatureAlgorithm::RsaPss(HashAlgorithm::Sha512)),
119            Self::ES256 => Ok(SignatureAlgorithm::Ecdsa(
120                EcCurve::P256,
121                HashAlgorithm::Sha256,
122            )),
123            Self::ES384 => Ok(SignatureAlgorithm::Ecdsa(
124                EcCurve::P384,
125                HashAlgorithm::Sha384,
126            )),
127            Self::ES512 => Ok(SignatureAlgorithm::Ecdsa(
128                EcCurve::P521,
129                HashAlgorithm::Sha512,
130            )),
131            Self::EdDSA | Self::Ed25519 => Ok(SignatureAlgorithm::Ed25519),
132            Self::Ed448 => Ok(SignatureAlgorithm::Ed448),
133            Self::ES256K => Err(JoseError::UnsupportedAlgorithm(
134                "ES256K (secp256k1) is not yet supported by kryptering".into(),
135            )),
136            #[cfg(feature = "post-quantum")]
137            Self::MlDsa44 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa44)),
138            #[cfg(feature = "post-quantum")]
139            Self::MlDsa65 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa65)),
140            #[cfg(feature = "post-quantum")]
141            Self::MlDsa87 => Ok(SignatureAlgorithm::MlDsa(kryptering::MlDsaVariant::MlDsa87)),
142            #[cfg(feature = "post-quantum")]
143            Self::MlDsa44Es256 => Ok(SignatureAlgorithm::CompositeMlDsa(
144                kryptering::CompositeMlDsaVariant::MlDsa44Es256,
145            )),
146            #[cfg(feature = "post-quantum")]
147            Self::MlDsa65Es256 => Ok(SignatureAlgorithm::CompositeMlDsa(
148                kryptering::CompositeMlDsaVariant::MlDsa65Es256,
149            )),
150            #[cfg(feature = "post-quantum")]
151            Self::MlDsa65Es384 => Ok(SignatureAlgorithm::CompositeMlDsa(
152                kryptering::CompositeMlDsaVariant::MlDsa65Es384,
153            )),
154            #[cfg(feature = "post-quantum")]
155            Self::MlDsa87Es384 => Ok(SignatureAlgorithm::CompositeMlDsa(
156                kryptering::CompositeMlDsaVariant::MlDsa87Es384,
157            )),
158            #[cfg(feature = "post-quantum")]
159            Self::MlDsa44Ed25519 => Ok(SignatureAlgorithm::CompositeMlDsa(
160                kryptering::CompositeMlDsaVariant::MlDsa44Ed25519,
161            )),
162            #[cfg(feature = "deprecated")]
163            Self::None => Err(JoseError::UnsupportedAlgorithm(
164                "\"none\" algorithm has no cryptographic operation".into(),
165            )),
166        }
167    }
168
169    /// Resolve a JOSE wire algorithm against a concrete backend primitive.
170    /// EdDSA is polymorphic; explicit names remain bound to one curve.
171    pub(crate) fn matches_crypto(self, actual: kryptering::SignatureAlgorithm) -> Result<bool> {
172        if self == Self::EdDSA && actual == kryptering::SignatureAlgorithm::Ed448 {
173            return Ok(true);
174        }
175        Ok(self.to_crypto()? == actual)
176    }
177
178    /// Select the primitive using the JWK curve for legacy EdDSA.
179    pub(crate) fn crypto_for_jwk(
180        self,
181        jwk: &crate::jwk::Jwk,
182    ) -> Result<kryptering::SignatureAlgorithm> {
183        if self == Self::EdDSA && jwk.kty == "OKP" && jwk.crv.as_deref() == Some("Ed448") {
184            return Ok(kryptering::SignatureAlgorithm::Ed448);
185        }
186        self.to_crypto()
187    }
188
189    /// Parse an exact JOSE `alg` name, also available through [`str::parse`].
190    ///
191    /// # Errors
192    ///
193    /// Returns [`JoseError::UnsupportedAlgorithm`] if this build does not recognize the name.
194    #[allow(
195        clippy::should_implement_trait,
196        reason = "retain the inherent API for callers without FromStr in scope"
197    )]
198    pub fn from_str(s: &str) -> Result<Self> {
199        match s {
200            "HS256" => Ok(Self::HS256),
201            "HS384" => Ok(Self::HS384),
202            "HS512" => Ok(Self::HS512),
203            "RS256" => Ok(Self::RS256),
204            "RS384" => Ok(Self::RS384),
205            "RS512" => Ok(Self::RS512),
206            "PS256" => Ok(Self::PS256),
207            "PS384" => Ok(Self::PS384),
208            "PS512" => Ok(Self::PS512),
209            "ES256" => Ok(Self::ES256),
210            "ES384" => Ok(Self::ES384),
211            "ES512" => Ok(Self::ES512),
212            "EdDSA" => Ok(Self::EdDSA),
213            "Ed25519" => Ok(Self::Ed25519),
214            "Ed448" => Ok(Self::Ed448),
215            "ES256K" => Ok(Self::ES256K),
216            #[cfg(feature = "post-quantum")]
217            "ML-DSA-44" => Ok(Self::MlDsa44),
218            #[cfg(feature = "post-quantum")]
219            "ML-DSA-65" => Ok(Self::MlDsa65),
220            #[cfg(feature = "post-quantum")]
221            "ML-DSA-87" => Ok(Self::MlDsa87),
222            #[cfg(feature = "post-quantum")]
223            "ML-DSA-44-ES256" => Ok(Self::MlDsa44Es256),
224            #[cfg(feature = "post-quantum")]
225            "ML-DSA-65-ES256" => Ok(Self::MlDsa65Es256),
226            #[cfg(feature = "post-quantum")]
227            "ML-DSA-65-ES384" => Ok(Self::MlDsa65Es384),
228            #[cfg(feature = "post-quantum")]
229            "ML-DSA-87-ES384" => Ok(Self::MlDsa87Es384),
230            #[cfg(feature = "post-quantum")]
231            "ML-DSA-44-Ed25519" => Ok(Self::MlDsa44Ed25519),
232            #[cfg(feature = "deprecated")]
233            "none" => Ok(Self::None),
234            other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
235        }
236    }
237
238    /// Return the `alg` header string.
239    pub fn as_str(self) -> &'static str {
240        match self {
241            Self::HS256 => "HS256",
242            Self::HS384 => "HS384",
243            Self::HS512 => "HS512",
244            Self::RS256 => "RS256",
245            Self::RS384 => "RS384",
246            Self::RS512 => "RS512",
247            Self::PS256 => "PS256",
248            Self::PS384 => "PS384",
249            Self::PS512 => "PS512",
250            Self::ES256 => "ES256",
251            Self::ES384 => "ES384",
252            Self::ES512 => "ES512",
253            Self::EdDSA => "EdDSA",
254            Self::Ed25519 => "Ed25519",
255            Self::Ed448 => "Ed448",
256            Self::ES256K => "ES256K",
257            #[cfg(feature = "post-quantum")]
258            Self::MlDsa44 => "ML-DSA-44",
259            #[cfg(feature = "post-quantum")]
260            Self::MlDsa65 => "ML-DSA-65",
261            #[cfg(feature = "post-quantum")]
262            Self::MlDsa87 => "ML-DSA-87",
263            #[cfg(feature = "post-quantum")]
264            Self::MlDsa44Es256 => "ML-DSA-44-ES256",
265            #[cfg(feature = "post-quantum")]
266            Self::MlDsa65Es256 => "ML-DSA-65-ES256",
267            #[cfg(feature = "post-quantum")]
268            Self::MlDsa65Es384 => "ML-DSA-65-ES384",
269            #[cfg(feature = "post-quantum")]
270            Self::MlDsa87Es384 => "ML-DSA-87-ES384",
271            #[cfg(feature = "post-quantum")]
272            Self::MlDsa44Ed25519 => "ML-DSA-44-Ed25519",
273            #[cfg(feature = "deprecated")]
274            Self::None => "none",
275        }
276    }
277}
278
279impl FromStr for JwsAlgorithm {
280    type Err = JoseError;
281
282    fn from_str(s: &str) -> Result<Self> {
283        JwsAlgorithm::from_str(s)
284    }
285}
286
287impl std::fmt::Display for JwsAlgorithm {
288    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289        f.write_str(self.as_str())
290    }
291}
292
293// ---------------------------------------------------------------------------
294// JWE key management algorithms (RFC 7518 §4)
295// ---------------------------------------------------------------------------
296
297/// JWE key management algorithm.
298#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
299pub enum JweAlgorithm {
300    /// Direct use of a shared symmetric key
301    #[serde(rename = "dir")]
302    Dir,
303    /// AES Key Wrap (128-bit)
304    A128KW,
305    /// AES Key Wrap (192-bit)
306    A192KW,
307    /// AES Key Wrap (256-bit)
308    A256KW,
309    /// AES GCM key wrapping with a 128-bit key (RFC 7518 §4.7)
310    #[serde(rename = "A128GCMKW")]
311    A128Gcmkw,
312    /// AES GCM key wrapping with a 192-bit key (RFC 7518 §4.7)
313    #[serde(rename = "A192GCMKW")]
314    A192Gcmkw,
315    /// AES GCM key wrapping with a 256-bit key (RFC 7518 §4.7)
316    #[serde(rename = "A256GCMKW")]
317    A256Gcmkw,
318    /// RSAES-OAEP using default parameters (SHA-1 — only available with the `rsa-oaep` feature)
319    #[cfg(feature = "rsa-oaep")]
320    #[serde(rename = "RSA-OAEP")]
321    RsaOaep,
322    /// RSAES-OAEP using SHA-256 and MGF1 with SHA-256
323    #[serde(rename = "RSA-OAEP-256")]
324    RsaOaep256,
325    /// ECDH-ES
326    #[serde(rename = "ECDH-ES")]
327    EcdhEs,
328    /// ECDH-ES + A128KW
329    #[serde(rename = "ECDH-ES+A128KW")]
330    EcdhEsA128kw,
331    /// ECDH-ES + A192KW
332    #[serde(rename = "ECDH-ES+A192KW")]
333    EcdhEsA192kw,
334    /// ECDH-ES + A256KW
335    #[serde(rename = "ECDH-ES+A256KW")]
336    EcdhEsA256kw,
337    /// PBES2-HS256+A128KW
338    #[serde(rename = "PBES2-HS256+A128KW")]
339    Pbes2Hs256A128kw,
340    /// PBES2-HS384+A192KW
341    #[serde(rename = "PBES2-HS384+A192KW")]
342    Pbes2Hs384A192kw,
343    /// PBES2-HS512+A256KW
344    #[serde(rename = "PBES2-HS512+A256KW")]
345    Pbes2Hs512A256kw,
346}
347
348impl JweAlgorithm {
349    /// Parse an exact JOSE `alg` name, also available through [`str::parse`].
350    ///
351    /// # Errors
352    ///
353    /// Returns [`JoseError::UnsupportedAlgorithm`] if this build does not recognize the name.
354    #[allow(
355        clippy::should_implement_trait,
356        reason = "retain the inherent API for callers without FromStr in scope"
357    )]
358    pub fn from_str(s: &str) -> Result<Self> {
359        match s {
360            "dir" => Ok(Self::Dir),
361            "A128KW" => Ok(Self::A128KW),
362            "A192KW" => Ok(Self::A192KW),
363            "A256KW" => Ok(Self::A256KW),
364            "A128GCMKW" => Ok(Self::A128Gcmkw),
365            "A192GCMKW" => Ok(Self::A192Gcmkw),
366            "A256GCMKW" => Ok(Self::A256Gcmkw),
367            #[cfg(feature = "rsa-oaep")]
368            "RSA-OAEP" => Ok(Self::RsaOaep),
369            "RSA-OAEP-256" => Ok(Self::RsaOaep256),
370            "ECDH-ES" => Ok(Self::EcdhEs),
371            "ECDH-ES+A128KW" => Ok(Self::EcdhEsA128kw),
372            "ECDH-ES+A192KW" => Ok(Self::EcdhEsA192kw),
373            "ECDH-ES+A256KW" => Ok(Self::EcdhEsA256kw),
374            "PBES2-HS256+A128KW" => Ok(Self::Pbes2Hs256A128kw),
375            "PBES2-HS384+A192KW" => Ok(Self::Pbes2Hs384A192kw),
376            "PBES2-HS512+A256KW" => Ok(Self::Pbes2Hs512A256kw),
377            other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
378        }
379    }
380
381    /// Return the `alg` header string.
382    pub fn as_str(self) -> &'static str {
383        match self {
384            Self::Dir => "dir",
385            Self::A128KW => "A128KW",
386            Self::A192KW => "A192KW",
387            Self::A256KW => "A256KW",
388            Self::A128Gcmkw => "A128GCMKW",
389            Self::A192Gcmkw => "A192GCMKW",
390            Self::A256Gcmkw => "A256GCMKW",
391            #[cfg(feature = "rsa-oaep")]
392            Self::RsaOaep => "RSA-OAEP",
393            Self::RsaOaep256 => "RSA-OAEP-256",
394            Self::EcdhEs => "ECDH-ES",
395            Self::EcdhEsA128kw => "ECDH-ES+A128KW",
396            Self::EcdhEsA192kw => "ECDH-ES+A192KW",
397            Self::EcdhEsA256kw => "ECDH-ES+A256KW",
398            Self::Pbes2Hs256A128kw => "PBES2-HS256+A128KW",
399            Self::Pbes2Hs384A192kw => "PBES2-HS384+A192KW",
400            Self::Pbes2Hs512A256kw => "PBES2-HS512+A256KW",
401        }
402    }
403}
404
405impl FromStr for JweAlgorithm {
406    type Err = JoseError;
407
408    fn from_str(s: &str) -> Result<Self> {
409        JweAlgorithm::from_str(s)
410    }
411}
412
413// ---------------------------------------------------------------------------
414// JWE content encryption algorithms (RFC 7518 §5)
415// ---------------------------------------------------------------------------
416
417/// JWE content encryption algorithm.
418#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
419pub enum JweEncryption {
420    /// AES-CBC using 128-bit key with HMAC SHA-256
421    #[serde(rename = "A128CBC-HS256")]
422    A128CbcHs256,
423    /// AES-CBC using 192-bit key with HMAC SHA-384
424    #[serde(rename = "A192CBC-HS384")]
425    A192CbcHs384,
426    /// AES-CBC using 256-bit key with HMAC SHA-512
427    #[serde(rename = "A256CBC-HS512")]
428    A256CbcHs512,
429    /// AES-GCM using 128-bit key
430    A128GCM,
431    /// AES-GCM using 192-bit key
432    A192GCM,
433    /// AES-GCM using 256-bit key
434    A256GCM,
435}
436
437impl JweEncryption {
438    /// Content Encryption Key size in bytes.
439    pub fn cek_size(self) -> usize {
440        match self {
441            Self::A128CbcHs256 => 32, // 128-bit AES + 128-bit HMAC
442            Self::A192CbcHs384 => 48, // 192-bit AES + 192-bit HMAC
443            Self::A256CbcHs512 => 64, // 256-bit AES + 256-bit HMAC
444            Self::A128GCM => 16,
445            Self::A192GCM => 24,
446            Self::A256GCM => 32,
447        }
448    }
449
450    /// Parse an exact JOSE `enc` name, also available through [`str::parse`].
451    ///
452    /// # Errors
453    ///
454    /// Returns [`JoseError::UnsupportedAlgorithm`] if this build does not recognize the name.
455    #[allow(
456        clippy::should_implement_trait,
457        reason = "retain the inherent API for callers without FromStr in scope"
458    )]
459    pub fn from_str(s: &str) -> Result<Self> {
460        match s {
461            "A128CBC-HS256" => Ok(Self::A128CbcHs256),
462            "A192CBC-HS384" => Ok(Self::A192CbcHs384),
463            "A256CBC-HS512" => Ok(Self::A256CbcHs512),
464            "A128GCM" => Ok(Self::A128GCM),
465            "A192GCM" => Ok(Self::A192GCM),
466            "A256GCM" => Ok(Self::A256GCM),
467            other => Err(JoseError::UnsupportedAlgorithm(other.to_string())),
468        }
469    }
470
471    /// Return the `enc` header string.
472    pub fn as_str(self) -> &'static str {
473        match self {
474            Self::A128CbcHs256 => "A128CBC-HS256",
475            Self::A192CbcHs384 => "A192CBC-HS384",
476            Self::A256CbcHs512 => "A256CBC-HS512",
477            Self::A128GCM => "A128GCM",
478            Self::A192GCM => "A192GCM",
479            Self::A256GCM => "A256GCM",
480        }
481    }
482}
483
484impl FromStr for JweEncryption {
485    type Err = JoseError;
486
487    fn from_str(s: &str) -> Result<Self> {
488        JweEncryption::from_str(s)
489    }
490}
491
492#[cfg(test)]
493mod tests {
494    use super::*;
495
496    #[test]
497    fn es256k_from_str() {
498        let alg = JwsAlgorithm::from_str("ES256K").unwrap();
499        assert_eq!(alg, JwsAlgorithm::ES256K);
500    }
501
502    #[test]
503    fn es256k_as_str() {
504        assert_eq!(JwsAlgorithm::ES256K.as_str(), "ES256K");
505    }
506
507    #[test]
508    fn es256k_to_crypto_returns_error() {
509        let result = JwsAlgorithm::ES256K.to_crypto();
510        assert!(result.is_err());
511    }
512
513    #[test]
514    fn es256k_roundtrip() {
515        let alg = JwsAlgorithm::from_str(JwsAlgorithm::ES256K.as_str()).unwrap();
516        assert_eq!(alg, JwsAlgorithm::ES256K);
517    }
518
519    #[test]
520    fn existing_algorithms_to_crypto_still_work() {
521        // Verify the signature change didn't break existing algorithms.
522        assert!(JwsAlgorithm::HS256.to_crypto().is_ok());
523        assert!(JwsAlgorithm::RS256.to_crypto().is_ok());
524        assert!(JwsAlgorithm::ES256.to_crypto().is_ok());
525        assert!(JwsAlgorithm::EdDSA.to_crypto().is_ok());
526    }
527
528    #[cfg(feature = "post-quantum")]
529    #[test]
530    fn composite_algorithms_roundtrip_and_map_to_kryptering() {
531        use kryptering::CompositeMlDsaVariant as Variant;
532
533        let cases = [
534            (
535                "ML-DSA-44-ES256",
536                JwsAlgorithm::MlDsa44Es256,
537                Variant::MlDsa44Es256,
538            ),
539            (
540                "ML-DSA-65-ES256",
541                JwsAlgorithm::MlDsa65Es256,
542                Variant::MlDsa65Es256,
543            ),
544            (
545                "ML-DSA-65-ES384",
546                JwsAlgorithm::MlDsa65Es384,
547                Variant::MlDsa65Es384,
548            ),
549            (
550                "ML-DSA-87-ES384",
551                JwsAlgorithm::MlDsa87Es384,
552                Variant::MlDsa87Es384,
553            ),
554            (
555                "ML-DSA-44-Ed25519",
556                JwsAlgorithm::MlDsa44Ed25519,
557                Variant::MlDsa44Ed25519,
558            ),
559        ];
560
561        for (name, jose, crypto) in cases {
562            assert_eq!(JwsAlgorithm::from_str(name).unwrap(), jose);
563            assert_eq!(jose.as_str(), name);
564            assert_eq!(
565                jose.to_crypto().unwrap(),
566                kryptering::SignatureAlgorithm::CompositeMlDsa(crypto)
567            );
568            assert_eq!(serde_json::to_string(&jose).unwrap(), format!("\"{name}\""));
569        }
570    }
571}