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 #![no_std]
15 #![forbid(unsafe_code)]
16 #![deny(
17     missing_docs,
18     clippy::indexing_slicing,
19     clippy::unwrap_used,
20     clippy::panic,
21     clippy::expect_used
22 )]
23 
24 //! Crate which provides impls for CryptoProvider backed by BoringSSL
25 //!
26 use bssl_crypto::rand_bytes;
27 use crypto_provider::{CryptoProvider, CryptoRng};
28 
29 /// Implementation of `crypto_provider::aes` types using BoringSSL
30 pub mod aes;
31 
32 /// Implementations of crypto_provider::hkdf traits backed by BoringSSL
33 pub mod hkdf;
34 
35 /// Implementations of crypto_provider::hmac traits backed by BoringSSL
36 pub mod hmac;
37 
38 /// Implementations of crypto_provider::ed25519 traits backed by BoringSSL
39 mod ed25519;
40 
41 /// Implementations of crypto_provider::aead traits backed by BoringSSL
42 mod aead;
43 
44 /// Implementations of crypto_provider::p256 traits backed by BoringSSL
45 mod p256;
46 
47 /// Implementations of crypto_provider::x25519 traits backed by BoringSSL
48 mod x25519;
49 
50 /// Implementations of crypto_provider::sha2 traits backed by BoringSSL
51 mod sha2;
52 
53 /// The BoringSSL backed struct which implements CryptoProvider
54 #[derive(Default, Clone, Debug, PartialEq, Eq)]
55 pub struct Boringssl;
56 
57 impl CryptoProvider for Boringssl {
58     type HkdfSha256 = hkdf::Hkdf<bssl_crypto::digest::Sha256>;
59     type HmacSha256 = hmac::HmacSha256;
60     type HkdfSha512 = hkdf::Hkdf<bssl_crypto::digest::Sha512>;
61     type HmacSha512 = hmac::HmacSha512;
62     type AesCbcPkcs7Padded = aes::cbc::AesCbcPkcs7Padded;
63     type X25519 = x25519::X25519Ecdh;
64     type P256 = p256::P256Ecdh;
65     type Sha256 = sha2::Sha256;
66     type Sha512 = sha2::Sha512;
67     type Aes128 = aes::Aes128;
68     type Aes256 = aes::Aes256;
69     type AesCtr128 = aes::ctr::AesCtr128;
70     type AesCtr256 = aes::ctr::AesCtr256;
71     type Ed25519 = ed25519::Ed25519;
72     type Aes128GcmSiv = aead::aes_gcm_siv::AesGcmSiv128;
73     type Aes256GcmSiv = aead::aes_gcm_siv::AesGcmSiv256;
74     type Aes128Gcm = aead::aes_gcm::AesGcm128;
75     type Aes256Gcm = aead::aes_gcm::AesGcm256;
76     type CryptoRng = BoringSslRng;
77 
constant_time_eq(a: &[u8], b: &[u8]) -> bool78     fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
79         bssl_crypto::constant_time_compare(a, b)
80     }
81 }
82 
83 /// BoringSSL implemented random number generator
84 pub struct BoringSslRng;
85 
86 impl CryptoRng for BoringSslRng {
new() -> Self87     fn new() -> Self {
88         BoringSslRng {}
89     }
90 
next_u64(&mut self) -> u6491     fn next_u64(&mut self) -> u64 {
92         let mut buf = [0; 8];
93         rand_bytes(&mut buf);
94         u64::from_be_bytes(buf)
95     }
96 
fill(&mut self, dest: &mut [u8])97     fn fill(&mut self, dest: &mut [u8]) {
98         rand_bytes(dest)
99     }
100 }
101 
102 #[cfg(test)]
103 mod tests {
104     use core::marker::PhantomData;
105     use crypto_provider_test::prelude::*;
106     use crypto_provider_test::sha2::*;
107 
108     use crate::Boringssl;
109 
110     #[apply(sha2_test_cases)]
sha2_tests(testcase: CryptoProviderTestCase<Boringssl>)111     fn sha2_tests(testcase: CryptoProviderTestCase<Boringssl>) {
112         testcase(PhantomData);
113     }
114 }
115