1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use core::str::FromStr;
#[cfg(feature = "alloc")]
use crate::buffer::SecretBytes;
use crate::{alg::normalize_alg, buffer::WriteBuffer, error::Error};
pub trait KeySign: KeySigVerify {
fn write_signature(
&self,
message: &[u8],
sig_type: Option<SignatureType>,
out: &mut dyn WriteBuffer,
) -> Result<(), Error>;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn create_signature(
&self,
message: &[u8],
sig_type: Option<SignatureType>,
) -> Result<SecretBytes, Error> {
let mut buf = SecretBytes::with_capacity(128);
self.write_signature(message, sig_type, &mut buf)?;
Ok(buf)
}
}
pub trait KeySigVerify {
fn verify_signature(
&self,
message: &[u8],
signature: &[u8],
sig_type: Option<SignatureType>,
) -> Result<bool, Error>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SignatureType {
EdDSA,
ES256,
ES256K,
}
impl FromStr for SignatureType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match normalize_alg(s)? {
a if a == "eddsa" => Ok(Self::EdDSA),
a if a == "es256" => Ok(Self::ES256),
a if a == "es256k" => Ok(Self::ES256K),
_ => Err(err_msg!(Unsupported, "Unknown signature algorithm")),
}
}
}
impl SignatureType {
pub const fn signature_length(&self) -> usize {
match self {
Self::EdDSA | Self::ES256 | Self::ES256K => 64,
}
}
}