minisign/
signature_bones.rs

1use crate::constants::*;
2use crate::errors::*;
3use crate::signature::*;
4use crate::signature_box::*;
5
6/// A trimmed-down signature, without any comments section, with binary
7/// serialization only
8#[derive(Clone)]
9pub struct SignatureBones {
10    pub(crate) signature: Signature,
11    pub(crate) is_prehashed: bool,
12}
13
14impl SignatureBones {
15    /// Size of a minimal signature in bytes
16    pub const BYTES: usize = Signature::BYTES;
17
18    /// Returns `true` if the signed data was pre-hashed.
19    pub fn is_prehashed(&self) -> bool {
20        self.is_prehashed
21    }
22
23    /// Create a new `SignatureBones` from a &[u8].
24    pub fn from_bytes(bytes: &[u8]) -> Result<SignatureBones> {
25        let signature = Signature::from_bytes(bytes)?;
26        let is_prehashed = match signature.sig_alg {
27            SIGALG => false,
28            SIGALG_PREHASHED => true,
29            _ => {
30                return Err(PError::new(
31                    ErrorKind::Verify,
32                    "Unsupported signature algorithm".to_string(),
33                ))
34            }
35        };
36        Ok(SignatureBones {
37            signature,
38            is_prehashed,
39        })
40    }
41
42    /// Return a `SignatureBones` as bytes, for storage.
43    pub fn to_bytes(&self) -> Vec<u8> {
44        self.signature.to_bytes()
45    }
46}
47
48impl From<SignatureBones> for SignatureBox {
49    fn from(signature: SignatureBones) -> SignatureBox {
50        let is_prehashed = signature.is_prehashed();
51        SignatureBox {
52            untrusted_comment: String::new(),
53            signature: signature.signature,
54            sig_and_trusted_comment: None,
55            global_sig: None,
56            is_prehashed,
57        }
58    }
59}
60
61impl From<SignatureBox> for SignatureBones {
62    fn from(signature: SignatureBox) -> SignatureBones {
63        let is_prehashed = signature.is_prehashed();
64        SignatureBones {
65            signature: signature.signature,
66            is_prehashed,
67        }
68    }
69}