aws_lc_rs/
encoding.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0 OR ISC
3
4//! Serialization formats
5
6use crate::buffer::Buffer;
7
8macro_rules! generated_encodings {
9    ($(($name:ident, $name_type:ident)),*) => {
10        use core::fmt::{Debug, Error, Formatter};
11        use core::ops::Deref;
12        mod buffer_type {
13            $(
14                pub struct $name_type {
15                    _priv: (),
16                }
17            )*
18        }
19        $(
20            /// Serialized bytes
21            pub struct $name<'a>(Buffer<'a, buffer_type::$name_type>);
22
23            impl<'a> Deref for $name<'a> {
24                type Target = Buffer<'a, buffer_type::$name_type>;
25
26                fn deref(&self) -> &Self::Target {
27                    &self.0
28                }
29            }
30
31            impl $name<'static> {
32                #[allow(dead_code)]
33                pub(crate) fn new(owned: Vec<u8>) -> Self {
34                    Self(Buffer::new(owned))
35                }
36                #[allow(dead_code)]
37                pub(crate) fn take_from_slice(owned: &mut [u8]) -> Self {
38                    Self(Buffer::take_from_slice(owned))
39                }
40            }
41
42            impl Debug for $name<'_> {
43                fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
44                    f.debug_struct(stringify!($name)).finish()
45                }
46            }
47
48            impl<'a> From<Buffer<'a, buffer_type::$name_type>> for $name<'a> {
49                fn from(value: Buffer<'a, buffer_type::$name_type>) -> Self {
50                    Self(value)
51                }
52            }
53        )*
54    }
55}
56pub(crate) use generated_encodings;
57generated_encodings!(
58    (Curve25519SeedBin, Curve25519SeedBinType),
59    (EcPrivateKeyBin, EcPrivateKeyBinType),
60    (EcPrivateKeyRfc5915Der, EcPrivateKeyRfc5915DerType),
61    (EcPublicKeyCompressedBin, EcPublicKeyCompressedBinType),
62    (EcPublicKeyUncompressedBin, EcPublicKeyUncompressedBinType),
63    (Pkcs8V1Der, Pkcs8V1DerType),
64    (Pkcs8V2Der, Pkcs8V2DerType),
65    (PqdsaPrivateKeyRaw, PqdsaPrivateKeyRawType),
66    (PqdsaSeedRaw, PqdsaSeedRawType),
67    (PublicKeyX509Der, PublicKeyX509DerType)
68);
69
70/// Trait for types that can be serialized into a DER format.
71pub trait AsDer<T> {
72    /// Serializes into a DER format.
73    ///
74    /// # Errors
75    /// Returns Unspecified if serialization fails.
76    fn as_der(&self) -> Result<T, crate::error::Unspecified>;
77}
78
79/// Trait for values that can be serialized into a big-endian format
80pub trait AsBigEndian<T> {
81    /// Serializes into a big-endian format.
82    ///
83    /// # Errors
84    /// Returns Unspecified if serialization fails.
85    fn as_be_bytes(&self) -> Result<T, crate::error::Unspecified>;
86}
87
88/// Trait for values that can be serialized into a raw format
89pub trait AsRawBytes<T> {
90    /// Serializes into a raw format.
91    ///
92    /// # Errors
93    /// Returns Unspecified if serialization fails.
94    fn as_raw_bytes(&self) -> Result<T, crate::error::Unspecified>;
95}