1use crate::error::{JoseError, Result};
18use serde::{Deserialize, Serialize};
19use std::str::FromStr;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27pub enum JwsAlgorithm {
28 HS256,
30 HS384,
32 HS512,
34 RS256,
36 RS384,
38 RS512,
40 PS256,
42 PS384,
44 PS512,
46 ES256,
48 ES384,
50 ES512,
52 EdDSA,
55 Ed25519,
58 Ed448,
60 ES256K,
62 #[cfg(feature = "post-quantum")]
64 #[serde(rename = "ML-DSA-44")]
65 MlDsa44,
66 #[cfg(feature = "post-quantum")]
68 #[serde(rename = "ML-DSA-65")]
69 MlDsa65,
70 #[cfg(feature = "post-quantum")]
72 #[serde(rename = "ML-DSA-87")]
73 MlDsa87,
74 #[cfg(feature = "post-quantum")]
76 #[serde(rename = "ML-DSA-44-ES256")]
77 MlDsa44Es256,
78 #[cfg(feature = "post-quantum")]
80 #[serde(rename = "ML-DSA-65-ES256")]
81 MlDsa65Es256,
82 #[cfg(feature = "post-quantum")]
84 #[serde(rename = "ML-DSA-65-ES384")]
85 MlDsa65Es384,
86 #[cfg(feature = "post-quantum")]
88 #[serde(rename = "ML-DSA-87-ES384")]
89 MlDsa87Es384,
90 #[cfg(feature = "post-quantum")]
92 #[serde(rename = "ML-DSA-44-Ed25519")]
93 MlDsa44Ed25519,
94 #[cfg(feature = "deprecated")]
96 None,
97}
98
99impl JwsAlgorithm {
100 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 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 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 #[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 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
299pub enum JweAlgorithm {
300 #[serde(rename = "dir")]
302 Dir,
303 A128KW,
305 A192KW,
307 A256KW,
309 #[serde(rename = "A128GCMKW")]
311 A128Gcmkw,
312 #[serde(rename = "A192GCMKW")]
314 A192Gcmkw,
315 #[serde(rename = "A256GCMKW")]
317 A256Gcmkw,
318 #[cfg(feature = "rsa-oaep")]
320 #[serde(rename = "RSA-OAEP")]
321 RsaOaep,
322 #[serde(rename = "RSA-OAEP-256")]
324 RsaOaep256,
325 #[serde(rename = "ECDH-ES")]
327 EcdhEs,
328 #[serde(rename = "ECDH-ES+A128KW")]
330 EcdhEsA128kw,
331 #[serde(rename = "ECDH-ES+A192KW")]
333 EcdhEsA192kw,
334 #[serde(rename = "ECDH-ES+A256KW")]
336 EcdhEsA256kw,
337 #[serde(rename = "PBES2-HS256+A128KW")]
339 Pbes2Hs256A128kw,
340 #[serde(rename = "PBES2-HS384+A192KW")]
342 Pbes2Hs384A192kw,
343 #[serde(rename = "PBES2-HS512+A256KW")]
345 Pbes2Hs512A256kw,
346}
347
348impl JweAlgorithm {
349 #[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 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
419pub enum JweEncryption {
420 #[serde(rename = "A128CBC-HS256")]
422 A128CbcHs256,
423 #[serde(rename = "A192CBC-HS384")]
425 A192CbcHs384,
426 #[serde(rename = "A256CBC-HS512")]
428 A256CbcHs512,
429 A128GCM,
431 A192GCM,
433 A256GCM,
435}
436
437impl JweEncryption {
438 pub fn cek_size(self) -> usize {
440 match self {
441 Self::A128CbcHs256 => 32, Self::A192CbcHs384 => 48, Self::A256CbcHs512 => 64, Self::A128GCM => 16,
445 Self::A192GCM => 24,
446 Self::A256GCM => 32,
447 }
448 }
449
450 #[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 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 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}