esp_hal/rom/
md5.rs

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! # MD5 Message-Digest Algorithm (MD5)
//!
//! ## ⚠️ Security Warning ⚠️
//!
//! MD5 is a **cryptographically broken** message digest. It should **not**
//! be used for data security, for example, to check if data has been
//! intentionally tampered with.
//!
//! However, it is still very useful for purposes of data **integrity**, for
//! example, to check if data has been **accidentally** tampered with, such as
//! detecting data corrupted during transmission in a stream or stored in
//! flash. This is especially important on microcontrollers where the
//! computational efficiency of MD5 is desired.
//!
//! ## Compatibility
//!
//! The public API exposed by this module tries to *mimic* the public API
//! offered by the [MD5][1] crate in an effort to act as a drop-in replacement.
//! The actual implementation, however, links to whatever is available in the
//! ROM of the particular ESP32 target. Each chip target may offer a different
//! underlying MD5 implementation with varying functionality.
//!
//! This module offers a least-common-denominator API to stay consistent with
//! all of them. Usage of this module may help make program binaries smaller
//! than it would be if you included an MD5 implementation in your project.
//!
//! ## Examples
//!
//! ### Compute a Full Digest From a Single Buffer
//!
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::rom::md5;
//! # let data = "Dummy";
//! let d: md5::Digest = md5::compute(&data);
//! println!("{}", d);
//! # Ok(())
//! # }
//! ```
//! 
//! ### Compute a Digest Over Multiple Buffers
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::rom::md5;
//! # let data0 = "Dummy";
//! # let data1 = "Dummy";
//! #
//! let mut ctx = md5::Context::new();
//! ctx.consume(&data0);
//! ctx.consume(&data1);
//! let d: md5::Digest = ctx.compute();
//! println!("{}", d);
//! # Ok(())
//! # }
//! ```
//! 
//! [1]: <https://crates.io/crates/md5>

#[allow(unused)]
use core::ffi::{c_int, c_uchar, c_void};
use core::{
    fmt,
    mem::MaybeUninit,
    ops::{Deref, DerefMut},
};

// If there is not exactly one of the MD5 variations defined in the device
// toml file then `InternalContext` will be either undefined or multiple
// defined and this module will fail to compile letting you know to fix it

#[cfg(rom_md5_bsd)]
#[derive(Clone)]
#[repr(C)]
struct InternalContext {
    buf: [u32; 4],
    bits: [u32; 2],
    _in: [u8; 64],
}

#[cfg(rom_md5_bsd)]
extern "C" {
    fn esp_rom_md5_init(context: *mut InternalContext);
    fn esp_rom_md5_update(context: *mut InternalContext, buf: *const c_void, len: u32);
    fn esp_rom_md5_final(digest: *mut u8, context: *mut InternalContext);
}

#[cfg(rom_md5_mbedtls)]
#[derive(Clone)]
#[repr(C)]
struct InternalContext {
    total: [u32; 2],
    state: [u32; 4],
    buffer: [c_uchar; 64],
}

#[cfg(rom_md5_mbedtls)]
extern "C" {
    fn esp_rom_mbedtls_md5_starts_ret(context: *mut InternalContext) -> c_int;
    fn esp_rom_mbedtls_md5_update_ret(
        context: *mut InternalContext,
        buf: *const c_void,
        len: u32,
    ) -> c_int;
    fn esp_rom_mbedtls_md5_finish_ret(context: *mut InternalContext, digest: *mut u8) -> c_int;
}

/// MD5 context for an ongoing computation
#[derive(Clone)]
pub struct Context(InternalContext);

impl Context {
    /// Create a new MD5 context
    #[inline]
    pub fn new() -> Self {
        let mut ctx = MaybeUninit::<InternalContext>::uninit();
        unsafe {
            #[cfg(rom_md5_bsd)]
            esp_rom_md5_init(ctx.as_mut_ptr());

            #[cfg(rom_md5_mbedtls)]
            let _ = esp_rom_mbedtls_md5_starts_ret(ctx.as_mut_ptr());

            Self(ctx.assume_init())
        }
    }

    /// Feed data to the hasher
    #[inline]
    pub fn consume<T: AsRef<[u8]>>(&mut self, data: T) {
        let data = data.as_ref();
        unsafe {
            #[cfg(rom_md5_bsd)]
            esp_rom_md5_update(
                &mut self.0 as *mut _,
                data.as_ptr() as *const c_void,
                data.len() as u32,
            );

            #[cfg(rom_md5_mbedtls)]
            let _ = esp_rom_mbedtls_md5_update_ret(
                &mut self.0 as *mut _,
                data.as_ptr() as *const c_void,
                data.len() as u32,
            );
        }
    }

    /// Finalize and return a digest
    #[inline]
    pub fn compute(mut self) -> Digest {
        let mut digest = MaybeUninit::<[u8; 16]>::uninit();
        unsafe {
            #[cfg(rom_md5_bsd)]
            esp_rom_md5_final(digest.as_mut_ptr() as *mut _, &mut self.0 as *mut _);

            #[cfg(rom_md5_mbedtls)]
            let _ = esp_rom_mbedtls_md5_finish_ret(
                &mut self.0 as *mut _,
                digest.as_mut_ptr() as *mut _,
            );

            Digest(digest.assume_init())
        }
    }
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

/// Compute a full digest from a single buffer
#[inline]
pub fn compute<T: AsRef<[u8]>>(data: T) -> Digest {
    let mut ctx = Context::new();
    ctx.consume(data);
    ctx.compute()
}

/// 16-byte message digest
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Digest(pub [u8; 16]);

impl fmt::LowerHex for Digest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for &byte in &self.0 {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}

impl fmt::UpperHex for Digest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for &byte in &self.0 {
            write!(f, "{:02X}", byte)?;
        }
        Ok(())
    }
}

impl fmt::Display for Digest {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <Digest as fmt::LowerHex>::fmt(self, f)
    }
}

impl fmt::Debug for Digest {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <Digest as fmt::LowerHex>::fmt(self, f)
    }
}

impl From<Digest> for [u8; 16] {
    #[inline]
    fn from(digest: Digest) -> Self {
        digest.0
    }
}

impl From<Context> for Digest {
    #[inline]
    fn from(context: Context) -> Digest {
        context.compute()
    }
}

impl Deref for Digest {
    type Target = [u8; 16];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Digest {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}