wolf_crypto/aead/chacha20_poly1305/
io.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use crate::aead::chacha20_poly1305::{ChaCha20Poly1305, states::UpdatingAad};
use crate::aead::Aad as _;
use crate::aead::chacha20_poly1305::states::Updating;
use crate::{can_cast_u32, Unspecified};

/// Combines a `ChaCha20Poly1305` instance in its AAD updating state with an IO type,
/// allowing for streaming AAD processing through standard Read and Write traits.
///
/// # Example
///
/// ```
/// # use wolf_crypto::aead::chacha20_poly1305::{ChaCha20Poly1305, Key};
/// # use wolf_crypto::MakeOpaque;
#[cfg_attr(all(feature = "embedded-io", not(feature = "std")), doc = "# use embedded_io::Write;")]
#[cfg_attr(feature = "std", doc = "# use std::io::Write;")]
#[cfg_attr(
    all(feature = "embedded-io", not(feature = "std")),
    doc = "# fn main() -> Result<(), wolf_crypto::Unspecified> {"
)]
#[cfg_attr(feature = "std", doc = "# fn main() -> Result<(), Box<dyn std::error::Error>> {")]
/// # let key = Key::new([0u8; 32]);
/// # let iv = [0u8; 12];
/// # let mut aad_buffer = [0u8; 64];
/// #
/// let mut aad_io = ChaCha20Poly1305::new_encrypt(key, iv)
///     .aad_io(&mut aad_buffer[..]);
///
/// aad_io.write(b"Additional Authenticated Data")?;
/// let (aead, _) = aad_io.finish();
///
/// // Continue with encryption...
/// # Ok(())
/// # }
/// ```
#[must_use]
pub struct Aad<S: UpdatingAad, IO> {
    aad: ChaCha20Poly1305<S>,
    io: IO
}

impl<S: UpdatingAad, IO> Aad<S, IO> {
    /// Creates a new `Aad` instance.
    ///
    /// # Arguments
    ///
    /// * `aad`: A ChaCha20Poly1305 instance in its AAD updating state.
    /// * `io`: The underlying IO type for reading or writing AAD.
    pub const fn new(aad: ChaCha20Poly1305<S>, io: IO) -> Self {
        Self { aad, io }
    }

    /// Signals that no more Additional Authenticated Data (AAD) will be provided, transitioning the
    /// cipher to the data processing state.
    ///
    /// This method finalizes the AAD input phase. After calling `finish`, you may [`finalize`] the
    /// state machine, or begin updating the cipher with data to be encrypted / decrypted.
    ///
    /// # Example
    ///
    /// ```
    /// use wolf_crypto::aead::chacha20_poly1305::{ChaCha20Poly1305, Key};
    /// use wolf_crypto::MakeOpaque;
    ///
    #[cfg_attr(all(feature = "embedded-io", not(feature = "std")), doc = "use embedded_io::Write;")]
    #[cfg_attr(feature = "std", doc = "use std::io::Write;")]
    #[cfg_attr(
        all(feature = "embedded-io", not(feature = "std")),
        doc = "# fn main() -> Result<(), wolf_crypto::Unspecified> {"
    )]
    #[cfg_attr(feature = "std", doc = "# fn main() -> Result<(), Box<dyn std::error::Error>> {")]
    /// let (key, iv) = (Key::new([7u8; 32]), [42; 12]);
    /// let mut some_io_write_implementor = [7u8; 64];
    ///
    /// let mut io = ChaCha20Poly1305::new_encrypt(key, iv)
    ///     .aad_io(some_io_write_implementor.as_mut_slice());
    ///
    /// let read = io.write(b"hello world")?;
    /// // _my_writer in this case is the remaining unwritten bytes...
    /// let (aead, _my_writer) = io.finish();
    ///
    /// assert_eq!(&some_io_write_implementor[..read], b"hello world");
    ///
    /// let tag = aead.finalize();
    /// # assert_ne!(tag, wolf_crypto::aead::Tag::new_zeroed()); // no warnings
    /// # Ok(()) }
    /// ```
    ///
    /// [`finalize`]: ChaCha20Poly1305::finalize
    #[inline]
    pub fn finish(self) -> (ChaCha20Poly1305<S::Mode>, IO) {
        (self.aad.finish(), self.io)
    }
}

std! {
    use std::io as std_io;

    impl<S, IO> std_io::Write for Aad<S, IO>
        where
            S: UpdatingAad,
            IO: std_io::Write
    {
        #[inline]
        fn write(&mut self, buf: &[u8]) -> std_io::Result<usize> {
            if !buf.is_valid_size() { return Err(std_io::Error::other(Unspecified)) }
            self.io.write(buf).map(
                // SAFETY: We ensured the AAD meets the required preconditions with checking
                // is_valid_size.
                |amount| unsafe { self.aad.update_aad_unchecked(&buf[..amount]); amount }
            )
        }

        #[inline]
        fn write_all(&mut self, buf: &[u8]) -> std_io::Result<()> {
            if !buf.is_valid_size() { return Err(std_io::Error::other(Unspecified)) }
            // SAFETY: We ensured the AAD meets the required preconditions with checking
            // is_valid_size.
            self.io.write_all(buf).map(|()| unsafe { self.aad.update_aad_unchecked(buf) })
        }

        #[inline]
        fn flush(&mut self) -> std_io::Result<()> {
            self.io.flush()
        }
    }

    impl<S, IO> std_io::Read for Aad<S, IO>
        where
            S: UpdatingAad,
            IO: std_io::Read
    {
        #[inline]
        fn read(&mut self, buf: &mut [u8]) -> std_io::Result<usize> {
            // we must ensure we are infallible prior to writing to the buffer
            if !buf.is_valid_size() { return Err(std_io::Error::other(Unspecified)) }
            match self.io.read(buf) {
                Ok(read) => {
                    // SAFETY: We ensured the AAD meets the required preconditions with checking
                    // is_valid_size.
                    unsafe { self.aad.update_aad_unchecked(&buf[..read]); }
                    Ok(read)
                },
                res @ Err(_) => res
            }
        }

        #[inline]
        fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std_io::Result<usize> {
            // we must ensure we are infallible prior to writing to the buffer
            if !buf.is_valid_size() { return Err(std_io::Error::other(Unspecified)) }

            let init_len = buf.len();
            match self.io.read_to_end(buf) {
                Ok(read) => {
                    unsafe {
                        // SAFETY: We ensured the AAD meets the required preconditions with checking
                        // is_valid_size.
                        self.aad.update_aad_unchecked(&buf.as_mut_slice()[init_len..init_len + read])
                    };
                    Ok(read)
                },
                res @ Err(_) => res
            }
        }
    }
}

no_std_io! {
    use embedded_io::{self as eio, ErrorType};

    impl<S: UpdatingAad, IO> ErrorType for Aad<S, IO> {
        type Error = Unspecified;
    }

    impl<S, IO> eio::Write for Aad<S, IO>
        where
            S: UpdatingAad,
            IO: eio::Write
    {
        #[inline]
        fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
            if !buf.is_valid_size() { return Err(Unspecified) }
            match self.io.write(buf) {
                Ok(amount) => {
                    // SAFETY: We ensured the AAD meets the required preconditions with checking
                    // is_valid_size.
                    unsafe { self.aad.update_aad_unchecked(&buf[..amount]); }
                    Ok(amount)
                },
                Err(_) => Err(Unspecified)
            }
        }

        #[inline]
        fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
            if !buf.is_valid_size() { return Err(Unspecified) }
            match self.io.write_all(buf) {
                Ok(()) => {
                    // SAFETY: We ensured the AAD meets the required preconditions with checking
                    // is_valid_size.
                    unsafe { self.aad.update_aad_unchecked(buf); }
                    Ok(())
                },
                Err(_) => Err(Unspecified)
            }
        }

        #[inline]
        fn flush(&mut self) -> Result<(), Self::Error> {
            self.io.flush().map_err(|_| Unspecified)
        }
    }

    impl<S, IO> eio::Read for Aad<S, IO>
        where
            S: UpdatingAad,
            IO: eio::Read
    {
        #[inline]
        fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
            // we must ensure we are infallible prior to writing to the buffer
            if !buf.is_valid_size() { return Err(Unspecified) }
            match self.io.read(buf) {
                Ok(read) => {
                    // SAFETY: We ensured the AAD meets the required preconditions with checking
                    // is_valid_size.
                    unsafe { self.aad.update_aad_unchecked(&buf[..read]); }
                    Ok(read)
                },
                Err(_) => Err(Unspecified)
            }
        }
    }
}

/// Combines a `ChaCha20Poly1305` instance in its data updating state with an IO type,
/// allowing for streaming encryption or decryption through the standard Read trait.
///
/// # Example
///
/// ```
/// # use wolf_crypto::aead::chacha20_poly1305::{ChaCha20Poly1305, Key};
/// # use wolf_crypto::MakeOpaque;
#[cfg_attr(all(feature = "embedded-io", not(feature = "std")), doc = "# use embedded_io::Read;")]
#[cfg_attr(feature = "std", doc = "# use std::io::Read;")]
#[cfg_attr(
    all(feature = "embedded-io", not(feature = "std")),
    doc = "# fn main() -> Result<(), wolf_crypto::Unspecified> {"
)]
#[cfg_attr(feature = "std", doc = "# fn main() -> Result<(), Box<dyn std::error::Error>> {")]
/// # let key = Key::new([0u8; 32]);
/// # let iv = [0u8; 12];
/// # let plaintext = b"Secret message";
/// # let mut ciphertext = [0u8; 64];
/// #
/// let mut io = ChaCha20Poly1305::new_encrypt(key, iv)
///     .data_io(plaintext.as_slice());
///
/// let bytes_read = io.read(&mut ciphertext)?;
/// let (aead, _) = io.finish();
///
/// assert_ne!(plaintext.as_slice(), &ciphertext[..bytes_read]);
/// assert_eq!(bytes_read, plaintext.len());
///
/// let tag = aead.finalize();
/// # Ok(())
/// # }
/// ```
#[must_use]
pub struct Data<S: Updating, IO> {
    aead: ChaCha20Poly1305<S>,
    io: IO
}

impl<S: Updating, IO> Data<S, IO> {
    /// Creates a new `Data` instance.
    ///
    /// # Arguments
    ///
    /// * `aead`: A ChaCha20Poly1305 instance in its data updating state.
    /// * `io`: The underlying IO type for reading data to be encrypted or decrypted.
    pub const fn new(aead: ChaCha20Poly1305<S>, io: IO) -> Self {
        Self { aead, io }
    }

    /// Finalizes the data processing and returns the updated `ChaCha20Poly1305` instance.
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - The updated ChaCha20Poly1305 instance, ready for finalization.
    /// - The underlying IO type.
    ///
    /// # Example
    ///
    /// ```
    /// # use wolf_crypto::aead::chacha20_poly1305::{ChaCha20Poly1305, Key};
    /// # use wolf_crypto::MakeOpaque;
    #[cfg_attr(all(feature = "embedded-io", not(feature = "std")), doc = "# use embedded_io::Read;")]
    #[cfg_attr(feature = "std", doc = "# use std::io::Read;")]
    #[cfg_attr(
        all(feature = "embedded-io", not(feature = "std")),
        doc = "# fn main() -> Result<(), wolf_crypto::Unspecified> {"
    )]
    #[cfg_attr(feature = "std", doc = "# fn main() -> Result<(), Box<dyn std::error::Error>> {")]
    /// # let key = Key::new([0u8; 32]);
    /// # let iv = [0u8; 12];
    /// # let plaintext = b"Secret message";
    /// # let mut ciphertext = [0u8; 64];
    /// let mut data_io = ChaCha20Poly1305::new_encrypt(key, iv)
    ///     .data_io(plaintext.as_slice());
    ///
    /// data_io.read(&mut ciphertext)?;
    /// let (aead, _) = data_io.finish();
    ///
    /// let tag = aead.finalize();
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn finish(self) -> (ChaCha20Poly1305<S>, IO) {
        (self.aead, self.io)
    }
}

std! {
    impl<S, IO> std_io::Read for Data<S, IO>
        where
            S: Updating,
            IO: std_io::Read
    {
        #[inline]
        fn read(&mut self, buf: &mut [u8]) -> std_io::Result<usize> {
            // We will do our safety checks in advance, we read from the io type we're wrapping,
            // and then we fail encrypt, potentially leaking sensitive data. So, doing these
            // checks in advance is necessary.
            if !can_cast_u32(buf.len()) { return Err(std_io::Error::other(Unspecified)) }

            self.io.read(buf).map(|amount| unsafe {
                self.aead.update_in_place_unchecked(&mut buf[..amount]);
                amount
            })
        }
    }
}

no_std_io! {
    impl<S: Updating, IO: ErrorType> ErrorType for Data<S, IO> {
        type Error = Unspecified;
    }

    impl<S, IO> eio::Read for Data<S, IO>
        where
            S: Updating,
            IO: eio::Read
    {
        #[inline]
        fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
            if !can_cast_u32(buf.len()) { return Err(Unspecified) }

            match self.io.read(buf) {
                Ok(amount) => {
                    unsafe { self.aead.update_in_place_unchecked(&mut buf[..amount]) };
                    Ok(amount)
                },
                Err(_) => Err(Unspecified)
            }
        }
    }
}