Skip to main content

at_cryptoauth/client/
sha.rs

1//! SHA-256 hashing operations
2
3use crate::Digest;
4use crate::command;
5use crate::error::{Error, ErrorKind};
6use embassy_sync::blocking_mutex::raw::RawMutex;
7use heapless::Vec;
8
9use super::AtCaClient;
10
11pub struct Sha<'a, M: RawMutex, PHY> {
12    pub(crate) atca: &'a AtCaClient<M, PHY>,
13    pub(crate) remaining_bytes: Vec<u8, 64>,
14}
15
16impl<'a, M, PHY> signature::digest::OutputSizeUser for Sha<'a, M, PHY>
17where
18    PHY: embedded_hal::i2c::I2c,
19    M: RawMutex,
20{
21    type OutputSize = signature::digest::generic_array::typenum::U32;
22}
23
24impl<'a, M, PHY> signature::digest::FixedOutput for Sha<'a, M, PHY>
25where
26    PHY: embedded_hal::i2c::I2c,
27    M: RawMutex,
28{
29    fn finalize_into(self, out: &mut signature::digest::Output<Self>) {
30        let response = self.finalize_blocking().unwrap();
31        out.as_mut_slice().copy_from_slice(response.as_ref());
32    }
33
34    fn finalize_fixed(self) -> signature::digest::Output<Self> {
35        let response = self.finalize_blocking().unwrap();
36        response.value
37    }
38}
39
40impl<'a, M, PHY> signature::digest::Update for Sha<'a, M, PHY>
41where
42    PHY: embedded_hal::i2c::I2c,
43    M: RawMutex,
44{
45    fn update(&mut self, data: &[u8]) {
46        self.update_blocking(data).unwrap()
47    }
48}
49
50impl<'a, M, PHY> signature::digest::HashMarker for Sha<'a, M, PHY>
51where
52    PHY: embedded_hal::i2c::I2c,
53    M: RawMutex,
54{
55}
56
57impl<'a, M, PHY> Sha<'a, M, PHY>
58where
59    PHY: embedded_hal_async::i2c::I2c,
60    M: RawMutex,
61{
62    pub async fn init(&mut self) -> Result<(), Error> {
63        let mut inner = self.atca.inner.lock().await;
64
65        let packet = command::Sha::new(inner.packet_builder()).start()?;
66        inner.execute(packet).await?;
67
68        Ok(())
69    }
70
71    pub async fn update(&mut self, data: &[u8]) -> Result<(), Error> {
72        let capacity = 0x40;
73        let length = data.len();
74
75        // Store remaining bytes for later processing
76        let remainder_length = data.len() % capacity;
77        let (bytes, remainder) = data.split_at(length - remainder_length);
78        self.remaining_bytes.extend_from_slice(remainder).ok();
79
80        // Execute update command
81        for chunk in bytes.chunks(capacity) {
82            let mut inner = self.atca.inner.lock().await;
83
84            let packet = command::Sha::new(inner.packet_builder()).update(chunk)?;
85            inner.execute(packet).await?;
86        }
87
88        Ok(())
89    }
90
91    pub async fn chain(&mut self, data: &[u8]) -> Result<&mut Self, Error> {
92        if !self.remaining_bytes.is_empty() {
93            // TODO: Concatenate remaining bytes and input data.
94        }
95
96        self.update(data).await?;
97        Ok(self)
98    }
99
100    pub async fn finalize(&mut self) -> Result<Digest, Error> {
101        let mut inner = self.atca.inner.lock().await;
102
103        let packet = command::Sha::new(inner.packet_builder()).end(&self.remaining_bytes)?;
104        inner.execute(packet).await?.as_ref().try_into()
105    }
106
107    pub async fn digest(&mut self, data: &[u8]) -> Result<Digest, Error> {
108        self.init().await?;
109        self.update(data).await?;
110        self.finalize().await
111    }
112}
113
114impl<'a, M, PHY> Sha<'a, M, PHY>
115where
116    PHY: embedded_hal::i2c::I2c,
117    M: RawMutex,
118{
119    pub fn init_blocking(&mut self) -> Result<(), Error> {
120        let mut inner = self
121            .atca
122            .inner
123            .try_lock()
124            .map_err(|_| ErrorKind::MutexLocked)?;
125
126        let packet = command::Sha::new(inner.packet_builder()).start()?;
127        inner.execute_blocking(packet)?;
128
129        Ok(())
130    }
131
132    pub fn update_blocking(&mut self, data: &[u8]) -> Result<(), Error> {
133        let capacity = 0x40;
134        let length = data.len();
135
136        // Store remaining bytes for later processing
137        let remainder_length = data.len() % capacity;
138        let (bytes, remainder) = data.split_at(length - remainder_length);
139        self.remaining_bytes.extend_from_slice(remainder).ok();
140
141        let mut inner = self
142            .atca
143            .inner
144            .try_lock()
145            .map_err(|_| ErrorKind::MutexLocked)?;
146
147        // Execute update command
148        for chunk in bytes.chunks(capacity) {
149            let packet = command::Sha::new(inner.packet_builder()).update(chunk)?;
150            inner.execute_blocking(packet)?;
151        }
152
153        Ok(())
154    }
155
156    pub fn chain_blocking(&mut self, data: &[u8]) -> Result<&mut Self, Error> {
157        if !self.remaining_bytes.is_empty() {
158            // TODO: Concatenate remaining bytes and input data.
159        }
160
161        self.update_blocking(data)?;
162        Ok(self)
163    }
164
165    pub fn finalize_blocking(self) -> Result<Digest, Error> {
166        let mut inner = self
167            .atca
168            .inner
169            .try_lock()
170            .map_err(|_| ErrorKind::MutexLocked)?;
171
172        let packet = command::Sha::new(inner.packet_builder()).end(&self.remaining_bytes)?;
173        inner.execute_blocking(packet)?.as_ref().try_into()
174    }
175
176    pub fn digest_blocking(mut self, data: &[u8]) -> Result<Digest, Error> {
177        self.init_blocking()?;
178        self.update_blocking(data)?;
179        self.finalize_blocking()
180    }
181}