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
use crate::constants::*;
use crate::errors::*;
use crate::signature::*;
use crate::signature_box::*;

/// A trimmed-down signature, without any comments section, with binary serialization only
#[derive(Clone)]
pub struct SignatureBones {
    pub(crate) signature: Signature,
    pub(crate) is_prehashed: bool,
}

impl SignatureBones {
    /// Returns `true` if the signed data was pre-hashed.
    pub fn is_prehashed(&self) -> bool {
        self.is_prehashed
    }

    /// Create a new `SignatureBones` from a &[u8].
    pub fn from_bytes(bytes: &[u8]) -> Result<SignatureBones> {
        let signature = Signature::from_bytes(&bytes)?;
        let is_prehashed = match signature.sig_alg {
            SIGALG => false,
            SIGALG_PREHASHED => true,
            _ => {
                return Err(PError::new(
                    ErrorKind::Verify,
                    "Unsupported signature algorithm".to_string(),
                ))
            }
        };
        Ok(SignatureBones {
            signature,
            is_prehashed,
        })
    }

    /// Return a `SignatureBones` as bytes, for storage.
    pub fn to_bytes(&self) -> Vec<u8> {
        self.signature.to_bytes()
    }

    /// Size of a minimal signature in bytes
    pub const BYTES: usize = Signature::BYTES;
}

impl Into<SignatureBox> for SignatureBones {
    fn into(self) -> SignatureBox {
        let is_prehashed = self.is_prehashed();
        SignatureBox {
            untrusted_comment: String::new(),
            signature: self.signature,
            sig_and_trusted_comment: None,
            global_sig: None,
            is_prehashed,
        }
    }
}

impl Into<SignatureBones> for SignatureBox {
    fn into(self) -> SignatureBones {
        let is_prehashed = self.is_prehashed();
        SignatureBones {
            signature: self.signature,
            is_prehashed,
        }
    }
}