jsonprooftoken/jpa/
algs.rs

1// Copyright 2023 Fondazione LINKS
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{fmt, str::FromStr};
16
17use serde::{Deserialize, Serialize};
18
19#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
20#[allow(non_camel_case_types)]
21///Proof algorithms for Issued JWP,
22///see https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-proof-algorithms-08#name-initial-registry-contents
23pub enum ProofAlgorithm {
24    ///BBS using SHA-256
25    #[serde(rename = "BBS")]
26    BBS,
27    ///BBS using SHAKE-256, temporary name not included in the JPA draft 08
28    #[serde(rename = "BBS-SHAKE256")]
29    BBS_SHAKE256,
30    ///Single-Use JWP using ES256
31    #[serde(rename = "SU-ES256")]
32    SU_ES256,
33    ///Single-Use JWP using ES384
34    #[serde(rename = "SU-ES384")]
35    SU_ES384,
36    ///Single-Use JWP using ES512
37    #[serde(rename = "SU-ES512")]
38    SU_ES512,
39    ///MAC-H256
40    #[serde(rename = "MAC-H256")]
41    MAC_H256,
42    ///MAC-H384
43    #[serde(rename = "MAC-H384")]
44    MAC_H384,
45    ///MAC-H512
46    #[serde(rename = "MAC-H512")]
47    MAC_H512,
48    ///MAC-K25519
49    #[serde(rename = "MAC-K25519")]
50    MAC_K25519,
51    ///MAC-K448
52    #[serde(rename = "MAC-K448")]
53    MAC_K448,
54    ///MAC-H256K
55    #[serde(rename = "MAC-H256K")]
56    MAC_H256K,
57}
58
59impl Into<PresentationProofAlgorithm> for ProofAlgorithm {
60    fn into(self) -> PresentationProofAlgorithm {
61        match self {
62            ProofAlgorithm::BBS => PresentationProofAlgorithm::BBS,
63            ProofAlgorithm::BBS_SHAKE256 => PresentationProofAlgorithm::BBS_SHAKE256,
64            ProofAlgorithm::SU_ES256 => PresentationProofAlgorithm::SU_ES256,
65            ProofAlgorithm::SU_ES384 => PresentationProofAlgorithm::SU_ES384,
66            ProofAlgorithm::SU_ES512 => PresentationProofAlgorithm::SU_ES512,
67            ProofAlgorithm::MAC_H256 => PresentationProofAlgorithm::MAC_H256,
68            ProofAlgorithm::MAC_H384 => PresentationProofAlgorithm::MAC_H384,
69            ProofAlgorithm::MAC_H512 => PresentationProofAlgorithm::MAC_H512,
70            ProofAlgorithm::MAC_K25519 => PresentationProofAlgorithm::MAC_K25519,
71            ProofAlgorithm::MAC_K448 => PresentationProofAlgorithm::MAC_K448,
72            ProofAlgorithm::MAC_H256K => PresentationProofAlgorithm::MAC_H256K,
73        }
74    }
75}
76
77impl fmt::Display for ProofAlgorithm {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        let variant_str = match self {
80            ProofAlgorithm::BBS => "BBS",
81            ProofAlgorithm::BBS_SHAKE256 => "BBS-SHAKE256",
82            ProofAlgorithm::SU_ES256 => "SU-ES256",
83            ProofAlgorithm::SU_ES384 => "SU-ES384",
84            ProofAlgorithm::SU_ES512 => "SU-ES512",
85            ProofAlgorithm::MAC_H256 => "MAC-H256",
86            ProofAlgorithm::MAC_H384 => "MAC-H384",
87            ProofAlgorithm::MAC_H512 => "MAC-H512",
88            ProofAlgorithm::MAC_K25519 => "MAC-K25519",
89            ProofAlgorithm::MAC_K448 => "MAC-K448",
90            ProofAlgorithm::MAC_H256K => "MAC-H256K",
91            
92        };
93        write!(f, "{}", variant_str)
94    }
95}
96
97impl FromStr for ProofAlgorithm {
98    type Err = &'static str;
99
100    fn from_str(s: &str) -> Result<Self, Self::Err> {
101        match s {
102            "BBS" => Ok(ProofAlgorithm::BBS),
103            "BBS-SHAKE256" => Ok(ProofAlgorithm::BBS_SHAKE256),
104            "SU-ES256" => Ok(ProofAlgorithm::SU_ES256),
105            "SU-ES384" => Ok(ProofAlgorithm::SU_ES384),
106            "MAC-H256" => Ok(ProofAlgorithm::MAC_H256),
107            "MAC-H384" => Ok(ProofAlgorithm::MAC_H384),
108            "MAC-H512" => Ok(ProofAlgorithm::MAC_H512),
109            "MAC-K25519" => Ok(ProofAlgorithm::MAC_K25519),
110            "MAC-K448" => Ok(ProofAlgorithm::MAC_K448),
111            "MAC-H256K" => Ok(ProofAlgorithm::MAC_H256K),
112            _ => Err("Invalid proof algorithm"),
113        }
114    }
115}
116
117#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
118#[allow(non_camel_case_types)]
119///Proof algorithms for Presented JWP,
120///see https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-proof-algorithms-08#name-initial-registry-contents
121pub enum PresentationProofAlgorithm {
122    ///BBS using SHA-256
123    #[serde(rename = "BBS")]
124    BBS,
125    ///BBS using SHAKE-256, temporary name not included in the JPA draft 08
126    #[serde(rename = "BBS-SHAKE256")]
127    BBS_SHAKE256,
128    ///Single-Use JWP using ES256
129    #[serde(rename = "SU-ES256")]
130    SU_ES256,
131    ///Single-Use JWP using ES384
132    #[serde(rename = "SU-ES384")]
133    SU_ES384,
134    ///Single-Use JWP using ES512
135    #[serde(rename = "SU-ES512")]
136    SU_ES512,
137    ///MAC-H256
138    #[serde(rename = "MAC-H256")]
139    MAC_H256,
140    ///MAC-H384
141    #[serde(rename = "MAC-H384")]
142    MAC_H384,
143    ///MAC-H512
144    #[serde(rename = "MAC-H512")]
145    MAC_H512,
146    ///MAC-K25519
147    #[serde(rename = "MAC-K25519")]
148    MAC_K25519,
149    ///MAC-K448
150    #[serde(rename = "MAC-K448")]
151    MAC_K448,
152    ///MAC-H256K
153    #[serde(rename = "MAC-H256K")]
154    MAC_H256K,
155}
156
157impl fmt::Display for PresentationProofAlgorithm {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        let variant_str = match self {
160            PresentationProofAlgorithm::BBS => "BBS",
161            PresentationProofAlgorithm::BBS_SHAKE256 => "BBS-SHAKE256",
162            PresentationProofAlgorithm::SU_ES256 => "SU-ES256",
163            PresentationProofAlgorithm::SU_ES384 => "SU-ES384",
164            PresentationProofAlgorithm::SU_ES512 => "SU-ES512",
165            PresentationProofAlgorithm::MAC_H256 => "MAC-H256",
166            PresentationProofAlgorithm::MAC_H384 => "MAC-H384",
167            PresentationProofAlgorithm::MAC_H512 => "MAC-H512",
168            PresentationProofAlgorithm::MAC_K25519 => "MAC-K25519",
169            PresentationProofAlgorithm::MAC_K448 => "MAC-K448",
170            PresentationProofAlgorithm::MAC_H256K => "MAC-H256K",
171        };
172        write!(f, "{}", variant_str)
173    }
174}
175
176impl FromStr for PresentationProofAlgorithm {
177    type Err = &'static str;
178
179    fn from_str(s: &str) -> Result<Self, Self::Err> {
180        match s {
181            "BBS-PROOF" => Ok(PresentationProofAlgorithm::BBS),
182            "BBS-SHAKE256-PROOF" => Ok(PresentationProofAlgorithm::BBS_SHAKE256),
183            "SU-ES256" => Ok(PresentationProofAlgorithm::SU_ES256),
184            "SU-ES384" => Ok(PresentationProofAlgorithm::SU_ES384),
185            "MAC-H256" => Ok(PresentationProofAlgorithm::MAC_H256),
186            "MAC-H384" => Ok(PresentationProofAlgorithm::MAC_H384),
187            "MAC-H512" => Ok(PresentationProofAlgorithm::MAC_H512),
188            "MAC-K25519" => Ok(PresentationProofAlgorithm::MAC_K25519),
189            "MAC-K448" => Ok(PresentationProofAlgorithm::MAC_K448),
190            "MAC-H256K" => Ok(PresentationProofAlgorithm::MAC_H256K),
191            _ => Err("Invalid proof algorithm"),
192        }
193    }
194}