/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rsa-0.9.10/src/pss/signature.rs
Line | Count | Source |
1 | | pub use ::signature::SignatureEncoding; |
2 | | use spki::{ |
3 | | der::{asn1::BitString, Result as DerResult}, |
4 | | SignatureBitStringEncoding, |
5 | | }; |
6 | | |
7 | | use crate::algorithms::pad::uint_to_be_pad; |
8 | | use alloc::{boxed::Box, string::ToString}; |
9 | | use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex}; |
10 | | use num_bigint::BigUint; |
11 | | |
12 | | /// RSASSA-PSS signatures as described in [RFC8017 § 8.1]. |
13 | | /// |
14 | | /// [RFC8017 § 8.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-8.1 |
15 | | #[derive(Clone, PartialEq, Eq)] |
16 | | pub struct Signature { |
17 | | pub(super) inner: BigUint, |
18 | | pub(super) len: usize, |
19 | | } |
20 | | |
21 | | impl SignatureEncoding for Signature { |
22 | | type Repr = Box<[u8]>; |
23 | | } |
24 | | |
25 | | impl SignatureBitStringEncoding for Signature { |
26 | 0 | fn to_bitstring(&self) -> DerResult<BitString> { |
27 | 0 | BitString::new(0, self.to_vec()) |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl TryFrom<&[u8]> for Signature { |
32 | | type Error = signature::Error; |
33 | | |
34 | 0 | fn try_from(bytes: &[u8]) -> signature::Result<Self> { |
35 | 0 | Ok(Self { |
36 | 0 | len: bytes.len(), |
37 | 0 | inner: BigUint::from_bytes_be(bytes), |
38 | 0 | }) |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl From<Signature> for Box<[u8]> { |
43 | 0 | fn from(signature: Signature) -> Box<[u8]> { |
44 | 0 | uint_to_be_pad(signature.inner, signature.len) |
45 | 0 | .expect("RSASSA-PKCS1-v1_5 length invariants should've been enforced") |
46 | 0 | .into_boxed_slice() |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl Debug for Signature { |
51 | 0 | fn fmt(&self, fmt: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> { |
52 | 0 | fmt.debug_tuple("Signature") |
53 | 0 | .field(&self.to_string()) |
54 | 0 | .finish() |
55 | 0 | } |
56 | | } |
57 | | |
58 | | impl LowerHex for Signature { |
59 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
60 | 0 | write!(f, "{:x}", &self.inner) |
61 | 0 | } |
62 | | } |
63 | | |
64 | | impl UpperHex for Signature { |
65 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
66 | 0 | write!(f, "{:X}", &self.inner) |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl Display for Signature { |
71 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
72 | 0 | write!(f, "{:X}", self) |
73 | 0 | } |
74 | | } |