1use core::str::FromStr;
2
3use alloc::string::String;
4use serde::{Deserialize, Serialize};
5use strum::{Display, EnumIter, IntoStaticStr};
6
7use crate::{error::InvalidAlgorithmError, strings::to_upper_remove_seperators};
8
9use super::{Curve, KeyType};
10
11#[derive(
12 Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, IntoStaticStr, Display, EnumIter,
13)]
14#[serde(try_from = "String", into = "&str")]
15pub enum Algorithm {
16 #[strum(serialize = "HS256")]
18 Hs256,
19
20 #[strum(serialize = "HS384")]
22 Hs384,
23
24 #[strum(serialize = "HS512")]
26 Hs512,
27
28 #[strum(serialize = "ES256")]
30 Es256,
31
32 #[strum(serialize = "ES256K")]
34 Es256K,
35
36 #[strum(serialize = "ES384")]
38 Es384,
39
40 #[strum(serialize = "ES512")]
42 Es512,
43
44 #[strum(serialize = "RS256")]
46 Rs256,
47
48 #[strum(serialize = "RS384")]
50 Rs384,
51
52 #[strum(serialize = "RS512")]
54 Rs512,
55
56 #[strum(serialize = "PS256")]
58 Ps256,
59
60 #[strum(serialize = "PS384")]
62 Ps384,
63
64 #[strum(serialize = "PS512")]
66 Ps512,
67
68 #[strum(serialize = "EdDSA")]
71 EdDsa,
72
73 #[strum(serialize = "RSA-OAEP")]
75 RsaOaep,
76
77 #[strum(serialize = "RSA-OAEP-256")]
79 RsaOaep256,
80
81 #[strum(serialize = "A128KW")]
83 A128Kw,
84
85 #[strum(serialize = "A192KW")]
87 A192Kw,
88
89 #[strum(serialize = "A256KW")]
91 A256Kw,
92
93 #[strum(serialize = "A128GCM")]
95 A128Gcm,
96
97 #[strum(serialize = "A192GCM")]
99 A192Gcm,
100
101 #[strum(serialize = "A256GCM")]
103 A256Gcm,
104
105 #[strum(serialize = "A128CBC-HS256")]
107 A128CbcHs256,
108
109 #[strum(serialize = "A192CBC-HS384")]
111 A192CbcHs384,
112
113 #[strum(serialize = "A256CBC-HS512")]
115 A256CbcHs512,
116
117 #[strum(serialize = "C20PKW")]
119 C20Pkw,
120
121 #[strum(serialize = "XC20PKW")]
123 Xc20Pkw,
124}
125
126impl TryFrom<(KeyType, Curve)> for Algorithm {
127 type Error = String;
128
129 fn try_from((key_type, curve): (KeyType, Curve)) -> Result<Self, Self::Error> {
130 match key_type {
131 KeyType::Rsa => {
132 Err("RSA algorithms can not be determined by the key type and curve".into())
133 }
134 KeyType::Oct => Err(
135 "octet sequence algorithms can not be determined by the key type and curve".into(),
136 ),
137 KeyType::Ec => match curve {
138 Curve::P256 => Ok(Algorithm::Es256),
139 Curve::P384 => Ok(Algorithm::Es384),
140 _ => Err("unsupported curve".into()),
141 },
142 KeyType::Okp => match curve {
143 Curve::Ed25519 => Ok(Algorithm::EdDsa),
144 _ => Err("unsupported curve".into()),
145 },
146 }
147 }
148}
149#[cfg(feature = "dsa")]
150impl From<crate::dsa::Algorithm> for Algorithm {
151 fn from(alg: crate::dsa::Algorithm) -> Self {
152
153 match alg {
154 crate::dsa::Algorithm::Ed25519 => Algorithm::EdDsa,
155 crate::dsa::Algorithm::Es256 => Algorithm::Es256,
156 crate::dsa::Algorithm::Es384 => Algorithm::Es384,
157 }
158 }
159}
160
161impl FromStr for Algorithm {
162 type Err = InvalidAlgorithmError;
163 fn from_str(s: &str) -> Result<Self, Self::Err> {
164 match to_upper_remove_seperators(s).as_str() {
165 "HS256" => Ok(Algorithm::Hs256),
166 "HS384" => Ok(Algorithm::Hs384),
167 "HS512" => Ok(Algorithm::Hs512),
168 "ES256" => Ok(Algorithm::Es256),
169 "ES256K" => Ok(Algorithm::Es256K),
170 "ES384" => Ok(Algorithm::Es384),
171 "ES512" => Ok(Algorithm::Es512),
172 "RS256" => Ok(Algorithm::Rs256),
173 "RS384" => Ok(Algorithm::Rs384),
174 "RS512" => Ok(Algorithm::Rs512),
175 "PS256" => Ok(Algorithm::Ps256),
176 "PS384" => Ok(Algorithm::Ps384),
177 "PS512" => Ok(Algorithm::Ps512),
178 "EDDSA" => Ok(Algorithm::EdDsa),
179 "RSAOAEP" => Ok(Algorithm::RsaOaep),
180 "RSAOAEP256" => Ok(Algorithm::RsaOaep256),
181 "A128KW" => Ok(Algorithm::A128Kw),
182 "A192KW" => Ok(Algorithm::A192Kw),
183 "A256KW" => Ok(Algorithm::A256Kw),
184 "A128GCM" => Ok(Algorithm::A128Gcm),
185 "A192GCM" => Ok(Algorithm::A192Gcm),
186 "A256GCM" => Ok(Algorithm::A256Gcm),
187 "A128CBCHS256" => Ok(Algorithm::A128CbcHs256),
188 "A192CBCHS384" => Ok(Algorithm::A192CbcHs384),
189 "A256CBCHS512" => Ok(Algorithm::A256CbcHs512),
190 "C20PKW" => Ok(Algorithm::C20Pkw),
191 "XC20PKW" => Ok(Algorithm::Xc20Pkw),
192
193 _ => Err(s.into()),
194 }
195 }
196}
197impl TryFrom<String> for Algorithm {
198 type Error = InvalidAlgorithmError;
199
200 fn try_from(value: String) -> Result<Self, Self::Error> {
201 Algorithm::from_str(value.as_str())
202 }
203}