1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16
17 //! This crate provides the default implementations for the Authgraph traits using
18 //! BoringSSL via the `openssl` crate.
19
20 extern crate alloc; // Needed for use of `ag_err!` from `authgraph_core`
21
22 use authgraph_core::traits;
23
24 /// Macro to auto-generate error mapping around invocations of `openssl` methods.
25 /// An invocation like:
26 ///
27 /// ```ignore
28 /// let x = ossl!(y.func(a, b))?;
29 /// ```
30 ///
31 /// will map to:
32 ///
33 /// ```ignore
34 /// let x = y.func(a, b).map_err(ag_err!(Internal, "failed to perform: y.func(a, b)"))?;
35 /// ```
36 ///
37 /// Requires local `use authgraph_core::{Error, ag_err}`.
38 #[macro_export]
39 macro_rules! ossl {
40 { $e:expr } => {
41 $e.map_err(|err| authgraph_core::error::Error(
42 authgraph_wire::ErrorCode::InternalError,
43 format!(concat!("failed to perform: ", stringify!($e), ": {:?}"), err)
44 ))
45 }
46 }
47
48 mod aes;
49 pub use aes::BoringAes;
50 pub mod ec;
51 pub use ec::BoringEcDh;
52 pub use ec::BoringEcDsa;
53 mod hmac;
54 pub use hmac::{BoringHkdf, BoringHmac, BoringSha256};
55 mod rng;
56 pub use rng::BoringRng;
57 pub mod test_device;
58
59 #[cfg(test)]
60 mod tests;
61
62 /// Return a populated [`traits::CryptoTraitImpl`] structure that uses BoringSSL implementations for
63 /// cryptographic traits.
crypto_trait_impls() -> traits::CryptoTraitImpl64 pub fn crypto_trait_impls() -> traits::CryptoTraitImpl {
65 traits::CryptoTraitImpl {
66 aes_gcm: Box::new(BoringAes),
67 ecdh: Box::new(BoringEcDh),
68 ecdsa: Box::new(BoringEcDsa),
69 hmac: Box::new(BoringHmac),
70 hkdf: Box::new(BoringHkdf),
71 sha256: Box::new(BoringSha256),
72 rng: Box::new(BoringRng),
73 }
74 }
75