aws_lc_rs/aead/
nonce.rs

1// Copyright 2018 Brian Smith.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5
6use crate::endian::{ArrayEncoding, BigEndian, Encoding, FromArray, LittleEndian};
7use crate::error;
8use crate::iv::FixedLength;
9
10/// A nonce for a single AEAD opening or sealing operation.
11///
12/// The user must ensure, for a particular key, that each nonce is unique.
13///
14/// `Nonce` intentionally doesn't implement `Clone` to ensure that each one is
15/// consumed at most once.
16pub struct Nonce(pub(crate) FixedLength<NONCE_LEN>);
17
18impl Nonce {
19    /// Constructs a `Nonce` with the given value, assuming that the value is
20    /// unique for the lifetime of the key it is being used with.
21    ///
22    /// Fails if `value` isn't `NONCE_LEN` bytes long.
23    /// # Errors
24    /// `error::Unspecified` when byte slice length is not `NONCE_LEN`
25    #[inline]
26    pub fn try_assume_unique_for_key(value: &[u8]) -> Result<Self, error::Unspecified> {
27        Ok(Self(FixedLength::<NONCE_LEN>::try_from(value)?))
28    }
29
30    /// Constructs a `Nonce` with the given value, assuming that the value is
31    /// unique for the lifetime of the key it is being used with.
32    #[inline]
33    #[must_use]
34    pub fn assume_unique_for_key(value: [u8; NONCE_LEN]) -> Self {
35        Self(FixedLength::<NONCE_LEN>::from(value))
36    }
37}
38
39impl AsRef<[u8; NONCE_LEN]> for Nonce {
40    #[inline]
41    fn as_ref(&self) -> &[u8; NONCE_LEN] {
42        self.0.as_ref()
43    }
44}
45
46impl From<&[u8; NONCE_LEN]> for Nonce {
47    #[inline]
48    fn from(bytes: &[u8; NONCE_LEN]) -> Self {
49        Self(FixedLength::from(bytes))
50    }
51}
52
53#[allow(useless_deprecated)] // https://github.com/rust-lang/rust/issues/39935
54#[deprecated]
55impl From<&[u32; NONCE_LEN / 4]> for Nonce {
56    #[inline]
57    fn from(values: &[u32; NONCE_LEN / 4]) -> Self {
58        Nonce::from(&LittleEndian::<u32>::from_array(values))
59    }
60}
61
62impl From<&[BigEndian<u32>; NONCE_LEN / 4]> for Nonce {
63    #[inline]
64    fn from(values: &[BigEndian<u32>; NONCE_LEN / 4]) -> Self {
65        Nonce(FixedLength::from(values.as_byte_array()))
66    }
67}
68
69impl From<&[LittleEndian<u32>; NONCE_LEN / 4]> for Nonce {
70    #[inline]
71    fn from(nonce: &[LittleEndian<u32>; NONCE_LEN / 4]) -> Self {
72        Nonce(FixedLength::from(nonce.as_byte_array()))
73    }
74}
75
76impl From<BigEndian<u32>> for Nonce {
77    #[inline]
78    fn from(number: BigEndian<u32>) -> Self {
79        Nonce::from([BigEndian::ZERO, BigEndian::ZERO, number].as_byte_array())
80    }
81}
82
83pub const IV_LEN: usize = 16;
84impl From<&[u8; IV_LEN]> for Nonce {
85    #[inline]
86    fn from(bytes: &[u8; IV_LEN]) -> Self {
87        let mut nonce_bytes = [0u8; NONCE_LEN];
88        nonce_bytes.copy_from_slice(&bytes[0..NONCE_LEN]);
89        Nonce(FixedLength::from(nonce_bytes))
90    }
91}
92
93/// All the AEADs we support use 96-bit nonces.
94pub const NONCE_LEN: usize = 96 / 8;
95
96#[cfg(test)]
97mod tests {
98
99    #[test]
100    fn test_nonce_from_byte_array() {
101        use crate::aead::nonce::IV_LEN;
102        use crate::aead::Nonce;
103        let iv: [u8; IV_LEN] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
104        let nonce = Nonce::from(&iv);
105
106        assert_eq!(&[1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], nonce.as_ref());
107    }
108}