Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / crypto_provider_rustcrypto / src / lib.rs
lib.rs138 lines · 4.6 KiBRaw
| 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 | #![no_std] |
| 16 | |
| 17 | //! Crate which provides impls for CryptoProvider backed by RustCrypto crates |
| 18 | |
| 19 | use core::{fmt::Debug, marker::PhantomData}; |
| 20 | |
| 21 | pub use aes; |
| 22 | use cfg_if::cfg_if; |
| 23 | pub use hkdf; |
| 24 | pub use hmac; |
| 25 | use rand::{Rng, SeedableRng}; |
| 26 | use rand_core::CryptoRng; |
| 27 | use subtle::ConstantTimeEq; |
| 28 | |
| 29 | /// Contains the RustCrypto backed impls for AES-GCM-SIV operations |
| 30 | mod aead; |
| 31 | /// Contains the RustCrypto backed AES impl for CryptoProvider |
| 32 | pub mod aes_cp; |
| 33 | /// Contains the RustCrypto backed ECDSA P256 impl for CryptoProvider |
| 34 | mod ecdsa; |
| 35 | /// Contains the RustCrypto backed impl for ed25519 key generation, signing, and verification |
| 36 | mod ed25519; |
| 37 | /// Contains the RustCrypto backed hkdf impl for CryptoProvider |
| 38 | mod hkdf_cp; |
| 39 | /// Contains the RustCrypto backed hmac impl for CryptoProvider |
| 40 | mod hmac_cp; |
| 41 | /// Contains the RustCrypto backed P256 impl for CryptoProvider |
| 42 | mod p256; |
| 43 | /// Contains the RustCrypto backed SHA2 impl for CryptoProvider |
| 44 | mod sha2_cp; |
| 45 | /// Contains the RustCrypto backed X25519 impl for CryptoProvider |
| 46 | mod x25519; |
| 47 | |
| 48 | cfg_if! { |
| 49 | if #[cfg(feature = "std")] { |
| 50 | /// Providing a type alias for compatibility with existing usage of RustCrypto |
| 51 | /// by default we use ChaCha20Rng for the underlying csprng (StdRng is no |
| 52 | /// longer Clone in rand 0.9+, and CryptoProvider requires Clone). |
| 53 | pub type RustCrypto = RustCryptoImpl<rand_chacha::ChaCha20Rng>; |
| 54 | } else if #[cfg(feature = "rand_chacha")] { |
| 55 | /// A no_std compatible implementation of CryptoProvider backed by RustCrypto crates |
| 56 | pub type RustCrypto = RustCryptoImpl<rand_chacha::ChaCha20Rng>; |
| 57 | } else { |
| 58 | compile_error!("Must specify either --features std or --features rand_chacha"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// The RustCrypto backed struct which implements CryptoProvider |
| 63 | #[derive(Default, Clone, Debug, PartialEq, Eq)] |
| 64 | pub struct RustCryptoImpl<R: CryptoRng + SeedableRng + Rng> { |
| 65 | _marker: PhantomData<R>, |
| 66 | } |
| 67 | |
| 68 | impl<R: CryptoRng + SeedableRng + Rng> RustCryptoImpl<R> { |
| 69 | /// Create a new instance of RustCrypto |
| 70 | pub fn new() -> Self { |
| 71 | Self { _marker: Default::default() } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl<R: CryptoRng + SeedableRng + Rng + Eq + PartialEq + Debug + Clone + Send> |
| 76 | crypto_provider::CryptoProvider for RustCryptoImpl<R> |
| 77 | { |
| 78 | type HkdfSha256 = hkdf_cp::Hkdf<sha2::Sha256>; |
| 79 | type HmacSha256 = hmac_cp::Hmac<sha2::Sha256>; |
| 80 | type HkdfSha512 = hkdf_cp::Hkdf<sha2::Sha512>; |
| 81 | type HmacSha512 = hmac_cp::Hmac<sha2::Sha512>; |
| 82 | type AesCbcPkcs7Padded = aes_cp::cbc::AesCbcPkcs7Padded; |
| 83 | type X25519 = x25519::X25519Ecdh<R>; |
| 84 | type P256 = p256::P256Ecdh<R>; |
| 85 | type Sha256 = sha2_cp::RustCryptoSha256; |
| 86 | type Sha512 = sha2_cp::RustCryptoSha512; |
| 87 | type Aes128 = aes_cp::Aes128; |
| 88 | type Aes256 = aes_cp::Aes256; |
| 89 | type AesCtr128 = aes_cp::ctr::AesCtr128; |
| 90 | type AesCtr256 = aes_cp::ctr::AesCtr256; |
| 91 | type Ed25519 = ed25519::Ed25519; |
| 92 | type Aes128GcmSiv = aead::aes_gcm_siv::AesGcmSiv<aes::Aes128>; |
| 93 | type Aes256GcmSiv = aead::aes_gcm_siv::AesGcmSiv<aes::Aes256>; |
| 94 | type Aes128Gcm = aead::aes_gcm::AesGcm<aes::Aes128>; |
| 95 | type Aes256Gcm = aead::aes_gcm::AesGcm<aes::Aes256>; |
| 96 | type EcdsaP256 = ecdsa::P256Ecdsa; |
| 97 | type CryptoRng = RcRng<R>; |
| 98 | |
| 99 | fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { |
| 100 | a.ct_eq(b).into() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// A RustCrypto wrapper for RNG |
| 105 | pub struct RcRng<R>(R); |
| 106 | |
| 107 | impl<R: rand_core::CryptoRng + Rng + SeedableRng> crypto_provider::CryptoRng for RcRng<R> { |
| 108 | fn new() -> Self { |
| 109 | let mut sys = rand::rngs::SysRng; |
| 110 | Self(R::try_from_rng(&mut sys).unwrap_or_else(|_| R::from_seed(Default::default()))) |
| 111 | } |
| 112 | |
| 113 | fn next_u64(&mut self) -> u64 { |
| 114 | self.0.next_u64() |
| 115 | } |
| 116 | |
| 117 | fn fill(&mut self, dest: &mut [u8]) { |
| 118 | self.0.fill_bytes(dest) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[cfg(test)] |
| 123 | mod testing; |
| 124 | |
| 125 | #[cfg(test)] |
| 126 | mod tests { |
| 127 | use core::marker::PhantomData; |
| 128 | |
| 129 | use crypto_provider_test::prelude::*; |
| 130 | use crypto_provider_test::sha2::*; |
| 131 | |
| 132 | use crate::RustCrypto; |
| 133 | |
| 134 | #[apply(sha2_test_cases)] |
| 135 | fn sha2_tests(testcase: CryptoProviderTestCase<RustCrypto>) { |
| 136 | testcase(PhantomData::<RustCrypto>); |
| 137 | } |
| 138 | } |