Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / crypto_provider / src / lib.rs
lib.rs155 lines · 5.0 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 | //! Crypto abstraction trait only crate, which provides traits for cryptographic primitives |
| 16 | |
| 17 | #![no_std] |
| 18 | |
| 19 | use core::fmt::Debug; |
| 20 | |
| 21 | use crate::aes::{Aes128Key, Aes256Key}; |
| 22 | |
| 23 | /// mod containing hmac trait |
| 24 | pub mod hkdf; |
| 25 | |
| 26 | /// mod containing hkdf trait |
| 27 | pub mod hmac; |
| 28 | |
| 29 | /// mod containing X25519 trait |
| 30 | pub mod x25519; |
| 31 | |
| 32 | /// mod containing traits for NIST-P256 elliptic curve implementation. |
| 33 | pub mod p256; |
| 34 | |
| 35 | /// mod containing traits for elliptic curve cryptography. |
| 36 | pub mod elliptic_curve; |
| 37 | |
| 38 | /// mod containing SHA256 trait. |
| 39 | pub mod sha2; |
| 40 | |
| 41 | /// mod containing aes trait. |
| 42 | pub mod aes; |
| 43 | |
| 44 | /// mod containing aead trait. |
| 45 | pub mod aead; |
| 46 | |
| 47 | /// mod containing traits for ed25519 key generation, signing, and verification |
| 48 | pub mod ed25519; |
| 49 | |
| 50 | /// mod containing ecdsa trait. |
| 51 | pub mod ecdsa; |
| 52 | |
| 53 | pub use tinyvec; |
| 54 | |
| 55 | /// Uber crypto trait which defines the traits for all crypto primitives as associated types |
| 56 | pub trait CryptoProvider: Clone + Debug + PartialEq + Eq + Send { |
| 57 | /// The Hkdf type which implements the hkdf trait |
| 58 | type HkdfSha256: hkdf::Hkdf; |
| 59 | /// The Hmac type which implements the hmac trait |
| 60 | type HmacSha256: hmac::Hmac<32>; |
| 61 | /// The Hkdf type which implements the hkdf trait |
| 62 | type HkdfSha512: hkdf::Hkdf; |
| 63 | /// The Hmac type which implements the hmac trait |
| 64 | type HmacSha512: hmac::Hmac<64>; |
| 65 | /// The AES-CBC-PKCS7 implementation to use |
| 66 | type AesCbcPkcs7Padded: aes::cbc::AesCbcPkcs7Padded; |
| 67 | /// The X25519 implementation to use for ECDH. |
| 68 | type X25519: elliptic_curve::EcdhProvider<x25519::X25519>; |
| 69 | /// The P256 implementation to use for ECDH. |
| 70 | type P256: p256::P256EcdhProvider; |
| 71 | /// The SHA256 hash implementation. |
| 72 | type Sha256: sha2::Sha256; |
| 73 | /// The SHA512 hash implementation. |
| 74 | type Sha512: sha2::Sha512; |
| 75 | /// Plain AES-128 implementation (without block cipher mode). |
| 76 | type Aes128: aes::Aes<Key = Aes128Key>; |
| 77 | /// Plain AES-256 implementation (without block cipher mode). |
| 78 | type Aes256: aes::Aes<Key = Aes256Key>; |
| 79 | /// AES-128 with CTR block mode |
| 80 | type AesCtr128: aes::ctr::AesCtr<Key = aes::Aes128Key>; |
| 81 | /// AES-256 with CTR block mode |
| 82 | type AesCtr256: aes::ctr::AesCtr<Key = aes::Aes256Key>; |
| 83 | /// The trait defining ed25519, a Edwards-curve Digital Signature Algorithm signature scheme |
| 84 | /// using SHA-512 (SHA-2) and Curve25519 |
| 85 | type Ed25519: ed25519::Ed25519Provider; |
| 86 | /// The trait defining AES-128-GCM-SIV, a nonce-misuse resistant AEAD with a key size of 16 bytes. |
| 87 | type Aes128GcmSiv: aead::AesGcmSiv + aead::AeadInit<Aes128Key>; |
| 88 | /// The trait defining AES-256-GCM-SIV, a nonce-misuse resistant AEAD with a key size of 32 bytes. |
| 89 | type Aes256GcmSiv: aead::AesGcmSiv + aead::AeadInit<Aes256Key>; |
| 90 | /// The trait defining AES-128-GCM, an AEAD with a key size of 16 bytes. |
| 91 | type Aes128Gcm: aead::AesGcm + aead::AeadInit<Aes128Key>; |
| 92 | /// The trait defining AES-256-GCM, an AEAD with a key size of 32 bytes. |
| 93 | type Aes256Gcm: aead::AesGcm + aead::AeadInit<Aes256Key>; |
| 94 | /// The trait defining ECDSA-P256. |
| 95 | type EcdsaP256: ecdsa::P256EcdsaProvider; |
| 96 | /// The cryptographically secure random number generator |
| 97 | type CryptoRng: CryptoRng; |
| 98 | |
| 99 | /// Compares the two given slices, in constant time, and returns true if they are equal. |
| 100 | fn constant_time_eq(a: &[u8], b: &[u8]) -> bool; |
| 101 | } |
| 102 | |
| 103 | /// Wrapper to a cryptographically secure pseudo random number generator |
| 104 | pub trait CryptoRng { |
| 105 | /// Returns an instance of the rng |
| 106 | fn new() -> Self; |
| 107 | |
| 108 | /// Return the next random u64 |
| 109 | fn next_u64(&mut self) -> u64; |
| 110 | |
| 111 | /// Fill dest with random data |
| 112 | fn fill(&mut self, dest: &mut [u8]); |
| 113 | |
| 114 | /// Generate a random entity |
| 115 | fn gen<F: FromCryptoRng>(&mut self) -> F |
| 116 | where |
| 117 | Self: Sized, |
| 118 | { |
| 119 | F::new_random::<Self>(self) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// If impls want to opt out of passing a Rng they can simply use `()` for the Rng associated type |
| 124 | impl CryptoRng for () { |
| 125 | fn new() -> Self {} |
| 126 | |
| 127 | fn next_u64(&mut self) -> u64 { |
| 128 | unimplemented!() |
| 129 | } |
| 130 | |
| 131 | fn fill(&mut self, _dest: &mut [u8]) { |
| 132 | unimplemented!() |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// For types that can be created from a `CryptoRng` |
| 137 | pub trait FromCryptoRng { |
| 138 | /// Construct a new `Self` with random data from `rng` |
| 139 | fn new_random<R: CryptoRng>(rng: &mut R) -> Self; |
| 140 | } |
| 141 | |
| 142 | impl<const N: usize> FromCryptoRng for [u8; N] { |
| 143 | fn new_random<R: CryptoRng>(rng: &mut R) -> Self { |
| 144 | let mut arr = [0; N]; |
| 145 | rng.fill(&mut arr); |
| 146 | arr |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl FromCryptoRng for u8 { |
| 151 | fn new_random<R: CryptoRng>(rng: &mut R) -> Self { |
| 152 | let arr: [u8; 1] = rng.gen(); |
| 153 | arr[0] |
| 154 | } |
| 155 | } |