/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ring-0.17.14/src/aead/nonce.rs
Line  | Count  | Source  | 
1  |  | // Copyright 2018 Brian Smith.  | 
2  |  | //  | 
3  |  | // Permission to use, copy, modify, and/or distribute this software for any  | 
4  |  | // purpose with or without fee is hereby granted, provided that the above  | 
5  |  | // copyright notice and this permission notice appear in all copies.  | 
6  |  | //  | 
7  |  | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
8  |  | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
9  |  | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY  | 
10  |  | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
11  |  | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION  | 
12  |  | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN  | 
13  |  | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
14  |  |  | 
15  |  | use crate::error;  | 
16  |  |  | 
17  |  | /// A nonce for a single AEAD opening or sealing operation.  | 
18  |  | ///  | 
19  |  | /// The user must ensure, for a particular key, that each nonce is unique.  | 
20  |  | ///  | 
21  |  | /// `Nonce` intentionally doesn't implement `Clone` to ensure that each one is  | 
22  |  | /// consumed at most once.  | 
23  |  | pub struct Nonce([u8; NONCE_LEN]);  | 
24  |  |  | 
25  |  | impl Nonce { | 
26  |  |     /// Constructs a `Nonce` with the given value, assuming that the value is  | 
27  |  |     /// unique for the lifetime of the key it is being used with.  | 
28  |  |     ///  | 
29  |  |     /// Fails if `value` isn't `NONCE_LEN` bytes long.  | 
30  |  |     #[inline]  | 
31  | 0  |     pub fn try_assume_unique_for_key(value: &[u8]) -> Result<Self, error::Unspecified> { | 
32  | 0  |         let value: &[u8; NONCE_LEN] = value.try_into()?;  | 
33  | 0  |         Ok(Self::assume_unique_for_key(*value))  | 
34  | 0  |     }  | 
35  |  |  | 
36  |  |     /// Constructs a `Nonce` with the given value, assuming that the value is  | 
37  |  |     /// unique for the lifetime of the key it is being used with.  | 
38  |  |     #[inline]  | 
39  | 0  |     pub fn assume_unique_for_key(value: [u8; NONCE_LEN]) -> Self { | 
40  | 0  |         Self(value)  | 
41  | 0  |     }  | 
42  |  | }  | 
43  |  |  | 
44  |  | impl AsRef<[u8; NONCE_LEN]> for Nonce { | 
45  | 0  |     fn as_ref(&self) -> &[u8; NONCE_LEN] { | 
46  | 0  |         &self.0  | 
47  | 0  |     }  | 
48  |  | }  | 
49  |  |  | 
50  |  | /// All the AEADs we support use 96-bit nonces.  | 
51  |  | pub const NONCE_LEN: usize = 96 / 8;  |