basil_cose/alg.rs
1// SPDX-FileCopyrightText: 2026 OpenBasil Contributors
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Closed algorithm enums and their COSE codepoints.
6//!
7//! Decode of any codepoint outside these enums is a
8//! [`DecodeError::UnknownAlgorithm`](crate::DecodeError::UnknownAlgorithm).
9//! There are no forward-compatible `Unknown` arms.
10
11/// Signature algorithms in the profile allow-set.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum SignatureAlgorithm {
14 /// `EdDSA` (COSE codepoint -8), Ed25519 keys.
15 EdDsa,
16 /// `ES256` (COSE codepoint -7): ECDSA with NIST P-256 and SHA-256.
17 Es256,
18}
19
20impl SignatureAlgorithm {
21 /// The COSE algorithm codepoint.
22 #[must_use]
23 pub const fn codepoint(self) -> i64 {
24 match self {
25 Self::EdDsa => -8,
26 Self::Es256 => -7,
27 }
28 }
29
30 /// Look a codepoint up in the allow-set.
31 #[must_use]
32 pub const fn from_codepoint(alg: i64) -> Option<Self> {
33 match alg {
34 -8 => Some(Self::EdDsa),
35 -7 => Some(Self::Es256),
36 _ => None,
37 }
38 }
39}
40
41/// Key-agreement algorithms in the profile allow-set.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43pub enum KeyAgreementAlgorithm {
44 /// ECDH-ES + HKDF-256 (COSE codepoint -25), X25519 keys.
45 EcdhEsHkdf256,
46}
47
48impl KeyAgreementAlgorithm {
49 /// The COSE algorithm codepoint.
50 #[must_use]
51 pub const fn codepoint(self) -> i64 {
52 match self {
53 Self::EcdhEsHkdf256 => -25,
54 }
55 }
56
57 /// Look a codepoint up in the allow-set.
58 #[must_use]
59 pub const fn from_codepoint(alg: i64) -> Option<Self> {
60 match alg {
61 -25 => Some(Self::EcdhEsHkdf256),
62 _ => None,
63 }
64 }
65}
66
67/// Content-encryption algorithms in the profile allow-set.
68///
69/// A parameter of every encrypting entry point: basil invocation v1 passes
70/// [`ContentAlgorithm::A256Gcm`]; clients may use
71/// [`ContentAlgorithm::ChaCha20Poly1305`].
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
73pub enum ContentAlgorithm {
74 /// AES-256-GCM (COSE codepoint 3).
75 A256Gcm,
76 /// `ChaCha20`-`Poly1305` (COSE codepoint 24).
77 ChaCha20Poly1305,
78}
79
80impl ContentAlgorithm {
81 /// The COSE algorithm codepoint.
82 #[must_use]
83 pub const fn codepoint(self) -> i64 {
84 match self {
85 Self::A256Gcm => 3,
86 Self::ChaCha20Poly1305 => 24,
87 }
88 }
89
90 /// Look a codepoint up in the allow-set.
91 #[must_use]
92 pub const fn from_codepoint(alg: i64) -> Option<Self> {
93 match alg {
94 3 => Some(Self::A256Gcm),
95 24 => Some(Self::ChaCha20Poly1305),
96 _ => None,
97 }
98 }
99
100 /// The content-encryption key length in bytes (256 bits for both).
101 #[must_use]
102 pub const fn key_len(self) -> usize {
103 32
104 }
105}