/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-rs-1.15.4/src/buffer.rs
Line | Count | Source |
1 | | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | | // SPDX-License-Identifier: Apache-2.0 OR ISC |
3 | | |
4 | | //! This module exposes a buffer type used in crate APIs returning private keys and other "private" |
5 | | //! contents. |
6 | | |
7 | | #![allow(clippy::module_name_repetitions)] |
8 | | |
9 | | use alloc::borrow::Cow; |
10 | | use core::fmt; |
11 | | use core::marker::PhantomData; |
12 | | |
13 | | use zeroize::Zeroize; |
14 | | |
15 | | /// This is a buffer type for some data exposed by various APIs in this crate. |
16 | | /// |
17 | | /// `T` acts as a discriminant between different kinds of data. |
18 | | /// |
19 | | /// The buffer will be zeroed on drop if it is owned. |
20 | | pub struct Buffer<'a, T>(Cow<'a, [u8]>, PhantomData<T>); |
21 | | |
22 | | impl<T> Drop for Buffer<'_, T> { |
23 | 0 | fn drop(&mut self) { |
24 | 0 | if let Cow::Owned(b) = &mut self.0 { |
25 | 0 | b.zeroize(); |
26 | 0 | } |
27 | 0 | } |
28 | | } |
29 | | |
30 | | impl<'a, T> Buffer<'a, T> { |
31 | 0 | pub(crate) fn new(owned: Vec<u8>) -> Buffer<'a, T> { |
32 | 0 | Buffer(Cow::Owned(owned), PhantomData) |
33 | 0 | } Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::kem::buffer_type::EncapsulationKeyBytesType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::Pkcs8V1DerType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::Pkcs8V2DerType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::EcPrivateKeyBinType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::PublicKeyX509DerType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::Curve25519SeedBinType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::EcPrivateKeyRfc5915DerType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::EcPublicKeyCompressedBinType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<aws_lc_rs::encoding::buffer_type::EcPublicKeyUncompressedBinType>>::new Unexecuted instantiation: <aws_lc_rs::buffer::Buffer<&[u8]>>::new |
34 | | |
35 | 0 | pub(crate) fn take_from_slice(slice: &mut [u8]) -> Buffer<'a, T> { |
36 | 0 | let owned = slice.to_vec(); |
37 | 0 | slice.zeroize(); |
38 | 0 | Buffer(Cow::Owned(owned), PhantomData) |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl<T> fmt::Debug for Buffer<'_, T> { |
43 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
44 | 0 | f.write_str("Buffer(...)") |
45 | 0 | } |
46 | | } |
47 | | |
48 | | impl<T> AsRef<[u8]> for Buffer<'_, T> { |
49 | | #[inline] |
50 | 0 | fn as_ref(&self) -> &[u8] { |
51 | 0 | self.0.as_ref() |
52 | 0 | } |
53 | | } |
54 | | |
55 | | #[cfg(test)] |
56 | | mod tests { |
57 | | use super::*; |
58 | | |
59 | | #[test] |
60 | | fn test_new() { |
61 | | let buffer: Buffer<u8> = Buffer::new(vec![1, 2, 3]); |
62 | | assert_eq!(buffer.as_ref(), &[1, 2, 3]); |
63 | | } |
64 | | |
65 | | #[test] |
66 | | fn test_take_from_slice() { |
67 | | let mut slice = [1, 2, 3]; |
68 | | let buffer: Buffer<u8> = Buffer::take_from_slice(&mut slice); |
69 | | assert_eq!(buffer.as_ref(), &[1, 2, 3]); |
70 | | assert_eq!(slice, [0, 0, 0]); |
71 | | } |
72 | | } |