Skip to main content

at_cryptoauth/client/
signing.rs

1//! ECDSA signing operations
2
3use crate::command::{self, PublicKey};
4use crate::error::{Error, ErrorKind};
5use crate::memory::Slot;
6use crate::{Digest, Signature};
7use embassy_sync::blocking_mutex::raw::RawMutex;
8use p256::ecdsa::DerSignature;
9use signature::Keypair;
10use signature::hazmat::PrehashSigner;
11
12use super::{AtCaClient, VerifyingKey};
13
14pub struct SigningKey<'a, M: RawMutex, PHY> {
15    pub(crate) atca: &'a AtCaClient<M, PHY>,
16    pub(crate) key_id: Slot,
17}
18
19impl<'a, M: RawMutex, PHY> SigningKey<'a, M, PHY> {
20    pub fn verifying_key(&'a mut self) -> VerifyingKey<'a, M, PHY> {
21        VerifyingKey {
22            atca: self.atca,
23            key_id: self.key_id,
24        }
25    }
26}
27
28impl<'a, M, PHY> SigningKey<'a, M, PHY>
29where
30    PHY: embedded_hal_async::i2c::I2c,
31    M: RawMutex,
32{
33    /// Takes a 32-byte message to be signed, typically the SHA256 hash of the full message.
34    pub async fn sign_digest(&self, digest: &Digest) -> Result<Signature, Error> {
35        // 1. Update RNG seed
36        self.atca.update_seed().await?;
37        // 2. Nonce load
38        self.atca.write_message_digest_buffer(digest).await?;
39        // 3. Sign
40        let mut inner = self.atca.inner.lock().await;
41        let packet = command::Sign::new(inner.packet_builder()).external(self.key_id)?;
42        let response = inner.execute(packet).await?;
43        Signature::from_bytes(response.as_ref().into()).map_err(|_| ErrorKind::BadParam.into())
44    }
45
46    pub async fn sign(&self, msg: &[u8]) -> Result<Signature, Error> {
47        let digest = self.atca.sha().digest(msg).await?;
48        self.sign_digest(&digest).await
49    }
50}
51
52impl<'a, M, PHY> SigningKey<'a, M, PHY>
53where
54    PHY: embedded_hal::i2c::I2c,
55    M: RawMutex,
56{
57    /// Takes a 32-byte message to be signed, typically the SHA256 hash of the full message.
58    pub fn sign_digest_blocking(&self, digest: &Digest) -> Result<Signature, Error> {
59        // 1. Update RNG seed
60        self.atca.update_seed_blocking()?;
61        // 2. Nonce load
62        self.atca.write_message_digest_buffer_blocking(digest)?;
63        // 3. Sign
64        let mut inner = self
65            .atca
66            .inner
67            .try_lock()
68            .map_err(|_| ErrorKind::MutexLocked)?;
69        let packet = command::Sign::new(inner.packet_builder()).external(self.key_id)?;
70        let response = inner.execute_blocking(packet)?;
71        Signature::from_bytes(response.as_ref().into()).map_err(|_| ErrorKind::BadParam.into())
72    }
73
74    pub fn sign_blocking(&self, msg: &[u8]) -> Result<Signature, Error> {
75        let digest = self.atca.sha().digest_blocking(msg)?;
76        self.sign_digest_blocking(&digest)
77    }
78}
79
80impl<'a, M, PHY> Keypair for SigningKey<'a, M, PHY>
81where
82    PHY: embedded_hal::i2c::I2c,
83    M: RawMutex,
84{
85    type VerifyingKey = PublicKey;
86
87    fn verifying_key(&self) -> Self::VerifyingKey {
88        let key_id = self.key_id;
89        self.atca.generate_pubkey_blocking(key_id).unwrap()
90    }
91}
92
93impl<'a, M, PHY> PrehashSigner<Signature> for SigningKey<'a, M, PHY>
94where
95    PHY: embedded_hal::i2c::I2c,
96    M: RawMutex,
97{
98    fn sign_prehash(&self, prehash: &[u8]) -> signature::Result<Signature> {
99        let digest = self
100            .atca
101            .sha()
102            .digest_blocking(prehash)
103            .map_err(|_| signature::Error::new())?;
104        self.sign_digest_blocking(&digest)
105            .map_err(|_| signature::Error::new())
106    }
107}
108
109impl<'a, M, PHY, D> signature::DigestSigner<D, Signature> for SigningKey<'a, M, PHY>
110where
111    PHY: embedded_hal::i2c::I2c,
112    D: signature::digest::Digest,
113    M: RawMutex,
114{
115    fn try_sign_digest(&self, digest: D) -> signature::Result<Signature> {
116        self.sign_prehash(&digest.finalize())
117    }
118}
119
120impl<'a, M, PHY> signature::Signer<Signature> for SigningKey<'a, M, PHY>
121where
122    PHY: embedded_hal::i2c::I2c,
123    M: RawMutex,
124{
125    fn try_sign(&self, msg: &[u8]) -> signature::Result<Signature> {
126        let digest = self
127            .atca
128            .sha()
129            .digest_blocking(msg)
130            .map_err(|_| signature::Error::new())?;
131        self.sign_digest_blocking(&digest)
132            .map_err(|_| signature::Error::new())
133    }
134}
135
136impl<'a, M, PHY> PrehashSigner<DerSignature> for SigningKey<'a, M, PHY>
137where
138    PHY: embedded_hal::i2c::I2c,
139    M: RawMutex,
140{
141    fn sign_prehash(&self, prehash: &[u8]) -> signature::Result<DerSignature> {
142        PrehashSigner::<Signature>::sign_prehash(self, prehash).map(Into::into)
143    }
144}
145
146impl<'a, M, PHY, D> signature::DigestSigner<D, DerSignature> for SigningKey<'a, M, PHY>
147where
148    PHY: embedded_hal::i2c::I2c,
149    D: signature::digest::Digest,
150    M: RawMutex,
151{
152    fn try_sign_digest(&self, digest: D) -> signature::Result<DerSignature> {
153        signature::DigestSigner::<D, Signature>::try_sign_digest(self, digest).map(Into::into)
154    }
155}
156
157impl<'a, M, PHY> signature::Signer<DerSignature> for SigningKey<'a, M, PHY>
158where
159    PHY: embedded_hal::i2c::I2c,
160    M: RawMutex,
161{
162    fn try_sign(&self, msg: &[u8]) -> signature::Result<DerSignature> {
163        signature::Signer::<Signature>::try_sign(self, msg).map(Into::into)
164    }
165}