wolf_crypto/aead/
aad.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
use crate::{can_cast_u32, const_can_cast_u32, to_u32};
use crate::sealed::AadSealed as Sealed;
use core::fmt;


/// A generic representation of additional authenticated data (AAD)
#[allow(clippy::missing_safety_doc)]
pub unsafe trait Aad: Sealed {
    /// Returns the length of the [`ptr`] result, represented as a `u32`.
    ///
    /// # Trait Safety
    ///
    /// - This must return the length as a `u32`, if the length is too large to be represented as
    ///   a `u32` then it should return `None`.
    /// - This returns `None` **if and only if** the length cannot be represented as a `u32`.
    ///   If no AAD is being provided, the returned length should **always** be zero. `None` is
    ///   reserved only for when casting the `usize` to a `u32` would overflow.
    #[doc(hidden)]
    #[must_use]
    fn try_size(&self) -> Option<u32>;

    /// Returns a pointer valid for the length of the [`try_size`] result.
    ///
    /// # Trait Safety
    ///
    /// - This must provide a valid pointer if the result of [`try_size`] is not `Some(0)`. If
    ///   `Some(0)` is returned from [`try_size`] it is acceptable for the result of this to be
    ///   null.
    /// - This must return a pointer which is valid for the result of [`try_size`], any less will
    ///   result in undefined behavior.
    ///
    /// [`try_size`]: Aad::try_size
    #[doc(hidden)]
    #[must_use]
    fn ptr(&self) -> *const u8;

    /// # Trait Safety
    ///
    /// Same invariants as [`try_size`], just this function does not need to check for overflow
    /// as to safely invoke this, the caller must ensure [`is_valid_size`] returns true.
    ///
    /// # Safety
    ///
    /// The caller must ensure the [`is_valid_size`] returns true prior to invoking this.
    ///
    /// [`try_size`]: Aad::try_size
    /// [`is_valid_size`]: Aad::is_valid_size
    #[doc(hidden)]
    #[must_use]
    unsafe fn size(&self) -> u32;

    /// Returns `true` IFF the result of [`try_size`] would be `Some`.
    ///
    /// [`try_size`]: Aad::try_size
    #[doc(hidden)]
    #[must_use]
    fn is_valid_size(&self) -> bool;
}

/// Represents Additional Authenticated Data (AAD) Slice.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct AadSlice<'s> {
    inner: Option<&'s [u8]>
}

impl<'s> fmt::Debug for AadSlice<'s> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("AadSlice(")
            .and_then(|()| match self.inner {
                None => f.write_str("EMPTY"),
                Some(inner) => <[u8] as fmt::Debug>::fmt(inner, f)
            })
            .and_then(|()| f.write_str(")"))
    }
}

impl<'a> PartialEq<[u8]> for AadSlice<'a> {
    #[inline]
    fn eq(&self, other: &[u8]) -> bool {
        self.inner.is_some_and(|inner| inner == other)
    }
}

impl<'s> AadSlice<'s> {
    /// An empty AAD.
    pub const EMPTY: Self = Self { inner: None };

    /// Create a new AAD instance from a byte slice.
    pub const fn new(aad: &'s [u8]) -> Self {
        Self { inner: Some(aad) }
    }

    /// Pointer may be null of the option was None
    #[inline]
    pub(crate) const fn as_ptr(&self) -> *const u8 {
        match self.inner {
            Some(inner) => inner.as_ptr(),
            None => core::ptr::null()
        }
    }
    
    /// Returns the length of the underlying slice if it can be represented as a [`u32`].
    /// 
    /// # Note
    /// 
    /// [`AadSlice::EMPTY`] will always return `Some(0)`.
    #[inline(always)]
    #[must_use]
    pub const fn size(&self) -> Option<u32> {
        match self.inner {
            None => Some(0),
            Some(val) => to_u32(val.len())
        }
    }

    /// Returns `true` if the length of the underlying slice can be safely represented as a [`u32`].
    #[inline]
    #[must_use]
    pub const fn valid_size(&self) -> bool {
        match self.inner {
            Some(inner) => can_cast_u32(inner.len()),
            None => true
        }
    }
}

impl<'a> From<&'a [u8]> for AadSlice<'a> {
    #[inline]
    fn from(value: &'a [u8]) -> Self {
        Self::new(value)
    }
}

impl<'a> Sealed for AadSlice<'a> {}

unsafe impl<'a> Aad for AadSlice<'a> {
    #[inline]
    fn try_size(&self) -> Option<u32> { self.size() }
    #[inline]
    fn ptr(&self) -> *const u8 { self.as_ptr() }

    #[inline]
    unsafe fn size(&self) -> u32 {
        debug_assert!(self.is_valid_size());
        self.inner.map_or(0, |inner| inner.len() as u32)
    }

    #[inline]
    fn is_valid_size(&self) -> bool { self.valid_size() }
}

impl<T: ?Sized + Sealed> Sealed for &T {}
unsafe impl<T: ?Sized + Aad> Aad for &T {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        T::try_size(self)
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        T::ptr(self)
    }
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    unsafe fn size(&self) -> u32 {
        T::size(self)
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        T::is_valid_size(self)
    }
}
impl<T: ?Sized + Sealed> Sealed for &mut T {}
unsafe impl<T: ?Sized + Aad> Aad for &mut T {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        T::try_size(self)
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        T::ptr(self)
    }
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    unsafe fn size(&self) -> u32 {
        T::size(self)
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        T::is_valid_size(self)
    }
}

impl Sealed for [u8] {}

unsafe impl Aad for [u8] {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        to_u32(self.len())
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        self.as_ptr()
    }
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    unsafe fn size(&self) -> u32 {
        debug_assert!(self.is_valid_size());
        self.len() as u32
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        can_cast_u32(self.len())
    }
}

impl<const C: usize> Sealed for [u8; C] {}

unsafe impl<const C: usize> Aad for [u8; C] {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        if const_can_cast_u32::<C>() {
            Some(C as u32)
        } else {
            None
        }
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        self.as_ptr()
    }
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    unsafe fn size(&self) -> u32 {
        debug_assert!(const_can_cast_u32::<C>());
        C as u32
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        const_can_cast_u32::<C>()
    }
}

impl Sealed for str {}

unsafe impl Aad for str {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        to_u32(self.len())
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        self.as_ptr()
    }
    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    unsafe fn size(&self) -> u32 {
        debug_assert!(self.is_valid_size());
        self.len() as u32
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        can_cast_u32(self.len())
    }
}

impl Sealed for () {}

unsafe impl Aad for () {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        Some(0)
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        core::ptr::null()
    }
    #[inline]
    unsafe fn size(&self) -> u32 {
        0
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        true
    }
}

impl<T: Sealed> Sealed for Option<T> {}

unsafe impl<T: Aad> Aad for Option<T> {
    #[inline]
    fn try_size(&self) -> Option<u32> {
        self.as_ref().map_or(Some(0), Aad::try_size)
    }
    #[inline]
    fn ptr(&self) -> *const u8 {
        self.as_ref().map_or(core::ptr::null(), Aad::ptr)
    }
    #[inline]
    unsafe fn size(&self) -> u32 {
        #[allow(clippy::option_if_let_else)]
        match self {
            None => 0,
            Some(inner) => inner.size()
        }
    }
    #[inline]
    fn is_valid_size(&self) -> bool {
        self.as_ref().map_or(true, Aad::is_valid_size)
    }
}

alloc! {
    impl Sealed for Vec<u8> {}

    unsafe impl Aad for Vec<u8> {
        #[inline]
        fn try_size(&self) -> Option<u32> {
            to_u32(self.len())
        }
        #[inline]
        fn ptr(&self) -> *const u8 {
            self.as_ptr()
        }
        #[inline]
        #[cfg_attr(debug_assertions, track_caller)]
        unsafe fn size(&self) -> u32 {
            debug_assert!(self.is_valid_size());
            self.len() as u32
        }
        #[inline]
        fn is_valid_size(&self) -> bool {
            can_cast_u32(self.len())
        }
    }

    impl Sealed for String {}

    unsafe impl Aad for String {
        #[inline]
        fn try_size(&self) -> Option<u32> {
            to_u32(self.len())
        }
        #[inline]
        fn ptr(&self) -> *const u8 {
            self.as_ptr()
        }
        #[inline]
        #[cfg_attr(debug_assertions, track_caller)]
        unsafe fn size(&self) -> u32 {
            debug_assert!(self.is_valid_size());
            self.len() as u32
        }
        #[inline]
        fn is_valid_size(&self) -> bool {
            can_cast_u32(self.len())
        }
    }
}