Skip to main content

smgw_sec/openssl/
signing.rs

1use super::BrainpoolP256PublicKey;
2use ::openssl::{
3    ecdsa::EcdsaSig,
4    error::ErrorStack,
5    hash::MessageDigest,
6    pkey::{PKey, Private},
7    sign::Signer,
8};
9use std::io;
10
11/// A commissioned Brainpool P-256 signing key for ECDSA/SHA-256.
12/// OpenSSL retains the original signing handle, including for provider keys.
13pub struct BrainpoolP256SigningKey(PKey<Private>);
14
15impl BrainpoolP256SigningKey {
16    /// Validate the public component of an already provisioned signing handle.
17    pub fn new(key: PKey<Private>) -> io::Result<Self> {
18        let public = PKey::public_key_from_der(&key.public_key_to_der()?)?;
19        BrainpoolP256PublicKey::new(public)
20            .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
21        Ok(Self(key))
22    }
23
24    /// Load a software private key or configured provider reference without prompting.
25    pub fn from_pem(pem: &[u8]) -> io::Result<Self> {
26        let key = PKey::private_key_from_pem_callback(pem, |_| Ok(0)).map_err(|_| {
27            io::Error::new(io::ErrorKind::InvalidInput, "unable to load signing key")
28        })?;
29        Self::new(key)
30    }
31
32    /// Sign the original message bytes. Return the two 32-byte plain ECDSA components
33    /// in the big-endian representation used by wake-up management datagrams.
34    pub fn sign(&self, message: &[u8]) -> Result<([u8; 32], [u8; 32]), ErrorStack> {
35        let mut signer = Signer::new(MessageDigest::sha256(), &self.0)?;
36        let signature = EcdsaSig::from_der(&signer.sign_oneshot_to_vec(message)?)?;
37        let mut r = [0; 32];
38        let mut s = [0; 32];
39        r.copy_from_slice(&signature.r().to_vec_padded(32)?);
40        s.copy_from_slice(&signature.s().to_vec_padded(32)?);
41        Ok((r, s))
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use crate::BrainpoolP256Verifier;
49    use ::openssl::{
50        ec::{EcGroup, EcKey},
51        nid::Nid,
52    };
53
54    fn key(curve: Nid) -> PKey<Private> {
55        PKey::from_ec_key(EcKey::generate(&EcGroup::from_curve_name(curve).unwrap()).unwrap())
56            .unwrap()
57    }
58
59    #[test]
60    fn binds_plain_signature_to_message_and_commissioned_curve() {
61        let valid = key(Nid::BRAINPOOL_P256R1);
62        let mut verifier =
63            BrainpoolP256PublicKey::from_pem(&valid.public_key_to_pem().unwrap()).unwrap();
64        let signing =
65            BrainpoolP256SigningKey::from_pem(&valid.private_key_to_pem_pkcs8().unwrap()).unwrap();
66        let (r, s) = signing.sign(b"wake-up").unwrap();
67        assert!(verifier.verify(b"wake-up", &r, &s).unwrap());
68        assert!(!verifier.verify(b"different", &r, &s).unwrap());
69        assert!(matches!(
70            BrainpoolP256SigningKey::new(key(Nid::X9_62_PRIME256V1)),
71            Err(error) if error.kind() == io::ErrorKind::InvalidInput
72        ));
73    }
74}