identity_jose/jwk/jwk_akp.rs
1// Copyright 2020-2025 IOTA Stiftung, Fondazione Links
2// SPDX-License-Identifier: Apache-2.0
3
4// =============================================================================
5// Algorithm Key Pair (AKP) key parameters for Post-quantum algorithm
6// =============================================================================
7
8use zeroize::Zeroize;
9
10use crate::jwk::JwkParams;
11
12use super::JwkType;
13
14/// Parameters for Post-Quantum algorithm keys
15///
16/// [More Info](https://datatracker.ietf.org/doc/html/draft-ietf-cose-dilithium-06)
17#[derive(
18 Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize, serde::Serialize, Zeroize,
19)]
20#[zeroize(drop)]
21pub struct JwkParamsAkp {
22 /// The public key as a base64url-encoded value.
23 #[serde(rename = "pub")]
24 pub public: String, // Public Key
25 /// The private key as a base64url-encoded value.
26 #[serde(skip_serializing_if = "Option::is_none", rename = "priv")]
27 pub private: Option<String>, // Private Key
28}
29
30impl JwkParamsAkp {
31 /// Creates new JWK AKP Params.
32 pub const fn new() -> Self {
33 Self {
34 public: String::new(),
35 private: None,
36 }
37 }
38
39 /// Returns the key type `kty`.
40 pub const fn kty(&self) -> JwkType {
41 JwkType::Akp
42 }
43
44 /// Returns a clone with _all_ private key components unset.
45 pub fn to_public(&self) -> Self {
46 Self {
47 public: self.public.clone(),
48 private: None,
49 }
50 }
51
52 /// Returns `true` if _all_ private key components of the key are unset, `false` otherwise.
53 pub fn is_public(&self) -> bool {
54 self.private.is_none()
55 }
56
57 /// Returns `true` if _all_ private key components of the key are set, `false` otherwise.
58 pub fn is_private(&self) -> bool {
59 self.private.is_some()
60 }
61
62 /// Unsets private key component.
63 pub fn strip_private(&mut self) {
64 self.private = None;
65 }
66
67 /// Returns this key with the private key components unset.
68 pub fn into_public(mut self) -> Self {
69 self.strip_private();
70 self
71 }
72}
73
74impl From<JwkParamsAkp> for JwkParams {
75 fn from(other: JwkParamsAkp) -> Self {
76 JwkParams::Akp(other)
77 }
78}