orion 0.18.1

Usable, easy and safe pure-Rust crypto
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// MIT License

// Copyright (c) 2026 The orion Developers

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{
    errors::UnknownCryptoError,
    generics::data::sealed::{Data, TryFromBytes},
};

#[cfg(feature = "alloc")]
use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

pub(crate) mod sealed {
    use crate::errors::UnknownCryptoError;

    /// Sealing marker trait for any public/private newtypes based on const-generics
    /// such that they cannot be implemented outside the library, given that [`Context`]
    /// requires [`Sealed`].
    pub trait Sealed {}

    pub trait TryFromBytes: Sized {
        fn try_from_bytes(bytes: &[u8]) -> Result<Self, UnknownCryptoError>;
    }

    /// A datatype that can be used as the internal backing of a newtype.
    pub trait Data: AsRef<[u8]> + AsMut<[u8]> + TryFromBytes + Sealed {
        /// Return the amount of data stored.
        fn len(&self) -> usize;

        /// Return `true` if no data is stored, `false` otherwise.
        fn is_empty(&self) -> bool;

        /// Create a new instance with `size`.
        fn new(size: usize) -> Result<Self, UnknownCryptoError>;

        #[cfg(feature = "zeroize")]
        /// Zero out the data stored.
        fn memzero(&mut self);
    }
}

#[derive(Debug, Clone)]
/// A newtype data-type that represents static constant size of [`u8`]s.
/// This is for types that only have _one_ valid size.
///
/// SECURITY:
/// While these types do derive non-constant time or plain debug impls, these
/// should *NEVER* be exposed publicly anyway. The implementations of [`crate::generics::Public`]/[`crate::generics::Secret`]
/// will define protections for their type.
pub struct ByteArrayData<const LEN: usize> {
    pub(crate) bytes: [u8; LEN],
}

impl<const LEN: usize> sealed::Sealed for ByteArrayData<LEN> {}

impl<const LEN: usize> AsRef<[u8]> for ByteArrayData<LEN> {
    fn as_ref(&self) -> &[u8] {
        &self.bytes
    }
}

impl<const LEN: usize> AsRef<[u8; LEN]> for ByteArrayData<LEN> {
    fn as_ref(&self) -> &[u8; LEN] {
        &self.bytes
    }
}

impl<const LEN: usize> AsMut<[u8]> for ByteArrayData<LEN> {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.bytes
    }
}

impl<const LEN: usize> AsMut<[u8; LEN]> for ByteArrayData<LEN> {
    fn as_mut(&mut self) -> &mut [u8; LEN] {
        &mut self.bytes
    }
}

impl<const LEN: usize> From<[u8; LEN]> for ByteArrayData<LEN> {
    fn from(value: [u8; LEN]) -> Self {
        Self { bytes: value }
    }
}

impl<const LEN: usize> TryFromBytes for ByteArrayData<LEN> {
    fn try_from_bytes(bytes: &[u8]) -> Result<Self, UnknownCryptoError> {
        Ok(Self {
            // NOTE: Length check here is implicit to const-def of LEN.
            bytes: bytes.try_into().map_err(|_| UnknownCryptoError)?,
        })
    }
}

impl<const LEN: usize> Data for ByteArrayData<LEN> {
    fn len(&self) -> usize {
        debug_assert_eq!(self.bytes.len(), LEN);
        LEN
    }

    fn is_empty(&self) -> bool {
        LEN == 0
    }

    fn new(_size: usize) -> Result<Self, UnknownCryptoError> {
        debug_assert_eq!(_size, LEN);
        Ok(Self { bytes: [0u8; LEN] })
    }

    #[cfg(feature = "zeroize")]
    fn memzero(&mut self) {
        self.bytes.iter_mut().zeroize();
    }
}

#[derive(Debug, Clone)]
/// A newtype data-type that represents array of bytes within a `MAX` known range.
/// This is for types that have a valid size within a stack-allocatable range, e.g. BLAKE2b secret keys.
///
/// It always allocates `MAX`, but keeps an extra length field to keep track of what the original value was.
/// If the provided byte slice was less than `MAX`, then the required bytes from left to right are returned.
///
/// SECURITY:
/// While these types do derive non-constant time or omitted debug impls, these
/// should *NEVER* be exposed publicly anyway. The implementations of [`crate::generics::Public`]/[`crate::generics::Secret`]
/// will define protections for their type.
pub struct ByteArrayVecData<const MIN: usize, const MAX: usize> {
    pub(crate) bytes: [u8; MAX],
    pub(crate) len: usize,
}

impl<const MIN: usize, const MAX: usize> sealed::Sealed for ByteArrayVecData<MIN, MAX> {}

impl<const MIN: usize, const MAX: usize> AsRef<[u8]> for ByteArrayVecData<MIN, MAX> {
    fn as_ref(&self) -> &[u8] {
        self.bytes[..self.len].as_ref()
    }
}

impl<const MIN: usize, const MAX: usize> AsMut<[u8]> for ByteArrayVecData<MIN, MAX> {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.bytes[..self.len]
    }
}

impl<const MIN: usize, const MAX: usize> TryFromBytes for ByteArrayVecData<MIN, MAX> {
    fn try_from_bytes(bytes: &[u8]) -> Result<Self, UnknownCryptoError> {
        if !(MIN..=MAX).contains(&bytes.len()) {
            return Err(UnknownCryptoError);
        }

        let mut ret = Self {
            bytes: [0u8; MAX],
            len: bytes.len(),
        };

        ret.bytes[..bytes.len()].copy_from_slice(bytes);

        Ok(ret)
    }
}

impl<const MIN: usize, const MAX: usize> Data for ByteArrayVecData<MIN, MAX> {
    fn len(&self) -> usize {
        self.len
    }

    fn is_empty(&self) -> bool {
        self.len == 0
    }

    fn new(size: usize) -> Result<Self, UnknownCryptoError> {
        if !(MIN..=MAX).contains(&size) {
            return Err(UnknownCryptoError);
        }

        Ok(Self {
            bytes: [0u8; MAX],
            len: size,
        })
    }

    #[cfg(feature = "zeroize")]
    fn memzero(&mut self) {
        self.bytes.iter_mut().zeroize();
    }
}

#[derive(Debug, Clone)]
#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
/// A newtype data-type that represents heap-allocated [`Vec<u8>`].
///
/// The maximum size for [`ByteVecData`] is [`isize::MAX`].
///
/// SECURITY:
/// While these types do derive non-constant time or omitted debug impls, these
/// should *NEVER* be exposed publicly anyway. The implementations of [`crate::generics::Public`]/[`crate::generics::Secret`]
/// will define protections for their type.
pub struct ByteVecData {
    pub(crate) bytes: Vec<u8>,
}

#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
impl sealed::Sealed for ByteVecData {}

#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
impl AsRef<[u8]> for ByteVecData {
    fn as_ref(&self) -> &[u8] {
        self.bytes.as_slice()
    }
}

#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
impl AsMut<[u8]> for ByteVecData {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.bytes
    }
}

#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
impl TryFromBytes for ByteVecData {
    fn try_from_bytes(bytes: &[u8]) -> Result<Self, UnknownCryptoError> {
        // See issue on `isize` limit: https://github.com/orion-rs/orion/issues/130
        if bytes.is_empty() || bytes.len() > (isize::MAX as usize) {
            return Err(UnknownCryptoError);
        }

        Ok(Self {
            bytes: Vec::from(bytes),
        })
    }
}

#[cfg(any(feature = "safe_api", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "safe_api", feature = "alloc"))))]
impl Data for ByteVecData {
    fn len(&self) -> usize {
        self.bytes.len()
    }

    fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    fn new(size: usize) -> Result<Self, UnknownCryptoError> {
        // See issue on `isize` limit: https://github.com/orion-rs/orion/issues/130
        if size == 0 || size > (isize::MAX as usize) {
            return Err(UnknownCryptoError);
        }

        Ok(Self {
            bytes: vec![0u8; size],
        })
    }

    #[cfg(feature = "zeroize")]
    fn memzero(&mut self) {
        self.bytes.iter_mut().zeroize();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic]
    #[cfg(debug_assertions)] // panics with debug_assertions.
    fn force_internal_new_size_contract() {
        // ByteArrayData::new() does not use the size argument. It uses LEN
        // directly. But if this should ever change or simply for the reason
        // of keeping things sane, we require the size argument then be LEN.
        assert!(ByteArrayData::<4>::new(3).is_ok());
    }

    #[test]
    fn test_arraydata_u8() {
        assert!(ByteArrayData::<4>::try_from_bytes(&[0u8; 4]).is_ok());
        assert!(ByteArrayData::<4>::try_from_bytes(&[0u8; 3]).is_err());
        assert!(ByteArrayData::<4>::try_from_bytes(&[0u8; 5]).is_err());
        assert!(ByteArrayData::<4>::new(4).is_ok());
        assert_eq!(&ByteArrayData::<4>::new(4).unwrap().as_ref(), &[0u8; 4]);

        let mut data = ByteArrayData::<4>::try_from_bytes(&[1u8; 4]).unwrap();
        assert_eq!(data.bytes, [1u8; 4]);
        assert_eq!(data.as_ref(), [1u8; 4]); // AsRef<[u8]>
        assert_eq!(data.as_mut(), [1u8; 4]); // AsMut<[u8]>
        assert_eq!(
            <ByteArrayData<4> as AsRef<[u8; 4]>>::as_ref(&data),
            &[1u8; 4]
        ); // AsRef<[; N]>
        assert_eq!(
            <ByteArrayData<4> as AsMut<[u8; 4]>>::as_mut(&mut data),
            &[1u8; 4]
        ); // AsRef<[; N]>
        assert_eq!(data.len(), 4);
        assert!(!data.is_empty());

        #[cfg(feature = "zeroize")]
        {
            data.memzero();
            assert_eq!(data.bytes, [0u8; 4]);
        }
    }

    #[test]
    fn test_arrayvecdata_u8() {
        assert!(ByteArrayVecData::<1, 4>::try_from_bytes(&[0u8; 0]).is_err());
        assert!(ByteArrayVecData::<1, 4>::try_from_bytes(&[0u8; 1]).is_ok());
        assert!(ByteArrayVecData::<1, 4>::try_from_bytes(&[0u8; 4]).is_ok());
        assert!(ByteArrayVecData::<1, 4>::try_from_bytes(&[0u8; 3]).is_ok());
        assert!(ByteArrayVecData::<1, 4>::try_from_bytes(&[0u8; 5]).is_err());
        assert!(ByteArrayVecData::<1, 4>::new(0).is_err());
        assert!(ByteArrayVecData::<1, 4>::new(1).is_ok());
        assert!(ByteArrayVecData::<1, 4>::new(4).is_ok());
        assert!(ByteArrayVecData::<1, 4>::new(3).is_ok());
        assert!(ByteArrayVecData::<1, 4>::new(5).is_err());

        assert_eq!(
            ByteArrayVecData::<1, 4>::new(4).unwrap().as_ref(),
            &[0u8; 4]
        );

        let mut data = ByteArrayVecData::<1, 4>::try_from_bytes(&[1u8; 4]).unwrap();
        assert_eq!(data.bytes, [1u8; 4]);
        assert_eq!(data.as_ref(), &[1u8; 4]);
        assert_eq!(data.as_mut(), &mut [1u8; 4]);
        assert_eq!(data.len(), 4);
        assert!(!data.is_empty());

        #[cfg(feature = "zeroize")]
        {
            data.memzero();
            assert_eq!(data.bytes, [0u8; 4]);
        }

        let mut data = ByteArrayVecData::<1, 4>::try_from_bytes(&[1u8; 3]).unwrap();
        assert_eq!(&data.bytes[..3], &[1u8; 3]);
        assert_eq!(&data.bytes[3], &0u8);
        assert_eq!(data.as_ref(), &[1u8; 3]);
        assert_eq!(data.as_mut(), &mut [1u8; 3]);
        assert_eq!(data.len(), 3);
        assert!(!data.is_empty());

        #[cfg(feature = "zeroize")]
        {
            // memzero() zeroes all bytes, even if the full range wasn't used.
            data.memzero();
            assert_eq!(data.bytes, [0u8; 4]);
        }
    }

    #[test]
    #[cfg(any(feature = "safe_api", feature = "alloc"))]
    fn test_vecdata_u8() {
        assert!(ByteVecData::try_from_bytes(&[0u8; 0]).is_err());
        assert!(ByteVecData::try_from_bytes(&[0u8; 4]).is_ok());
        assert!(ByteVecData::try_from_bytes(&[0u8; 3]).is_ok());
        assert!(ByteVecData::try_from_bytes(&[0u8; 5]).is_ok());
        assert!(ByteVecData::new(0).is_err());
        assert!(ByteVecData::new(4).is_ok());
        assert!(ByteVecData::new(3).is_ok());
        assert!(ByteVecData::new(5).is_ok());

        assert_eq!(ByteVecData::new(4).unwrap().as_ref(), &[0u8; 4]);

        let mut data = ByteVecData::try_from_bytes(&[1u8; 4]).unwrap();
        assert_eq!(data.bytes, [1u8; 4]);
        assert_eq!(data.as_ref(), &[1u8; 4]);
        assert_eq!(data.as_mut(), &mut [1u8; 4]);
        assert_eq!(data.len(), 4);
        assert!(!data.is_empty());

        #[cfg(feature = "zeroize")]
        {
            data.memzero();
            assert_eq!(data.bytes, [0u8; 4]);
        }

        let mut data = ByteVecData::try_from_bytes(&[1u8; 3]).unwrap();
        assert_eq!(data.bytes, [1u8; 3]);
        assert_eq!(data.as_ref(), &[1u8; 3]);
        assert_eq!(data.as_mut(), &mut [1u8; 3]);
        assert_eq!(data.len(), 3);
        assert!(!data.is_empty());

        #[cfg(feature = "zeroize")]
        {
            // memzero() zeroes all bytes, even if the full range wasn't used.
            data.memzero();
            assert_eq!(data.bytes, [0u8; 3]);
        }
    }
}